Ready for alpha release

This commit is contained in:
Anthony Wang 2021-05-07 13:21:19 -05:00
parent 59f8165697
commit 87f9951aad
Signed by: a
GPG key ID: 6FD3502572299774
2 changed files with 23 additions and 50 deletions

View file

@ -19,13 +19,10 @@ class Player {
}
sendGameState() {
const i = this.game.players.indexOf(this);
const otherPlayers = [];
for (let j = 1; j < this.game.players.length; ++j)
otherPlayers.push(this.game.players[(i + j) % this.game.players.length]);
this.client.socket.emit('gameState', {
cards: this.cards,
rank: this.rank,
players: otherPlayers.map((p: Player) => ({
players: this.game.players.map((p: Player) => ({
username: p.client.username,
numCards: p.cards.length,
stackSize: p.stack.length,
@ -129,8 +126,7 @@ export default class Game {
delete p.disconnectListener;
(() => {
p.stack = stack;
p.client.socket.disconnect();
logSocket(p.client.socket, 'Bad cards argument on turn');
return;
})();
resolve();
});
@ -180,7 +176,7 @@ export default class Game {
if (this.players[selectedPlayer].stack.length > 0) {
if (this.players[selectedPlayer].stack[0].suit === Suit.Diamonds ||
this.players[selectedPlayer].stack[0].suit === Suit.Hearts) this.phase = 3; // Red card
this.players[selectedPlayer].stack.splice(0);
this.players[selectedPlayer].stack.splice(0, 1);
return;
}
p.client.socket.disconnect();
@ -202,9 +198,8 @@ export default class Game {
p.client.once('giveup', card => {
delete p.disconnectListener;
(() => {
p.cards.splice(card); // Remove card, may want to check if it is valid
p.client.socket.disconnect();
logSocket(p.client.socket, 'Bad cards argument on turn');
p.cards.splice(card, 1);
return;
})();
resolve();
});

View file

@ -38,8 +38,6 @@ export default function Game() {
const [gameState, setGameState] = useState<GameState | null>(null);
const [num, setNum] = useState(0);
const [stackSelected, setStackSelected] = useState<boolean[]>([]);
const [cardSelected, setCardSelected] = useState<boolean[]>([]);
useEffect(() => {
const socket = io(process.env.NEXT_PUBLIC_BACK_HOST!);
@ -64,8 +62,6 @@ export default function Game() {
socket.on('gameState', (data: GameState) => {
setGameState(data);
if (data.cards.length !== cardSelected.length)
setCardSelected(new Array(data.cards.length).fill(false));
});
socket.on('endGame', () => setGameState(null));
@ -116,8 +112,6 @@ export default function Game() {
</>
);
}
const selectedStacks = [...Array(gameState.players.length).keys()].filter((_, i) => stackSelected[i]);
const selectedCards = gameState.cards.filter((_, i) => cardSelected[i]);
if (gameState.phase === 0) {
return (
<>
@ -219,24 +213,15 @@ export default function Game() {
<p>Stacks:</p>
{gameState.players.map((player, i) => (
<label key={player.username+': '+player.stackSize}>
<input
type="checkbox"
checked={stackSelected[i] || false}
onChange={() => {
const stackSelected2 = [...stackSelected];
stackSelected2[i] = !stackSelected[i];
setStackSelected(stackSelected2);
}}
/>
{player.username+': '+player.stackSize+' cards '}
<button
onClick={() => socket.emit('flip', i)}
disabled={username !== gameState.lastPlayedPlayer}
>
Flip!
</button>
{' '+player.username+': '+player.stackSize+' cards '}
</label>
))}
<button
onClick={() => socket.emit('flip', selectedStacks[0])}
disabled={username !== gameState.lastPlayedPlayer || selectedStacks.length !== 1}
>
Flip!
</button>
</div>
</>
);
@ -256,26 +241,19 @@ export default function Game() {
<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);
}}
/>
<span style={{color: (card.suit === Suit.Hearts || card.suit === Suit.Diamonds ? 'red' : 'black')}}>
{' '+rankStrs[card.rank]+' '+suitChars[card.suit]}
</span>
<div>
<button
onClick={() => socket.emit('giveup', i)}
disabled={username !== (gameState.lastPlayed ? gameState.lastPlayedPlayer : gameState.playerTurn)}
>
Give up this card!
</button>
<span style={{color: (card.suit === Suit.Hearts || card.suit === Suit.Diamonds ? 'red' : 'black')}}>
{' '+rankStrs[card.rank]+' '+suitChars[card.suit]}
</span>
</div>
</label>
))}
<button
onClick={() => socket.emit('giveup', selectedCards[0])}
disabled={username !== (gameState.lastPlayed ? gameState.lastPlayedPlayer : gameState.playerTurn) || selectedCards.length !== 1}
>
Give up this card!
</button>
</div>
</>
);