diff options
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/server/GameStateSerializer.java | 130 |
1 files changed, 71 insertions, 59 deletions
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; | |||
2 | 2 | ||
3 | import ch.epfl.xblast.Cell; | 3 | import ch.epfl.xblast.Cell; |
4 | import ch.epfl.xblast.Direction; | 4 | import ch.epfl.xblast.Direction; |
5 | import ch.epfl.xblast.Lists; | ||
5 | import ch.epfl.xblast.RunLengthEncoder; | 6 | import ch.epfl.xblast.RunLengthEncoder; |
6 | import ch.epfl.xblast.server.painter.*; | 7 | import ch.epfl.xblast.server.painter.BoardPainter; |
8 | import ch.epfl.xblast.server.painter.ExplosionPainter; | ||
9 | import ch.epfl.xblast.server.painter.PlayerPainter; | ||
7 | 10 | ||
8 | import java.util.*; | 11 | import java.util.*; |
9 | import java.util.stream.Collectors; | 12 | import java.util.stream.Collectors; |
13 | import java.util.stream.Stream; | ||
10 | 14 | ||
11 | /** | 15 | /** |
16 | * The game state serializer that serializes a GameState into a byte sequence | ||
17 | * for easy and efficient transmission over the EPFL's Gigabit network. | ||
18 | * | ||
19 | * @author Pacien TRAN-GIRARD (261948) | ||
12 | * @author Timothée FLOURE (257420) | 20 | * @author Timothée FLOURE (257420) |
13 | */ | 21 | */ |
14 | public final class GameStateSerializer { | 22 | public final class GameStateSerializer { |
15 | 23 | ||
24 | private GameStateSerializer() { | ||
25 | // Static class | ||
26 | } | ||
27 | |||
16 | /** | 28 | /** |
17 | * Serialize a Board. | 29 | * Serializes a Board. |
18 | * | 30 | * |
19 | * @param painter the painter used to serialize de board's blocks | 31 | * @param painter the painter used to serialize de board's blocks |
20 | * @param board the board to be serialized | 32 | * @param board the board to be serialized |
21 | * @return a list of byte corresponding to the blocks' images | 33 | * @return a list of byte corresponding to the blocks' images |
22 | */ | 34 | */ |
23 | private static List<Byte> serializeBoard(BoardPainter painter, Board board) { | 35 | private static List<Byte> serializeBoard(BoardPainter painter, Board board) { |
@@ -27,10 +39,10 @@ public final class GameStateSerializer { | |||
27 | } | 39 | } |
28 | 40 | ||
29 | /** | 41 | /** |
30 | * Set the image ID of the explosion on a cell given its neighbors blasts. | 42 | * Sets the image ID of the explosion on a cell given its neighbors blasts. |
31 | * | 43 | * |
32 | * @param blastedCells cells containing a blast | 44 | * @param blastedCells cells containing a blast |
33 | * @param cell given cell | 45 | * @param cell given cell |
34 | * @return the block image id related to the given cell | 46 | * @return the block image id related to the given cell |
35 | */ | 47 | */ |
36 | private static byte serializeBlast(Set<Cell> blastedCells, Cell cell) { | 48 | private static byte serializeBlast(Set<Cell> blastedCells, Cell cell) { |
@@ -42,12 +54,12 @@ public final class GameStateSerializer { | |||
42 | } | 54 | } |
43 | 55 | ||
44 | /** | 56 | /** |
45 | * Serialize the explosion on a given cell. | 57 | * Serializes an explosion on a given cell. |
46 | * | 58 | * |
47 | * @param bombedCells cells containing a bomb | 59 | * @param bombedCells cells containing a bomb |
48 | * @param blastedCells cells containing a blast | 60 | * @param blastedCells cells containing a blast |
49 | * @param cell cell hosting the explosion | 61 | * @param cell cell hosting the explosion |
50 | * @param board actual Board | 62 | * @param board current Board |
51 | * @return the serialized explosion | 63 | * @return the serialized explosion |
52 | */ | 64 | */ |
53 | private static byte serializeExplosion(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Cell cell, Board board) { | 65 | private static byte serializeExplosion(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Cell cell, Board board) { |
@@ -61,11 +73,11 @@ public final class GameStateSerializer { | |||
61 | } | 73 | } |
62 | 74 | ||
63 | /** | 75 | /** |
64 | * Serialize the explosions (Blasts and Bombs). | 76 | * Serializes explosions (Blasts and Bombs). |
65 | * | 77 | * |
66 | * @param bombedCells cells containing a bomb | 78 | * @param bombedCells cells containing a bomb |
67 | * @param blastedCells cells containing a blast | 79 | * @param blastedCells cells containing a blast |
68 | * @param board actual Board | 80 | * @param board current Board |
69 | * @return the serialized explosions | 81 | * @return the serialized explosions |
70 | */ | 82 | */ |
71 | private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Board board) { | 83 | private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Board board) { |
@@ -75,67 +87,67 @@ public final class GameStateSerializer { | |||
75 | } | 87 | } |
76 | 88 | ||
77 | /** | 89 | /** |
78 | * Prepend its size to a Byte List. | 90 | * Serializes a Player. |
79 | * | 91 | * |
80 | * @param list Byte List to be prefixed | 92 | * @param player player to be serialized |
81 | * @return the Byte List prefixed with its size | 93 | * @param tick current GameState tick |
94 | * @return the serialized player | ||
82 | */ | 95 | */ |
83 | private static List<Byte> prefixByteListWithSize(List<Byte> list) { | 96 | private static List<Byte> serializePlayer(Player player, int tick) { |
84 | List<Byte> prefixedList = new ArrayList<>(); | 97 | return Arrays.asList( |
85 | prefixedList.add((byte) list.size()); | 98 | (byte) player.lives(), |
86 | prefixedList.addAll(list); | 99 | (byte) player.position().x(), |
87 | return prefixedList; | 100 | (byte) player.position().y(), |
101 | PlayerPainter.byteForPlayer(player, tick)); | ||
88 | } | 102 | } |
89 | 103 | ||
90 | /** | 104 | /** |
91 | * Serialize a Player. | 105 | * Serializes Players. |
92 | * | 106 | * |
93 | * @param player player to be serialized | 107 | * @param players the list of players to be serialized |
94 | * @param tick tick related to the GameState | 108 | * @param tick current GameState tick |
95 | * @return the serialized player | 109 | * @return the serialized players |
96 | */ | 110 | */ |
97 | private static List<Byte> serializePlayer(Player player, int tick) { | 111 | private static List<Byte> serializePlayers(List<Player> players, int tick) { |
98 | List<Byte> serializedPlayer = new ArrayList<>(); | 112 | return players.stream() |
113 | .flatMap(p -> serializePlayer(p, tick).stream()) | ||
114 | .collect(Collectors.toList()); | ||
115 | } | ||
99 | 116 | ||
100 | serializedPlayer.add((byte) player.lives()); | 117 | /** |
101 | serializedPlayer.add((byte) player.position().x()); | 118 | * Serializes the game remaining time. |
102 | serializedPlayer.add((byte) player.position().y()); | 119 | * |
103 | serializedPlayer.add(PlayerPainter.byteForPlayer(player, tick)); | 120 | * @param remainingTime the remaining time (in seconds) |
121 | * @return the serialized remaining time | ||
122 | */ | ||
123 | private static List<Byte> serializeRemainingTime(double remainingTime) { | ||
124 | return Collections.singletonList((byte) (remainingTime / 2)); | ||
125 | } | ||
104 | 126 | ||
105 | return serializedPlayer; | 127 | /** |
128 | * Prepends its size to a Byte List. | ||
129 | * | ||
130 | * @param l Byte List to be prefixed | ||
131 | * @return the Byte List prefixed with its size | ||
132 | */ | ||
133 | private static List<Byte> prependSize(List<Byte> l) { | ||
134 | return Lists.prepended(l, (byte) l.size()); | ||
106 | } | 135 | } |
107 | 136 | ||
108 | /** | 137 | /** |
109 | * Serialize a whole GameState. | 138 | * Serializes a whole GameState. |
110 | * | 139 | * |
111 | * @param boardPainter board painter used to serialize the board | 140 | * @param bp board painter used to serialize the board |
112 | * @param gameState GameState to be serialized | 141 | * @param gs GameState to be serialized |
113 | * @return the serialized GameState | 142 | * @return the serialized GameState |
114 | */ | 143 | */ |
115 | public static List<Byte> serialize(BoardPainter boardPainter, GameState gameState) { | 144 | public static List<Byte> serialize(BoardPainter bp, GameState gs) { |
116 | List<Byte> output = new ArrayList<>(); | 145 | return Stream.of( |
117 | 146 | prependSize(serializeBoard(bp, gs.board())), | |
118 | // Add the serialized Board to the output | 147 | prependSize(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), |
119 | output.addAll( | 148 | serializePlayers(gs.players(), gs.ticks()), |
120 | prefixByteListWithSize( | 149 | serializeRemainingTime(gs.remainingTime()) |
121 | serializeBoard(boardPainter, gameState.board()) | 150 | ).flatMap(List::stream).collect(Collectors.toList()); |
122 | ) | ||
123 | ); | ||
124 | |||
125 | // Add the serialized Explosions to the output | ||
126 | output.addAll( | ||
127 | prefixByteListWithSize( | ||
128 | serializeExplosions(gameState.bombedCells(), gameState.blastedCells(), gameState.board()) | ||
129 | ) | ||
130 | ); | ||
131 | |||
132 | // Add the serialized Players to the output | ||
133 | for (Player player : gameState.players()) { | ||
134 | output.addAll(serializePlayer(player, gameState.ticks())); // Serialize each Player | ||
135 | } | ||
136 | |||
137 | output.add((byte) (gameState.remainingTime() / 2)); // Add the state Tick to the output | ||
138 | |||
139 | return output; | ||
140 | } | 151 | } |
152 | |||
141 | } | 153 | } |