Skip to content

Commit

Permalink
feat: improve song change and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
miksuh-dev committed Nov 16, 2022
1 parent 8e2162f commit 5d109a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
26 changes: 17 additions & 9 deletions server/common/playlist/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ let playing: PlayingSong | undefined;
let stream: Readable | undefined;
let endTimeout: NodeJS.Timeout | undefined;

export const onSongEnd = async () => {
if (!playing) return;

export const onSongEnd = async (song: Song) => {
await prisma.song.update({
where: {
id: playing.id,
id: song.id,
},
data: {
ended: true,
},
});

sendMessage(`Kappale ${playing.title} päättyi.`);
sendMessage(`Kappale ${song.title} päättyi.`);

stopCurrentSong();

Expand All @@ -44,16 +42,21 @@ const onPlayError = (song: Song, error: string) => {
);

setTimeout(() => {
if (playing?.id === song.id) {
onSongEnd().catch((e) => console.log("e", e));
if (playing?.id === song.id || !playing) {
onSongEnd(song).catch((e) => {
if (e instanceof Error) {
onPlayError(song, `Virhe kappaleen päättämisessä: ${e.message}`);
}
console.log("e", e);
});
}
}, 5000);
};

export const playSong = async (song: Song) => {
const videoInfo = await getVideoInfo(song);
if (!videoInfo) {
onPlayError(song, "Ei videoinfoa");
onPlayError(song, "Videon tiedot eivät ole saatavilla");
return;
}

Expand Down Expand Up @@ -82,7 +85,12 @@ export const playSong = async (song: Song) => {
.diffNow("seconds").seconds;

endTimeout = setTimeout(() => {
onSongEnd().catch((e) => console.log("e", e));
onSongEnd(song).catch((e) => {
if (e instanceof Error) {
onPlayError(song, `Virhe kappaleen päättämisessä: ${e.message}`);
}
console.log("e", e);
});
}, secondsLeft * 1000);

stream = createStream(song);
Expand Down
19 changes: 5 additions & 14 deletions server/common/playlist/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,20 @@ export const removeSong = async (id: number, user: MumbleUser) => {
},
});

if (!getCurrentSong()) {
const nextSong = await getNextSong();
if (nextSong) {
await playSong(nextSong);
}
}

if (song.id === getCurrentSong()?.id) {
sendMessage(`${user.name} ohitti kappaleen "${song.title}"`);

stopCurrentSong();
} else {
sendMessage(`${user.name} poisti "${song.title}" kappaleen jonosta`);
ee.emit(`onUpdate`, { song: { remove: song.id } });
}

if (!getCurrentSong()) {
const nextSong = await getNextSong();
if (nextSong) {
await playSong(nextSong);
}

return song;
}

ee.emit(`onUpdate`, { song: { remove: song.id } });

sendMessage(`${user.name} poisti "${song.title}" kappaleen jonosta`);

return song;
};

0 comments on commit 5d109a6

Please sign in to comment.