aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-12 15:43:28 +0200
committerPacien TRAN-GIRARD2016-05-12 15:44:10 +0200
commit060d36a02e06fdc2943c8ff0b915057d50a13cb7 (patch)
tree20d4e949c628863c4992bf1697956f5f2e0a13fb /src/ch/epfl
parent8fb64e8969f093c896ad9b5e327738e82941d30c (diff)
downloadxblast-060d36a02e06fdc2943c8ff0b915057d50a13cb7.tar.gz
Properly handle observer mode
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/PlayerID.java2
-rw-r--r--src/ch/epfl/xblast/client/Client.java30
-rw-r--r--src/ch/epfl/xblast/client/XBlastComponent.java3
-rw-r--r--src/ch/epfl/xblast/server/Server.java5
4 files changed, 31 insertions, 9 deletions
diff --git a/src/ch/epfl/xblast/PlayerID.java b/src/ch/epfl/xblast/PlayerID.java
index 96b98e4..72a48b9 100644
--- a/src/ch/epfl/xblast/PlayerID.java
+++ b/src/ch/epfl/xblast/PlayerID.java
@@ -13,8 +13,6 @@ public enum PlayerID {
13 PLAYER_3, 13 PLAYER_3,
14 PLAYER_4; 14 PLAYER_4;
15 15
16 public static final byte OBSERVER = -1;
17
18 public Cell initialPosition() { 16 public Cell initialPosition() {
19 switch (this) { 17 switch (this) {
20 case PLAYER_1: 18 case PLAYER_1:
diff --git a/src/ch/epfl/xblast/client/Client.java b/src/ch/epfl/xblast/client/Client.java
index 5b331f2..b522312 100644
--- a/src/ch/epfl/xblast/client/Client.java
+++ b/src/ch/epfl/xblast/client/Client.java
@@ -15,10 +15,8 @@ import java.net.SocketAddress;
15import java.net.StandardProtocolFamily; 15import java.net.StandardProtocolFamily;
16import java.nio.ByteBuffer; 16import java.nio.ByteBuffer;
17import java.nio.channels.DatagramChannel; 17import java.nio.channels.DatagramChannel;
18import java.util.ArrayList; 18import java.util.*;
19import java.util.Collections;
20import java.util.List; 19import java.util.List;
21import java.util.Optional;
22import java.util.function.Consumer; 20import java.util.function.Consumer;
23 21
24/** 22/**
@@ -154,6 +152,25 @@ public class Client {
154 152
155 } 153 }
156 154
155 private static PlayerID deserializePlayerID(byte b) {
156 if (b == Server.OBSERVER)
157 return null;
158
159 try {
160 return PlayerID.fromByte(b);
161 } catch (IllegalArgumentException e) {
162 return null;
163 }
164 }
165
166 private static GameState deserializeGameState(List<Byte> b) {
167 try {
168 return GameStateDeserializer.deserialize(b);
169 } catch (IllegalArgumentException e) {
170 return null;
171 }
172 }
173
157 private final Channel channel; 174 private final Channel channel;
158 private final GUI gui; 175 private final GUI gui;
159 176
@@ -193,8 +210,11 @@ public class Client {
193 private void updateGameState() { 210 private void updateGameState() {
194 List<Byte> serializedGameState = this.channel.receiveGameState(true); 211 List<Byte> serializedGameState = this.channel.receiveGameState(true);
195 if (serializedGameState.size() < 1) return; 212 if (serializedGameState.size() < 1) return;
196 PlayerID player = PlayerID.fromByte(serializedGameState.get(0)); 213
197 GameState gameState = GameStateDeserializer.deserialize(Lists.firstDropped(serializedGameState, 1)); 214 PlayerID player = deserializePlayerID(serializedGameState.get(0));
215 GameState gameState = deserializeGameState(Lists.firstDropped(serializedGameState, 1));
216 if (Objects.isNull(gameState)) return;
217
198 this.gui.setGameState(gameState, player); 218 this.gui.setGameState(gameState, player);
199 } 219 }
200 220
diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java
index 0ef8c5c..dfc0be5 100644
--- a/src/ch/epfl/xblast/client/XBlastComponent.java
+++ b/src/ch/epfl/xblast/client/XBlastComponent.java
@@ -121,6 +121,9 @@ public final class XBlastComponent extends JComponent {
121 } 121 }
122 122
123 private static List<GameState.Player> sortPlayers(List<GameState.Player> players, PlayerID currentPlayerID) { 123 private static List<GameState.Player> sortPlayers(List<GameState.Player> players, PlayerID currentPlayerID) {
124 if (Objects.isNull(currentPlayerID))
125 return players;
126
124 return Lists.sorted( 127 return Lists.sorted(
125 players, 128 players,
126 GameState.Player 129 GameState.Player
diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java
index 1f29607..8a4b09b 100644
--- a/src/ch/epfl/xblast/server/Server.java
+++ b/src/ch/epfl/xblast/server/Server.java
@@ -22,9 +22,10 @@ import java.util.*;
22public class Server { 22public class Server {
23 23
24 public static final int DEFAULT_PORT = 2016; 24 public static final int DEFAULT_PORT = 2016;
25
26 private static final long REFRESH_RATE = (long) (50 * 1E6); // nanosecond 25 private static final long REFRESH_RATE = (long) (50 * 1E6); // nanosecond
26
27 private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; 27 private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length;
28 public static final byte OBSERVER = -1;
28 29
29 private static class Channel { 30 private static class Channel {
30 31
@@ -198,7 +199,7 @@ public class Server {
198 List<Byte> serialized = GameStateSerializer.serialize(BoardPainter.DEFAULT_BOARD_PAINTER, gs); 199 List<Byte> serialized = GameStateSerializer.serialize(BoardPainter.DEFAULT_BOARD_PAINTER, gs);
199 200
200 for (Map.Entry<SocketAddress, PlayerID> client : this.registeredClientsMap.entrySet()) { 201 for (Map.Entry<SocketAddress, PlayerID> client : this.registeredClientsMap.entrySet()) {
201 byte activePlayer = Objects.nonNull(client) ? client.getValue().toByte() : PlayerID.OBSERVER; 202 byte activePlayer = Objects.nonNull(client) ? client.getValue().toByte() : OBSERVER;
202 this.channel.send(Lists.prepended(serialized, activePlayer), client.getKey()); 203 this.channel.send(Lists.prepended(serialized, activePlayer), client.getKey());
203 } 204 }
204 } 205 }