Skip to content

Commit

Permalink
Merge pull request #12 from SharifAIChallenge/feat/jocker_and_batman
Browse files Browse the repository at this point in the history
Feat/jocker and batman
  • Loading branch information
Parsa2820 authored Sep 1, 2022
2 parents a3f3007 + 24234a5 commit e130535
Show file tree
Hide file tree
Showing 15 changed files with 721 additions and 626 deletions.
21 changes: 12 additions & 9 deletions map.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ agents:
nodeId: 1
token: "token_1"
team: FIRST
type: THIEF
type: JOKER
balance: 100.0
- id: 2
nodeId: 1
Expand All @@ -15,13 +15,13 @@ agents:
nodeId: 1
token: "token_3"
team: FIRST
type: THIEF
type: JOKER
balance: 100.0
- id: 4
nodeId: 1
token: "token_4"
team: FIRST
type: POLICE
type: BATMAN
balance: 100.0
- id: 5
nodeId: 1
Expand All @@ -45,13 +45,13 @@ agents:
nodeId: 1
token: "token_8"
team: FIRST
type: POLICE
type: BATMAN
balance: 100.0
- id: 9
nodeId: 1
token: "token_9"
team: SECOND
type: THIEF
type: JOKER
balance: 100.0
- id: 10
nodeId: 1
Expand All @@ -63,13 +63,13 @@ agents:
nodeId: 1
token: "token_11"
team: SECOND
type: THIEF
type: JOKER
balance: 100.0
- id: 12
nodeId: 1
token: "token_12"
team: SECOND
type: POLICE
type: BATMAN
balance: 100.0
- id: 13
nodeId: 1
Expand All @@ -93,14 +93,17 @@ agents:
nodeId: 1
token: "token_16"
team: SECOND
type: POLICE
type: BATMAN
balance: 100.0

income:
policeIncomeEachTurn: 10
thiefIncomeEachTurn: 10

graph:
visibleRadiusXPoliceThief: 4
visibleRadiusYPoliceJoker: 3
visibleRadiusZThiefBatman: 3
paths:
- id: 1
firstNodeId: 1
Expand Down Expand Up @@ -1507,4 +1510,4 @@ turnSettings:

chatSettings:
chatBoxMaxSize: 100
chatCostPerCharacter: 1
chatCostPerCharacter: 1
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>ir.sharif.aic</groupId>
<artifactId>hideandseek</artifactId>
<version>0.1.71</version>
<version>0.1.72-final-server</version>
<name>HideAndSeek</name>
<description>the implementation of aic 2022 game server</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void watch(GameEvent event) {
event instanceof GameResultChangedEvent ||
event instanceof AgentDeclaredReadinessEvent))
return;
// logger.info("this log is from event : " + event.getType());
var view = this.gameService.getView(this.agentToken);
this.observer.onNext(view);
if (isAgentArrested(event)) {
Expand Down
114 changes: 106 additions & 8 deletions src/main/java/ir/sharif/aic/hideandseek/config/GameConfigInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ir.sharif.aic.hideandseek.api.grpc.HideAndSeek;
import ir.sharif.aic.hideandseek.core.exceptions.NotFoundException;
import ir.sharif.aic.hideandseek.core.models.*;
import ir.sharif.aic.hideandseek.core.watchers.EventLogger;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
Expand All @@ -27,17 +28,22 @@ public class GameConfigInjector {
private static final String JAVA_EXEC_CMD = "java -jar";
private static final Logger LOGGER = LoggerFactory.getLogger(GameConfigInjector.class);
private static String FIRST_TEAM_PATH = null;
private static String FIRST_TEAM_NAME = null;
private static String SECOND_TEAM_PATH = null;
private static String SECOND_TEAM_NAME = null;
private static String GAME_CONFIG_PATH = null;
private static String MAP_PATH = null;
private final static int INF = Integer.MAX_VALUE;


public static void handleCMDArgs(String[] args) {
int namedArgsCount = 0;
for (String arg : args) {
handleArg(arg);
namedArgsCount += handleArg(arg) ? 1 : 0;
}
try {
GAME_CONFIG_PATH = args[2];
MAP_PATH = args[3];
GAME_CONFIG_PATH = args[namedArgsCount];
MAP_PATH = args[namedArgsCount+1];
} catch (Exception ignore) {
LOGGER.error("Invalid args.");
}
Expand All @@ -54,19 +60,34 @@ public static void handleCMDArgs(String[] args) {
if (MAP_PATH == null) {
LOGGER.warn("No path for map.json");
}

String teamsNames;
if (FIRST_TEAM_NAME != null && SECOND_TEAM_NAME != null) {
teamsNames = String.format("{\"first\":\"%s\", \"second\":\"%s\"}", FIRST_TEAM_NAME, SECOND_TEAM_NAME);
} else {
teamsNames = "{\"first\":\"FIRST\", \"second\":\"SECOND\"}";
}
GraphicLogger.getInstance().appendLog(teamsNames);
}

private static void handleArg(String arg) {
private static boolean handleArg(String arg) {
String[] split = arg.split("=");
try {
if (split[0].equals("--first-team")) {
FIRST_TEAM_PATH = split[1];
} else if (split[0].equals("--second-team")) {
SECOND_TEAM_PATH = split[1];
} else if (split[0].equals("--first-team-name")) {
FIRST_TEAM_NAME = split[1];
} else if (split[0].equals("--second-team-name")) {
SECOND_TEAM_NAME = split[1];
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

private static String createRunCMD(String path) {
Expand All @@ -79,20 +100,21 @@ private static String createRunCMD(String path) {

@Bean
public GameConfig createGameConfig() throws IOException {
var graph = new Graph();
var mapper = new ObjectMapper(new YAMLFactory());
try {
var settings = mapper.readValue(new File(GAME_CONFIG_PATH), GameSettings.class);
var graph = new Graph(settings.graph.visibleRadiusXPoliceThief, settings.graph.visibleRadiusYPoliceJoker, settings.graph.visibleRadiusZThiefBatman);
if (MAP_PATH != null) {
try {
Scanner scanner = new Scanner(new File(MAP_PATH));
var map = scanner.nextLine();
var map = scanner.nextLine();
LOGGER.info(map);
GraphicLogger.getInstance().appendLog(map);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
AddRadiusToNodes(settings.graph, settings.getGraph().visibleRadiusXPoliceThief, settings.getGraph().visibleRadiusYPoliceJoker, settings.getGraph().visibleRadiusZThiefBatman);
settings.graph.nodes.forEach(graph::addNode);
settings.graph.paths.forEach(path -> addPathToGraph(settings.graph.nodes, graph, path));

Expand All @@ -110,7 +132,7 @@ public GameConfig createGameConfig() throws IOException {
public void run() {
try {
Thread.sleep(4000);
Process p = Runtime.getRuntime().exec( runCommand + ' ' + agent.getToken());
Process p = Runtime.getRuntime().exec(runCommand + ' ' + agent.getToken());
var error = p.getErrorStream();
var input = p.getInputStream();

Expand Down Expand Up @@ -164,6 +186,79 @@ public void run() {
}
}

private void AddRadiusToNodes(GraphSettings graphSettings, int XRadius, int YRadius, int ZRadius) {
var baseGraph = createFloydWarshallGraph(graphSettings);
baseGraph = calculateVisibleRadiusGraph(baseGraph, graphSettings.nodes.size());
addVisibleRadiusToNodes(graphSettings, baseGraph, XRadius, YRadius, ZRadius);
}

private int[][] createFloydWarshallGraph(GraphSettings graphSettings) {
var nodes = graphSettings.getNodes();
final var V = nodes.size();
int[][] graph = new int[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
graph[i][j] = checkIfAPathWeightIsZero(graphSettings, i, j);
}
}
return graph;
}


private int[][] calculateVisibleRadiusGraph(int[][] graph, int V) {
int dist[][] = new int[V][V];
int i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++) {
// Pick all vertices as source one by one
for (i = 0; i < V; i++) {
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++) {
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
return dist;
}

private void addVisibleRadiusToNodes(GraphSettings graphSettings, int[][] graph, int XRadius, int YRadius, int ZRadius) {
var nodes = graphSettings.getNodes();
nodes.forEach(e -> {
var nodes_from_e = graph[e.getId() - 1];
for (int i = 0; i < nodes_from_e.length; i++) {
int distance = graph[e.getId() - 1][i];
var node = findNodeById(nodes, i + 1);
if (distance <= XRadius)
e.getVisibleRadiusXPoliceThief().add(node);
if (distance <= YRadius)
e.getVisibleRadiusYPoliceJoker().add(node);
if (distance <= ZRadius)
e.getVisibleRadiusZThiefBatman().add(node);

}
});
}


private int checkIfAPathWeightIsZero(GraphSettings graphSettings, int src, int dest) {
if (src == dest)
return 0;
var paths = graphSettings.getPaths();
var path = paths.stream().filter(e -> (
(e.getFirstNodeId() == src && e.getSecondNodeId() == dest) ||
(e.getFirstNodeId() == dest && e.getSecondNodeId() == src))).findFirst();
if (path.isPresent() && path.get().getPrice() == 0)
return 1;
return INF;

}

private Node findNodeById(List<Node> nodes, int nodeId) {
return nodes.stream().filter(node -> node.getId() == nodeId).findFirst().orElseThrow(() -> new NotFoundException(Node.class.getSimpleName(), nodeId));
}
Expand All @@ -176,6 +271,9 @@ private void addPathToGraph(List<Node> nodes, Graph graph, Path path) {
private static class GraphSettings {
private List<Node> nodes;
private List<Path> paths;
private int visibleRadiusXPoliceThief;
private int visibleRadiusYPoliceJoker;
private int visibleRadiusZThiefBatman;
}

@Data
Expand Down Expand Up @@ -208,7 +306,7 @@ public HideAndSeek.ChatSettings toProto() {
}
}

@Value( "${clientReadinessThresholdTimeMillisecond:7000}")
@Value("${clientReadinessThresholdTimeMillisecond:7000}")
private int clientReadinessThresholdTimeMillisecond;

@Value("${magicTurnTime: 1000}")
Expand Down
Loading

0 comments on commit e130535

Please sign in to comment.