1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import { useContext, useEffect, useState } from "react";
import { useTimer } from "react-use-precision-timer";
import {
ApiAuthTokenContext,
apiGetGame,
apiGetGameWatchLatestStates,
apiGetGameWatchRanking,
} from "../api/client";
import type { components } from "../api/schema";
import {
gameStateKindAtom,
rankingAtom,
setCurrentTimestampAtom,
setGameStartedAtAtom,
setLatestGameStatesAtom,
} from "../states/watch";
import GolfWatchAppGaming1v1 from "./GolfWatchApps/GolfWatchAppGaming1v1";
import GolfWatchAppGamingMultiplayer from "./GolfWatchApps/GolfWatchAppGamingMultiplayer";
import GolfWatchAppLoading from "./GolfWatchApps/GolfWatchAppLoading";
import GolfWatchAppStarting from "./GolfWatchApps/GolfWatchAppStarting";
import GolfWatchAppWaiting1v1 from "./GolfWatchApps/GolfWatchAppWaiting1v1";
import GolfWatchAppWaitingMultiplayer from "./GolfWatchApps/GolfWatchAppWaitingMultiplayer";
type Game = components["schemas"]["Game"];
export type Props = {
game: Game;
};
export default function GolfWatchApp({ game }: Props) {
const apiAuthToken = useContext(ApiAuthTokenContext);
const gameStateKind = useAtomValue(gameStateKindAtom);
const setGameStartedAt = useSetAtom(setGameStartedAtAtom);
const setCurrentTimestamp = useSetAtom(setCurrentTimestampAtom);
const setLatestGameStates = useSetAtom(setLatestGameStatesAtom);
const [ranking, setRanking] = useAtom(rankingAtom);
useTimer({ delay: 1000, startImmediately: true }, setCurrentTimestamp);
const playerA = game.main_players[0];
const playerB = game.main_players[1];
const playerProfileA = playerA
? {
id: playerA.user_id,
displayName: playerA.display_name,
iconPath: playerA.icon_path ?? null,
}
: null;
const playerProfileB = playerB
? {
id: playerB.user_id,
displayName: playerB.display_name,
iconPath: playerB.icon_path ?? null,
}
: null;
const [isDataPolling, setIsDataPolling] = useState(false);
useEffect(() => {
if (isDataPolling) {
return;
}
const timerId = setInterval(async () => {
if (isDataPolling) {
return;
}
setIsDataPolling(true);
try {
if (gameStateKind === "waiting") {
const { game: g } = await apiGetGame(apiAuthToken, game.game_id);
if (g.started_at != null) {
setGameStartedAt(g.started_at);
}
} else if (gameStateKind === "gaming") {
const { states } = await apiGetGameWatchLatestStates(
apiAuthToken,
game.game_id,
);
setLatestGameStates(states);
const { ranking } = await apiGetGameWatchRanking(
apiAuthToken,
game.game_id,
);
setRanking(ranking);
}
} catch (error) {
console.error(error);
} finally {
setIsDataPolling(false);
}
}, 1000);
return () => {
clearInterval(timerId);
};
}, [
isDataPolling,
apiAuthToken,
game.game_id,
gameStateKind,
setGameStartedAt,
setLatestGameStates,
setRanking,
]);
if (gameStateKind === "loading") {
return <GolfWatchAppLoading />;
} else if (gameStateKind === "waiting") {
return game.game_type === "1v1" ? (
<GolfWatchAppWaiting1v1
gameDisplayName={game.display_name}
playerProfileA={playerProfileA!}
playerProfileB={playerProfileB!}
/>
) : (
<GolfWatchAppWaitingMultiplayer gameDisplayName={game.display_name} />
);
} else if (gameStateKind === "starting") {
return <GolfWatchAppStarting gameDisplayName={game.display_name} />;
} else if (gameStateKind === "gaming" || gameStateKind === "finished") {
return game.game_type === "1v1" ? (
<GolfWatchAppGaming1v1
gameDisplayName={game.display_name}
playerProfileA={playerProfileA!}
playerProfileB={playerProfileB!}
problemTitle={game.problem.title}
problemDescription={game.problem.description}
sampleCode={game.problem.sample_code}
gameResult={null /* TODO */}
/>
) : (
<GolfWatchAppGamingMultiplayer
gameDisplayName={game.display_name}
ranking={ranking}
problemTitle={game.problem.title}
problemDescription={game.problem.description}
sampleCode={game.problem.sample_code}
gameResult={null /* TODO */}
/>
);
}
}
|