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
|
import { atom } from "jotai";
import type { components } from "../.server/api/schema";
import type { SubmitResult } from "../types/SubmitResult";
type RawGameState =
| {
kind: "connecting";
startedAtTimestamp: null;
}
| {
kind: "waiting";
startedAtTimestamp: null;
}
| {
kind: "starting";
startedAtTimestamp: number;
};
const rawGameStateAtom = atom<RawGameState>({
kind: "connecting",
startedAtTimestamp: null,
});
export type GameStateKind =
| "connecting"
| "waiting"
| "starting"
| "gaming"
| "finished";
export const gameStateKindAtom = atom<GameStateKind>((get) => {
const { kind: rawKind, startedAtTimestamp } = get(rawGameStateAtom);
if (rawKind === "connecting" || rawKind === "waiting") {
return rawKind;
} else {
const durationSeconds = get(rawDurationSecondsAtom);
const finishedAtTimestamp = startedAtTimestamp + durationSeconds;
const currentTimestamp = get(rawCurrentTimestampAtom);
if (currentTimestamp < startedAtTimestamp) {
return "starting";
} else if (currentTimestamp < finishedAtTimestamp) {
return "gaming";
} else {
return "finished";
}
}
});
export const gameStartAtom = atom(null, (get, set, value: number) => {
const { kind } = get(rawGameStateAtom);
if (kind === "starting") {
return;
}
set(rawGameStateAtom, {
kind: "starting",
startedAtTimestamp: value,
});
});
export const setGameStateConnectingAtom = atom(null, (_, set) =>
set(rawGameStateAtom, { kind: "connecting", startedAtTimestamp: null }),
);
export const setGameStateWaitingAtom = atom(null, (_, set) =>
set(rawGameStateAtom, { kind: "waiting", startedAtTimestamp: null }),
);
const rawCurrentTimestampAtom = atom(0);
export const setCurrentTimestampAtom = atom(null, (_, set) =>
set(rawCurrentTimestampAtom, Math.floor(Date.now() / 1000)),
);
const rawDurationSecondsAtom = atom<number>(0);
export const setDurationSecondsAtom = atom(null, (_, set, value: number) =>
set(rawDurationSecondsAtom, value),
);
export const startingLeftTimeSecondsAtom = atom<number | null>((get) => {
const { startedAtTimestamp } = get(rawGameStateAtom);
if (startedAtTimestamp === null) {
return null;
}
const currentTimestamp = get(rawCurrentTimestampAtom);
return Math.max(0, startedAtTimestamp - currentTimestamp);
});
export const gamingLeftTimeSecondsAtom = atom<number | null>((get) => {
const { startedAtTimestamp } = get(rawGameStateAtom);
if (startedAtTimestamp === null) {
return null;
}
const durationSeconds = get(rawDurationSecondsAtom);
const finishedAtTimestamp = startedAtTimestamp + durationSeconds;
const currentTimestamp = get(rawCurrentTimestampAtom);
return Math.min(
durationSeconds,
Math.max(0, finishedAtTimestamp - currentTimestamp),
);
});
export const handleWsConnectionClosedAtom = atom(null, (get, set) => {
const kind = get(gameStateKindAtom);
if (kind !== "finished") {
set(setGameStateConnectingAtom);
}
});
export const scoreAtom = atom<number | null>(null);
export const submitResultAtom = atom<SubmitResult>({
status: "waiting_submission",
execResults: [],
});
export const handleSubmitCodeAtom = atom(null, (_, set) => {
set(submitResultAtom, (prev) => ({
status: "running",
execResults: prev.execResults.map((r) => ({
...r,
status: "running",
stdout: "",
stderr: "",
})),
}));
});
type GamePlayerMessageS2CExecResultPayload =
components["schemas"]["GamePlayerMessageS2CExecResultPayload"];
type GamePlayerMessageS2CSubmitResultPayload =
components["schemas"]["GamePlayerMessageS2CSubmitResultPayload"];
export const handleWsExecResultMessageAtom = atom(
null,
(
get,
set,
data: GamePlayerMessageS2CExecResultPayload,
callback: (submissionResult: SubmitResult) => void,
) => {
const { testcase_id, status, stdout, stderr } = data;
const prev = get(submitResultAtom);
const newResult = {
...prev,
execResults: prev.execResults.map((r) =>
r.testcase_id === testcase_id && r.status === "running"
? {
...r,
status,
stdout,
stderr,
}
: r,
),
};
set(submitResultAtom, newResult);
callback(newResult);
},
);
export const handleWsSubmitResultMessageAtom = atom(
null,
(
get,
set,
data: GamePlayerMessageS2CSubmitResultPayload,
callback: (submissionResult: SubmitResult, score: number | null) => void,
) => {
const { status, score } = data;
const prev = get(submitResultAtom);
const newResult = {
...prev,
status,
};
if (status !== "success") {
newResult.execResults = prev.execResults.map((r) =>
r.status === "running" ? { ...r, status: "canceled" } : r,
);
}
set(submitResultAtom, newResult);
if (status === "success" && score !== null) {
const currentScore = get(scoreAtom);
if (currentScore === null || score < currentScore) {
set(scoreAtom, score);
}
}
callback(newResult, score);
},
);
|