From 4f86fae0301584eb07ddeff6204a7ac6598ed69c Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Fri, 29 Apr 2016 15:46:31 +0200 Subject: Clean the GameStateSerializer class --- src/ch/epfl/xblast/server/GameStateSerializer.java | 81 ++++++++++++++-------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/src/ch/epfl/xblast/server/GameStateSerializer.java b/src/ch/epfl/xblast/server/GameStateSerializer.java index 4a133e5..998ff21 100644 --- a/src/ch/epfl/xblast/server/GameStateSerializer.java +++ b/src/ch/epfl/xblast/server/GameStateSerializer.java @@ -14,9 +14,11 @@ import java.util.stream.Collectors; public final class GameStateSerializer { /** - * @param painter - * @param board - * @return + * Serialize a Board. + * + * @param painter the painter used to serialize de board's blocks + * @param board the board to be serialized + * @return a list of byte corresponding to the blocks' images */ private static List serializeBoard(BoardPainter painter, Board board) { return RunLengthEncoder.encode(Cell.SPIRAL_ORDER.stream() @@ -24,6 +26,13 @@ public final class GameStateSerializer { .collect(Collectors.toList())); } + /** + * Set the image ID of the explosion on a cell given its neighbors blasts. + * + * @param blastedCells cells containing a blast + * @param cell given cell + * @return the block image id related to the given cell + */ private static byte serializeBlast(Set blastedCells, Cell cell) { return ExplosionPainter.byteForBlast( blastedCells.contains(cell.neighbor(Direction.N)), @@ -32,6 +41,14 @@ public final class GameStateSerializer { blastedCells.contains(cell.neighbor(Direction.W))); } + /** + * Serialize the explosion on a given cell. + * + * @param bombedCells cells containing a bomb + * @param blastedCells cells containing a blast + * @param cell cell hosting the explosion + * @return the serialized explosion + */ private static byte serializeExplosion(Map bombedCells, Set blastedCells, Cell cell) { if (bombedCells.containsKey(cell)) return ExplosionPainter.byteForBomb(bombedCells.get(cell)); @@ -40,9 +57,11 @@ public final class GameStateSerializer { } /** - * @param bombedCells - * @param blastedCells - * @return + * Serialize the explosions (Blasts and Bombs). + * + * @param bombedCells cells containing a bomb + * @param blastedCells cells containing a blast + * @return the serialized explosions */ private static List serializeExplosions(Map bombedCells, Set blastedCells) { return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream() @@ -51,39 +70,43 @@ public final class GameStateSerializer { } /** - * Serialize a whole game state. + * Serialize a Player. * - * @param boardPainter board painter related to the actual level + * @param player player to be serialized + * @param tick tick related to the GameState + * @return the serialized player + */ + private static List serializePlayer(Player player, int tick) { + List serializedPlayer = new ArrayList<>(); + + serializedPlayer.add((byte) player.lives()); + serializedPlayer.add((byte) player.position().x()); + serializedPlayer.add((byte) player.position().y()); + serializedPlayer.add(PlayerPainter.byteForPlayer(player, tick)); + + return serializedPlayer; + } + + /** + * Serialize a whole GameState. + * + * @param boardPainter board painter used to serialize the board * @param gameState GameState to be serialized - * @return the serialized game state + * @return the serialized GameState */ public static List serialize(BoardPainter boardPainter, GameState gameState) { List output = new ArrayList<>(); List data = new ArrayList<>(); - // Board - data.addAll(serializeBoard(boardPainter, gameState.board())); - - // Blasts and Bombs - data.addAll(serializeExplosions(gameState.bombedCells(), gameState.blastedCells())); - - // Players + data.addAll(serializeBoard(boardPainter, gameState.board())); // Serialize the Board + data.addAll(serializeExplosions(gameState.bombedCells(), gameState.blastedCells())); // Serialize Explosions for (Player player : gameState.players()) { - List serializedPlayer = new ArrayList<>(); - serializedPlayer.add((byte) player.lives()); - serializedPlayer.add((byte) player.position().x()); - serializedPlayer.add((byte) player.position().y()); - serializedPlayer.add(PlayerPainter.byteForPlayer(player, gameState.ticks())); - - data.addAll(serializedPlayer); + data.addAll(serializePlayer(player, gameState.ticks())); // Serialize each Player } - // Ticks - data.add((byte) (gameState.remainingTime() / 2)); - - // Build output - output.add((byte) data.size()); - output.addAll(data); + data.add((byte) (gameState.remainingTime() / 2)); // Add the state Tick to the data + output.add((byte) data.size()); // Get the size of the whole data chunk and add it as first element of the output + output.addAll(data); // Add all the data to the output return output; } -- cgit v1.2.3