-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProcessor.java
More file actions
236 lines (213 loc) · 9.85 KB
/
DataProcessor.java
File metadata and controls
236 lines (213 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import java.util.ArrayList;
// import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.awt.Color;
/**
* The sole purpose of this class is to provide an easy way for
* players and servers to process data they want to send or
* receive to and from the players and servers.
*
* The data needed for a full update is as follows:
* - Ship color (represented as three integers)
* - Ship positionx, positiony, facing
* - (Projectile p : projectiles)
* p positionx, positiony, direction
* - (Asteroid a : asteroids)
* a type, scale, positionx, positiony,
* direction, velocityx, velocityy
*
* For the projectiles and asteroids, only those that are still
* alive will be processed
*
* @author Herman Lin
* @author Devin Zhu
*/
class DataProcessor {
DataProcessor() {}
/**
* This will compress the necessary values from a game state
* into a String that can be sent to the Server and
* subsequently to other players via an input/output stream.
*
* @param player the Ship of the player sending data
* @param projectiles the list of Projectiles from the player
* @param asteroids the list of Asteroids from the player
* @return a formatted String containing game state data
*/
public String compress(Ship player,
ArrayList<Projectile> projectiles,
ArrayList<Asteroid> asteroids) {
// get player properties
String playerData = player.dataString();
// get all projectile properties and put into one String
String projectileData = "";
for (Projectile p : projectiles)
projectileData += p.dataString();
if (projectileData.isEmpty()) projectileData = "NONE ";
// get all asteroid properties and put into one string
String asteroidData = "";
for (Asteroid a : asteroids)
asteroidData += a.dataString();
if (asteroidData.isEmpty()) asteroidData = "NONE ";
// combine all the data into a String, separating each
// data set with a "SEP "
return playerData + "SEP " + projectileData + "SEP " + asteroidData;
}
/**
* This will decompress and extract the necessary values from
* a compressed game state String. The extracted data is put
* into a Data object that can then be referenced by a player.
*
* @param data the String containing all necessary game state
* values from a player
* @return a Data object containing the player ship, projectiles,
* and asteroids
*/
public Data decompressAll(String data) {
// each set of data is separated by a special "SEP " element
String[] sets = data.split("SEP ");
// each set of data is also internally separated by a space
String[] playerData = sets[0].split(" ");
String[] projectileData = sets[1].split(" ");
String[] asteroidData = sets[2].split(" ");
/* extract the data from each String array into Data */
int red = Integer.parseInt(playerData[0]);
int green = Integer.parseInt(playerData[1]);
int blue = Integer.parseInt(playerData[2]);
Color color = new Color(red, green, blue);
// extract player specific data
double playerX = Double.parseDouble(playerData[3]);
double playerY = Double.parseDouble(playerData[4]);
double playerFacing = Double.parseDouble(playerData[5]);
Ship playerShip = new Ship(color, playerX, playerY, playerFacing);
// extract projectile specific data into an ArrayList
ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
if (projectileData[0].compareTo("NONE") != 0) {
for (int i = 0; i < projectileData.length; i += 3) {
double posX = Double.parseDouble(projectileData[i]);
double posY = Double.parseDouble(projectileData[i+1]);
double dir = Double.parseDouble(projectileData[i+2]);
projectiles.add(new Projectile(color,dir, posX, posY));
}
}
// extract asteroid specific data into an ArrayList
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
if (asteroidData[0].compareTo("NONE") != 0) {
for (int i = 0; i < asteroidData.length; i += 7) {
int type = Integer.parseInt(asteroidData[i]);
int scale = Integer.parseInt(asteroidData[i+1]);
double posX = Double.parseDouble(asteroidData[i+2]);
double posY = Double.parseDouble(asteroidData[i+3]);
double velX = Double.parseDouble(asteroidData[i+4]);
double velY = Double.parseDouble(asteroidData[i+5]);
double dir = Double.parseDouble(asteroidData[i+6]);
asteroids.add(new Asteroid(posX, posY, dir, velX, velY,
type, scale, asteroids));
}
}
return new Data(playerShip, projectiles, asteroids);
}
/**
* This decompression method is used only when a new player
* needs the current state of all the asteroids in the game.
* The new player will receive data regarding the rest of the
* players when it fully joins the universe.
*
* @param data the String containing all necessary game state
* values from a player
* @return a Data object containing only the asteroids
* present in the universe
*/
public Data decompressAsteroids(String data) {
// each set of data is separated by a special "SEP " element
String[] sets = data.split("SEP ");
// extract only the asteroids data
String[] asteroidData = sets[2].split(" ");
// extract asteroid specific data into an ArrayList
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
if (asteroidData[0].compareTo("NONE") != 0) {
for (int i = 0; i < asteroidData.length; i += 7) {
int type = Integer.parseInt(asteroidData[i]);
int scale = Integer.parseInt(asteroidData[i+1]);
double posX = Double.parseDouble(asteroidData[i+2]);
double posY = Double.parseDouble(asteroidData[i+3]);
double velX = Double.parseDouble(asteroidData[i+4]);
double velY = Double.parseDouble(asteroidData[i+5]);
double dir = Double.parseDouble(asteroidData[i+6]);
asteroids.add(new Asteroid(posX, posY, dir, velX, velY,
type, scale, asteroids));
}
}
return new Data(asteroids);
}
/**
* This decompression method is used only when a player needs
* only the ship data and projectile data of other players.
* All players that are already in the game keep track of the
* asteroids in the universe and update their asteroids
* independently. Since all players have the same asteroid
* state as all others when they join, that set of data is
* unneeded when updating others and themselves.
*
* @param data the String containing all necessary game state
* values from a player
* @return a Data object containing only the player's ship and
* associated projectiles
*/
public Data decompressUpdate(String data) {
// each set of data is separated by a special "SEP " element
String[] sets = data.split("SEP ");
// each set of data is also internally separated by a space
String[] playerData = sets[0].split(" ");
String[] projectileData = sets[1].split(" ");
/* extract the data from each String array into Data */
int red = Integer.parseInt(playerData[0]);
int green = Integer.parseInt(playerData[1]);
int blue = Integer.parseInt(playerData[2]);
Color color = new Color(red, green, blue);
// extract player specific data
double playerX = Double.parseDouble(playerData[3]);
double playerY = Double.parseDouble(playerData[4]);
double playerFacing = Double.parseDouble(playerData[5]);
Ship playerShip = new Ship(color, playerX, playerY, playerFacing);
// extract projectile specific data into an ArrayList
ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
if (projectileData[0].compareTo("NONE") != 0) {
for (int i = 0; i < projectileData.length; i += 3) {
double posX = Double.parseDouble(projectileData[i]);
double posY = Double.parseDouble(projectileData[i+1]);
double dir = Double.parseDouble(projectileData[i+2]);
projectiles.add(new Projectile(color,dir, posX, posY));
}
}
return new Data(playerShip, projectiles);
}
/* public static void main(String[] args) {
Ship test = new Ship(Color.RED);
System.out.println(test);
ArrayList<Projectile> testp = new ArrayList<Projectile>();
for (int i = 0; i < 3; i++) {
Projectile newP =
new Projectile(test.getColor(),
test.getFacing(),
test.getTranslatedCentroidX(),
test.getTranslatedCentroidY());
testp.add(newP);
System.out.println(newP);
}
ArrayList<Asteroid> testa = new ArrayList<Asteroid>();
for (int i = 0; i < 5; i++) {
Asteroid newA = new Asteroid(testa);
testa.add(newA);
System.out.println(newA);
}
System.out.println("=====");
DataProcessor dp = new DataProcessor();
String woohoo = dp.compress(test, testp, testa);
System.out.println(woohoo);
System.out.println("=====");
Data result = dp.decompress(woohoo);
System.out.println(result);
}
*/
}