Skip to content

Commit d6bfcb9

Browse files
committed
Log matches with sequential match ids
1 parent c898564 commit d6bfcb9

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AsciiMatchPrinter implements MatchListener<Integer> {
1818

1919
private static final char FREE = ' ';
2020
private static final char BLOCK = 'X';
21-
private static final char COIN = '*';
21+
private static final char COIN = '¤';
2222
private static final char VIEW_MASK = '.';
2323
private static final char MINING_MASK = '+';
2424
private static final char ATTACK_MASK = '-';

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ private void runMatch(MatchMode mode, List<MatchRequest> matchRequests) {
8787
botKey++;
8888
}
8989

90+
String matchId = MatchId.nextId();
9091
MatchMap map = mapRegistry.randomMap(numBots);
9192
MatchConfig config = buildMatchConfig(mode, map);
9293
List<MatchListener<Integer>> listeners = Arrays.asList(
9394
new AsciiMatchPrinter(),
94-
new MatchFileLogger<>(this.matchLogsFolder)
95+
new MatchFileLogger<>(matchId, this.matchLogsFolder)
9596
);
9697
Match<Integer> match = new Match<>(map, config, botNames, listeners);
9798
new MatchRunner(match, botSessions).run();
@@ -113,7 +114,7 @@ public void close() throws IOException {
113114
}
114115

115116
public static void main(String[] args) throws IOException {
116-
System.out.println("Запуск сервера...");
117+
System.out.print("¤ ¤ ¤ HyperNull...");
117118
String configPath = args.length > 0
118119
? args[0]
119120
: "hypernull.properties";
@@ -130,6 +131,7 @@ public static void main(String[] args) throws IOException {
130131
app.close();
131132
ThreadPools.shutdownAll();
132133
})));
134+
System.out.println(" READY ¤ ¤ ¤ ");
133135
app.run();
134136
}
135137
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
1515
import java.util.Map;
16-
import java.util.UUID;
1716

1817
public class MatchFileLogger<K> implements MatchListener<K> {
1918

20-
private final static String LOG_FILE_TEMPLATE = "%s/Match_log_%s.txt";
19+
private final static String LOG_FILE_TEMPLATE = "%s/match_%s.log";
2120

22-
private final UUID matchId;
2321
private final PrintWriter logWriter;
2422

25-
public MatchFileLogger(String logsFolder) {
26-
this.matchId = UUID.randomUUID();
23+
public MatchFileLogger(String matchId, String logsFolder) {
2724
Path path = Paths.get(logsFolder);
2825
try {
2926
Files.createDirectories(path);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.croccode.hypernull.server;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
public final class MatchId {
6+
7+
private final static AtomicInteger COUNTER = new AtomicInteger(0);
8+
9+
private MatchId() {
10+
}
11+
12+
public static String nextId() {
13+
// format: current time (epoch millis)
14+
// + 2 chars of sync match counter reminder
15+
// separated by underscore
16+
int n = 'z' - 'a' + 1;
17+
int mod = n * n;
18+
int k = COUNTER.getAndIncrement() % mod;
19+
if (k < 0)
20+
k += mod;
21+
long now = System.currentTimeMillis();
22+
return now
23+
+ "_"
24+
+ (char)('a' + (k / n))
25+
+ (char)('a' + (k % n));
26+
}
27+
}

0 commit comments

Comments
 (0)