aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/client/Client.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/xblast/client/Client.java')
-rw-r--r--src/ch/epfl/xblast/client/Client.java30
1 files changed, 25 insertions, 5 deletions
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