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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
import { useAtomValue, useSetAtom } from "jotai";
import { useEffect } from "react";
import { useTimer } from "react-use-precision-timer";
import { useDebouncedCallback } from "use-debounce";
import type { components } from "../.server/api/schema";
import useWebSocket, { ReadyState } from "../hooks/useWebSocket";
import {
gameStartAtom,
gameStateKindAtom,
handleSubmitCodeAtom,
handleWsConnectionClosedAtom,
handleWsExecResultMessageAtom,
handleWsSubmitResultMessageAtom,
setCurrentTimestampAtom,
setGameStateConnectingAtom,
setGameStateWaitingAtom,
} from "../states/play";
import GolfPlayAppConnecting from "./GolfPlayApps/GolfPlayAppConnecting";
import GolfPlayAppFinished from "./GolfPlayApps/GolfPlayAppFinished";
import GolfPlayAppGaming from "./GolfPlayApps/GolfPlayAppGaming";
import GolfPlayAppStarting from "./GolfPlayApps/GolfPlayAppStarting";
import GolfPlayAppWaiting from "./GolfPlayApps/GolfPlayAppWaiting";
type GamePlayerMessageS2C = components["schemas"]["GamePlayerMessageS2C"];
type GamePlayerMessageC2S = components["schemas"]["GamePlayerMessageC2S"];
type Game = components["schemas"]["Game"];
type User = components["schemas"]["User"];
type Props = {
game: Game;
player: User;
initialCode: string;
sockToken: string;
};
export default function GolfPlayApp({
game,
player,
initialCode,
sockToken,
}: Props) {
const socketUrl =
process.env.NODE_ENV === "development"
? `ws://localhost:8003/phperkaigi/2025/code-battle/sock/golf/${game.game_id}/play?token=${sockToken}`
: `wss://t.nil.ninja/phperkaigi/2025/code-battle/sock/golf/${game.game_id}/play?token=${sockToken}`;
const gameStateKind = useAtomValue(gameStateKindAtom);
const setCurrentTimestamp = useSetAtom(setCurrentTimestampAtom);
const gameStart = useSetAtom(gameStartAtom);
const setGameStateConnecting = useSetAtom(setGameStateConnectingAtom);
const setGameStateWaiting = useSetAtom(setGameStateWaitingAtom);
const handleWsConnectionClosed = useSetAtom(handleWsConnectionClosedAtom);
const handleWsExecResultMessage = useSetAtom(handleWsExecResultMessageAtom);
const handleWsSubmitResultMessage = useSetAtom(
handleWsSubmitResultMessageAtom,
);
const handleSubmitCode = useSetAtom(handleSubmitCodeAtom);
useTimer({ delay: 1000, startImmediately: true }, setCurrentTimestamp);
const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket<
GamePlayerMessageS2C,
GamePlayerMessageC2S
>(socketUrl);
const playerProfile = {
displayName: player.display_name,
iconPath: player.icon_path ?? null,
};
const onCodeChange = useDebouncedCallback((code: string) => {
console.log("player:c2s:code");
sendJsonMessage({
type: "player:c2s:code",
data: { code },
});
const baseKey = `playerState:${game.game_id}:${player.user_id}`;
window.localStorage.setItem(`${baseKey}:code`, code);
}, 1000);
const onCodeSubmit = useDebouncedCallback((code: string) => {
if (code === "") {
return;
}
console.log("player:c2s:submit");
sendJsonMessage({
type: "player:c2s:submit",
data: { code },
});
handleSubmitCode();
}, 1000);
if (readyState === ReadyState.UNINSTANTIATED) {
throw new Error("WebSocket is not connected");
}
useEffect(() => {
if (readyState === ReadyState.CLOSING || readyState === ReadyState.CLOSED) {
handleWsConnectionClosed();
} else if (readyState === ReadyState.CONNECTING) {
setGameStateConnecting();
} else if (readyState === ReadyState.OPEN) {
if (lastJsonMessage !== null) {
console.log(lastJsonMessage.type);
console.log(lastJsonMessage.data);
if (lastJsonMessage.type === "player:s2c:start") {
const { start_at } = lastJsonMessage.data;
gameStart(start_at);
} else if (lastJsonMessage.type === "player:s2c:execresult") {
handleWsExecResultMessage(
lastJsonMessage.data,
(submissionResult) => {
const baseKey = `playerState:${game.game_id}:${player.user_id}`;
window.localStorage.setItem(
`${baseKey}:submissionResult`,
JSON.stringify(submissionResult),
);
},
);
} else if (lastJsonMessage.type === "player:s2c:submitresult") {
handleWsSubmitResultMessage(
lastJsonMessage.data,
(submissionResult, score) => {
const baseKey = `playerState:${game.game_id}:${player.user_id}`;
window.localStorage.setItem(
`${baseKey}:submissionResult`,
JSON.stringify(submissionResult),
);
window.localStorage.setItem(
`${baseKey}:score`,
score === null ? "" : score.toString(),
);
},
);
}
} else {
if (game.started_at) {
gameStart(game.started_at);
} else {
setGameStateWaiting();
}
}
}
}, [
game.game_id,
game.started_at,
player.user_id,
sendJsonMessage,
lastJsonMessage,
readyState,
gameStart,
handleWsConnectionClosed,
handleWsExecResultMessage,
handleWsSubmitResultMessage,
setGameStateConnecting,
setGameStateWaiting,
]);
if (gameStateKind === "connecting") {
return <GolfPlayAppConnecting />;
} else if (gameStateKind === "waiting") {
return (
<GolfPlayAppWaiting
gameDisplayName={game.display_name}
playerProfile={playerProfile}
/>
);
} else if (gameStateKind === "starting") {
return <GolfPlayAppStarting gameDisplayName={game.display_name} />;
} else if (gameStateKind === "gaming") {
return (
<GolfPlayAppGaming
gameDisplayName={game.display_name}
playerProfile={playerProfile}
problemTitle={game.problem.title}
problemDescription={game.problem.description}
initialCode={initialCode}
onCodeChange={onCodeChange}
onCodeSubmit={onCodeSubmit}
/>
);
} else if (gameStateKind === "finished") {
return <GolfPlayAppFinished />;
} else {
return null;
}
}
|