From 635694370ce04f9ae6da4b137883586b70f14a7b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 5 May 2016 21:18:11 +0200 Subject: Refactor GameStateSerializer --- src/ch/epfl/xblast/server/GameStateSerializer.java | 130 +++++++++++---------- 1 file changed, 71 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/server/GameStateSerializer.java b/src/ch/epfl/xblast/server/GameStateSerializer.java index 049e993..7747ecb 100644 --- a/src/ch/epfl/xblast/server/GameStateSerializer.java +++ b/src/ch/epfl/xblast/server/GameStateSerializer.java @@ -2,22 +2,34 @@ package ch.epfl.xblast.server; import ch.epfl.xblast.Cell; import ch.epfl.xblast.Direction; +import ch.epfl.xblast.Lists; import ch.epfl.xblast.RunLengthEncoder; -import ch.epfl.xblast.server.painter.*; +import ch.epfl.xblast.server.painter.BoardPainter; +import ch.epfl.xblast.server.painter.ExplosionPainter; +import ch.epfl.xblast.server.painter.PlayerPainter; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; /** + * The game state serializer that serializes a GameState into a byte sequence + * for easy and efficient transmission over the EPFL's Gigabit network. + * + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public final class GameStateSerializer { + private GameStateSerializer() { + // Static class + } + /** - * Serialize a Board. + * Serializes a Board. * * @param painter the painter used to serialize de board's blocks - * @param board the board to be serialized + * @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) { @@ -27,10 +39,10 @@ public final class GameStateSerializer { } /** - * Set the image ID of the explosion on a cell given its neighbors blasts. + * Sets the image ID of the explosion on a cell given its neighbors blasts. * * @param blastedCells cells containing a blast - * @param cell given cell + * @param cell given cell * @return the block image id related to the given cell */ private static byte serializeBlast(Set blastedCells, Cell cell) { @@ -42,12 +54,12 @@ public final class GameStateSerializer { } /** - * Serialize the explosion on a given cell. + * Serializes an explosion on a given cell. * - * @param bombedCells cells containing a bomb + * @param bombedCells cells containing a bomb * @param blastedCells cells containing a blast - * @param cell cell hosting the explosion - * @param board actual Board + * @param cell cell hosting the explosion + * @param board current Board * @return the serialized explosion */ private static byte serializeExplosion(Map bombedCells, Set blastedCells, Cell cell, Board board) { @@ -61,11 +73,11 @@ public final class GameStateSerializer { } /** - * Serialize the explosions (Blasts and Bombs). + * Serializes explosions (Blasts and Bombs). * - * @param bombedCells cells containing a bomb + * @param bombedCells cells containing a bomb * @param blastedCells cells containing a blast - * @param board actual Board + * @param board current Board * @return the serialized explosions */ private static List serializeExplosions(Map bombedCells, Set blastedCells, Board board) { @@ -75,67 +87,67 @@ public final class GameStateSerializer { } /** - * Prepend its size to a Byte List. + * Serializes a Player. * - * @param list Byte List to be prefixed - * @return the Byte List prefixed with its size + * @param player player to be serialized + * @param tick current GameState tick + * @return the serialized player */ - private static List prefixByteListWithSize(List list) { - List prefixedList = new ArrayList<>(); - prefixedList.add((byte) list.size()); - prefixedList.addAll(list); - return prefixedList; + private static List serializePlayer(Player player, int tick) { + return Arrays.asList( + (byte) player.lives(), + (byte) player.position().x(), + (byte) player.position().y(), + PlayerPainter.byteForPlayer(player, tick)); } /** - * Serialize a Player. + * Serializes Players. * - * @param player player to be serialized - * @param tick tick related to the GameState - * @return the serialized player + * @param players the list of players to be serialized + * @param tick current GameState tick + * @return the serialized players */ - private static List serializePlayer(Player player, int tick) { - List serializedPlayer = new ArrayList<>(); + private static List serializePlayers(List players, int tick) { + return players.stream() + .flatMap(p -> serializePlayer(p, tick).stream()) + .collect(Collectors.toList()); + } - serializedPlayer.add((byte) player.lives()); - serializedPlayer.add((byte) player.position().x()); - serializedPlayer.add((byte) player.position().y()); - serializedPlayer.add(PlayerPainter.byteForPlayer(player, tick)); + /** + * Serializes the game remaining time. + * + * @param remainingTime the remaining time (in seconds) + * @return the serialized remaining time + */ + private static List serializeRemainingTime(double remainingTime) { + return Collections.singletonList((byte) (remainingTime / 2)); + } - return serializedPlayer; + /** + * Prepends its size to a Byte List. + * + * @param l Byte List to be prefixed + * @return the Byte List prefixed with its size + */ + private static List prependSize(List l) { + return Lists.prepended(l, (byte) l.size()); } /** - * Serialize a whole GameState. + * Serializes a whole GameState. * - * @param boardPainter board painter used to serialize the board - * @param gameState GameState to be serialized + * @param bp board painter used to serialize the board + * @param gs GameState to be serialized * @return the serialized GameState */ - public static List serialize(BoardPainter boardPainter, GameState gameState) { - List output = new ArrayList<>(); - - // Add the serialized Board to the output - output.addAll( - prefixByteListWithSize( - serializeBoard(boardPainter, gameState.board()) - ) - ); - - // Add the serialized Explosions to the output - output.addAll( - prefixByteListWithSize( - serializeExplosions(gameState.bombedCells(), gameState.blastedCells(), gameState.board()) - ) - ); - - // Add the serialized Players to the output - for (Player player : gameState.players()) { - output.addAll(serializePlayer(player, gameState.ticks())); // Serialize each Player - } - - output.add((byte) (gameState.remainingTime() / 2)); // Add the state Tick to the output - - return output; + public static List serialize(BoardPainter bp, GameState gs) { + return Stream.of( + prependSize(serializeBoard(bp, gs.board())), + prependSize(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), + serializePlayers(gs.players(), gs.ticks()), + serializeRemainingTime(gs.remainingTime()) + ).flatMap(List::stream).collect(Collectors.toList()); } + } -- cgit v1.2.3