adds endgame
This commit is contained in:
parent
456b0c2b1e
commit
7c91ca112e
2 changed files with 52 additions and 16 deletions
|
|
@ -3,7 +3,6 @@ import { CardStack } from "./card";
|
||||||
import { Card, ObfuscatedCardStack } from "./card";
|
import { Card, ObfuscatedCardStack } from "./card";
|
||||||
import { Socket } from "socket.io";
|
import { Socket } from "socket.io";
|
||||||
import { io } from "../server";
|
import { io } from "../server";
|
||||||
import { all } from "axios";
|
|
||||||
|
|
||||||
type PlayerSocketSet = Set<string>;
|
type PlayerSocketSet = Set<string>;
|
||||||
|
|
||||||
|
|
@ -107,6 +106,7 @@ export class Game {
|
||||||
player.roundPoints = 0;
|
player.roundPoints = 0;
|
||||||
player.totalPoints = startOver ? 0 : player.totalPoints;
|
player.totalPoints = startOver ? 0 : player.totalPoints;
|
||||||
player.closedRound = false;
|
player.closedRound = false;
|
||||||
|
player.place = null;
|
||||||
});
|
});
|
||||||
this.discardPile = [this.cardStack.cards.pop()!];
|
this.discardPile = [this.cardStack.cards.pop()!];
|
||||||
this.phase = gamePhase.revealTwoCards;
|
this.phase = gamePhase.revealTwoCards;
|
||||||
|
|
@ -140,6 +140,7 @@ export class Game {
|
||||||
break;
|
break;
|
||||||
case gamePhase.newRound:
|
case gamePhase.newRound:
|
||||||
console.log("\nGame phase: newRound");
|
console.log("\nGame phase: newRound");
|
||||||
|
this.checkIfPointLimitReached();
|
||||||
await this.nextRound();
|
await this.nextRound();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -179,9 +180,7 @@ export class Game {
|
||||||
this.sendObfuscatedGameUpdate();
|
this.sendObfuscatedGameUpdate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.sendMessageToAllPlayers(
|
console.log(`Waiting for ${playerOnTurn.name} to pick up card`);
|
||||||
`Waiting for ${playerOnTurn.name} to pick up card`
|
|
||||||
);
|
|
||||||
await this.waitForPlayerActions(
|
await this.waitForPlayerActions(
|
||||||
[
|
[
|
||||||
["draw-from-card-stack", this.drawCardAction.bind(this)],
|
["draw-from-card-stack", this.drawCardAction.bind(this)],
|
||||||
|
|
@ -195,9 +194,7 @@ export class Game {
|
||||||
|
|
||||||
async placeCard() {
|
async placeCard() {
|
||||||
const playerOnTurn = this.getPlayersTurn();
|
const playerOnTurn = this.getPlayersTurn();
|
||||||
this.sendMessageToAllPlayers(
|
console.log(`Waiting for ${playerOnTurn.name} to place card`);
|
||||||
`Waiting for ${playerOnTurn.name} to place card`
|
|
||||||
);
|
|
||||||
|
|
||||||
const expectedActions: ExpectedPlayerActions = [
|
const expectedActions: ExpectedPlayerActions = [
|
||||||
["click-card", this.placeCardAction.bind(this)],
|
["click-card", this.placeCardAction.bind(this)],
|
||||||
|
|
@ -215,9 +212,7 @@ export class Game {
|
||||||
|
|
||||||
async revealCard() {
|
async revealCard() {
|
||||||
const playerOnTurn = this.getPlayersTurn();
|
const playerOnTurn = this.getPlayersTurn();
|
||||||
this.sendMessageToAllPlayers(
|
console.log(`Waiting for ${playerOnTurn.name} to reveal a card`);
|
||||||
`Waiting for ${playerOnTurn.name} to reveal a card`
|
|
||||||
);
|
|
||||||
|
|
||||||
const numberOfRevealedCards = playerOnTurn.getRevealedCardCount();
|
const numberOfRevealedCards = playerOnTurn.getRevealedCardCount();
|
||||||
// ensures that the player does not select an already revealed card
|
// ensures that the player does not select an already revealed card
|
||||||
|
|
@ -237,7 +232,7 @@ export class Game {
|
||||||
this.evaluateAndSavePoints();
|
this.evaluateAndSavePoints();
|
||||||
this.phase = gamePhase.newRound;
|
this.phase = gamePhase.newRound;
|
||||||
this.sendObfuscatedGameUpdate();
|
this.sendObfuscatedGameUpdate();
|
||||||
this.sendMessageToAllPlayers("Waiting for next round");
|
console.log("Waiting for next round");
|
||||||
}
|
}
|
||||||
|
|
||||||
async nextRound() {
|
async nextRound() {
|
||||||
|
|
@ -448,6 +443,7 @@ export class Game {
|
||||||
evaluateAndSavePoints() {
|
evaluateAndSavePoints() {
|
||||||
const playersWithLowestPoints = this.getPlayersWithLowestPoints();
|
const playersWithLowestPoints = this.getPlayersWithLowestPoints();
|
||||||
const playerClosedRound = this.getPlayerThatClosedRound();
|
const playerClosedRound = this.getPlayerThatClosedRound();
|
||||||
|
let playerClosedRoundLostMessage = "";
|
||||||
if (
|
if (
|
||||||
playersWithLowestPoints.includes(playerClosedRound) &&
|
playersWithLowestPoints.includes(playerClosedRound) &&
|
||||||
playersWithLowestPoints.length === 1
|
playersWithLowestPoints.length === 1
|
||||||
|
|
@ -458,12 +454,12 @@ export class Game {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
} else if (playersWithLowestPoints.length === 1) {
|
} else if (playersWithLowestPoints.length === 1) {
|
||||||
this.sendMessageToAllPlayers(
|
playerClosedRoundLostMessage = playerClosedRoundLostMessage.concat(
|
||||||
`${playersWithLowestPoints[0].name} won the round!`
|
`${playersWithLowestPoints[0].name} won the round!`
|
||||||
);
|
);
|
||||||
} else if (playersWithLowestPoints.length > 1) {
|
} else if (playersWithLowestPoints.length > 1) {
|
||||||
this.sendMessageToAllPlayers(
|
playerClosedRoundLostMessage = playerClosedRoundLostMessage.concat(
|
||||||
`${playersWithLowestPoints
|
`\n ${playersWithLowestPoints
|
||||||
.map((player) => player.name)
|
.map((player) => player.name)
|
||||||
.join(", ")} scored equally the lowest points!`
|
.join(", ")} scored equally the lowest points!`
|
||||||
);
|
);
|
||||||
|
|
@ -472,9 +468,10 @@ export class Game {
|
||||||
if (player.closedRound) player.totalPoints += player.roundPoints * 2;
|
if (player.closedRound) player.totalPoints += player.roundPoints * 2;
|
||||||
else player.totalPoints += player.roundPoints;
|
else player.totalPoints += player.roundPoints;
|
||||||
});
|
});
|
||||||
this.sendMessageToAllPlayers(
|
playerClosedRoundLostMessage = playerClosedRoundLostMessage.concat(
|
||||||
`${playerClosedRound.name} points are doubled!`
|
`\n ${playerClosedRound.name} points are doubled!`
|
||||||
);
|
);
|
||||||
|
this.sendMessageToAllPlayers(playerClosedRoundLostMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkForFullRevealedCards() {
|
checkForFullRevealedCards() {
|
||||||
|
|
@ -493,6 +490,43 @@ export class Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkIfPointLimitReached() {
|
||||||
|
const highestPoints = Math.max(
|
||||||
|
...this.players.map((player) => player.totalPoints)
|
||||||
|
);
|
||||||
|
|
||||||
|
const lowestPoints = Math.min(
|
||||||
|
...this.players.map((player) => player.totalPoints)
|
||||||
|
);
|
||||||
|
const playersWithHighestPoints = this.players.filter(
|
||||||
|
(player) => player.totalPoints === highestPoints
|
||||||
|
);
|
||||||
|
|
||||||
|
if (highestPoints >= 100) {
|
||||||
|
if (playersWithHighestPoints.length === 1) {
|
||||||
|
const playerWithHighestPoints = playersWithHighestPoints[0];
|
||||||
|
this.sendMessageToAllPlayers(
|
||||||
|
`${playerWithHighestPoints.name} lost with ${playerWithHighestPoints.totalPoints}!`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const playerNames = playersWithHighestPoints
|
||||||
|
.map((player) => player.name)
|
||||||
|
.join(", ");
|
||||||
|
this.sendMessageToAllPlayers(
|
||||||
|
`Multiple players: ${playerNames} lost with ${highestPoints} points!`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const playersWithLowestPoints = this.players.filter(
|
||||||
|
(player) => player.totalPoints === lowestPoints
|
||||||
|
);
|
||||||
|
playersWithLowestPoints.forEach((player) => (player.place = 1));
|
||||||
|
|
||||||
|
this.phase = gamePhase.gameEnded;
|
||||||
|
this.sendObfuscatedGameUpdate();
|
||||||
|
allGames.splice(allGames.indexOf(this), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
checkForPlayerLeave() {
|
checkForPlayerLeave() {
|
||||||
const playersInSession = io.sockets.adapter.rooms.get(this.sessionId);
|
const playersInSession = io.sockets.adapter.rooms.get(this.sessionId);
|
||||||
if (playersInSession?.size ?? 0 < this.playerCount) {
|
if (playersInSession?.size ?? 0 < this.playerCount) {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ export class Player {
|
||||||
roundPoints: number;
|
roundPoints: number;
|
||||||
totalPoints: number;
|
totalPoints: number;
|
||||||
closedRound: boolean;
|
closedRound: boolean;
|
||||||
|
place: number | null; // indicates the place the player got in the last round
|
||||||
constructor(id: number, socketId: string, name: string, cards: Card[]) {
|
constructor(id: number, socketId: string, name: string, cards: Card[]) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.socketId = socketId;
|
this.socketId = socketId;
|
||||||
|
|
@ -52,6 +53,7 @@ export class Player {
|
||||||
this.roundPoints = 0;
|
this.roundPoints = 0;
|
||||||
this.totalPoints = 0;
|
this.totalPoints = 0;
|
||||||
this.closedRound = false;
|
this.closedRound = false;
|
||||||
|
this.place = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
hasInitialCardsRevealed(): boolean {
|
hasInitialCardsRevealed(): boolean {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue