Skip to content

Commit

Permalink
fix: Player score not updated (#27)
Browse files Browse the repository at this point in the history
When syncing player answer logs to main thread it only updated the
player log, not the player score. This broke UI that used the
player.score property rather than looking up the last score in the
player.log. Fixed game module so player score is updated correctly.
Changed some UI components to use the player.score property as this
should always be set.
  • Loading branch information
snorremd authored Jul 19, 2024
1 parent 62673ff commit d8cdaff
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/game/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const newWorker = (player: Player) => {
const player = state.players.find((p) => p.uuid === uuid);
if (player) {
player.log.unshift(log);
player.score += log.score;
}
// We need to notify all relevant listeners about the new log
// Multiple listeners can be listening for the same player
Expand Down
2 changes: 1 addition & 1 deletion src/pages/admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const PlayerRow = (player: Player) => {
</td>
<td safe>{player.url}</td>
<td sse-swap={`player-score-${player.uuid}`} hx-swap="innerHTML">
{player.log?.[0]?.score ?? 0}
{player.score ?? 0}
</td>
<td>
{player.playing ? (
Expand Down
6 changes: 3 additions & 3 deletions src/pages/scoreboard/scoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const PlayerRow = ({ player }: { player: Player }) => {
hx-swap="innerHTML"
sse-swap={`player-score-${player.nick}`}
>
{player.log[0]?.score ?? 0}
{player.score}
</span>
<span // Use a hidden element to swap the chart data, don't actually swap json into the DOM
class="hidden"
Expand Down Expand Up @@ -132,7 +132,7 @@ export const scoreboardPlugin = basePluginSetup()
.get("/scoreboard/winners", ({ htmx, store: { state } }) => {
const Layout = htmx.is ? HXLayout : HTMLLayout;
const winners = state.players
.toSorted((a, b) => (b.log[0]?.score ?? 0) - (a.log[0]?.score ?? 0))
.toSorted((a, b) => b.score - a.score)
.slice(0, 3);
const medals = ["🥇", "🥈", "🥉"];
return (
Expand Down Expand Up @@ -198,7 +198,7 @@ export const scoreboardPlugin = basePluginSetup()
data: [
{
x: new Date().toISOString(),
y: player.log[0]?.score ?? 0,
y: player.score,
},
],
borderColor: player.color.hex,
Expand Down

0 comments on commit d8cdaff

Please sign in to comment.