diff options
Diffstat (limited to 'frontend/app/states/play.ts')
| -rw-r--r-- | frontend/app/states/play.ts | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/frontend/app/states/play.ts b/frontend/app/states/play.ts index 22d338c..a8a4727 100644 --- a/frontend/app/states/play.ts +++ b/frontend/app/states/play.ts @@ -4,121 +4,121 @@ import type { SupportedLanguage } from "../types/SupportedLanguage"; const gameStartedAtAtom = atom<number | null>(null); export const setGameStartedAtAtom = atom(null, (_, set, value: number | null) => - set(gameStartedAtAtom, value), + set(gameStartedAtAtom, value), ); export type GameStateKind = - | "loading" - | "waiting" - | "starting" - | "gaming" - | "finished"; + | "loading" + | "waiting" + | "starting" + | "gaming" + | "finished"; type ExecutionStatus = components["schemas"]["ExecutionStatus"]; type LatestGameState = components["schemas"]["LatestGameState"]; export const gameStateKindAtom = atom<GameStateKind>((get) => { - const now = get(currentTimestampAtom); - if (!now) { - return "loading"; - } - const startedAt = get(gameStartedAtAtom); - if (!startedAt) { - return "waiting"; - } - const durationSeconds = get(durationSecondsAtom); - const finishedAt = startedAt + durationSeconds; - if (now < startedAt) { - return "starting"; - } else if (now < finishedAt) { - return "gaming"; - } else { - return "finished"; - } + const now = get(currentTimestampAtom); + if (!now) { + return "loading"; + } + const startedAt = get(gameStartedAtAtom); + if (!startedAt) { + return "waiting"; + } + const durationSeconds = get(durationSecondsAtom); + const finishedAt = startedAt + durationSeconds; + if (now < startedAt) { + return "starting"; + } else if (now < finishedAt) { + return "gaming"; + } else { + return "finished"; + } }); const currentTimestampAtom = atom<number | null>(null); export const setCurrentTimestampAtom = atom(null, (_, set) => - set(currentTimestampAtom, Math.floor(Date.now() / 1000)), + set(currentTimestampAtom, Math.floor(Date.now() / 1000)), ); const durationSecondsAtom = atom<number>(0); export const setDurationSecondsAtom = atom(null, (_, set, value: number) => - set(durationSecondsAtom, value), + set(durationSecondsAtom, value), ); export const startingLeftTimeSecondsAtom = atom<number | null>((get) => { - const startedAt = get(gameStartedAtAtom); - if (startedAt === null) { - return null; - } - const currentTimestamp = get(currentTimestampAtom); - if (currentTimestamp === null) { - return null; - } - return Math.max(0, startedAt - currentTimestamp); + const startedAt = get(gameStartedAtAtom); + if (startedAt === null) { + return null; + } + const currentTimestamp = get(currentTimestampAtom); + if (currentTimestamp === null) { + return null; + } + return Math.max(0, startedAt - currentTimestamp); }); export const gamingLeftTimeSecondsAtom = atom<number | null>((get) => { - const startedAt = get(gameStartedAtAtom); - if (startedAt === null) { - return null; - } - const durationSeconds = get(durationSecondsAtom); - const finishedAt = startedAt + durationSeconds; - const currentTimestamp = get(currentTimestampAtom); - if (currentTimestamp === null) { - return null; - } - return Math.min(durationSeconds, Math.max(0, finishedAt - currentTimestamp)); + const startedAt = get(gameStartedAtAtom); + if (startedAt === null) { + return null; + } + const durationSeconds = get(durationSecondsAtom); + const finishedAt = startedAt + durationSeconds; + const currentTimestamp = get(currentTimestampAtom); + if (currentTimestamp === null) { + return null; + } + return Math.min(durationSeconds, Math.max(0, finishedAt - currentTimestamp)); }); const rawStatusAtom = atom<ExecutionStatus>("none"); const rawScoreAtom = atom<number | null>(null); export const statusAtom = atom<ExecutionStatus>((get) => { - const isSubmittingCode = get(isSubmittingCodeAtom); - if (isSubmittingCode) { - return "running"; - } else { - return get(rawStatusAtom); - } + const isSubmittingCode = get(isSubmittingCodeAtom); + if (isSubmittingCode) { + return "running"; + } else { + return get(rawStatusAtom); + } }); export const scoreAtom = atom<number | null>((get) => { - return get(rawScoreAtom); + return get(rawScoreAtom); }); const isSubmittingCodeAtom = atom(false); export const handleSubmitCodePreAtom = atom(null, (_, set) => { - set(isSubmittingCodeAtom, true); + set(isSubmittingCodeAtom, true); }); export const handleSubmitCodePostAtom = atom(null, (_, set) => { - set(isSubmittingCodeAtom, false); + set(isSubmittingCodeAtom, false); }); export const setLatestGameStateAtom = atom( - null, - (_, set, value: LatestGameState) => { - set(rawStatusAtom, value.status); - set(rawScoreAtom, value.score); - }, + null, + (_, set, value: LatestGameState) => { + set(rawStatusAtom, value.status); + set(rawScoreAtom, value.score); + }, ); function cleanCode(code: string, language: SupportedLanguage) { - if (language === "php") { - return code - .replace(/\s+/g, "") - .replace(/^<\?php/, "") - .replace(/^<\?/, "") - .replace(/\?>$/, ""); - } else { - return code.replace(/\s+/g, ""); - } + if (language === "php") { + return code + .replace(/\s+/g, "") + .replace(/^<\?php/, "") + .replace(/^<\?/, "") + .replace(/\?>$/, ""); + } else { + return code.replace(/\s+/g, ""); + } } export function calcCodeSize( - code: string, - language: SupportedLanguage, + code: string, + language: SupportedLanguage, ): number { - const trimmed = cleanCode(code, language); - const utf8Encoded = new TextEncoder().encode(trimmed); - return utf8Encoded.length; + const trimmed = cleanCode(code, language); + const utf8Encoded = new TextEncoder().encode(trimmed); + return utf8Encoded.length; } |
