Skip to content

Commit

Permalink
fix: append game events (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Dec 13, 2024
1 parent 8a57763 commit 0b57a35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/games/plugins/sync-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export default fp(async app => {
}

if (before.events.length < after.events.length) {
app.gateway.broadcast(async () => await GameEventList({ game: after }))
const n = before.events.length - after.events.length
const newEvents = after.events.slice(n)
for (const event of newEvents) {
app.gateway.broadcast(async () => await GameEventList.append({ game: after, event }))
}
}

await Promise.all(
Expand Down
16 changes: 13 additions & 3 deletions src/games/views/html/game-event-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const renderedEvents = [
]

export async function GameEventList(props: { game: GameModel }) {
const events = props.game.events
.filter(({ event }) => renderedEvents.includes(event))
.toSorted((a, b) => b.at.getTime() - a.at.getTime())
const events = props.game.events.toSorted((a, b) => b.at.getTime() - a.at.getTime())

return (
<div
Expand All @@ -42,7 +40,19 @@ export async function GameEventList(props: { game: GameModel }) {
)
}

GameEventList.append = function (props: { game: GameModel; event: GameEventModel }) {
return (
<div id={`game-${props.game.number}-event-list`} hx-swap-oob="afterbegin">
<GameEvent {...props} />
</div>
)
}

function GameEvent(props: { event: GameEventModel; game: GameModel }) {
if (!renderedEvents.includes(props.event.event)) {
return <></>
}

return (
<div
class={[
Expand Down

0 comments on commit 0b57a35

Please sign in to comment.