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 80c3cc9..68b380e 100644 --- a/pac-man-board-game/ClientApp/src/types/types.d.ts +++ b/pac-man-board-game/ClientApp/src/types/types.d.ts @@ -5,8 +5,8 @@ type Setter = React.Dispatch>; type WebSocketData = string | ArrayBufferLike | Blob | ArrayBufferView; type ActionMessage = { - Action: import("../utils/actions").GameAction, - Data?: T + readonly Action: import("../utils/actions").GameAction, + readonly Data?: T } type Action = (obj: T) => void; diff --git a/pac-man-board-game/ClientApp/src/utils/actions.ts b/pac-man-board-game/ClientApp/src/utils/actions.ts index f0ac9c0..74efc1f 100644 --- a/pac-man-board-game/ClientApp/src/utils/actions.ts +++ b/pac-man-board-game/ClientApp/src/utils/actions.ts @@ -20,47 +20,39 @@ const ghosts = [ const store = getDefaultStore(); -export const doAction: MessageEventFunction = (message): void => { // TODO divide into smaller functions - const parsed: ActionMessage = JSON.parse(message.data); +export const doAction: MessageEventFunction = (event): void => { // TODO divide into smaller functions + const message: ActionMessage = JSON.parse(event.data); - switch (parsed.Action as GameAction) { + switch (message.Action as GameAction) { case GameAction.rollDice: - store.set(diceAtom, parsed.Data as number[]); + setDice(message.Data); break; case GameAction.moveCharacter: - store.set(diceAtom, parsed.Data?.dice as number[]); - updateCharacters(parsed); - removeEatenPellets(parsed); + moveCharacter(message.Data); break; case GameAction.playerInfo: - const playerProps = parsed.Data as PlayerProps[]; - console.log(playerProps); - store.set(playersAtom, 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 - store.set(charactersAtom, [...pacMen, ...ghosts]); + playerInfo(message.Data); break; case GameAction.ready: - const isReady = parsed.Data.AllReady as boolean; - if (isReady) { - store.set(currentPlayerAtom, new Player(parsed.Data.Starter as PlayerProps)); - } - store.set(playersAtom, (parsed.Data.Players as PlayerProps[]).map(p => new Player(p))); + ready(message.Data); break; } }; -function removeEatenPellets(parsed: ActionMessage): void { - const pellets = parsed.Data?.eatenPellets as Position[]; - - for (const pellet of pellets) { - testMap[pellet.y][pellet.x] = TileType.empty; - } +function setDice(data?: number[]): void { + store.set(diceAtom, data); } -function updateCharacters(parsed: ActionMessage): void { - const updatedCharacters = parsed.Data?.characters as CharacterProps[] | undefined; +type MoveCharacterData = { dice: number[], characters: CharacterProps[], eatenPellets: Position[] }; + +function moveCharacter(data?: MoveCharacterData): void { + store.set(diceAtom, data?.dice); + updateCharacters(data); + removeEatenPellets(data); +} + +function updateCharacters(data?: MoveCharacterData): void { + const updatedCharacters = data?.characters; if (updatedCharacters) { const newList: Character[] = []; @@ -70,3 +62,36 @@ function updateCharacters(parsed: ActionMessage): void { store.set(charactersAtom, newList); } } + +function removeEatenPellets(data?: MoveCharacterData): void { + const pellets = data?.eatenPellets; + + for (const pellet of pellets ?? []) { + testMap[pellet.y][pellet.x] = TileType.empty; + } +} + +function playerInfo(data?: PlayerProps[]): void { + const playerProps = data ?? []; + console.log(playerProps); + store.set(playersAtom, 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 + store.set(charactersAtom, [...pacMen, ...ghosts]); +} + +type ReadyData = + | { AllReady: true, Starter: PlayerProps, Players: PlayerProps[] } + | { AllReady: false, Players: PlayerProps[] } + | string; + +function ready(data?: ReadyData): void { + if (data && typeof data !== "string") { + const isReady = data.AllReady; + if (isReady) { + store.set(currentPlayerAtom, new Player(data.Starter)); + } + store.set(playersAtom, (data.Players).map(p => new Player(p))); + } +}