diff --git a/pac-man-board-game/ClientApp/src/components/gameComponent.tsx b/pac-man-board-game/ClientApp/src/components/gameComponent.tsx index cb382ef..0db69eb 100644 --- a/pac-man-board-game/ClientApp/src/components/gameComponent.tsx +++ b/pac-man-board-game/ClientApp/src/components/gameComponent.tsx @@ -8,6 +8,7 @@ import {testMap} from "../game/map"; import {TileType} from "../game/tileType"; import Player, {State} from "../game/player"; import {Colour} from "../game/colour"; +import PlayerStats from "../game/playerStats"; const wsService = new WebSocketService(import.meta.env.VITE_API); @@ -16,12 +17,10 @@ const ghosts = [ new Ghost({Colour: Colour.Purple}), ]; -export const GameComponent: Component<{ player: Player }> = ( - { - player - }) => { +export const GameComponent: Component<{ player: Player }> = ({player}) => { // TODO find spawn points const [characters, setCharacters] = useState(); + const [players, setPlayers] = useState([player]); const [dice, setDice] = useState(); const [selectedDice, setSelectedDice] = useState(); @@ -46,7 +45,7 @@ export const GameComponent: Component<{ player: Player }> = ( rollDice(); } - function doAction(message: MessageEvent): void { + function doAction(message: MessageEvent): void { // TODO move to Service const parsed: ActionMessage = JSON.parse(message.data); switch (parsed.Action) { @@ -59,9 +58,10 @@ export const GameComponent: Component<{ player: Player }> = ( removeEatenPellets(parsed); break; case GameAction.playerInfo: - const players = parsed.Data as PlayerProps[]; - console.log(players); - const pacMen = players.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!)); + const playerProps = parsed.Data as PlayerProps[]; + console.log(playerProps); + setPlayers(playerProps.map(p => new Player(p))); + const pacMen = playerProps.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!)); console.log(pacMen); // TODO find spawn points setCharacters([...pacMen, ...ghosts]); @@ -69,10 +69,9 @@ export const GameComponent: Component<{ player: Player }> = ( case GameAction.ready: const isReady = parsed.Data.AllReady as boolean; if (isReady) { - setCurrentPlayer(parsed.Data.Starter as Player); - } else { - // TODO update player states + setCurrentPlayer(new Player(parsed.Data.Starter as PlayerProps)); } + setPlayers((parsed.Data.Players as PlayerProps[]).map(p => new Player(p))); break; } } @@ -136,21 +135,13 @@ export const GameComponent: Component<{ player: Player }> = ( <>
{ - player.State === State.waitingForPlayers ? + currentPlayer === undefined || currentPlayer.State === State.waitingForPlayers ? : }
- { - (characters?.filter(c => c.isPacMan()) as PacMan[] | undefined)?.map(c => - /*TODO use PlayerStats instead*/ -
-

Player: {player.Colour}

-

Pellets: {player.Box.count}

-

PowerPellets: {player.Box.countPowerPellets}

-
) - } + {players?.map(p => )} {characters && = ( }) => (

Player: {player.Name}

+

Colour: {player.Colour}

{player.State === State.inGame ? <>

Pellets: {player.Box.count}

diff --git a/pac-man-board-game/Services/ActionService.cs b/pac-man-board-game/Services/ActionService.cs index c7d7c2f..e2ceb3d 100644 --- a/pac-man-board-game/Services/ActionService.cs +++ b/pac-man-board-game/Services/ActionService.cs @@ -9,9 +9,9 @@ namespace pacMan.Services; public interface IActionService { void DoAction(ActionMessage message); - void RollDice(ActionMessage message); - void PlayerInfo(ActionMessage message); - void Ready(ActionMessage message); + List RollDice(ActionMessage message); + List PlayerInfo(ActionMessage message); + object Ready(ActionMessage message); } public class ActionService : IActionService // TODO tests @@ -32,54 +32,54 @@ public class ActionService : IActionService // TODO tests public void DoAction(ActionMessage message) { - switch (message.Action) + message.Data = message.Action switch { - case GameAction.RollDice: - RollDice(message); - break; - case GameAction.PlayerInfo: - PlayerInfo(message); - break; - case GameAction.Ready: - Ready(message); - break; - case GameAction.MoveCharacter: - default: - _logger.Log(LogLevel.Information, "Forwarding message to all clients"); - break; - } + GameAction.RollDice => RollDice(message), + + GameAction.PlayerInfo => PlayerInfo(message), + GameAction.Ready => Ready(message), + _ => message.Data + }; } - public void RollDice(ActionMessage message) + public List RollDice(ActionMessage message) { var rolls = _diceCup.Roll(); _logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls)); - message.Data = rolls; + return rolls; } - public void PlayerInfo(ActionMessage message) + public List PlayerInfo(ActionMessage message) { _player = JsonSerializer.Deserialize(message.Data); _group = _wsService.AddPlayer(_player); // TODO missing some data? - message.Data = _group.Players; + return _group.Players; } - public void Ready(ActionMessage message) + public object Ready(ActionMessage message) { + object data; if (_player != null) { var players = _group.SetReady(_player).ToArray(); if (players.All(p => p.State == State.Ready)) + { // TODO roll to start - message.Data = new { AllReady = true, Starter = _group.RandomPlayer }; + _group.SetAllInGame(); + data = new { AllReady = true, Players = players, Starter = _group.RandomPlayer }; + } else - message.Data = new { AllReady = false, players }; + { + data = new { AllReady = false, Players = players }; + } } else { - message.Data = "Player not found, please create a new player"; + data = "Player not found, please create a new player"; } + + return data; } } diff --git a/pac-man-board-game/Services/GameGroup.cs b/pac-man-board-game/Services/GameGroup.cs index 2e62ac0..1e03804 100644 --- a/pac-man-board-game/Services/GameGroup.cs +++ b/pac-man-board-game/Services/GameGroup.cs @@ -28,4 +28,9 @@ public class GameGroup // TODO tests player.State = State.Ready; return Players; } + + public void SetAllInGame() + { + foreach (var player in Players) player.State = State.InGame; + } }