From 922bc6a1f52d8f01600e9a61ce31963075ec59a5 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 22 Aug 2024 00:46:06 +0900 Subject: refactor(frontend): organize PlayerInfo --- frontend/app/components/GolfWatchApp.client.tsx | 48 +++++++++++++++---------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'frontend/app/components/GolfWatchApp.client.tsx') diff --git a/frontend/app/components/GolfWatchApp.client.tsx b/frontend/app/components/GolfWatchApp.client.tsx index d09a4ae..9eacb2d 100644 --- a/frontend/app/components/GolfWatchApp.client.tsx +++ b/frontend/app/components/GolfWatchApp.client.tsx @@ -2,7 +2,7 @@ import { useEffect, useState } from "react"; import { AudioController } from "../.client/audio/AudioController"; import type { components } from "../.server/api/schema"; import useWebSocket, { ReadyState } from "../hooks/useWebSocket"; -import type { PlayerInfo } from "../models/PlayerInfo"; +import type { PlayerState } from "../types/PlayerState"; import GolfWatchAppConnecting from "./GolfWatchApps/GolfWatchAppConnecting"; import GolfWatchAppGaming from "./GolfWatchApps/GolfWatchAppGaming"; import GolfWatchAppStarting from "./GolfWatchApps/GolfWatchAppStarting"; @@ -73,12 +73,14 @@ export default function GolfWatchApp({ } }, [gameState, startedAt, game.duration_seconds, audioController]); - const playerA = game.players[0]; - const playerB = game.players[1]; + const playerA = game.players[0]!; + const playerB = game.players[1]!; - const [playerInfoA, setPlayerInfoA] = useState({ - displayName: playerA?.display_name ?? null, - iconPath: playerA?.icon_path ?? null, + const playerProfileA = { + displayName: playerA.display_name, + iconPath: playerA.icon_path ?? null, + }; + const [playerStateA, setPlayerStateA] = useState({ score: null, code: "", submitResult: { @@ -92,9 +94,11 @@ export default function GolfWatchApp({ })), }, }); - const [playerInfoB, setPlayerInfoB] = useState({ - displayName: playerB?.display_name ?? null, - iconPath: playerB?.icon_path ?? null, + const playerProfileB = { + displayName: playerB.display_name, + iconPath: playerB.icon_path ?? null, + }; + const [playerStateB, setPlayerStateB] = useState({ score: null, code: "", submitResult: { @@ -138,12 +142,12 @@ export default function GolfWatchApp({ } else if (lastJsonMessage.type === "watcher:s2c:code") { const { player_id, code } = lastJsonMessage.data; const setter = - player_id === playerA?.user_id ? setPlayerInfoA : setPlayerInfoB; + player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; setter((prev) => ({ ...prev, code })); } else if (lastJsonMessage.type === "watcher:s2c:submit") { const { player_id } = lastJsonMessage.data; const setter = - player_id === playerA?.user_id ? setPlayerInfoA : setPlayerInfoB; + player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; setter((prev) => ({ ...prev, submitResult: { @@ -160,7 +164,7 @@ export default function GolfWatchApp({ const { player_id, testcase_id, status, stdout, stderr } = lastJsonMessage.data; const setter = - player_id === playerA?.user_id ? setPlayerInfoA : setPlayerInfoB; + player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; setter((prev) => { const ret = { ...prev }; ret.submitResult = { @@ -181,7 +185,7 @@ export default function GolfWatchApp({ } else if (lastJsonMessage.type === "watcher:s2c:submitresult") { const { player_id, status, score } = lastJsonMessage.data; const setter = - player_id === playerA?.user_id ? setPlayerInfoA : setPlayerInfoB; + player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; setter((prev) => { const ret = { ...prev }; ret.submitResult = { @@ -235,8 +239,8 @@ export default function GolfWatchApp({ lastJsonMessage, readyState, gameState, - playerA?.user_id, - playerB?.user_id, + playerA.user_id, + playerB.user_id, ]); if (gameState === "connecting") { @@ -245,8 +249,8 @@ export default function GolfWatchApp({ return ( ); } else if (gameState === "starting") { @@ -262,8 +266,14 @@ export default function GolfWatchApp({ gameDisplayName={game.display_name} gameDurationSeconds={game.duration_seconds} leftTimeSeconds={leftTimeSeconds!} - playerInfoA={playerInfoA} - playerInfoB={playerInfoB} + playerInfoA={{ + profile: playerProfileA, + state: playerStateA, + }} + playerInfoB={{ + profile: playerProfileB, + state: playerStateB, + }} problemTitle={game.problem.title} problemDescription={game.problem.description} gameResult={null /* TODO */} -- cgit v1.2.3-70-g09d2 From c6ab1db6688f26880503e59b165cba4849f924be Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 22 Aug 2024 12:09:04 +0900 Subject: feat(frontend): jotai for watch app --- frontend/app/components/GolfWatchApp.client.tsx | 295 ++++++++------------- .../GolfWatchApps/GolfWatchAppGaming.tsx | 76 +++--- .../GolfWatchApps/GolfWatchAppStarting.tsx | 13 +- frontend/app/routes/golf.$gameId.watch.tsx | 162 ++++++++++- frontend/app/states/watch.ts | 247 +++++++++++++++++ 5 files changed, 553 insertions(+), 240 deletions(-) (limited to 'frontend/app/components/GolfWatchApp.client.tsx') diff --git a/frontend/app/components/GolfWatchApp.client.tsx b/frontend/app/components/GolfWatchApp.client.tsx index 9eacb2d..7ea2ced 100644 --- a/frontend/app/components/GolfWatchApp.client.tsx +++ b/frontend/app/components/GolfWatchApp.client.tsx @@ -1,8 +1,21 @@ -import { useEffect, useState } from "react"; +import { useAtomValue, useSetAtom } from "jotai"; +import { useCallback, useEffect } from "react"; +import { useTimer } from "react-use-precision-timer"; import { AudioController } from "../.client/audio/AudioController"; import type { components } from "../.server/api/schema"; import useWebSocket, { ReadyState } from "../hooks/useWebSocket"; -import type { PlayerState } from "../types/PlayerState"; +import { + gameStartAtom, + gameStateKindAtom, + handleWsCodeMessageAtom, + handleWsConnectionClosedAtom, + handleWsExecResultMessageAtom, + handleWsSubmitMessageAtom, + handleWsSubmitResultMessageAtom, + setCurrentTimestampAtom, + setGameStateConnectingAtom, + setGameStateWaitingAtom, +} from "../states/watch"; import GolfWatchAppConnecting from "./GolfWatchApps/GolfWatchAppConnecting"; import GolfWatchAppGaming from "./GolfWatchApps/GolfWatchAppGaming"; import GolfWatchAppStarting from "./GolfWatchApps/GolfWatchAppStarting"; @@ -13,8 +26,6 @@ type GameWatcherMessageC2S = never; type Game = components["schemas"]["Game"]; -type GameState = "connecting" | "waiting" | "starting" | "gaming" | "finished"; - export type Props = { game: Game; sockToken: string; @@ -31,87 +42,47 @@ export default function GolfWatchApp({ ? `ws://localhost:8002/iosdc-japan/2024/code-battle/sock/golf/${game.game_id}/watch?token=${sockToken}` : `wss://t.nil.ninja/iosdc-japan/2024/code-battle/sock/golf/${game.game_id}/watch?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 handleWsCodeMessage = useSetAtom(handleWsCodeMessageAtom); + const handleWsSubmitMessage = useSetAtom(handleWsSubmitMessageAtom); + const handleWsExecResultMessage = useSetAtom(handleWsExecResultMessageAtom); + const handleWsSubmitResultMessage = useSetAtom( + handleWsSubmitResultMessageAtom, + ); + + useTimer({ delay: 1000, startImmediately: true }, setCurrentTimestamp); + const { lastJsonMessage, readyState } = useWebSocket< GameWatcherMessageS2C, GameWatcherMessageC2S >(socketUrl); - const [gameState, setGameState] = useState("connecting"); - - const [startedAt, setStartedAt] = useState(null); - - const [leftTimeSeconds, setLeftTimeSeconds] = useState(null); - - useEffect(() => { - if ( - (gameState === "starting" || gameState === "gaming") && - startedAt !== null - ) { - const timer = setInterval(() => { - setLeftTimeSeconds((prev) => { - if (prev === null) { - return null; - } - if (prev <= 1) { - const nowSec = Math.floor(Date.now() / 1000); - const finishedAt = startedAt + game.duration_seconds; - if (nowSec >= finishedAt) { - clearInterval(timer); - setGameState("finished"); - audioController.playSoundEffectFinish(); - } else { - setGameState("gaming"); - } - } - return prev - 1; - }); - }, 1000); - - return () => { - clearInterval(timer); - }; - } - }, [gameState, startedAt, game.duration_seconds, audioController]); - const playerA = game.players[0]!; const playerB = game.players[1]!; + const getTargetAtomByPlayerId: ( + player_id: number, + atomA: T, + atomB: T, + ) => T = useCallback( + (player_id, atomA, atomB) => + player_id === playerA.user_id ? atomA : atomB, + [playerA.user_id], + ); + const playerProfileA = { displayName: playerA.display_name, iconPath: playerA.icon_path ?? null, }; - const [playerStateA, setPlayerStateA] = useState({ - score: null, - code: "", - submitResult: { - status: "waiting_submission", - execResults: game.exec_steps.map((r) => ({ - testcase_id: r.testcase_id, - status: "waiting_submission", - label: r.label, - stdout: "", - stderr: "", - })), - }, - }); const playerProfileB = { displayName: playerB.display_name, iconPath: playerB.icon_path ?? null, }; - const [playerStateB, setPlayerStateB] = useState({ - score: null, - code: "", - submitResult: { - status: "waiting_submission", - execResults: game.exec_steps.map((r) => ({ - testcase_id: r.testcase_id, - status: "waiting_submission", - label: r.label, - stdout: "", - stderr: "", - })), - }, - }); if (readyState === ReadyState.UNINSTANTIATED) { throw new Error("WebSocket is not connected"); @@ -119,133 +90,92 @@ export default function GolfWatchApp({ useEffect(() => { if (readyState === ReadyState.CLOSING || readyState === ReadyState.CLOSED) { - if (gameState !== "finished") { - setGameState("connecting"); - } + handleWsConnectionClosed(); } else if (readyState === ReadyState.CONNECTING) { - setGameState("connecting"); + setGameStateConnecting(); } else if (readyState === ReadyState.OPEN) { if (lastJsonMessage !== null) { console.log(lastJsonMessage.type); if (lastJsonMessage.type === "watcher:s2c:start") { - if ( - gameState !== "starting" && - gameState !== "gaming" && - gameState !== "finished" - ) { - const { start_at } = lastJsonMessage.data; - setStartedAt(start_at); - const nowSec = Math.floor(Date.now() / 1000); - setLeftTimeSeconds(start_at - nowSec); - setGameState("starting"); - } + const { start_at } = lastJsonMessage.data; + gameStart(start_at); } else if (lastJsonMessage.type === "watcher:s2c:code") { - const { player_id, code } = lastJsonMessage.data; - const setter = - player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; - setter((prev) => ({ ...prev, code })); + handleWsCodeMessage( + lastJsonMessage.data, + getTargetAtomByPlayerId, + (player_id, code) => { + const baseKey = `watcherState:${game.game_id}:${player_id}`; + window.localStorage.setItem(`${baseKey}:code`, code); + }, + ); } else if (lastJsonMessage.type === "watcher:s2c:submit") { - const { player_id } = lastJsonMessage.data; - const setter = - player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; - setter((prev) => ({ - ...prev, - submitResult: { - status: "running", - execResults: prev.submitResult.execResults.map((r) => ({ - ...r, - status: "running", - stdout: "", - stderr: "", - })), + handleWsSubmitMessage( + lastJsonMessage.data, + getTargetAtomByPlayerId, + (player_id, submissionResult) => { + const baseKey = `watcherState:${game.game_id}:${player_id}`; + window.localStorage.setItem( + `${baseKey}:submissionResult`, + JSON.stringify(submissionResult), + ); }, - })); + ); } else if (lastJsonMessage.type === "watcher:s2c:execresult") { - const { player_id, testcase_id, status, stdout, stderr } = - lastJsonMessage.data; - const setter = - player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; - setter((prev) => { - const ret = { ...prev }; - ret.submitResult = { - ...prev.submitResult, - execResults: prev.submitResult.execResults.map((r) => - r.testcase_id === testcase_id && r.status === "running" - ? { - ...r, - status, - stdout, - stderr, - } - : r, - ), - }; - return ret; - }); + handleWsExecResultMessage( + lastJsonMessage.data, + getTargetAtomByPlayerId, + (player_id, submissionResult) => { + const baseKey = `watcherState:${game.game_id}:${player_id}`; + window.localStorage.setItem( + `${baseKey}:submissionResult`, + JSON.stringify(submissionResult), + ); + }, + ); } else if (lastJsonMessage.type === "watcher:s2c:submitresult") { - const { player_id, status, score } = lastJsonMessage.data; - const setter = - player_id === playerA.user_id ? setPlayerStateA : setPlayerStateB; - setter((prev) => { - const ret = { ...prev }; - ret.submitResult = { - ...prev.submitResult, - status, - }; - if (status === "success") { - if (score) { - if (ret.score === null || score < ret.score) { - ret.score = score; - } - } - } else { - ret.submitResult.execResults = prev.submitResult.execResults.map( - (r) => - r.status === "running" ? { ...r, status: "canceled" } : r, + handleWsSubmitResultMessage( + lastJsonMessage.data, + getTargetAtomByPlayerId, + (player_id, submissionResult, score) => { + const baseKey = `watcherState:${game.game_id}:${player_id}`; + window.localStorage.setItem( + `${baseKey}:submissionResult`, + JSON.stringify(submissionResult), ); - } - return ret; - }); + window.localStorage.setItem( + `${baseKey}:score`, + score === null ? "" : score.toString(), + ); + }, + ); } } else { if (game.started_at) { - const nowSec = Math.floor(Date.now() / 1000); - if (game.started_at <= nowSec) { - // The game has already started. - if (gameState !== "gaming" && gameState !== "finished") { - setStartedAt(game.started_at); - setLeftTimeSeconds(game.started_at - nowSec); - setGameState("gaming"); - } - } else { - // The game is starting. - if ( - gameState !== "starting" && - gameState !== "gaming" && - gameState !== "finished" - ) { - setStartedAt(game.started_at); - setLeftTimeSeconds(game.started_at - nowSec); - setGameState("starting"); - } - } + gameStart(game.started_at); } else { - setGameState("waiting"); + setGameStateWaiting(); } } } }, [ game.started_at, + game.game_id, lastJsonMessage, readyState, - gameState, - playerA.user_id, - playerB.user_id, + gameStart, + getTargetAtomByPlayerId, + handleWsCodeMessage, + handleWsConnectionClosed, + handleWsExecResultMessage, + handleWsSubmitMessage, + handleWsSubmitResultMessage, + setGameStateConnecting, + setGameStateWaiting, ]); - if (gameState === "connecting") { + if (gameStateKind === "connecting") { return ; - } else if (gameState === "waiting") { + } else if (gameStateKind === "waiting") { return ( ); - } else if (gameState === "starting") { - return ( - - ); - } else if (gameState === "gaming" || gameState === "finished") { + } else if (gameStateKind === "starting") { + return ; + } else if (gameStateKind === "gaming" || gameStateKind === "finished") { return ( { - const k = gameDurationSeconds + leftTimeSeconds; - if (k <= 0) { - return "00:00"; - } - const m = Math.floor(k / 60); - const s = k % 60; + const m = Math.floor(leftTimeSeconds / 60); + const s = leftTimeSeconds % 60; return `${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`; })(); @@ -49,43 +59,43 @@ export default function GolfWatchAppGaming({
- {playerInfoA.profile.iconPath && ( + {playerProfileA.iconPath && ( )}
Player 1
-
{playerInfoA.profile.displayName}
+
{playerProfileA.displayName}
-
{playerInfoA.state.score}
+
{scoreA}
{gameDisplayName}
{gameResult ? gameResult === "winA" - ? `勝者 ${playerInfoA.profile.displayName}` + ? `勝者 ${playerProfileA.displayName}` : gameResult === "winB" - ? `勝者 ${playerInfoB.profile.displayName}` + ? `勝者 ${playerProfileB.displayName}` : "引き分け" : leftTime}
-
{playerInfoB.state.score}
+
{scoreB}
Player 2
-
{playerInfoB.profile.displayName}
+
{playerProfileB.displayName}
- {playerInfoB.profile.iconPath && ( + {playerProfileB.iconPath && ( )} @@ -93,17 +103,17 @@ export default function GolfWatchAppGaming({
- +
- - + +
@@ -112,7 +122,7 @@ export default function GolfWatchAppGaming({ {problemDescription}
- +
); diff --git a/frontend/app/components/GolfWatchApps/GolfWatchAppStarting.tsx b/frontend/app/components/GolfWatchApps/GolfWatchAppStarting.tsx index cd4195d..684d2af 100644 --- a/frontend/app/components/GolfWatchApps/GolfWatchAppStarting.tsx +++ b/frontend/app/components/GolfWatchApps/GolfWatchAppStarting.tsx @@ -1,18 +1,19 @@ +import { useAtomValue } from "jotai"; +import { startingLeftTimeSecondsAtom } from "../../states/watch"; + type Props = { gameDisplayName: string; - leftTimeSeconds: number; }; -export default function GolfWatchAppStarting({ - gameDisplayName, - leftTimeSeconds, -}: Props) { +export default function GolfWatchAppStarting({ gameDisplayName }: Props) { + const leftTimeSeconds = useAtomValue(startingLeftTimeSecondsAtom)!; + return (
{gameDisplayName}
-
+
{leftTimeSeconds}
diff --git a/frontend/app/routes/golf.$gameId.watch.tsx b/frontend/app/routes/golf.$gameId.watch.tsx index 7e90b2d..f04f6b0 100644 --- a/frontend/app/routes/golf.$gameId.watch.tsx +++ b/frontend/app/routes/golf.$gameId.watch.tsx @@ -1,10 +1,21 @@ import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node"; -import { useLoaderData } from "@remix-run/react"; -import { ClientOnly } from "remix-utils/client-only"; +import { ClientLoaderFunctionArgs, useLoaderData } from "@remix-run/react"; +import { useHydrateAtoms } from "jotai/utils"; import { apiGetGame, apiGetToken } from "../.server/api/client"; import { ensureUserLoggedIn } from "../.server/auth"; import GolfWatchAppWithAudioPlayRequest from "../components/GolfWatchAppWithAudioPlayRequest.client"; import GolfWatchAppConnecting from "../components/GolfWatchApps/GolfWatchAppConnecting"; +import { + codeAAtom, + codeBAtom, + scoreAAtom, + scoreBAtom, + setCurrentTimestampAtom, + setDurationSecondsAtom, + submitResultAAtom, + submitResultBAtom, +} from "../states/watch"; +import { PlayerState } from "../types/PlayerState"; export const meta: MetaFunction = ({ data }) => [ { @@ -27,23 +38,150 @@ export async function loader({ params, request }: LoaderFunctionArgs) { const [game, sockToken] = await Promise.all([fetchGame(), fetchSockToken()]); if (game.game_type !== "1v1") { - return new Response("Not Found", { status: 404 }); + throw new Response("Not Found", { status: 404 }); } + const playerStateA: PlayerState = { + code: "", + score: null, + submitResult: { + status: "waiting_submission", + execResults: game.exec_steps.map((r) => ({ + testcase_id: r.testcase_id, + status: "waiting_submission", + label: r.label, + stdout: "", + stderr: "", + })), + }, + }; + const playerStateB = structuredClone(playerStateA); + return { game, sockToken, + playerStateA, + playerStateB, }; } +export async function clientLoader({ serverLoader }: ClientLoaderFunctionArgs) { + const data = await serverLoader(); + + const playerIdA = data.game.players[0]?.user_id; + const playerIdB = data.game.players[1]?.user_id; + + if (playerIdA !== null) { + const baseKeyA = `watcherState:${data.game.game_id}:${playerIdA}`; + + const localCodeA = (() => { + const rawValue = window.localStorage.getItem(`${baseKeyA}:code`); + + if (rawValue === null) { + return null; + } + return rawValue; + })(); + + const localScoreA = (() => { + const rawValue = window.localStorage.getItem(`${baseKeyA}:score`); + if (rawValue === null || rawValue === "") { + return null; + } + return Number(rawValue); + })(); + + const localSubmissionResultA = (() => { + const rawValue = window.localStorage.getItem( + `${baseKeyA}:submissionResult`, + ); + if (rawValue === null) { + return null; + } + const parsed = JSON.parse(rawValue); + if (typeof parsed !== "object") { + return null; + } + return parsed; + })(); + + if (localCodeA !== null) { + data.playerStateA.code = localCodeA; + } + if (localScoreA !== null) { + data.playerStateA.score = localScoreA; + } + if (localSubmissionResultA !== null) { + data.playerStateA.submitResult = localSubmissionResultA; + } + } + + if (playerIdB !== null) { + const baseKeyB = `watcherState:${data.game.game_id}:${playerIdB}`; + + const localCodeB = (() => { + const rawValue = window.localStorage.getItem(`${baseKeyB}:code`); + if (rawValue === null) { + return null; + } + return rawValue; + })(); + + const localScoreB = (() => { + const rawValue = window.localStorage.getItem(`${baseKeyB}:score`); + if (rawValue === null || rawValue === "") { + return null; + } + return Number(rawValue); + })(); + + const localSubmissionResultB = (() => { + const rawValue = window.localStorage.getItem( + `${baseKeyB}:submissionResult`, + ); + if (rawValue === null) { + return null; + } + const parsed = JSON.parse(rawValue); + if (typeof parsed !== "object") { + return null; + } + return parsed; + })(); + + if (localCodeB !== null) { + data.playerStateB.code = localCodeB; + } + if (localScoreB !== null) { + data.playerStateB.score = localScoreB; + } + if (localSubmissionResultB !== null) { + data.playerStateB.submitResult = localSubmissionResultB; + } + } + + return data; +} +clientLoader.hydrate = true; + +export function HydrateFallback() { + return ; +} + export default function GolfWatch() { - const { game, sockToken } = useLoaderData(); - - return ( - }> - {() => ( - - )} - - ); + const { game, sockToken, playerStateA, playerStateB } = + useLoaderData(); + + useHydrateAtoms([ + [setCurrentTimestampAtom, undefined], + [setDurationSecondsAtom, game.duration_seconds], + [codeAAtom, playerStateA.code], + [codeBAtom, playerStateB.code], + [scoreAAtom, playerStateA.score], + [scoreBAtom, playerStateB.score], + [submitResultAAtom, playerStateA.submitResult], + [submitResultBAtom, playerStateB.submitResult], + ]); + + return ; } diff --git a/frontend/app/states/watch.ts b/frontend/app/states/watch.ts index e69de29..3b80f2f 100644 --- a/frontend/app/states/watch.ts +++ b/frontend/app/states/watch.ts @@ -0,0 +1,247 @@ +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({ + kind: "connecting", + startedAtTimestamp: null, +}); + +export type GameStateKind = + | "connecting" + | "waiting" + | "starting" + | "gaming" + | "finished"; + +export const gameStateKindAtom = atom((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(0); +export const setDurationSecondsAtom = atom(null, (_, set, value: number) => + set(rawDurationSecondsAtom, value), +); + +export const startingLeftTimeSecondsAtom = atom((get) => { + const { startedAtTimestamp } = get(rawGameStateAtom); + if (startedAtTimestamp === null) { + return null; + } + const currentTimestamp = get(rawCurrentTimestampAtom); + return Math.max(0, startedAtTimestamp - currentTimestamp); +}); + +export const gamingLeftTimeSecondsAtom = atom((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 codeAAtom = atom(""); +export const codeBAtom = atom(""); +export const scoreAAtom = atom(null); +export const scoreBAtom = atom(null); +export const submitResultAAtom = atom({ + status: "waiting_submission", + execResults: [], +}); +export const submitResultBAtom = atom({ + status: "waiting_submission", + execResults: [], +}); + +type GameWatcherMessageS2CSubmitPayload = + components["schemas"]["GameWatcherMessageS2CSubmitPayload"]; +type GameWatcherMessageS2CCodePayload = + components["schemas"]["GameWatcherMessageS2CCodePayload"]; +type GameWatcherMessageS2CExecResultPayload = + components["schemas"]["GameWatcherMessageS2CExecResultPayload"]; +type GameWatcherMessageS2CSubmitResultPayload = + components["schemas"]["GameWatcherMessageS2CSubmitResultPayload"]; + +export const handleWsCodeMessageAtom = atom( + null, + ( + _, + set, + data: GameWatcherMessageS2CCodePayload, + getTarget: (player_id: number, atomA: T, atomB: T) => T, + callback: (player_id: number, code: string) => void, + ) => { + const { player_id, code } = data; + const codeAtom = getTarget(player_id, codeAAtom, codeBAtom); + set(codeAtom, code); + callback(player_id, code); + }, +); + +export const handleWsSubmitMessageAtom = atom( + null, + ( + get, + set, + data: GameWatcherMessageS2CSubmitPayload, + getTarget: (player_id: number, atomA: T, atomB: T) => T, + callback: (player_id: number, submissionResult: SubmitResult) => void, + ) => { + const { player_id } = data; + const submitResultAtom = getTarget( + player_id, + submitResultAAtom, + submitResultBAtom, + ); + const prev = get(submitResultAtom); + const newResult = { + status: "running" as const, + execResults: prev.execResults.map((r) => ({ + ...r, + status: "running" as const, + stdout: "", + stderr: "", + })), + }; + set(submitResultAtom, newResult); + callback(player_id, newResult); + }, +); + +export const handleWsExecResultMessageAtom = atom( + null, + ( + get, + set, + data: GameWatcherMessageS2CExecResultPayload, + getTarget: (player_id: number, atomA: T, atomB: T) => T, + callback: (player_id: number, submissionResult: SubmitResult) => void, + ) => { + const { player_id, testcase_id, status, stdout, stderr } = data; + const submitResultAtom = getTarget( + player_id, + submitResultAAtom, + submitResultBAtom, + ); + 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(player_id, newResult); + }, +); + +export const handleWsSubmitResultMessageAtom = atom( + null, + ( + get, + set, + data: GameWatcherMessageS2CSubmitResultPayload, + getTarget: (player_id: number, atomA: T, atomB: T) => T, + callback: ( + player_id: number, + submissionResult: SubmitResult, + score: number | null, + ) => void, + ) => { + const { player_id, status, score } = data; + const submitResultAtom = getTarget( + player_id, + submitResultAAtom, + submitResultBAtom, + ); + const scoreAtom = getTarget(player_id, scoreAAtom, scoreBAtom); + 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(player_id, newResult, score); + }, +); -- cgit v1.2.3-70-g09d2 From dfd33e58a5e6f830d60e978afad7348f7a16068d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 22 Aug 2024 13:27:44 +0900 Subject: feat(frontend): add minimal style to watch with audio play request --- frontend/app/.client/audio/AudioController.ts | 11 +++++++ frontend/app/components/GolfWatchApp.client.tsx | 8 +---- .../GolfWatchAppWithAudioPlayRequest.client.tsx | 38 ++++++++++------------ frontend/app/states/watch.ts | 3 ++ 4 files changed, 33 insertions(+), 27 deletions(-) (limited to 'frontend/app/components/GolfWatchApp.client.tsx') diff --git a/frontend/app/.client/audio/AudioController.ts b/frontend/app/.client/audio/AudioController.ts index 6ed6180..296f685 100644 --- a/frontend/app/.client/audio/AudioController.ts +++ b/frontend/app/.client/audio/AudioController.ts @@ -51,6 +51,17 @@ export class AudioController { }); } + async playDummySoundEffect(): Promise { + const audio = this.audioElements["good_1"]; + if (!audio) { + return; + } + audio.muted = true; + audio.currentTime = 0; + await audio.play(); + audio.muted = false; + } + async playSoundEffect(soundEffect: SoundEffect): Promise { const audio = this.audioElements[soundEffect]; if (!audio) { diff --git a/frontend/app/components/GolfWatchApp.client.tsx b/frontend/app/components/GolfWatchApp.client.tsx index 7ea2ced..c8b1d53 100644 --- a/frontend/app/components/GolfWatchApp.client.tsx +++ b/frontend/app/components/GolfWatchApp.client.tsx @@ -1,7 +1,6 @@ import { useAtomValue, useSetAtom } from "jotai"; import { useCallback, useEffect } from "react"; import { useTimer } from "react-use-precision-timer"; -import { AudioController } from "../.client/audio/AudioController"; import type { components } from "../.server/api/schema"; import useWebSocket, { ReadyState } from "../hooks/useWebSocket"; import { @@ -29,14 +28,9 @@ type Game = components["schemas"]["Game"]; export type Props = { game: Game; sockToken: string; - audioController: AudioController; }; -export default function GolfWatchApp({ - game, - sockToken, - audioController, -}: Props) { +export default function GolfWatchApp({ game, sockToken }: Props) { const socketUrl = process.env.NODE_ENV === "development" ? `ws://localhost:8002/iosdc-japan/2024/code-battle/sock/golf/${game.game_id}/watch?token=${sockToken}` diff --git a/frontend/app/components/GolfWatchAppWithAudioPlayRequest.client.tsx b/frontend/app/components/GolfWatchAppWithAudioPlayRequest.client.tsx index e299f4b..ce5a59c 100644 --- a/frontend/app/components/GolfWatchAppWithAudioPlayRequest.client.tsx +++ b/frontend/app/components/GolfWatchAppWithAudioPlayRequest.client.tsx @@ -1,35 +1,33 @@ -import { useState } from "react"; +import { useAtom } from "jotai"; import { AudioController } from "../.client/audio/AudioController"; +import { audioControllerAtom } from "../states/watch"; import GolfWatchApp, { type Props } from "./GolfWatchApp.client"; +import SubmitButton from "./SubmitButton"; export default function GolfWatchAppWithAudioPlayRequest({ game, sockToken, }: Omit) { - const [audioController, setAudioController] = - useState(null); + const [audioController, setAudioController] = useAtom(audioControllerAtom); const audioPlayPermitted = audioController !== null; if (audioPlayPermitted) { - return ( - - ); + return ; } else { return ( -
- +
+
+ { + const audioController = new AudioController(); + await audioController.loadAll(); + await audioController.playDummySoundEffect(); + setAudioController(audioController); + }} + > + 開始 + +
); } diff --git a/frontend/app/states/watch.ts b/frontend/app/states/watch.ts index 3b80f2f..ba3dd2a 100644 --- a/frontend/app/states/watch.ts +++ b/frontend/app/states/watch.ts @@ -1,4 +1,5 @@ import { atom } from "jotai"; +import { AudioController } from "../.client/audio/AudioController"; import type { components } from "../.server/api/schema"; import type { SubmitResult } from "../types/SubmitResult"; @@ -245,3 +246,5 @@ export const handleWsSubmitResultMessageAtom = atom( callback(player_id, newResult, score); }, ); + +export const audioControllerAtom = atom(null); -- cgit v1.2.3-70-g09d2