From 060d36a02e06fdc2943c8ff0b915057d50a13cb7 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 12 May 2016 15:43:28 +0200 Subject: Properly handle observer mode --- src/ch/epfl/xblast/PlayerID.java | 2 -- src/ch/epfl/xblast/client/Client.java | 30 +++++++++++++++++++++----- src/ch/epfl/xblast/client/XBlastComponent.java | 3 +++ src/ch/epfl/xblast/server/Server.java | 5 +++-- 4 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src') 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 { PLAYER_3, PLAYER_4; - public static final byte OBSERVER = -1; - public Cell initialPosition() { switch (this) { 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; import java.net.StandardProtocolFamily; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; -import java.util.ArrayList; -import java.util.Collections; +import java.util.*; import java.util.List; -import java.util.Optional; import java.util.function.Consumer; /** @@ -154,6 +152,25 @@ public class Client { } + private static PlayerID deserializePlayerID(byte b) { + if (b == Server.OBSERVER) + return null; + + try { + return PlayerID.fromByte(b); + } catch (IllegalArgumentException e) { + return null; + } + } + + private static GameState deserializeGameState(List b) { + try { + return GameStateDeserializer.deserialize(b); + } catch (IllegalArgumentException e) { + return null; + } + } + private final Channel channel; private final GUI gui; @@ -193,8 +210,11 @@ public class Client { private void updateGameState() { List serializedGameState = this.channel.receiveGameState(true); if (serializedGameState.size() < 1) return; - PlayerID player = PlayerID.fromByte(serializedGameState.get(0)); - GameState gameState = GameStateDeserializer.deserialize(Lists.firstDropped(serializedGameState, 1)); + + PlayerID player = deserializePlayerID(serializedGameState.get(0)); + GameState gameState = deserializeGameState(Lists.firstDropped(serializedGameState, 1)); + if (Objects.isNull(gameState)) return; + this.gui.setGameState(gameState, player); } 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 { } private static List sortPlayers(List players, PlayerID currentPlayerID) { + if (Objects.isNull(currentPlayerID)) + return players; + return Lists.sorted( players, 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.*; public class Server { public static final int DEFAULT_PORT = 2016; - private static final long REFRESH_RATE = (long) (50 * 1E6); // nanosecond + private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; + public static final byte OBSERVER = -1; private static class Channel { @@ -198,7 +199,7 @@ public class Server { List serialized = GameStateSerializer.serialize(BoardPainter.DEFAULT_BOARD_PAINTER, gs); for (Map.Entry client : this.registeredClientsMap.entrySet()) { - byte activePlayer = Objects.nonNull(client) ? client.getValue().toByte() : PlayerID.OBSERVER; + byte activePlayer = Objects.nonNull(client) ? client.getValue().toByte() : OBSERVER; this.channel.send(Lists.prepended(serialized, activePlayer), client.getKey()); } } -- cgit v1.2.3