aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-11 17:14:34 +0200
committerPacien TRAN-GIRARD2016-05-11 17:15:30 +0200
commite4b599c9bb3f2c1bfcebadc832f9d291ab8e4e93 (patch)
tree88578e65e784e685240040a1cb03c22a5590f6d2
parent21b25ef18f9a4c11cf0066b2d750a36e7fad2533 (diff)
downloadxblast-e4b599c9bb3f2c1bfcebadc832f9d291ab8e4e93.tar.gz
Implement game state updating
-rw-r--r--src/ch/epfl/xblast/PlayerAction.java19
-rw-r--r--src/ch/epfl/xblast/server/GameState.java17
-rw-r--r--src/ch/epfl/xblast/server/Server.java21
3 files changed, 52 insertions, 5 deletions
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 @@
1package ch.epfl.xblast; 1package ch.epfl.xblast;
2 2
3import java.util.Optional;
4
3/** 5/**
4 * The player action enum. 6 * The player action enum.
5 * 7 *
@@ -27,4 +29,21 @@ public enum PlayerAction {
27 return (byte) this.ordinal(); 29 return (byte) this.ordinal();
28 } 30 }
29 31
32 public Optional<Direction> associatedSpeedChangeEvent() {
33 switch (this) {
34 case MOVE_N:
35 return Optional.of(Direction.N);
36 case MOVE_E:
37 return Optional.of(Direction.E);
38 case MOVE_S:
39 return Optional.of(Direction.S);
40 case MOVE_W:
41 return Optional.of(Direction.W);
42 case STOP:
43 return Optional.empty();
44 default:
45 return null;
46 }
47 }
48
30} 49}
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 {
207 return new GameState(this.ticks + 1, board1, players1, bombs1, explosions1, blasts1); 207 return new GameState(this.ticks + 1, board1, players1, bombs1, explosions1, blasts1);
208 } 208 }
209 209
210 /**
211 * Computes and returns the game state for the next tick, given the current state and the given events.
212 *
213 * @param playerActions the player actions map
214 * @return the next game state
215 */
216 public GameState next(Map<PlayerID, PlayerAction> playerActions) {
217 Map<PlayerID, Optional<Direction>> speedChangeEvents = playerActions.entrySet().stream()
218 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().associatedSpeedChangeEvent()));
219
220 Set<PlayerID> bombDropEvents = playerActions.entrySet().stream()
221 .filter(e -> e.getValue() == PlayerAction.DROP_BOMB)
222 .map(Map.Entry::getKey)
223 .collect(Collectors.toSet());
224
225 return this.next(speedChangeEvents, bombDropEvents);
226 }
210 227
211 /** 228 /**
212 * Returns a mapping of players from their location. 229 * 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 {
157 } 157 }
158 158
159 private void runGame() { 159 private void runGame() {
160 try { 160 GameState gameState = GameState.DEFAULT_GAME_STATE;
161 Thread.sleep(10000); 161
162 } catch (InterruptedException e) { 162 while (!gameState.isGameOver()) {
163 e.printStackTrace(); 163 gameState = updateGameState(updateGameState(gameState));
164 // TODO: send updated game state to clients
165
166 try {
167 Thread.sleep(10000);
168 // TODO: adapt sleeping time
169 } catch (InterruptedException e) {
170 e.printStackTrace();
171 }
164 } 172 }
173 }
165 174
166 Map<SocketAddress, PlayerAction> actions = this.channel.collectActions(); 175 private GameState updateGameState(GameState gs) {
176 Map<PlayerID, PlayerAction> events = Lists.traverseMaps(this.playersAddressMap, this.channel.collectActions());
177 return gs.next(events);
167 } 178 }
168 179
169} 180}