aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/client/GameStateDeserializer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/xblast/client/GameStateDeserializer.java')
-rw-r--r--src/ch/epfl/xblast/client/GameStateDeserializer.java59
1 files changed, 51 insertions, 8 deletions
diff --git a/src/ch/epfl/xblast/client/GameStateDeserializer.java b/src/ch/epfl/xblast/client/GameStateDeserializer.java
index 20b44f9..8b203b1 100644
--- a/src/ch/epfl/xblast/client/GameStateDeserializer.java
+++ b/src/ch/epfl/xblast/client/GameStateDeserializer.java
@@ -1,15 +1,13 @@
1package ch.epfl.xblast.client; 1package ch.epfl.xblast.client;
2 2
3import ch.epfl.xblast.Lists; 3import ch.epfl.xblast.*;
4import ch.epfl.xblast.PlayerID;
5import ch.epfl.xblast.RunLengthEncoder;
6import ch.epfl.xblast.SubCell;
7import ch.epfl.xblast.client.GameState.Player; 4import ch.epfl.xblast.client.GameState.Player;
8 5
9import java.awt.*; 6import java.awt.*;
10import java.util.*; 7import java.util.*;
11import java.util.List; 8import java.util.List;
12import java.util.stream.Collectors; 9import java.util.stream.Collectors;
10import java.util.stream.IntStream;
13 11
14/** 12/**
15 * The client game state deserializer. 13 * The client game state deserializer.
@@ -110,10 +108,48 @@ public final class GameStateDeserializer {
110 * @return a list of Images corresponding to the serialized element 108 * @return a list of Images corresponding to the serialized element
111 */ 109 */
112 private static List<Image> deserializeImageChunk(List<Byte> data, ImageCollection imageCollection) { 110 private static List<Image> deserializeImageChunk(List<Byte> data, ImageCollection imageCollection) {
113 return RunLengthEncoder.decode(data).stream() 111 return Collections.unmodifiableList(RunLengthEncoder.decode(data).stream()
114 .map(Byte::toUnsignedInt) 112 .map(Byte::toUnsignedInt)
115 .map(imageCollection::imageOrNull) 113 .map(imageCollection::imageOrNull)
116 .collect(Collectors.toList()); 114 .collect(Collectors.toList()));
115 }
116
117 /**
118 * Unserializes a spiral-ordered byte representation of a board.
119 *
120 * @param serializedBoard the run-length-compressed, serialized spiral ordered board
121 * @return the row-major-ordered board
122 * @throws IllegalArgumentException if the length of the serialized board is invalid
123 */
124 private static List<Image> deserializeBoard(List<Byte> serializedBoard) {
125 List<Image> spiralOrdered = deserializeImageChunk(serializedBoard, ImageCollection.BOARD_IMAGES);
126
127 if (spiralOrdered.size() != Cell.SPIRAL_ORDER.size())
128 throw new IllegalArgumentException();
129
130 Map<Cell, Image> imageMap = IntStream
131 .range(0, Cell.SPIRAL_ORDER.size()).mapToObj(i -> i)
132 .collect(Collectors.toMap(Cell.SPIRAL_ORDER::get, spiralOrdered::get));
133
134 return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream()
135 .map(imageMap::get)
136 .collect(Collectors.toList()));
137 }
138
139 /**
140 * Unserializes a byte representation of explosions and bombs.
141 *
142 * @param serializedExplosions the run-length-compressed, serialized explosions
143 * @return the explosions
144 * @throws IllegalArgumentException if the length of the serialized explosions is invalid
145 */
146 private static List<Image> deserializeExplosions(List<Byte> serializedExplosions) {
147 List<Image> explosions = deserializeImageChunk(serializedExplosions, ImageCollection.EXPLOSIONS_IMAGES);
148
149 if (explosions.size() != Cell.ROW_MAJOR_ORDER.size())
150 throw new IllegalArgumentException();
151
152 return explosions;
117 } 153 }
118 154
119 /** 155 /**
@@ -160,6 +196,13 @@ public final class GameStateDeserializer {
160 .collect(Collectors.toList())); 196 .collect(Collectors.toList()));
161 } 197 }
162 198
199 /**
200 * Unserializes a byte representation of the ticks.
201 *
202 * @param serializedTicks the serialized ticks
203 * @return the ticks
204 * @throws IllegalArgumentException if the byte chunk is invalid.
205 */
163 private static int deserializeTicks(List<Byte> serializedTicks) { 206 private static int deserializeTicks(List<Byte> serializedTicks) {
164 if (Objects.isNull(serializedTicks) || serializedTicks.size() != SERIALIZED_TICKS_LENGTH) 207 if (Objects.isNull(serializedTicks) || serializedTicks.size() != SERIALIZED_TICKS_LENGTH)
165 throw new IllegalArgumentException(); 208 throw new IllegalArgumentException();
@@ -176,10 +219,10 @@ public final class GameStateDeserializer {
176 public static GameState deserialize(List<Byte> serializedData) { 219 public static GameState deserialize(List<Byte> serializedData) {
177 VariableLengthChunk chunks = new VariableLengthChunk(serializedData); 220 VariableLengthChunk chunks = new VariableLengthChunk(serializedData);
178 221
179 List<Image> board = deserializeImageChunk(chunks.head(), ImageCollection.BOARD_IMAGES); 222 List<Image> board = deserializeBoard(chunks.head());
180 chunks = chunks.tail(); 223 chunks = chunks.tail();
181 224
182 List<Image> explosions = deserializeImageChunk(chunks.head(), ImageCollection.EXPLOSIONS_IMAGES); 225 List<Image> explosions = deserializeExplosions(chunks.head());
183 chunks = chunks.tail(); 226 chunks = chunks.tail();
184 227
185 List<Player> players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH)); 228 List<Player> players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH));