Skip to content

Commit

Permalink
feat: game live indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc committed Aug 7, 2024
1 parent f2ab4d8 commit 19f8c9f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/games/plugins/update-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { LogsLink } from '../views/html/logs-link'
import { PlayerConnectionStatusIndicator } from '../views/html/player-connection-status-indicator'
import { GameEventList } from '../views/html/game-event-list'
import { GameSlot } from '../views/html/game-slot'
import { whenGameEnds } from '../when-game-ends'
import { GamesLink } from '../../html/components/games-link'

// eslint-disable-next-line @typescript-eslint/require-await
export default fp(async app => {
Expand Down Expand Up @@ -44,6 +46,18 @@ export default fp(async app => {
}
})

events.on('game:created', async () => {
const cmp = await GamesLink()
app.gateway.broadcast(() => cmp)
})

events.on('game:updated', d =>
whenGameEnds(d, async () => {
const cmp = await GamesLink()
app.gateway.broadcast(() => cmp)
}),
)

events.on('match/player:connected', async ({ steamId }) => {
const player = await collections.players.findOne({ steamId })
if (!player) {
Expand Down
19 changes: 19 additions & 0 deletions src/games/when-game-ends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GameState } from '../database/models/game.model'
import type { Events } from '../events'

type EventProps = Events['game:updated']

export async function whenGameEnds(
{ before, after }: EventProps,
fn: (props: EventProps) => Promise<void> | void,
) {
if (
before.state !== after.state &&
[GameState.created, GameState.configuring, GameState.launching, GameState.started].includes(
before.state,
) &&
[GameState.ended, GameState.interrupted].includes(after.state)
) {
await fn({ before, after })
}
}
17 changes: 17 additions & 0 deletions src/html/components/games-link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { collections } from '../../database/collections'
import { GameState } from '../../database/models/game.model'
import { GameLiveIndicator } from './game-live-indicator'

export async function GamesLink() {
const gamesLiveCount = await collections.games.countDocuments({
state: {
$in: [GameState.created, GameState.configuring, GameState.launching, GameState.started],
},
})
return (
<a href="/games" class={['menu-item', gamesLiveCount > 0 && 'accent']} id="navbar-games-link">
{gamesLiveCount > 0 ? <GameLiveIndicator /> : <></>}
Games
</a>
)
}
3 changes: 2 additions & 1 deletion src/html/components/navigation-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { User } from '../../auth/types/user'
import { GamesLink } from './games-link'
import { IconBrandDiscord, IconChartPie, IconCrown, IconHeart } from './icons'
import { IconBrandSteam } from './icons/icon-brand-steam'
import { Profile } from './profile'
Expand Down Expand Up @@ -29,7 +30,7 @@ function Menu(props: Html.PropsWithChildren<{ user?: User | undefined }>) {

return (
<div class="flex flex-col gap-[10px] px-4 lg:flex-row lg:items-center lg:px-0">
<MenuItem href="/games">Games</MenuItem>
<GamesLink />
<MenuItem href="/players">Players</MenuItem>
<MenuItem href="/rules">Rules</MenuItem>

Expand Down
4 changes: 4 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ dialog::backdrop {
&.active {
background-color: theme(colors.abru.dark.20);
}

&.accent {
color: theme(colors.accent.DEFAULT);
}
}

.profile-menu-item {
Expand Down

0 comments on commit 19f8c9f

Please sign in to comment.