From 5f8c6994aa87388a03d4e59158d2fb7747a00814 Mon Sep 17 00:00:00 2001 From: DTieman Date: Fri, 19 Apr 2024 22:12:31 +0200 Subject: [PATCH] Added the ability to choose your cardtype when playing a jack or the newly added joker --- .dockerignore | 2 ++ .gitignore | 1 + src/layout/components/Card.tsx | 16 ++++++++++++---- src/layout/components/Game.tsx | 19 ++++++++++++++----- src/layout/pages/Room.tsx | 23 +++++++++++++---------- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/.dockerignore b/.dockerignore index 6f692d5..e01e7e7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,5 @@ .env.example +.env .idea README.md +.gitea \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2a1832d..96f1b63 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ yarn-debug.log* yarn-error.log* .idea +.env \ No newline at end of file diff --git a/src/layout/components/Card.tsx b/src/layout/components/Card.tsx index 1de719d..28001ef 100644 --- a/src/layout/components/Card.tsx +++ b/src/layout/components/Card.tsx @@ -11,9 +11,17 @@ const Card: FunctionComponent = ({cardString, handleClick, isHidden, isCl const cardType = cardString.split(' ')[0].toLowerCase(); const cardValue = cardString.split(' ')[1].toLowerCase(); - const cardSource = isHidden ? - require(`../../assets/cards/back.png`) : - require(`../../assets/cards/${cardType}_${cardValue}.png`); + const cardSource = (): string => { + if (isHidden) return require("../../assets/cards/back.png"); + if (cardType === "JOKER") { + if (cardValue === "RED") { + return require("../../assets/cards/joker_red.png") + } + return require("../../assets/cards/joker_black.png") + } + return require(`../../assets/cards/${cardType}_${cardValue}.png`); + } + const cardName = isHidden ? 'back' : `${cardType} ${cardValue}`; const handleCardClick = () => { @@ -24,7 +32,7 @@ const Card: FunctionComponent = ({cardString, handleClick, isHidden, isCl return (
- {cardName} + {cardName}
) } diff --git a/src/layout/components/Game.tsx b/src/layout/components/Game.tsx index 7822622..50bc1c2 100644 --- a/src/layout/components/Game.tsx +++ b/src/layout/components/Game.tsx @@ -1,9 +1,11 @@ import React, {FunctionComponent} from "react"; import Deck from "./Deck"; import Hand from "./Hand"; +import {GHButton} from "./Button"; -interface GameState { +export interface GameState { PlayerName: string; + CurrentState: string; Hand: string[]; CurrentCard: string; CurrentPlayer: string; @@ -14,9 +16,12 @@ interface Props { gameState: GameState handleCardSend: (cardString: string) => void; handleDraw: () => void; + handleChoice: (choice: string) => void; } -const Game:FunctionComponent = ({gameState, handleCardSend, handleDraw}) => { +const CHOICES = ['SPADES', 'HEARTS', 'DIAMONDS', 'CLUBS']; + +const Game: FunctionComponent = ({gameState, handleCardSend, handleDraw, handleChoice}) => { return (
{ @@ -27,13 +32,17 @@ const Game:FunctionComponent = ({gameState, handleCardSend, handleDraw}) gameState.Hand && } + { + gameState.CurrentState === 'CHOOSE' && + CHOICES.map(choice => handleChoice(choice)}>{choice}) + }
    { - gameState.Players && - gameState.Players.map((player, index) => { + gameState?.Players && + gameState.Players.map((player) => { const isCurrentPlayer = player === gameState.CurrentPlayer; const isMe = player === gameState.PlayerName; - return
  • + return
  • {player} {isMe && '(You)'}
  • }) diff --git a/src/layout/pages/Room.tsx b/src/layout/pages/Room.tsx index 6c9f3cd..7ccb713 100644 --- a/src/layout/pages/Room.tsx +++ b/src/layout/pages/Room.tsx @@ -3,15 +3,7 @@ import React from "react"; import {useNavigate, useParams} from "react-router"; import {GHButton} from "../components/Button"; import useTitle from "../../utils/hooks/TitleHook"; -import Game from "../components/Game"; - -interface GameState { - PlayerName: string; - Hand: string[]; - CurrentCard: string; - CurrentPlayer: string; - Players: string[]; -} +import Game, {GameState} from "../components/Game"; interface ChatMessage { PlayerName: string; @@ -35,6 +27,7 @@ const Room = () => { const [gameState, setGameState] = React.useState({ PlayerName: '', + CurrentState: '', Hand: [], CurrentCard: '', CurrentPlayer: '', @@ -78,6 +71,16 @@ const Room = () => { }) } + const handleChoice = (choice: string) => { + handleSend({ + Type: "GAME", + Payload: JSON.stringify({ + Action: "CHOOSE", + Data: choice + }) + }); + } + const handleDraw = () => { handleSend({ Type: "GAME", @@ -99,7 +102,7 @@ const Room = () => {

    Room {roomId}

    Leave Room - + setChatInput(e.target.value)} />