Implemented phases 2 and 3

This commit is contained in:
Anthony Wang 2021-05-07 10:00:02 -05:00
parent 23968cba69
commit f8b54ea60c
Signed by: a
GPG key ID: 6FD3502572299774
2 changed files with 66 additions and 7 deletions

View file

@ -28,6 +28,7 @@ class Player {
players: otherPlayers.map((p: Player) => ({ players: otherPlayers.map((p: Player) => ({
username: p.client.username, username: p.client.username,
numCards: p.cards.length, numCards: p.cards.length,
stackSize: p.stack.length,
rank: p.rank, rank: p.rank,
})), })),
phase: this.game.phase, phase: this.game.phase,

View file

@ -8,7 +8,7 @@ import LoginForm from '../components/LoginForm';
interface GameState { interface GameState {
cards: Card[], cards: Card[],
players: {username: string, numCards: number, rank: number}[], players: {username: string, numCards: number, stackSize: number, rank: number}[],
lastPlayed: number, lastPlayed: number,
lastPlayedPlayer: string | null, lastPlayedPlayer: string | null,
playerTurn: string playerTurn: string
@ -33,6 +33,8 @@ export default function Game() {
const [num, setNum] = useState(0); const [num, setNum] = useState(0);
const [stackSelected, setStackSelected] = useState<boolean[]>([]);
useEffect(() => { useEffect(() => {
const socket = io(process.env.NEXT_PUBLIC_BACK_HOST!); const socket = io(process.env.NEXT_PUBLIC_BACK_HOST!);
setSocket(socket); setSocket(socket);
@ -108,6 +110,7 @@ export default function Game() {
</> </>
); );
} }
const selectedStacks = gameState.players.filter((_, i) => stackSelected[i]);
const selectedCards = gameState.cards.filter((_, i) => cardSelected[i]); const selectedCards = gameState.cards.filter((_, i) => cardSelected[i]);
if (gameState.phase === 0) { if (gameState.phase === 0) {
return ( return (
@ -124,7 +127,7 @@ export default function Game() {
</> </>
); );
} }
else if (gameState.phase === 1) { if (gameState.phase === 1) {
return ( return (
<> <>
<ul> <ul>
@ -148,7 +151,7 @@ export default function Game() {
<p>Your cards:</p> <p>Your cards:</p>
<input <input
type="text" type="text"
placeholder="Play a number greater than the last played number..." placeholder="Claim a number greater than the last claimed number..."
value={num} value={num}
onChange={(e) => setNum(+e.target.value)} onChange={(e) => setNum(+e.target.value)}
/> />
@ -156,19 +159,19 @@ export default function Game() {
onClick={() => socket.emit('turn', num)} onClick={() => socket.emit('turn', num)}
disabled={username !== gameState.playerTurn || num <= gameState.lastPlayed} disabled={username !== gameState.playerTurn || num <= gameState.lastPlayed}
> >
Play! Claim!
</button> </button>
<button <button
onClick={() => socket.emit('turn', -1)} onClick={() => socket.emit('turn', -1)}
disabled={username !== gameState.playerTurn} disabled={username !== gameState.playerTurn}
> >
Call BS! BS!
</button> </button>
</div> </div>
</> </>
); );
} }
else if (gameState.phase === 2) { if (gameState.phase === 2) {
return ( return (
<> <>
<ul> <ul>
@ -178,10 +181,34 @@ export default function Game() {
</li> </li>
))} ))}
</ul> </ul>
{`${gameState.playerTurn} has called BS! ${gameState.lastPlayedPlayer} must flip over ${gameState.lastPlayed} black cards!`}
<div>
<p>Stacks:</p>
{gameState.players.map((player, i) => (
<label key={player.username+': '+player.stackSize+' cards '}>
<input
type="checkbox"
checked={stackSelected[i] || false}
onChange={() => {
const stackSelected2 = [...stackSelected];
stackSelected2[i] = !stackSelected[i];
setStackSelected(stackSelected2);
}}
/>
{player.username+': '+player.stackSize+' cards '}
</label>
))}
<button
onClick={() => socket.emit('flip', selectedStacks[0])}
disabled={username !== gameState.lastPlayedPlayer || selectedStacks.length !== 1}
>
Flip!
</button>
</div>
</> </>
); );
} }
else { if (gameState.phase === 3) {
return ( return (
<> <>
<ul> <ul>
@ -191,7 +218,38 @@ export default function Game() {
</li> </li>
))} ))}
</ul> </ul>
{(gameState.lastPlayed ? `${gameState.lastPlayedPlayer}` : `${gameState.playerTurn}`) + ` lost! Now they must give up one of their cards!`}
<div>
<p>Your cards:</p>
{gameState.cards.map((card, i) => (
<label key={card.rank+' '+card.suit}>
<input
type="checkbox"
checked={cardSelected[i] || false}
onChange={() => {
const cardSelected2 = [...cardSelected];
cardSelected2[i] = !cardSelected[i];
setCardSelected(cardSelected2);
}}
/>
{rankStrs[card.rank]+' '+suitChars[card.suit]}
</label>
))}
<button
onClick={() => socket.emit('turn', selectedCards)}
disabled={username !== gameState.playerTurn || selectedCards.length !== 1}
>
Give up this card!
</button>
</div>
</> </>
); );
} }
return (
<>
<div>
<p>Something went wrong! :/</p>
</div>
</>
);
} }