Skip to content

Commit 637dc53

Browse files
author
denrus
committed
Логирование хода матча в файл
1 parent c564969 commit 637dc53

File tree

7 files changed

+139
-3
lines changed

7 files changed

+139
-3
lines changed

server/hypernull.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
server.port=2021
2+
match.log.folder=./matchlogs/

server/src/main/java/ru/croccode/hypernull/match/Match.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,23 @@ private void collectCoins() {
365365
}
366366
}
367367
}
368+
369+
public void finished() {
370+
//TODO Надо ли оповещать через MatchListener, что матч совсем всё?
371+
for (MatchListener<K> listener : listeners) {
372+
try {
373+
listener.close();
374+
} catch (Exception e) {
375+
throw new RuntimeException(
376+
String.format(
377+
"Ошибка при закрытии слушателя %s матча %s: %s",
378+
listener.getClass().getSimpleName(),
379+
this,//TODO id?
380+
e.getMessage()
381+
),
382+
e
383+
);
384+
}
385+
}
386+
}
368387
}

server/src/main/java/ru/croccode/hypernull/match/MatchListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import ru.croccode.hypernull.domain.MatchMap;
66
import ru.croccode.hypernull.geometry.Point;
77

8-
public interface MatchListener<K> {
8+
public interface MatchListener<K> extends AutoCloseable{
99

1010
void matchStarted(MatchMap map, MatchConfig config, Map<K, String> botNames);
1111

server/src/main/java/ru/croccode/hypernull/server/AsciiMatchPrinter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public class AsciiMatchPrinter implements MatchListener<Integer> {
3333

3434
private int round;
3535

36+
@Override
37+
public void close() {
38+
//nothing
39+
}
40+
3641
static class BotState {
3742

3843
String name = Strings.empty();

server/src/main/java/ru/croccode/hypernull/server/HyperNull.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ public class HyperNull implements Runnable, Closeable {
3434
private final MapRegistry mapRegistry;
3535

3636
private final Server server;
37+
private final String matchLogsFolder;
3738

3839
public HyperNull(Properties properties) throws IOException {
3940
Check.notNull(properties);
4041
mapRegistry = new RandomMapRegistry(); // TODO implement MapRegistry
4142
// start server
4243
int serverPort = Integer.parseInt(
4344
properties.getProperty("server.port", "2021"));
45+
46+
matchLogsFolder = properties.getProperty("match.log.folder","./matchlogs/");
4447
this.server = new Server(serverPort);
4548
}
4649

@@ -87,7 +90,8 @@ private void runMatch(MatchMode mode, List<MatchRequest> matchRequests) {
8790
MatchMap map = mapRegistry.randomMap(numBots);
8891
MatchConfig config = buildMatchConfig(mode, map);
8992
List<MatchListener<Integer>> listeners = Arrays.asList(
90-
new AsciiMatchPrinter()
93+
new AsciiMatchPrinter(),
94+
new MatchFileLogger<>(this.matchLogsFolder)
9195
);
9296
Match<Integer> match = new Match<>(map, config, botNames, listeners);
9397
new MatchRunner(match, botSessions).run();
@@ -109,6 +113,7 @@ public void close() throws IOException {
109113
}
110114

111115
public static void main(String[] args) throws IOException {
116+
System.out.println("Запуск сервера...");
112117
String configPath = args.length > 0
113118
? args[0]
114119
: "hypernull.properties";
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ru.croccode.hypernull.server;
2+
3+
import ru.croccode.hypernull.domain.MatchMap;
4+
import ru.croccode.hypernull.geometry.Point;
5+
import ru.croccode.hypernull.match.MatchConfig;
6+
import ru.croccode.hypernull.match.MatchListener;
7+
8+
import java.io.IOException;
9+
import java.io.PrintWriter;
10+
import java.lang.reflect.Field;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.Map;
15+
import java.util.UUID;
16+
17+
public class MatchFileLogger<K> implements MatchListener<K> {
18+
19+
private final static String LOG_FILE_TEMPLATE = "%s/Match_log_%s.txt";
20+
21+
private final UUID matchId;
22+
private final PrintWriter logWriter;
23+
24+
public MatchFileLogger(String logsFolder) {
25+
this.matchId = UUID.randomUUID();
26+
Path path = Paths.get(logsFolder);
27+
try {
28+
Files.createDirectories(path);
29+
final String logFileName = String.format(LOG_FILE_TEMPLATE, logsFolder, matchId);
30+
this.logWriter = new PrintWriter(logFileName);
31+
} catch (IOException ex) {
32+
throw new RuntimeException(
33+
"Ошибка инициализации логера в файл: " + ex.getMessage(),
34+
ex
35+
);
36+
}
37+
}
38+
39+
@Override
40+
public void matchStarted(MatchMap map, MatchConfig config, Map<K, String> botNames) {
41+
write("match");
42+
for (Field declaredField : config.getClass().getDeclaredFields()) {
43+
try {
44+
declaredField.setAccessible(true);
45+
write(declaredField.getName() + " " + declaredField.get(config));
46+
} catch (IllegalAccessException e) {
47+
throw new RuntimeException(
48+
"Ошибка получения значения поля через рефлексию: " + e.getMessage(),
49+
e
50+
);
51+
}
52+
}
53+
for (Map.Entry<K, String> botEntry : botNames.entrySet()) {
54+
write("bot_name " + botEntry.getKey() + " " + botEntry.getValue());
55+
}
56+
}
57+
58+
@Override
59+
public void matchRound(int round) {
60+
write("round " + round);
61+
}
62+
63+
@Override
64+
public void coinSpawned(Point position) {
65+
write("coin " + position.toString());
66+
}
67+
68+
@Override
69+
public void coinCollected(Point position, K botKey) {
70+
write("coin_collected " + position.toString() + " " + botKey);
71+
}
72+
73+
@Override
74+
public void botSpawned(K botKey, Point position) {
75+
botMoved(botKey, position);
76+
}
77+
78+
@Override
79+
public void botMoved(K botKey, Point position) {
80+
write("bot " + botKey + " " + position.toString());
81+
}
82+
83+
@Override
84+
public void attack(K attackingKey, K defendingKey) {
85+
write("attack " + attackingKey + " " + defendingKey);
86+
}
87+
88+
@Override
89+
public void botCoinsChanged(K botKey, int numCoins) {
90+
write("bot_coins " + botKey + " " + numCoins); //TODO надо ли выводить предыдущее кол-во монет?
91+
}
92+
93+
@Override
94+
public void matchOver(K botKey) {
95+
write("match_over " + botKey);
96+
}
97+
98+
private void write(String msg) {
99+
logWriter.println(msg);
100+
logWriter.flush();
101+
}
102+
@Override
103+
public void close() {
104+
logWriter.close();
105+
}
106+
}

server/src/main/java/ru/croccode/hypernull/server/MatchRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void run() {
9393
}
9494
match.completeRound(botMoves);
9595
}
96-
96+
match.finished();
9797
new ArrayList<>(botSessions.keySet()).forEach(this::closeSession);
9898
}
9999

0 commit comments

Comments
 (0)