Skip to content

Commit b28a1e2

Browse files
committed
Server security enhancement
1 parent df7dbec commit b28a1e2

28 files changed

Lines changed: 432 additions & 276 deletions

Client/src/client/AI.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import util.Constants;
77
import util.ServerConstants;
88

9+
import java.util.ArrayList;
10+
import java.util.Arrays;
911
import java.util.Random;
1012

1113
/**
@@ -19,41 +21,27 @@
1921
public class AI {
2022

2123
public void doTurn(World world) {
24+
ArrayList<Cell> cells = world.getMyCells();
25+
Object[] xs = cells.stream().map(Cell::getPos).toArray();
26+
System.out.println(Arrays.toString(xs));
27+
2228
long start = System.currentTimeMillis();
23-
System.out.println(start);
24-
// System.out.println(model.getTurnRemainingTime());
25-
/*System.out.println(world.getMapSize().getWidth());
26-
System.out.println(world.getMapSize().getHeight());
27-
System.out.println(world.getMyId());
28-
System.out.println(world.getMyName());
29-
System.out.println(world.getTeams().length);
30-
System.out.println(world.getTurn());*/
31-
System.out.println(world.getMyCells().size());
3229
Random rnd = new Random();
33-
for(Cell c : world.getMyCells())
34-
{
30+
for(Cell c : world.getMyCells()) {
3531
Block block = world.getMap().at(c.getPos());
36-
if(c.getEnergy() >= Constants.CELL_MIN_ENERGY_FOR_MITOSIS && block.getType().equals(Constants.BLOCK_TYPE_MITOSIS))
37-
{
32+
if(c.getEnergy() >= Constants.CELL_MIN_ENERGY_FOR_MITOSIS && block.getType().equals(Constants.BLOCK_TYPE_MITOSIS)) {
3833
System.out.println("mitosis");
3934
System.out.println(c.getEnergy());
4035
c.mitosis();
41-
}
42-
else if(c.getEnergy() < Constants.CELL_MAX_ENERGY && block.getType().equals(Constants.BLOCK_TYPE_RESOURCE) && block.getResource() > 0)
43-
{
44-
//System.out.println("gain");
45-
//System.out.println(block.getResource());
46-
//System.out.println(c.getEnergy());
36+
} else if(c.getEnergy() < Constants.CELL_MAX_ENERGY && block.getType().equals(Constants.BLOCK_TYPE_RESOURCE) && block.getResource() > 0) {
4737
c.gainResource();
4838
}
4939
else {
5040
c.move(Direction.values()[rnd.nextInt(6)]);
5141
}
5242
}
53-
//GameEvent event = new GameEvent();
54-
//model.addEvent(event);
55-
// you should fill this method
56-
// an example is included here
43+
long end = System.currentTimeMillis();
44+
System.out.println(end - start);
5745
}
5846

5947
}

Client/src/client/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ public static void main(String[] args) {
99
new Controller("resources/client/connection.conf").start();
1010
}
1111

12-
}
12+
}
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package core;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
36
import core.model.*;
47
import model.*;
58
import core.model.Map;
69
import data.*;
10+
import server.core.model.Configs;
711
import util.ServerConstants;
812
import server.Server;
913
import server.core.GameLogic;
@@ -12,16 +16,21 @@
1216
import network.data.Message;
1317

1418
import javax.xml.bind.SchemaOutputResolver;
19+
import java.io.File;
1520
import java.io.IOException;
21+
import java.nio.charset.Charset;
22+
import java.nio.file.Files;
1623
import java.util.*;
1724

1825
/**
1926
* Created by rajabzz on 2/2/15.
2027
*/
2128
public class MitosisGameLogic implements GameLogic {
2229

23-
private final long GAME_LONG_TIME_TURN = 10;//TODO 500
30+
private final long GAME_LONG_TIME_TURN;
2431

32+
private static final Charset CONFIG_ENCODING = Charset.forName("UTF-8");
33+
private static final String RESOURCE_PATH_GAME = "resources/mitosis/game.conf";
2534
private static final String RESOURCE_PATH_CLIENTS = "resources/mitosis/clients.conf";
2635

2736

@@ -44,9 +53,10 @@ public static void main(String[] args) {
4453
public MitosisGameLogic(String[] options) throws IOException {
4554
super();
4655

47-
ctx = new Context(ServerConstants.TURN_INIT, options[0], RESOURCE_PATH_CLIENTS);
56+
String gameConfig = new String(Files.readAllBytes(new File(RESOURCE_PATH_GAME).toPath()), CONFIG_ENCODING);
57+
GAME_LONG_TIME_TURN = new Gson().fromJson(gameConfig, JsonObject.class).get("turn").getAsLong();
4858

49-
Map map = ctx.getMap();
59+
ctx = new Context(ServerConstants.TURN_INIT, options[0], RESOURCE_PATH_CLIENTS);
5060

5161
mTeams = ctx.getTeams();
5262
}
@@ -208,8 +218,7 @@ public void simulateEvents(Event[] terminalEvent, Event[] environmentEvent, Even
208218
{
209219
GameEvent event = new GameEvent(clientsEvent[i][j]);
210220
//event.getGameObjectId() TODO CHECK OWNER
211-
if(ctx.getDynamicObject(event.getObjectId()).getTeamId() != i)
212-
{
221+
if(ctx.getDynamicObject(event.getObjectId()).getTeamId() != i) {
213222
continue;
214223
}
215224
event.setTeamId(i);
File renamed without changes.

0 commit comments

Comments
 (0)