Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add player to scoreboard when they join #24

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"outline.collapseItems": "alwaysExpand",
"editor.foldingImportsByDefault": false
"editor.foldingImportsByDefault": false,
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
13 changes: 10 additions & 3 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ htmx.on("htmx:sseMessage", (evt) => {
sortScoreboard();
}

// If the event is a player-chart event, update the chart with the new data
// If the event is a player-score-chart event, update the chart with the new score data
if (
evt instanceof CustomEvent &&
evt.detail.type.startsWith("player-chart-")
evt.detail.type.startsWith("player-score-chart-")
) {
const nick = evt.detail.type.replace("player-chart-", "");
const nick = evt.detail.type.replace("player-score-chart-", "");
const data = JSON.parse(evt.detail.data);
const dataset = chart.data.datasets.find((d) => d.label === nick);

Expand All @@ -179,4 +179,11 @@ htmx.on("htmx:sseMessage", (evt) => {
chart.update();
}
}

// If the event is a player-joined-chart event, add the player to the chart
if (evt instanceof CustomEvent && evt.detail.type === "player-joined-chart") {
const data = JSON.parse(evt.detail.data);
chart.data.datasets.push(data);
chart.update();
}
});
44 changes: 41 additions & 3 deletions src/pages/scoreboard/scoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const PlayerRow = ({ player }: { player: Player }) => {
</span>
<span // Use a hidden element to swap the chart data, don't actually swap json into the DOM
class="hidden"
sse-swap={`player-chart-${player.nick}`}
sse-swap={`player-score-chart-${player.nick}`}
hx-swap="none"
/>
</li>
Expand All @@ -38,7 +38,9 @@ const PlayerList = ({ players }: PlayerTableProps) => {
return (
<ul
id="scoreboard-list"
class="auto-animate flex flex-col gap-2 z-10 bg-opacity-80 backdrop-blur-sm drop-shadow-lg max-h-[80vh] overflow-y-scroll scrollbar-w-none"
class="auto-animate ßflex flex-col gap-2 z-10 bg-opacity-80 backdrop-blur-sm drop-shadow-lg max-h-[80vh] overflow-y-scroll scrollbar-w-none"
sse-swap="player-joined"
hx-swap="afterbegin"
>
{players.map((player) => (
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
Expand Down Expand Up @@ -89,6 +91,11 @@ export const scoreboardPlugin = basePluginSetup()
<div class="epic-dark" />
<canvas id="score-board-chart" class="relative" />
</div>
<span // Use a hidden element to swap the chart data, don't actually swap json into the DOM
class="hidden"
sse-swap="player-joined-chart"
hx-swap="none"
/>
</div>
<script>
{htmx.is
Expand All @@ -110,7 +117,7 @@ export const scoreboardPlugin = basePluginSetup()
data: `${event.log.score}`,
},
{
event: `player-chart-${event.nick}`,
event: `player-score-chart-${event.nick}`,
data: JSON.stringify({
x: new Date(event.log.date).toISOString(),
y: event.log.score,
Expand All @@ -119,6 +126,37 @@ export const scoreboardPlugin = basePluginSetup()
];
}

if (event.type === "player-joined") {
const player = state.players.find((p) => p.uuid === event.uuid);
return player
? [
{
event: "player-joined",
data: `${<PlayerRow player={player} />}`,
},
{
// Pass chart data config as SSE event
event: "player-joined-chart",
data: JSON.stringify({
label: player.nick,
pointRadius: 3,
backgroundColor: player.color.hex,
data: [
{
x: new Date().toISOString(),
y: player.log[0]?.score ?? 0,
},
],
borderColor: player.color.hex,
borderWidth: 2,
fill: false,
tension: 0.3,
}),
},
]
: [];
}

return [];
},
]);
Expand Down