Skip to content

Commit

Permalink
Release v1.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
s1hofmann authored Apr 13, 2020
2 parents badd1f1 + 2172fad commit 0f9da6a
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "1.2.4"
"version": "1.2.5"
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zahlenspiel",
"version": "1.2.4",
"version": "1.2.5",
"description": "Zahlenspiel",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/zahlenspiel-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/zahlenspiel-backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zahlenspiel-backend",
"version": "1.2.4",
"version": "1.2.5",
"description": "Zahlenspiel Backend",
"main": "dist/index",
"types": "dist/index",
Expand Down Expand Up @@ -38,6 +38,6 @@
"sneer": "^1.0.1",
"uuid": "^7.0.2",
"winston": "^3.2.1",
"zahlenspiel-shared-entities": "^1.2.4"
"zahlenspiel-shared-entities": "^1.2.5"
}
}
42 changes: 35 additions & 7 deletions packages/zahlenspiel-backend/src/rooms/zahlenspiel-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class ZahlenspielRoom extends Room<GameState> {
this.send(player.client, new TurnValidMessage(player.cards));
this.updateCardStacks();
if (this.state.isGameWon()) {
this.broadcast(new GameWonMessage());
this.broadcastWin();
return;
} else if (this.state.isGameLost()) {
this.broadcast(new GameLostMessage(this.state.totalRemainingCards(), "You can't drop anymore cards!"));
this.broadcastLoss("You can't drop anymore cards!");
} else if (turnFinishable(this.state.validDroppedCards, this.state.currentDeck.length)) {
this.send(player.client, new TurnFinishableMessage());
}
Expand All @@ -111,9 +111,9 @@ export class ZahlenspielRoom extends Room<GameState> {
const leavingPlayer = this.state.getPlayer(client.id);
this.state.removePlayer(client.id);
const playerList = marshallPlayers(this.state.players);
this.broadcast(new PlayerLeaveMessage(playerList), {except: client});
this.broadcastDelayed(new PlayerLeaveMessage(playerList), {except: client});
if (this.isGamePhase()) {
this.broadcastDelayed(new GameLostMessage(this.state.totalRemainingCards(), `${leavingPlayer.name} couldn't handle the pressure and left!`), {except: client});
this.broadcastLoss(`${leavingPlayer.name} couldn't handle the pressure and left!`, {except: client});
}
} else {
const reconnectedClient = await this.allowReconnection(client, MAX_RECONNECTION_TIME_IN_SECONDS);
Expand All @@ -126,9 +126,19 @@ export class ZahlenspielRoom extends Room<GameState> {
onDispose() {
}

private broadcastWin() {
this.state.currentGameState = GameStates.WON;
this.broadcastDelayed(new GameWonMessage());
}

private broadcastLoss(reason?: string, options?: BroadcastOptions) {
this.state.currentGameState = GameStates.LOST;
this.broadcastDelayed(new GameLostMessage(this.state.totalRemainingCards(), reason), options);
}

private nextPlayer() {
if (this.state.isGameLost()) {
this.broadcast(new GameLostMessage(this.state.totalRemainingCards(), "You can't drop anymore cards!"));
this.broadcastLoss("You can't drop anymore cards!");
} else {
this.state.nextPlayer();
this.broadcast(new PlayerSwitchMessage(toPlayerDTO(this.state.currentPlayer)));
Expand Down Expand Up @@ -183,14 +193,22 @@ export class ZahlenspielRoom extends Room<GameState> {
}
}

private isPreGamePhase(): boolean {
return this.state.currentGameState === GameStates.PREGAME;
private isGameLost(): boolean {
return this.state.currentGameState === GameStates.LOST;
}

private isGameWon(): boolean {
return this.state.currentGameState === GameStates.WON;
}

private isGamePhase(): boolean {
return this.state.currentGameState === GameStates.GAME;
}

private isPreGamePhase(): boolean {
return this.state.currentGameState === GameStates.PREGAME;
}

private isWaitingPhase(): boolean {
return this.state.currentGameState === GameStates.WAITING;
}
Expand All @@ -210,6 +228,16 @@ export class ZahlenspielRoom extends Room<GameState> {
if (this.state.isCurrentPlayer(player) && turnFinishable(this.state.validDroppedCards, this.state.currentDeck.length)) {
this.sendDelayed(player.client, new TurnFinishableMessage());
}
} else if (this.isGameWon()) {
this.sendDelayed(player.client, new UpdateCardStackMessage(marshallCardStacks(this.state.cardStacks), this.state.remainingCardsOnStack()));
this.sendDelayed(player.client, new NewCardMessage(player.cards));
this.sendDelayed(player.client, new PlayerSwitchMessage(toPlayerDTO(this.state.currentPlayer)));
this.sendDelayed(player.client, new GameWonMessage());
} else if (this.isGameLost()) {
this.sendDelayed(player.client, new UpdateCardStackMessage(marshallCardStacks(this.state.cardStacks), this.state.remainingCardsOnStack()));
this.sendDelayed(player.client, new NewCardMessage(player.cards));
this.sendDelayed(player.client, new PlayerSwitchMessage(toPlayerDTO(this.state.currentPlayer)));
this.sendDelayed(player.client, new GameLostMessage(this.state.totalRemainingCards()));
}
}

Expand Down
14 changes: 14 additions & 0 deletions packages/zahlenspiel-backend/src/states/game-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,19 @@ describe("GameState", () => {
// THEN
expect(gameState.currentPlayer).toEqual(gameState.players[2]);
});

it("should not loop endlessly if no players has anymore cards", () => {
// GIVEN
const clientMock = mockPartial<Client>({});
const gameState = new GameState("pw");
["first", "second", "third"].forEach(playerName => gameState.addNewPlayer(clientMock, playerName));
gameState.setCurrentPlayer(gameState.players[0]);

// WHEN
const SUT = () => gameState.nextPlayer();

// THEN
expect(SUT).not.toThrow();
});
});
});
4 changes: 4 additions & 0 deletions packages/zahlenspiel-backend/src/states/game-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class GameState extends Schema {
}

nextPlayer() {
const playersWithCardsLeft = this.players.filter(player => player.cards.length).length > 0;
if (!playersWithCardsLeft) {
return;
}
const nextPlayerOrdinal = (this.currentPlayer.order + 1) % this.players.length;
this.currentPlayer = this.players.find((player: Player) => player.order === nextPlayerOrdinal);
if (this.currentPlayer.cards.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/zahlenspiel-shared-entities/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/zahlenspiel-shared-entities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zahlenspiel-shared-entities",
"version": "1.2.4",
"version": "1.2.5",
"description": "Zahlenspiel Shared Entities",
"main": "dist/index",
"types": "dist/index",
Expand Down
2 changes: 1 addition & 1 deletion packages/zahlenspiel-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/zahlenspiel-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zahlenspiel-ui",
"version": "1.2.4",
"version": "1.2.5",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.28",
Expand All @@ -21,7 +21,7 @@
"react-scripts": "3.4.1",
"styled-components": "^5.0.1",
"typescript": "~3.7.2",
"zahlenspiel-shared-entities": "^1.2.4"
"zahlenspiel-shared-entities": "^1.2.5"
},
"scripts": {
"start": "react-scripts start",
Expand Down
11 changes: 5 additions & 6 deletions packages/zahlenspiel-ui/src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,12 @@ const Game = (props: GameProps) => {
</CardStacks>
</Top>
<Center>
{"Great job, you beat the game!"}
<ActionButton onClick={() => leaveGame(true)}>{"New game!"}</ActionButton>
</Center>;
<Column>
{"Great job, you beat the game!"}
<ActionButton onClick={() => leaveGame(true)}>{"New game!"}</ActionButton>
</Column>
</Center>
<Bottom>
<CardDeck>
{renderCards()}
</CardDeck>
</Bottom>
</>);
case GameStates.LOST:
Expand Down

0 comments on commit 0f9da6a

Please sign in to comment.