diff options
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/client/GameStateDeserializer.java | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/src/ch/epfl/xblast/client/GameStateDeserializer.java b/src/ch/epfl/xblast/client/GameStateDeserializer.java index 20b44f9..143641d 100644 --- a/src/ch/epfl/xblast/client/GameStateDeserializer.java +++ b/src/ch/epfl/xblast/client/GameStateDeserializer.java | |||
@@ -1,15 +1,13 @@ | |||
1 | package ch.epfl.xblast.client; | 1 | package ch.epfl.xblast.client; |
2 | 2 | ||
3 | import ch.epfl.xblast.Lists; | 3 | import ch.epfl.xblast.*; |
4 | import ch.epfl.xblast.PlayerID; | ||
5 | import ch.epfl.xblast.RunLengthEncoder; | ||
6 | import ch.epfl.xblast.SubCell; | ||
7 | import ch.epfl.xblast.client.GameState.Player; | 4 | import ch.epfl.xblast.client.GameState.Player; |
8 | 5 | ||
9 | import java.awt.*; | 6 | import java.awt.*; |
10 | import java.util.*; | 7 | import java.util.*; |
11 | import java.util.List; | 8 | import java.util.List; |
12 | import java.util.stream.Collectors; | 9 | import java.util.stream.Collectors; |
10 | import java.util.stream.IntStream; | ||
13 | 11 | ||
14 | /** | 12 | /** |
15 | * The client game state deserializer. | 13 | * The client game state deserializer. |
@@ -117,6 +115,38 @@ public final class GameStateDeserializer { | |||
117 | } | 115 | } |
118 | 116 | ||
119 | /** | 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 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 | */ | ||
145 | private static List<Image> deserializeExplosions(List<Byte> serializedExplosions) { | ||
146 | return deserializeImageChunk(serializedExplosions, ImageCollection.EXPLOSIONS_IMAGES); | ||
147 | } | ||
148 | |||
149 | /** | ||
120 | * Unserializes a byte representation of a Player. | 150 | * Unserializes a byte representation of a Player. |
121 | * | 151 | * |
122 | * @param id the PlayerID | 152 | * @param id the PlayerID |
@@ -176,10 +206,10 @@ public final class GameStateDeserializer { | |||
176 | public static GameState deserialize(List<Byte> serializedData) { | 206 | public static GameState deserialize(List<Byte> serializedData) { |
177 | VariableLengthChunk chunks = new VariableLengthChunk(serializedData); | 207 | VariableLengthChunk chunks = new VariableLengthChunk(serializedData); |
178 | 208 | ||
179 | List<Image> board = deserializeImageChunk(chunks.head(), ImageCollection.BOARD_IMAGES); | 209 | List<Image> board = deserializeBoard(chunks.head()); |
180 | chunks = chunks.tail(); | 210 | chunks = chunks.tail(); |
181 | 211 | ||
182 | List<Image> explosions = deserializeImageChunk(chunks.head(), ImageCollection.EXPLOSIONS_IMAGES); | 212 | List<Image> explosions = deserializeExplosions(chunks.head()); |
183 | chunks = chunks.tail(); | 213 | chunks = chunks.tail(); |
184 | 214 | ||
185 | List<Player> players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH)); | 215 | List<Player> players = deserializePlayers(chunks.head(SERIALIZED_PLAYERS_LENGTH)); |