summaryrefslogtreecommitdiff
path: root/backend/src/main/java/org/luxons
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-20 10:25:08 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-20 10:25:08 +0200
commite0f3a4caeb4e7889f354450baabc56f02e21fc10 (patch)
treec1dbc29fe57cbd3e5a8dac2ef86263e285ebe361 /backend/src/main/java/org/luxons
parentAdd error toasts (diff)
downloadseven-wonders-e0f3a4caeb4e7889f354450baabc56f02e21fc10.tar.gz
seven-wonders-e0f3a4caeb4e7889f354450baabc56f02e21fc10.tar.bz2
seven-wonders-e0f3a4caeb4e7889f354450baabc56f02e21fc10.zip
Add "sayReady" for players to call to receive their hand
Diffstat (limited to 'backend/src/main/java/org/luxons')
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java55
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/Game.java5
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java15
3 files changed, 61 insertions, 14 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java
index 4c8891cb..c5723658 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/controllers/GameController.java
@@ -1,12 +1,14 @@
package org.luxons.sevenwonders.controllers;
import java.security.Principal;
+import java.util.List;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.luxons.sevenwonders.actions.PrepareMoveAction;
import org.luxons.sevenwonders.game.Game;
import org.luxons.sevenwonders.game.api.PlayerTurnInfo;
+import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.cards.CardBack;
import org.luxons.sevenwonders.lobby.Lobby;
import org.luxons.sevenwonders.lobby.Player;
@@ -35,7 +37,39 @@ public class GameController {
this.playerRepository = playerRepository;
}
- @ApiMethod(description = "Prepares the user's next move. When all players have prepared their moves, all moves "
+ @ApiMethod(description = "Notifies the game that the player is ready to receive his hand.")
+ @MessageMapping("/game/sayReady")
+ public void ready(Principal principal) {
+ Player player = playerRepository.find(principal.getName());
+ player.setReady(true);
+ Game game = player.getGame();
+ logger.info("Game '{}': player '{}' is ready for the next turn", game.getId(), player);
+
+ Lobby lobby = player.getLobby();
+ List<Player> players = lobby.getPlayers();
+
+ boolean allReady = players.stream().allMatch(Player::isReady);
+ if (allReady) {
+ logger.info("Game '{}': all players ready, sending turn info", game.getId());
+ players.forEach(p -> p.setReady(false));
+ sendTurnInfo(players, game);
+ } else {
+ sendPlayerReady(game.getId(), player);
+ }
+ }
+
+ private void sendTurnInfo(List<Player> players, Game game) {
+ for (PlayerTurnInfo turnInfo : game.getCurrentTurnInfo()) {
+ Player player = players.get(turnInfo.getPlayerIndex());
+ template.convertAndSendToUser(player.getUsername(), "/queue/game/turn", turnInfo);
+ }
+ }
+
+ private void sendPlayerReady(long gameId, Player player) {
+ template.convertAndSend("/topic/game/" + gameId + "/playerReady", player.getUsername());
+ }
+
+ @ApiMethod(description = "Prepares the player's next move. When all players have prepared their moves, all moves "
+ "are executed.")
@MessageMapping("/game/prepareMove")
public void prepareMove(PrepareMoveAction action, Principal principal) {
@@ -45,23 +79,20 @@ public class GameController {
PreparedCard preparedCard = new PreparedCard(player, preparedCardBack);
logger.info("Game '{}': player {} prepared move {}", game.getId(), principal.getName(), action.getMove());
- if (game.areAllPlayersReady()) {
+ if (game.allPlayersPreparedTheirMove()) {
logger.info("Game '{}': all players have prepared their move, executing turn...", game.getId());
- game.playTurn();
- sendTurnInfo(player.getLobby(), game);
+ Table table = game.playTurn();
+ sendPlayedMoves(game.getId(), table);
} else {
- sendPreparedCard(preparedCard, game);
+ sendPreparedCard(game.getId(), preparedCard);
}
}
- private void sendPreparedCard(PreparedCard preparedCard, Game game) {
- template.convertAndSend("/topic/game/" + game.getId() + "/prepared", preparedCard);
+ private void sendPlayedMoves(long gameId, Table table) {
+ template.convertAndSend("/topic/game/" + gameId + "/tableUpdates", table);
}
- private void sendTurnInfo(Lobby lobby, Game game) {
- for (PlayerTurnInfo turnInfo : game.getCurrentTurnInfo()) {
- Player player = lobby.getPlayers().get(turnInfo.getPlayerIndex());
- template.convertAndSendToUser(player.getUsername(), "/queue/game/turn", turnInfo);
- }
+ private void sendPreparedCard(long gameId, PreparedCard preparedCard) {
+ template.convertAndSend("/topic/game/" + gameId + "/prepared", preparedCard);
}
}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/Game.java b/backend/src/main/java/org/luxons/sevenwonders/game/Game.java
index 45f12be0..ed11913b 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/Game.java
@@ -120,12 +120,12 @@ public class Game {
move.validate(table, hand);
}
- public boolean areAllPlayersReady() {
+ public boolean allPlayersPreparedTheirMove() {
long nbExpectedMoves = currentTurnInfo.values().stream().filter(pti -> pti.getAction() != Action.WAIT).count();
return preparedMoves.size() == nbExpectedMoves;
}
- public void playTurn() {
+ public Table playTurn() {
makeMoves();
if (endOfAgeReached()) {
executeEndOfAgeEvents();
@@ -136,6 +136,7 @@ public class Game {
rotateHandsIfRelevant();
startNewTurn();
}
+ return table;
}
private void rotateHandsIfRelevant() {
diff --git a/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java b/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java
index 6698c476..6143dfb5 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/lobby/Player.java
@@ -11,6 +11,8 @@ public class Player {
private int index;
+ private boolean ready;
+
private transient Lobby lobby;
private transient Game game;
@@ -40,6 +42,14 @@ public class Player {
this.index = index;
}
+ public boolean isReady() {
+ return ready;
+ }
+
+ public void setReady(boolean ready) {
+ this.ready = ready;
+ }
+
@JsonIgnore
public Lobby getLobby() {
return lobby;
@@ -57,4 +67,9 @@ public class Player {
public void setGame(Game game) {
this.game = game;
}
+
+ @Override
+ public String toString() {
+ return "'" + displayName + "' (" + username + ")";
+ }
}
bgstack15