Skip to content

Commit 2119655

Browse files
feat: adapt gameplay to api changes
1 parent a07b828 commit 2119655

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

app/src/api/api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
GetServerSidePropsResult,
55
} from "next";
66
import { API_URL, SESSION_COOKIE_NAME } from "./config";
7-
import { GameState, MoveSequence, SubmissionLanguage } from "./models";
7+
import { GameState, MoveSequence, SubmissionLanguage, TurnStatus } from "./models";
88

99
export function requireSession<T extends { [key: string]: any }>(
1010
onSuccess: (
@@ -68,14 +68,14 @@ export async function createGame(
6868

6969
export async function getGameState(
7070
session: string
71-
): Promise<GameState | Error> {
71+
): Promise<TurnStatus | Error> {
7272
return await (await apiCall("game", { session, method: "GET" })).json();
7373
}
7474

7575
export async function makeMove(
7676
moves: MoveSequence,
7777
session: string
78-
): Promise<GameState | Error> {
78+
): Promise<TurnStatus | Error> {
7979
return await (
8080
await apiCall("game", { session, body: moves, method: "POST" })
8181
).json();

app/src/api/models.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ export interface GameState {
33
current_player: Player;
44
}
55

6+
export interface TurnStatus {
7+
game: GameState;
8+
ai_output: string; // Everything printed by the AI on stderr since the start/last move.
9+
}
10+
611
export type Board = (Piece | null)[][];
712

813
export interface Piece {

app/src/components/Board.tsx

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type BoardProps = {
2525
gameOngoing: boolean;
2626
currentTurn: Player | null;
2727
setCurrentTurn: (player: Player) => void;
28+
board: BoardState;
29+
setBoard: (board: BoardState) => void;
2830
};
2931

3032
export default function Board({
@@ -33,8 +35,10 @@ export default function Board({
3335
gameOngoing,
3436
currentTurn,
3537
setCurrentTurn,
38+
board,
39+
setBoard,
3640
}: BoardProps) {
37-
const [board, setBoard] = useState(initialBoards[player]);
41+
3842
const [currentMoveSequence, setCurrentMoveSequence] = useState<MoveSequence>(
3943
[]
4044
);
@@ -98,13 +102,13 @@ export default function Board({
98102
player == Player.White ? Player.Black : Player.White
99103
); // switch turn
100104
makeMove(currentMoveSequence, username).then(
101-
(game) => {
102-
if (game instanceof Error) {
103-
alert(game.message);
105+
(turnStatus) => {
106+
if (turnStatus instanceof Error) {
107+
alert(turnStatus.message);
104108
} else {
105109
// TODO: add buffer time before updating board ?
106-
setBoard(game.board); // update board with server response
107-
setCurrentTurn(game.current_player);
110+
setBoard(turnStatus.game.board); // update board with server response
111+
setCurrentTurn(turnStatus.game.current_player);
108112
}
109113
},
110114
(error) => {

app/src/pages/index.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Board from '@/components/Board';
66
import {
77
Board as BoardState,
88
emptyBoard,
9+
initialBoards,
910
Player,
1011
SubmissionLanguage,
1112
} from '../api/models';
@@ -27,6 +28,7 @@ export default function Home({ username }: { username: string }) {
2728
const [gameOngoing, setGameOngoing] = useState(false);
2829
const [player, setPlayer] = useState(Player.White);
2930
const [currentTurn, setCurrentTurn] = useState<Player | null>(null);
31+
const [board, setBoard] = useState(initialBoards[player]);
3032

3133
useEffect(() => {
3234
getInitialCode(SubmissionLanguage.Java).then((code) => setFile(code));
@@ -87,6 +89,8 @@ export default function Home({ username }: { username: string }) {
8789
alert(game.message);
8890
} else {
8991
setGameOngoing(true);
92+
setBoard(game.board);
93+
setCurrentTurn(game.current_player);
9094
}
9195
},
9296
(err) => alert(err.message)
@@ -156,11 +160,14 @@ export default function Home({ username }: { username: string }) {
156160
gameOngoing={gameOngoing}
157161
currentTurn={currentTurn}
158162
setCurrentTurn={setCurrentTurn}
163+
board={board}
164+
setBoard={setBoard}
159165
/>
160166
</div>
161167
<div className="game-info">
162168
<p>Game State: {gameOngoing ? 'Ongoing' : 'Stopped'}</p>
163169
<p>Current Turn: {currentTurn ?? 'None'}</p>
170+
<p>Player: {player}</p>
164171
</div>
165172
</div>
166173
</div>

0 commit comments

Comments
 (0)