Skip to content

Commit

Permalink
fix: update game score (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Dec 14, 2024
1 parent fdf031a commit 6b283a3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/games/plugins/sync-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GameSlot } from '../views/html/game-slot'
import { whenGameEnds } from '../when-game-ends'
import { GamesLink } from '../../html/components/games-link'
import { safe } from '../../utils/safe'
import { GameScore } from '../views/html/game-score'

// eslint-disable-next-line @typescript-eslint/require-await
export default fp(async app => {
Expand All @@ -25,6 +26,10 @@ export default fp(async app => {
}
}

if (before.score?.blu !== after.score?.blu || before.score?.red !== after.score?.red) {
app.gateway.broadcast(async () => await GameScore({ game: after }))
}

if (
before.connectString !== after.connectString ||
before.stvConnectString !== after.stvConnectString
Expand Down
4 changes: 4 additions & 0 deletions src/games/plugins/track-match-rounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default fp(
await update(
{ number: gameNumber },
{
$set: {
'score.blu': value.score.blu,
'score.red': value.score.red,
},
$push: {
events: {
at: new Date(),
Expand Down
17 changes: 17 additions & 0 deletions src/games/views/html/game-score.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { GameModel } from '../../../database/models/game.model'

export async function GameScore(props: { game: GameModel }) {
return (
<div id={`game-${props.game.number}-score`} class="grid grid-cols-2 gap-[4px]">
<div class="score-header blu">
<span class="uppercase">blu</span>
<span aria-label="blu team score">{props.game.score?.blu ?? ''}</span>
</div>

<div class="score-header red">
<span class="uppercase">red</span>
<span aria-label="red team score">{props.game.score?.red ?? ''}</span>
</div>
</div>
)
}
14 changes: 2 additions & 12 deletions src/games/views/html/game-slot-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GameClassIcon } from '../../../html/components/game-class-icon'
import { tf2ClassOrder } from '../../../shared/tf2-class-order'
import type { SteamId64 } from '../../../shared/types/steam-id-64'
import { Tf2Team } from '../../../shared/types/tf2-team'
import { GameScore } from './game-score'
import { GameSlot } from './game-slot'

export function GameSlotList(props: { game: GameModel; actor?: SteamId64 | undefined }) {
Expand All @@ -14,18 +15,7 @@ export function GameSlotList(props: { game: GameModel; actor?: SteamId64 | undef

return (
<>
<div class="grid grid-cols-2 gap-[4px]">
<div class="score-header blu">
<span class="uppercase">blu</span>
<span>{props.game.score?.blu ?? ''}</span>
</div>

<div class="score-header red">
<span class="uppercase">red</span>
<span>{props.game.score?.red ?? ''}</span>
</div>
</div>

<GameScore game={props.game} />
<div class="slot-list" id={`game-${props.game.number}-slots`}>
{slotPairs.map(({ red, blu, gameClass }) => (
<>
Expand Down
10 changes: 10 additions & 0 deletions tests/20-game/05-report-rounds.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ launchGame('report rounds', async ({ gameNumber, page, gameServer }) => {
await gameServer.matchStarts()
await waitABit(secondsToMilliseconds(1))

await expect(gamePage.page.getByLabel('blu team score')).toHaveText('0')
await expect(gamePage.page.getByLabel('red team score')).toHaveText('0')

await gameServer.roundEnds('blu')
await expect(gamePage.page.getByText('Round ended')).toBeVisible()
await expect(gamePage.page.getByLabel('blu team score')).toHaveText('1')
await expect(gamePage.page.getByLabel('red team score')).toHaveText('0')

await waitABit(secondsToMilliseconds(1))

await gameServer.roundEnds('red')
await expect(gamePage.page.getByText('Round ended')).toHaveCount(2)
await expect(gamePage.page.getByLabel('blu team score')).toHaveText('1')
await expect(gamePage.page.getByLabel('red team score')).toHaveText('1')

await waitABit(secondsToMilliseconds(1))
await gameServer.matchEnds()
})

0 comments on commit 6b283a3

Please sign in to comment.