diff --git a/ui/api/game.api.ts b/ui/api/game.api.ts index 00a0df9..ca4b769 100644 --- a/ui/api/game.api.ts +++ b/ui/api/game.api.ts @@ -1,18 +1,20 @@ import wretch from "wretch"; import { Cell } from "../types"; -const USER_ID_HEADER = 'Player-id'; +const USER_ID_HEADER = "Player-id"; export const fetchGameState = async ( playerId: string, gameId: number -): Promise => { - const response = await wretch(`/game/${gameId}`) +): Promise<{ cells: Cell[]; cellWidth: number }> => { + const response: { board: Cell[][] } = await wretch(`/api/game/${gameId}`) .headers({ - USER_ID_HEADER: playerId + [USER_ID_HEADER]: playerId, }) .get() .json(); - console.log(response); - return []; + return { + cells: response.board.flat(), + cellWidth: response.board[0].length, + }; }; diff --git a/ui/components/board.tsx b/ui/components/board.tsx index 1e04adb..f6f4537 100644 --- a/ui/components/board.tsx +++ b/ui/components/board.tsx @@ -16,7 +16,7 @@ const Board = (props: BoardProps) => { }} > {props.cells.map((cell) => ( -
{cell.terrainType}
+
{cell.piece}
))} ); diff --git a/ui/components/game.tsx b/ui/components/game.tsx index facf1a0..e6283d4 100644 --- a/ui/components/game.tsx +++ b/ui/components/game.tsx @@ -1,32 +1,26 @@ import React, { useState, useEffect } from "react"; import { fetchGameState } from "../api/game.api"; +import { Cell } from "../types"; import Board from "./board"; -const Game = () => { +interface GameProps { + gameId: number; +} + +const Game = (props: GameProps) => { const [isLoading, setLoading] = useState(false); const [cellWidth, setCellWidth] = useState(4); - const [cells, setCells] = useState([ - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - { terrainType: 1 }, - ]); + const [cells, setCells] = useState([] as Cell[]); useEffect(() => { setLoading(true); - fetchGameState("red", 1).then(console.log); - }); + fetchGameState("red", props.gameId).then( + ({ cells: cellList, cellWidth: width }) => { + setCellWidth(width); + setCells(cellList); + setLoading(false); + } + ); + }, [props.gameId]); return ; }; diff --git a/ui/pages/index.tsx b/ui/pages/index.tsx index d0dc116..0b16e8e 100644 --- a/ui/pages/index.tsx +++ b/ui/pages/index.tsx @@ -11,7 +11,7 @@ const Home: NextPage = () => (
- +
); diff --git a/ui/styles/board.module.css b/ui/styles/board.module.css index 88f52e0..ddf1ee2 100644 --- a/ui/styles/board.module.css +++ b/ui/styles/board.module.css @@ -4,5 +4,5 @@ .gridCell { border: 1px solid black; - padding-top: 91%; /* 100% is supposed to be 1:1 aspect ratio but it doesn't look like it */ + padding-top: 100%; /* 100% is supposed to be 1:1 aspect ratio but it doesn't look like it */ } \ No newline at end of file diff --git a/ui/types.ts b/ui/types.ts index 4618879..774c563 100644 --- a/ui/types.ts +++ b/ui/types.ts @@ -1,9 +1,6 @@ -export interface Piece { - rank: number; - owner: number; -} - export interface Cell { - piece?: Piece; - terrainType: number; + piece?: string; + terrain: boolean; + hidden: boolean; + empty: boolean; } \ No newline at end of file