From e4b599c9bb3f2c1bfcebadc832f9d291ab8e4e93 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 11 May 2016 17:14:34 +0200 Subject: Implement game state updating --- src/ch/epfl/xblast/PlayerAction.java | 19 +++++++++++++++++++ src/ch/epfl/xblast/server/GameState.java | 17 +++++++++++++++++ src/ch/epfl/xblast/server/Server.java | 21 ++++++++++++++++----- 3 files changed, 52 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/PlayerAction.java b/src/ch/epfl/xblast/PlayerAction.java index 67920eb..6ce12ba 100644 --- a/src/ch/epfl/xblast/PlayerAction.java +++ b/src/ch/epfl/xblast/PlayerAction.java @@ -1,5 +1,7 @@ package ch.epfl.xblast; +import java.util.Optional; + /** * The player action enum. * @@ -27,4 +29,21 @@ public enum PlayerAction { return (byte) this.ordinal(); } + public Optional associatedSpeedChangeEvent() { + switch (this) { + case MOVE_N: + return Optional.of(Direction.N); + case MOVE_E: + return Optional.of(Direction.E); + case MOVE_S: + return Optional.of(Direction.S); + case MOVE_W: + return Optional.of(Direction.W); + case STOP: + return Optional.empty(); + default: + return null; + } + } + } diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 16cb01b..2aea7f2 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -207,6 +207,23 @@ public final class GameState { return new GameState(this.ticks + 1, board1, players1, bombs1, explosions1, blasts1); } + /** + * Computes and returns the game state for the next tick, given the current state and the given events. + * + * @param playerActions the player actions map + * @return the next game state + */ + public GameState next(Map playerActions) { + Map> speedChangeEvents = playerActions.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().associatedSpeedChangeEvent())); + + Set bombDropEvents = playerActions.entrySet().stream() + .filter(e -> e.getValue() == PlayerAction.DROP_BOMB) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); + + return this.next(speedChangeEvents, bombDropEvents); + } /** * Returns a mapping of players from their location. diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java index 6d1d62f..0989d55 100644 --- a/src/ch/epfl/xblast/server/Server.java +++ b/src/ch/epfl/xblast/server/Server.java @@ -157,13 +157,24 @@ public class Server { } private void runGame() { - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); + GameState gameState = GameState.DEFAULT_GAME_STATE; + + while (!gameState.isGameOver()) { + gameState = updateGameState(updateGameState(gameState)); + // TODO: send updated game state to clients + + try { + Thread.sleep(10000); + // TODO: adapt sleeping time + } catch (InterruptedException e) { + e.printStackTrace(); + } } + } - Map actions = this.channel.collectActions(); + private GameState updateGameState(GameState gs) { + Map events = Lists.traverseMaps(this.playersAddressMap, this.channel.collectActions()); + return gs.next(events); } } -- cgit v1.2.3