From ee5f48b1f9c862bf8f24b17564af068a999e481e Mon Sep 17 00:00:00 2001
From: Martin Berg Alstad <600878@stud.hvl.no>
Date: Sat, 15 Jul 2023 15:09:32 +0200
Subject: [PATCH] Fixed not being able to pick up pellets
---
.../.idea/compiler.xml | 6 ++++
BackendTests/Services/ActionServiceTests.cs | 4 +--
.../ClientApp/src/components/gameBoard.tsx | 3 ++
.../src/components/gameComponent.tsx | 2 +-
.../ClientApp/src/utils/actions.ts | 6 ++--
.../ClientApp/src/utils/state.ts | 34 +++++++++++++++++--
pac-man-board-game/Services/ActionService.cs | 2 +-
7 files changed, 48 insertions(+), 9 deletions(-)
create mode 100644 .idea/.idea.pac-man-board-game/.idea/compiler.xml
diff --git a/.idea/.idea.pac-man-board-game/.idea/compiler.xml b/.idea/.idea.pac-man-board-game/.idea/compiler.xml
new file mode 100644
index 0000000..9014410
--- /dev/null
+++ b/.idea/.idea.pac-man-board-game/.idea/compiler.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/BackendTests/Services/ActionServiceTests.cs b/BackendTests/Services/ActionServiceTests.cs
index 35405e4..fd46214 100644
--- a/BackendTests/Services/ActionServiceTests.cs
+++ b/BackendTests/Services/ActionServiceTests.cs
@@ -159,7 +159,7 @@ public class ActionServiceTests
var result = _service.Ready();
// If selected the state is changed to InGame
_whitePlayer.State = State.InGame;
- Assert.That(result.GetType().GetProperty("Starter")?.GetValue(result), Is.EqualTo(_whitePlayer));
+ Assert.That(result.GetType().GetProperty("Starter")?.GetValue(result), Is.EqualTo(_whitePlayer.Name));
}
[Test]
@@ -178,7 +178,7 @@ public class ActionServiceTests
result = _service.Ready();
Assert.That(result.GetType().GetProperty("Starter")?.GetValue(result),
- Is.EqualTo(_whitePlayer).Or.EqualTo(_blackPlayer));
+ Is.EqualTo(_whitePlayer.Name).Or.EqualTo(_blackPlayer.Name));
}
#endregion
diff --git a/pac-man-board-game/ClientApp/src/components/gameBoard.tsx b/pac-man-board-game/ClientApp/src/components/gameBoard.tsx
index 87dfd22..f828cae 100644
--- a/pac-man-board-game/ClientApp/src/components/gameBoard.tsx
+++ b/pac-man-board-game/ClientApp/src/components/gameBoard.tsx
@@ -27,6 +27,9 @@ const Board: Component = (
const [hoveredPosition, setHoveredPosition] = useState();
function handleSelectCharacter(character: Character): void {
+ if (character.isPacMan() && currentPlayer?.PacMan.Colour !== character.Colour) {
+ return;
+ }
setSelectedCharacter(character);
}
diff --git a/pac-man-board-game/ClientApp/src/components/gameComponent.tsx b/pac-man-board-game/ClientApp/src/components/gameComponent.tsx
index 5990d1f..0f0b928 100644
--- a/pac-man-board-game/ClientApp/src/components/gameComponent.tsx
+++ b/pac-man-board-game/ClientApp/src/components/gameComponent.tsx
@@ -13,8 +13,8 @@ import GameButton from "./gameButton";
const wsService = new WebSocketService(import.meta.env.VITE_API);
-// TODO do not allow players to move other players' characters
// TODO do not allow players to roll dice multiple times
+// TODO fix tailwind colours from getBgCssColour
export const GameComponent: Component<{ player: Player }> = ({player}) => {
const players = useAtomValue(playersAtom);
diff --git a/pac-man-board-game/ClientApp/src/utils/actions.ts b/pac-man-board-game/ClientApp/src/utils/actions.ts
index 4eb2ad0..bf7e5ef 100644
--- a/pac-man-board-game/ClientApp/src/utils/actions.ts
+++ b/pac-man-board-game/ClientApp/src/utils/actions.ts
@@ -3,7 +3,7 @@ import {CharacterType, Ghost} from "../game/character";
import {getCharacterSpawns, testMap} from "../game/map";
import {TileType} from "../game/tileType";
import {getDefaultStore} from "jotai";
-import {currentPlayerAtom, diceAtom, ghostsAtom, playersAtom} from "./state";
+import {currentPlayerNameAtom, diceAtom, ghostsAtom, playersAtom} from "./state";
import {Colour} from "../game/colour";
export enum GameAction {
@@ -94,7 +94,7 @@ function playerInfo(data?: PlayerProps[]): void {
}
type ReadyData =
- | { AllReady: true, Starter: PlayerProps, Players: PlayerProps[] }
+ | { AllReady: true, Starter: string, Players: PlayerProps[] }
| { AllReady: false, Players: PlayerProps[] }
| string;
@@ -103,7 +103,7 @@ function ready(data?: ReadyData): void {
const players = data.Players.map(p => new Player(p));
store.set(playersAtom, players);
if (data.AllReady) {
- store.set(currentPlayerAtom, players.find(p => p.Name === data.Starter.Name));
+ store.set(currentPlayerNameAtom, data.Starter);
}
}
}
diff --git a/pac-man-board-game/ClientApp/src/utils/state.ts b/pac-man-board-game/ClientApp/src/utils/state.ts
index 289d670..0be939d 100644
--- a/pac-man-board-game/ClientApp/src/utils/state.ts
+++ b/pac-man-board-game/ClientApp/src/utils/state.ts
@@ -4,11 +4,25 @@ import {atomWithStorage, createJSONStorage} from "jotai/utils";
import {Ghost} from "../game/character";
const playerStorage = createJSONStorage(() => sessionStorage);
-
+/**
+ * All players in the game.
+ */
export const playersAtom = atom([]);
+/**
+ * All player characters (Pac-Man) in the game.
+ */
export const playerCharactersAtom = atom(get => get(playersAtom).map(player => player.PacMan));
+/**
+ * All ghosts in the game.
+ */
export const ghostsAtom = atom([]);
+/**
+ * All characters in the game.
+ */
export const allCharactersAtom = atom(get => [...get(playerCharactersAtom), ...get(ghostsAtom)]);
+/**
+ * The player that is currently using this browser.
+ */
export const thisPlayerAtom = atomWithStorage("player", undefined, {
...playerStorage,
getItem(key, initialValue): Player | undefined {
@@ -16,6 +30,22 @@ export const thisPlayerAtom = atomWithStorage("player", unde
return playerProps ? new Player(playerProps) : undefined;
},
});
+/**
+ * All dice that have been rolled.
+ */
export const diceAtom = atom(undefined);
+/**
+ * The dice that have been selected by the player.
+ */
export const selectedDiceAtom = atom(undefined);
-export const currentPlayerAtom = atom(undefined);
+/**
+ * The name of the player whose turn it is.
+ */
+export const currentPlayerNameAtom = atom(undefined);
+/**
+ * The player whose turn it is.
+ */
+export const currentPlayerAtom = atom(get => {
+ const currentPlayerName = get(currentPlayerNameAtom);
+ return get(playersAtom).find(player => player.Name === currentPlayerName);
+});
diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs
index 5d0f61c..e8992b8 100644
--- a/pac-man-board-game/Services/ActionService.cs
+++ b/pac-man-board-game/Services/ActionService.cs
@@ -83,7 +83,7 @@ public class ActionService : IActionService
{
// TODO roll to start
Group.SetAllInGame();
- data = new { AllReady = true, Players = players, Starter = Group.RandomPlayer };
+ data = new { AllReady = true, Players = players, Starter = Group.RandomPlayer.Name };
}
else
{