Skip to content

Commit dd7f61d

Browse files
committed
Timeouts configured.
1 parent 977d502 commit dd7f61d

16 files changed

Lines changed: 166 additions & 133 deletions

File tree

Mithosis/src/core/Context.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import core.model.Map;
77
import model.*;
88
import server.core.model.ClientInfo;
9-
import util.Constants;
9+
import util.ServerConstants;
1010

1111
import java.io.*;
1212
import java.nio.file.Files;
@@ -72,15 +72,15 @@ public class Context {
7272

7373
private void loadMap(String dir) throws IOException {
7474
String[] types = {
75-
Constants.BLOCK_TYPE_NONE,
76-
Constants.BLOCK_TYPE_NORMAL,
77-
Constants.BLOCK_TYPE_MITOSIS,
78-
Constants.BLOCK_TYPE_RESOURCE,
79-
Constants.BLOCK_TYPE_IMPASSABLE
75+
ServerConstants.BLOCK_TYPE_NONE,
76+
ServerConstants.BLOCK_TYPE_NORMAL,
77+
ServerConstants.BLOCK_TYPE_MITOSIS,
78+
ServerConstants.BLOCK_TYPE_RESOURCE,
79+
ServerConstants.BLOCK_TYPE_IMPASSABLE
8080
};
8181

8282
File file = new File(dir);
83-
String json = new String(Files.readAllBytes(file.toPath()), Constants.MAP_FILE_ENCODING);
83+
String json = new String(Files.readAllBytes(file.toPath()), ServerConstants.MAP_FILE_ENCODING);
8484
//System.out.println(json);
8585
Map.FileStructure fs = new Gson().fromJson(json, Map.FileStructure.class);
8686

@@ -93,8 +93,8 @@ private void loadMap(String dir) throws IOException {
9393
int type = block.values[0].intValue();
9494
int height = block.values[1].intValue();
9595
int resource = block.values[2].intValue();
96-
boolean isMovable = !types[type].equals(Constants.BLOCK_TYPE_IMPASSABLE);
97-
Block b = new Block(this, Constants.TURN_MAKE_MAP, j, i, height, resource, types[type], isMovable);
96+
boolean isMovable = !types[type].equals(ServerConstants.BLOCK_TYPE_IMPASSABLE);
97+
Block b = new Block(this, ServerConstants.TURN_MAKE_MAP, j, i, height, resource, types[type], isMovable);
9898
map.set(j, i, b);
9999
}
100100
}
@@ -169,7 +169,7 @@ public Cell getCell(String id)
169169

170170
public boolean addCell(Cell cell) {
171171
//checking
172-
if (!cell.getType().equals(Constants.GAME_OBJECT_TYPE_CELL)) {
172+
if (!cell.getType().equals(ServerConstants.GAME_OBJECT_TYPE_CELL)) {
173173
return false;
174174
}
175175
if (!checkBounds(cell.getPos())) {

Mithosis/src/core/MitosisGameLogic.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import model.*;
55
import core.model.Map;
66
import data.*;
7-
import util.Constants;
7+
import util.ServerConstants;
88
import server.Server;
99
import server.core.GameLogic;
1010
import server.core.model.ClientInfo;
@@ -19,7 +19,7 @@
1919
*/
2020
public class MitosisGameLogic implements GameLogic {
2121

22-
private final long GAME_LONG_TIME_TURN = 100; //TODO can change
22+
private final long GAME_LONG_TIME_TURN = 500;
2323

2424
private static final String RESOURCE_PATH_CLIENTS = "resources/mitosis/clients.conf";
2525

@@ -43,7 +43,7 @@ public static void main(String[] args) {
4343
public MitosisGameLogic(String[] options) throws IOException {
4444
super();
4545

46-
ctx = new Context(Constants.TURN_INIT, options[0], RESOURCE_PATH_CLIENTS);
46+
ctx = new Context(ServerConstants.TURN_INIT, options[0], RESOURCE_PATH_CLIENTS);
4747

4848
Map map = ctx.getMap();
4949

@@ -59,7 +59,7 @@ public void init() {
5959
}
6060

6161
ArrayList<String> viewsList = (ArrayList<String>) teamsList.clone();
62-
viewsList.add(Constants.VIEW_GLOBAL);
62+
viewsList.add(ServerConstants.VIEW_GLOBAL);
6363

6464
Map map = ctx.getMap();
6565

@@ -74,14 +74,14 @@ public void init() {
7474
for (int col = 0; col < width; col++) {
7575
HashMap<String, Object> blockMap = new HashMap<>();
7676
Block block = map.at(col, row);
77-
blockMap.put(Constants.GAME_OBJECT_KEY_ID, block.getId());
78-
blockMap.put(Constants.GAME_OBJECT_KEY_TYPE, Constants.BLOCK_TYPE_NONE);
79-
blockMap.put(Constants.GAME_OBJECT_KEY_TURN, Constants.TURN_WORLD_CREATION);
80-
blockMap.put(Constants.GAME_OBJECT_KEY_POSITION, new Position(block.getX(), block.getY()));
77+
blockMap.put(ServerConstants.GAME_OBJECT_KEY_ID, block.getId());
78+
blockMap.put(ServerConstants.GAME_OBJECT_KEY_TYPE, ServerConstants.BLOCK_TYPE_NONE);
79+
blockMap.put(ServerConstants.GAME_OBJECT_KEY_TURN, ServerConstants.TURN_WORLD_CREATION);
80+
blockMap.put(ServerConstants.GAME_OBJECT_KEY_POSITION, new Position(block.getX(), block.getY()));
8181
HashMap<String,Object> otherDict = new HashMap<>();
82-
otherDict.put(Constants.BLOCK_KEY_HEIGHT, 0);
83-
otherDict.put(Constants.BLOCK_KEY_RESOURCE, 0);
84-
blockMap.put(Constants.GAME_OBJECT_KEY_OTHER, otherDict);
82+
otherDict.put(ServerConstants.BLOCK_KEY_HEIGHT, 0);
83+
otherDict.put(ServerConstants.BLOCK_KEY_RESOURCE, 0);
84+
blockMap.put(ServerConstants.GAME_OBJECT_KEY_OTHER, otherDict);
8585
unknownMap.add(blockMap);
8686
}
8787
}
@@ -96,10 +96,10 @@ public void init() {
9696
TeamInfo teamInfo = new TeamInfo(ctx.getClientsInfo()[t].getName(), ctx.getClientsInfo()[t].getID());
9797

9898
HashMap<String, Object> info = new HashMap<>();
99-
info.put(Constants.INFO_KEY_TURN, ctx.getTurn());
100-
info.put(Constants.INFO_KEY_TEAMS, teamsList);
101-
info.put(Constants.INFO_KEY_YOUR_INFO, teamInfo);
102-
info.put(Constants.INFO_KEY_MAPSIZE, mapSize);
99+
info.put(ServerConstants.INFO_KEY_TURN, ctx.getTurn());
100+
info.put(ServerConstants.INFO_KEY_TEAMS, teamsList);
101+
info.put(ServerConstants.INFO_KEY_YOUR_INFO, teamInfo);
102+
info.put(ServerConstants.INFO_KEY_MAP_SIZE, mapSize);
103103

104104

105105
//make static diff
@@ -117,10 +117,10 @@ public void init() {
117117

118118
//make info
119119
HashMap<String, Object> info = new HashMap<>();
120-
info.put(Constants.INFO_KEY_TURN, ctx.getTurn());
121-
info.put(Constants.INFO_KEY_TEAMS, teamsList);
122-
info.put(Constants.INFO_KEY_VIEWS, viewsList);
123-
info.put(Constants.INFO_KEY_MAPSIZE, mapSize);
120+
info.put(ServerConstants.INFO_KEY_TURN, ctx.getTurn());
121+
info.put(ServerConstants.INFO_KEY_TEAMS, teamsList);
122+
info.put(ServerConstants.INFO_KEY_VIEWS, viewsList);
123+
info.put(ServerConstants.INFO_KEY_MAP_SIZE, mapSize);
124124

125125
//make map
126126
// map is ready
@@ -132,7 +132,7 @@ public void init() {
132132
for (int t = 0; t < mTeams.length; t++)
133133
{
134134
HashMap<String,Object> viewDif = new HashMap<>();
135-
viewDif.put(Constants.VIEW,"team" + t);
135+
viewDif.put(ServerConstants.VIEW,"team" + t);
136136

137137
//calculate static diff for each team
138138
ArrayList<StaticGameObject> staticDiff = new ArrayList<>();
@@ -146,7 +146,7 @@ public void init() {
146146
//Generate Global diff
147147
{
148148
HashMap<String, Object> viewDif = new HashMap<>();
149-
viewDif.put(Constants.VIEW, Constants.VIEW_GLOBAL);
149+
viewDif.put(ServerConstants.VIEW, ServerConstants.VIEW_GLOBAL);
150150

151151
//calculate static diff for global view
152152
ArrayList<StaticData> staticDiff = new ArrayList<>();
@@ -267,7 +267,7 @@ public void simulateEvents(Event[] terminalEvent, Event[] environmentEvent, Even
267267
if (cell == null) continue;
268268

269269
Block block = map.at(cell.getPos());
270-
if(!block.getType().equals(Constants.BLOCK_TYPE_MITOSIS))
270+
if(!block.getType().equals(ServerConstants.BLOCK_TYPE_MITOSIS))
271271
{
272272
continue;
273273
}
@@ -318,7 +318,7 @@ public void simulateEvents(Event[] terminalEvent, Event[] environmentEvent, Even
318318
if (cell == null) continue;
319319

320320
Block block = map.at(cell.getPos());
321-
if(!block.getType().equals(Constants.BLOCK_TYPE_RESOURCE))
321+
if(!block.getType().equals(ServerConstants.BLOCK_TYPE_RESOURCE))
322322
{
323323
continue;
324324
}
@@ -446,7 +446,7 @@ public void generateOutputs() {
446446
}
447447
}
448448

449-
uiTurnData.setView( Constants.VIEW_GLOBAL);
449+
uiTurnData.setView( ServerConstants.VIEW_GLOBAL);
450450
uiTurnData.setDynamics(dynamics);
451451
uiTurnData.setStatics(statics);
452452
uiTurnData.setTransients(transients);

Mithosis/src/core/model/Block.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import data.BlockData;
55
import data.StaticData;
66
import model.Position;
7-
import util.Constants;
7+
import util.ServerConstants;
88
import util.UID;
99

1010
public class Block {
@@ -25,11 +25,11 @@ public class Block {
2525

2626

2727
public Block(Context ctx, int turn, int x, int y) {
28-
this(ctx, turn, x, y, 0, 0, Constants.BLOCK_TYPE_NONE, true);
28+
this(ctx, turn, x, y, 0, 0, ServerConstants.BLOCK_TYPE_NONE, true);
2929
}
3030

3131
public Block(Context ctx, int turn, int x, int y, int height, int resource) {
32-
this(ctx, turn, x, y, height, resource, Constants.BLOCK_TYPE_NONE, true);
32+
this(ctx, turn, x, y, height, resource, ServerConstants.BLOCK_TYPE_NONE, true);
3333
}
3434

3535
public Block(Context ctx, int turn, int x, int y, int height, int resource, String type) {

Mithosis/src/core/model/Cell.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import model.Direction;
77
import model.Position;
88
import util.Constants;
9+
import util.ServerConstants;
910
import util.UID;
1011

1112
import java.util.ArrayList;
@@ -30,7 +31,7 @@ public Cell (Context ctx, Position pos, int teamId, int dof, int energy, int gai
3031
super(teamId);
3132
this.ctx = ctx;
3233
id = UID.getUID();
33-
this.type = Constants.GAME_OBJECT_TYPE_CELL;
34+
this.type = ServerConstants.GAME_OBJECT_TYPE_CELL;
3435
this.position = pos;
3536
depthOfField = dof;
3637
this.energy = energy;
@@ -81,6 +82,10 @@ public void setGainRate(int gainRate) {
8182

8283
public boolean mitosis() {
8384
//System.out.println("mitosis start");
85+
if(energy < ServerConstants.CELL_MIN_ENERGY_FOR_MITOSIS)
86+
{
87+
return false;
88+
}
8489
Position secondCellPos = null;
8590
ArrayList<Direction> directions = ctx.getShuffledListOfDirections();
8691
for(Direction d : directions)
@@ -96,10 +101,15 @@ public boolean mitosis() {
96101
break;
97102
}
98103
}
99-
if(secondCellPos == null)
100-
{
104+
if(secondCellPos == null) {
105+
return false;
106+
}
107+
int currentHeight = ctx.getMap().at(position).getHeight();
108+
int nextHeight = ctx.getMap().at(secondCellPos).getHeight();
109+
if (nextHeight > currentHeight + 1) {
101110
return false;
102111
}
112+
int energy = ServerConstants.CELL_MIN_ENERGY_FOR_MITOSIS;
103113
int secondEnergy = energy / 2;
104114
setEnergy(energy - secondEnergy);
105115
Cell newCell = new Cell
@@ -147,7 +157,7 @@ public boolean gainResource () {
147157
//System.out.println("gain start");
148158
//System.out.println(ctx.getMap().at(position).getResource());
149159
//System.out.println(energy);
150-
if(energy >= Constants.CELL_MAX_ENERGY)
160+
if(energy >= ServerConstants.CELL_MAX_ENERGY)
151161
{
152162
return false;
153163
}
@@ -161,9 +171,9 @@ public boolean gainResource () {
161171
if( blockResource < gainRate) {
162172
gain = blockResource;
163173
}
164-
else if(gainRate + blockResource > Constants.CELL_MAX_ENERGY)
174+
else if(gainRate + blockResource > ServerConstants.CELL_MAX_ENERGY)
165175
{
166-
gain = Constants.CELL_MAX_ENERGY - energy;
176+
gain = ServerConstants.CELL_MAX_ENERGY - energy;
167177
}
168178
else{
169179
gain = gainRate;

Mithosis/src/core/model/Map.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import core.Context;
55
import model.Position;
66
import util.Constants;
7+
import util.ServerConstants;
78

89
import java.io.File;
910
import java.io.IOException;
@@ -30,7 +31,7 @@ public static class TypeStructure {
3031
public String name;
3132
public String[] keys;
3233
public Number[] defaults;
33-
public String[] coloring;
34+
public String coloring;
3435
public ObjectStructure[] instances;
3536
}
3637

@@ -42,7 +43,7 @@ public static class MapStructure {
4243
public int width, height;
4344
public String[] keys;
4445
public Number[] defaults;
45-
public String[] coloring;
46+
public String coloring;
4647
public BlockStructure[][] blocks;
4748
}
4849

@@ -55,15 +56,15 @@ public static Map load(Context ctx, String dir) throws IOException, IOException
5556

5657

5758
String[] types = {
58-
Constants.BLOCK_TYPE_NONE,
59-
Constants.BLOCK_TYPE_NORMAL,
60-
Constants.BLOCK_TYPE_MITOSIS,
61-
Constants.BLOCK_TYPE_RESOURCE,
62-
Constants.BLOCK_TYPE_IMPASSABLE
59+
ServerConstants.BLOCK_TYPE_NONE,
60+
ServerConstants.BLOCK_TYPE_NORMAL,
61+
ServerConstants.BLOCK_TYPE_MITOSIS,
62+
ServerConstants.BLOCK_TYPE_RESOURCE,
63+
ServerConstants.BLOCK_TYPE_IMPASSABLE
6364
};
6465

6566
File file = new File(dir);
66-
String json = new String(Files.readAllBytes(file.toPath()), Constants.MAP_FILE_ENCODING);
67+
String json = new String(Files.readAllBytes(file.toPath()), ServerConstants.MAP_FILE_ENCODING);
6768
System.out.println(json);
6869
FileStructure fs = new Gson().fromJson(json, FileStructure.class);
6970

@@ -76,8 +77,8 @@ public static Map load(Context ctx, String dir) throws IOException, IOException
7677
int type = block.values[0].intValue();
7778
int height = block.values[1].intValue();
7879
int resource = block.values[2].intValue();
79-
boolean isMovable = !types[type].equals(Constants.BLOCK_TYPE_IMPASSABLE);
80-
Block b = new Block(ctx, Constants.TURN_MAKE_MAP, j, i, height, resource, types[type], isMovable);
80+
boolean isMovable = !types[type].equals(ServerConstants.BLOCK_TYPE_IMPASSABLE);
81+
Block b = new Block(ctx, ServerConstants.TURN_MAKE_MAP, j, i, height, resource, types[type], isMovable);
8182
map.set(j, i, b);
8283
}
8384
}
@@ -91,6 +92,8 @@ public static Map load(Context ctx, String dir) throws IOException, IOException
9192
int dof = instance.values[1].intValue();
9293
int energy = instance.values[2].intValue();
9394
int gainRate = instance.values[3].intValue();
95+
// dof = Constants.CELL_DEPTH_OF_FIELD;
96+
// gainRate = Constants.CELL_GAIN_RATE;
9497
Cell cell = new Cell(ctx, new Position(x, y), teamID, dof, energy, gainRate);
9598
ctx.addCell(cell);
9699
}

Mithosis/src/core/model/Team.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import core.Context;
44
import model.Position;
55
import server.core.model.ClientInfo;
6-
import util.Constants;
6+
import util.ServerConstants;
77

88
import java.util.ArrayList;
99

@@ -33,7 +33,7 @@ public void makeMap()
3333
viewHistory = new int[map.getHeight()][map.getWidth()];
3434
for (int row = 0; row < map.getHeight(); row++) {
3535
for (int col = 0; col < map.getWidth(); col++) {
36-
viewHistory[row][col] = Constants.TURN_TEAM_VIEW_HISTORY_DEFAULT;
36+
viewHistory[row][col] = ServerConstants.TURN_TEAM_VIEW_HISTORY_DEFAULT;
3737
/*Block block = map.getBlocks()[row][col];
3838
if(!block.isEmpty()) {
3939
Cell c = block.getCell();

0 commit comments

Comments
 (0)