From c2e3b7aa3c367a406175f45fbfecf511c39c623a Mon Sep 17 00:00:00 2001 From: Danny Piper Date: Fri, 30 Aug 2024 13:56:31 +0200 Subject: [PATCH] feat: czar can now select a card --- .../src/components/gameControls/GameLobby.tsx | 5 ++- .../gameItems/CurrentRoundResults.tsx | 41 +++++++++++++------ cahfrontend/src/gameState/gameState.ts | 21 ++++++++++ 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/cahfrontend/src/components/gameControls/GameLobby.tsx b/cahfrontend/src/components/gameControls/GameLobby.tsx index ccbeba9..42f0b1d 100644 --- a/cahfrontend/src/components/gameControls/GameLobby.tsx +++ b/cahfrontend/src/components/gameControls/GameLobby.tsx @@ -34,6 +34,8 @@ import CurrentRoundResults from "../gameItems/CurrentRoundResults"; // Exported for testing reasons export function GameLobbyLoaded(props: Readonly) { const isGameOwner = () => props.state.ownerId === gameState.getPlayerId(); + const isCzar = () => + props.roundState.currentCardCzarId === gameState.getPlayerId(); const settings = () => props.state.settings; const isGameStarted = () => props.state.gameState !== GameStateInLobby; const isCzarJudgingPhase = () => @@ -86,9 +88,10 @@ export function GameLobbyLoaded(props: Readonly) { { return { diff --git a/cahfrontend/src/components/gameItems/CurrentRoundResults.tsx b/cahfrontend/src/components/gameItems/CurrentRoundResults.tsx index a36f70e..a2eab5d 100644 --- a/cahfrontend/src/components/gameItems/CurrentRoundResults.tsx +++ b/cahfrontend/src/components/gameItems/CurrentRoundResults.tsx @@ -1,6 +1,7 @@ import { BlackCard, WhiteCard } from "../../gameLogicTypes"; import { For } from "solid-js"; import Card from "./Card"; +import { gameState } from "../../gameState/gameState"; interface PlayerPlay { whiteCards: WhiteCard[]; @@ -10,28 +11,44 @@ interface PlayerPlay { interface PlayerPlayProps { play: PlayerPlay; index: number; + isCzar: boolean; } function PlayerCard(props: Readonly) { + const cardNode = ( +
+ {props.play.whiteCards.map((card, index) => ( + + ))} +
+ ) + + if (!props.isCzar) { + return cardNode; + } + return ( -
{ + gameState.czarSelectCards(props.play.whiteCards.map((x) => x.id)); + }} > - {props.play.whiteCards.map((card, index) => ( - - ))} -
+ {cardNode} + ); } interface Props { blackCard: BlackCard; plays: PlayerPlay[]; + isCzar: boolean; } export default function CurrentRoundResults(props: Readonly) { @@ -45,7 +62,7 @@ export default function CurrentRoundResults(props: Readonly) { /> {(play, index) => { - return ; + return ; }} diff --git a/cahfrontend/src/gameState/gameState.ts b/cahfrontend/src/gameState/gameState.ts index faecce4..a9c70c7 100644 --- a/cahfrontend/src/gameState/gameState.ts +++ b/cahfrontend/src/gameState/gameState.ts @@ -9,6 +9,7 @@ import { import { MsgChangeSettings, MsgCommandError, + MsgCzarSelectCard, MsgNewOwner, MsgOnCardPlayed, MsgOnCzarJudgingPhase, @@ -17,12 +18,14 @@ import { MsgOnPlayerDisconnect, MsgOnPlayerJoin, MsgOnPlayerLeave, + MsgOnWhiteCardPlayPhase, MsgPing, MsgPlayCards, MsgRoundInformation, MsgStartGame, RpcChangeSettingsMsg, RpcCommandErrorMsg, + RpcCzarSelectCardMsg, RpcMessage, RpcMessageBody, RpcMessageType, @@ -395,6 +398,9 @@ class GameState { return this.handleOnCzarJudgingPhase( rpcMessage.data as RpcOnCzarJudgingPhaseMsg, ); + case MsgOnWhiteCardPlayPhase: + console.log("TODO Implement me"); + break; default: throw new Error( `Cannot handle RPC message as type is not valid ${rpcMessage.type}`, @@ -457,6 +463,21 @@ class GameState { JSON.stringify(this.encodeMessage(MsgPlayCards, msg)), ); } + + public czarSelectCards(cards: number[]) { + console.log("Czar is selecting cards: ", cards) + if (!this.wsClient) { + throw new Error("Cannot play cards as websocket is not connected"); + } + + const msg: RpcCzarSelectCardMsg = { + cards: cards + } + + this.wsClient.sendMessage( + JSON.stringify(this.encodeMessage(MsgCzarSelectCard, msg)), + ); + } } export const gameState: GameState = new GameState();