Skip to content

Commit

Permalink
chore: Remove player from scoreboard if removed from game state (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
snorremd authored Jul 18, 2024
1 parent 2ec230e commit 2634212
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,14 @@ htmx.on("htmx:sseMessage", (evt) => {
chart.data.datasets.push(data);
chart.update();
}

// If player leaves, remove them from the chart
if (evt instanceof CustomEvent && evt.detail.type === "player-left-chart") {
const nick = JSON.parse(evt.detail.data).nick;
const datasetIndex = chart.data.datasets.findIndex((d) => d.label === nick);
if (datasetIndex !== -1) {
chart.data.datasets.splice(datasetIndex, 1);
chart.update();
}
}
});
2 changes: 1 addition & 1 deletion src/game/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface PlayerJoined extends Pick<Player, "uuid" | "nick" | "url"> {
* Event sent to player worker thread when a player leaves the game.
* At this point the worker thread should be terminated.
*/
interface PlayerLeft extends Pick<Player, "uuid"> {
interface PlayerLeft extends Pick<Player, "uuid" | "nick"> {
type: "player-left";
}

Expand Down
18 changes: 16 additions & 2 deletions src/game/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,32 @@ export function playerSurrender(state: State, uuid: string) {
const player = state.players.find((p) => p.uuid === uuid);
if (player) {
player.playing = false;
player.worker?.postMessage({ type: "player-left", uuid });
player.worker?.postMessage({
type: "player-left",
uuid,
nick: player.nick,
});
player.worker = undefined;
saveState(state);
}
}

export const removePlayer = (state: State, uuid: string) => {
const player = state.players.find((p) => p.uuid === uuid);
player?.worker?.postMessage({ type: "player-left", uuid });
player?.worker?.postMessage({ type: "player-left", uuid, nick: player.nick });
state.players = state.players.filter((p) => p.uuid !== uuid);

saveState(state);

if (player) {
for (const listener of Object.values(state.uiListeners)) {
listener({
type: "player-left",
uuid,
nick: player.nick,
});
}
}
};

export const startGame = (state: State, mode: State["mode"]) => {
Expand Down
15 changes: 15 additions & 0 deletions src/pages/scoreboard/scoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const PlayerRow = ({ player }: { player: Player }) => {
<li
class="flex flex-row justify-between bg-base-300 px-4 py-2 rounded-2xl bg-opacity-30 shadow-lg z-1"
id={`player-${player.nick}`}
sse-swap={`player-left-${player.nick}`}
hx-swap="delete"
>
<h2 class={`text-xl ${player.color.class}`} safe>
{player.nick}
Expand Down Expand Up @@ -96,6 +98,7 @@ export const scoreboardPlugin = basePluginSetup()
sse-swap="player-joined-chart"
hx-swap="none"
/>
<span class="hidden" sse-swap="player-left-chart" hx-swap="none" />
</div>
<script>
{htmx.is
Expand Down Expand Up @@ -157,6 +160,18 @@ export const scoreboardPlugin = basePluginSetup()
: [];
}

if (event.type === "player-left") {
return [
{
event: `player-left-${event.nick}`,
},
{
event: "player-left-chart",
data: JSON.stringify({ nick: event.nick }),
},
];
}

return [];
},
]);
Expand Down

0 comments on commit 2634212

Please sign in to comment.