From 0c9ba333ea1f2e01a203075ad6d29752e344a4d8 Mon Sep 17 00:00:00 2001
From: Martin Berg Alstad <600878@stud.hvl.no>
Date: Thu, 20 Jul 2023 16:14:27 +0200
Subject: [PATCH] Added createGame button
---
.../src/components/gameComponent.tsx | 9 ++---
pac-man-board-game/ClientApp/src/game/map.ts | 6 +++
.../ClientApp/src/pages/lobby.tsx | 39 ++++++++++++++++---
.../ClientApp/src/types/types.d.ts | 5 +++
.../Controllers/GameController.cs | 2 +-
pac-man-board-game/GameStuff/Items/Player.cs | 5 ++-
6 files changed, 52 insertions(+), 14 deletions(-)
diff --git a/pac-man-board-game/ClientApp/src/components/gameComponent.tsx b/pac-man-board-game/ClientApp/src/components/gameComponent.tsx
index d011d93..701ed44 100644
--- a/pac-man-board-game/ClientApp/src/components/gameComponent.tsx
+++ b/pac-man-board-game/ClientApp/src/components/gameComponent.tsx
@@ -3,12 +3,11 @@ import {AllDice} from "./dice";
import {doAction, GameAction} from "../utils/actions";
import GameBoard from "./gameBoard";
import WebSocketService from "../websockets/WebSocketService";
-import {getCharacterSpawns} from "../game/map";
+import {getPacManSpawns} from "../game/map";
import Player from "../game/player";
import PlayerStats from "../components/playerStats";
import {useAtom, useAtomValue, useSetAtom} from "jotai";
import {diceAtom, ghostsAtom, playersAtom, rollDiceButtonAtom, selectedDiceAtom} from "../utils/state";
-import {CharacterType} from "../game/character";
import GameButton from "./gameButton";
const wsService = new WebSocketService(import.meta.env.VITE_API_WS);
@@ -66,10 +65,8 @@ export const GameComponent: FC<{ player: Player, map: GameMap }> = ({player, map
wsService.send({
Action: GameAction.playerInfo,
Data: {
- Player: player, Spawns: getCharacterSpawns(map)
- .filter(s => s.type === CharacterType.pacMan)
- .map(s => s.position)
- }
+ Player: player, Spawns: getPacManSpawns(map)
+ } as PlayerInfoData
});
}
diff --git a/pac-man-board-game/ClientApp/src/game/map.ts b/pac-man-board-game/ClientApp/src/game/map.ts
index 0e50342..8b7730b 100644
--- a/pac-man-board-game/ClientApp/src/game/map.ts
+++ b/pac-man-board-game/ClientApp/src/game/map.ts
@@ -40,4 +40,10 @@ export function getCharacterSpawns(map: GameMap): { type: CharacterType, positio
}
return result;
+}
+
+export function getPacManSpawns(map: GameMap): DirectionalPosition[] {
+ return getCharacterSpawns(map)
+ .filter(s => s.type === CharacterType.pacMan)
+ .map(s => s.position)
}
\ No newline at end of file
diff --git a/pac-man-board-game/ClientApp/src/pages/lobby.tsx b/pac-man-board-game/ClientApp/src/pages/lobby.tsx
index a1cba24..b9a4b26 100644
--- a/pac-man-board-game/ClientApp/src/pages/lobby.tsx
+++ b/pac-man-board-game/ClientApp/src/pages/lobby.tsx
@@ -3,6 +3,8 @@ import {atom, useAtomValue} from "jotai";
import {Button} from "../components/button";
import {thisPlayerAtom} from "../utils/state";
import {getData, postData} from "../utils/api";
+import {getPacManSpawns, testMap} from "../game/map";
+import {useNavigate} from "react-router-dom";
const fetchAtom = atom(async () => {
const response = await getData("/game/all");
@@ -10,11 +12,38 @@ const fetchAtom = atom(async () => {
});
// TODO create game button
-const LobbyPage: FC = () => ( // TODO check if player is defined in storage, if not redirect to login
-
-
-
-)
+const LobbyPage: FC = () => { // TODO check if player is defined in storage, if not redirect to login
+
+ const thisPlayer = useAtomValue(thisPlayerAtom);
+ const navigate = useNavigate();
+
+ async function createGame(): Promise {
+
+ const response = await postData("/game/create", {
+ body: {Player: thisPlayer, Spawns: getPacManSpawns(testMap)} as PlayerInfoData
+ });
+
+ const data = await response.json();
+
+ if (response.ok) {
+ console.debug("Game created: ", data);
+ // TODO redirect to game page
+ } else {
+ console.error("Error: ", data);
+ // TODO display error
+ }
+
+ }
+
+ return (
+ <>
+
+
+
+
+ >
+ );
+}
export default LobbyPage;
diff --git a/pac-man-board-game/ClientApp/src/types/types.d.ts b/pac-man-board-game/ClientApp/src/types/types.d.ts
index 7223e37..5a59c7d 100644
--- a/pac-man-board-game/ClientApp/src/types/types.d.ts
+++ b/pac-man-board-game/ClientApp/src/types/types.d.ts
@@ -53,3 +53,8 @@ type ApiRequest = {
headers?: HeadersInit,
body?: any
}
+
+type PlayerInfoData = {
+ readonly Player: PlayerProps,
+ readonly Spawns: DirectionalPosition[],
+}
diff --git a/pac-man-board-game/Controllers/GameController.cs b/pac-man-board-game/Controllers/GameController.cs
index 87964ef..7a89127 100644
--- a/pac-man-board-game/Controllers/GameController.cs
+++ b/pac-man-board-game/Controllers/GameController.cs
@@ -51,7 +51,7 @@ public class GameController : GenericController // TODO reconnect using player i
}
}
- [HttpPost("createGame")]
+ [HttpPost("create")]
public IActionResult CreateGame([FromBody] PlayerInfoData data)
{
Logger.Log(LogLevel.Debug, "Creating game");
diff --git a/pac-man-board-game/GameStuff/Items/Player.cs b/pac-man-board-game/GameStuff/Items/Player.cs
index e3b4236..927e2f3 100644
--- a/pac-man-board-game/GameStuff/Items/Player.cs
+++ b/pac-man-board-game/GameStuff/Items/Player.cs
@@ -28,6 +28,7 @@ public class Player : IPlayer, IEquatable
return Username == other.Username;
}
+ // [JsonPropertyName("username")]
public required string Username { get; init; }
public required Character PacMan { get; init; }
public required string Colour { get; init; }
@@ -49,9 +50,9 @@ public class Player : IPlayer, IEquatable
Username = user.Username,
PacMan = new Character
{
- Colour = user.Colour,
+ Colour = user.Colour ?? "white",
Type = CharacterType.PacMan
},
- Colour = user.Colour
+ Colour = user.Colour ?? "white"
};
}