-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show player online statuses plus new headlines
- Loading branch information
Showing
10 changed files
with
103 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import useSWR from 'swr' | ||
import buildError from '../utils/buildError' | ||
import { Game } from '../entities/game' | ||
import makeValidatedGetRequest from './makeValidatedGetRequest' | ||
import { z } from 'zod' | ||
import { PlayerHeadlines } from '../entities/playerHeadline' | ||
|
||
const defaultHeadlines: PlayerHeadlines = { | ||
total_players: { count: 0 }, | ||
online_players: { count: 0 } | ||
} | ||
|
||
export default function usePlayerHeadlines(activeGame: Game | null, includeDevData: boolean) { | ||
const fetcher = async ([url]: [string]) => { | ||
const headlines: (keyof PlayerHeadlines)[] = ['total_players', 'online_players'] | ||
const res = await Promise.all(headlines.map((headline) => makeValidatedGetRequest(`${url}/${headline}`, z.object({ | ||
count: z.number() | ||
})))) | ||
|
||
return headlines.reduce((acc, curr, idx) => ({ | ||
...acc, | ||
[curr]: res[idx] | ||
}), defaultHeadlines) | ||
} | ||
|
||
const { data, error } = useSWR( | ||
activeGame ? [`/games/${activeGame.id}/headlines`, includeDevData] : null, | ||
fetcher | ||
) | ||
|
||
return { | ||
headlines: data ?? defaultHeadlines, | ||
loading: !data && !error, | ||
error: error && buildError(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { z } from 'zod' | ||
|
||
const countSchema = z.object({ | ||
count: z.number() | ||
}) | ||
|
||
export const playerHeadlinesSchema = z.object({ | ||
total_players: countSchema, | ||
online_players: countSchema | ||
}) | ||
|
||
export type PlayerHeadlines = z.infer<typeof playerHeadlinesSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from 'zod' | ||
|
||
export const playerPresenceSchema = z.object({ | ||
online: z.boolean(), | ||
customStatus: z.string(), | ||
updatedAt: z.string().datetime() | ||
}) | ||
|
||
export type PlayerPresence = z.infer<typeof playerPresenceSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters