Skip to content

Commit

Permalink
Log matches with sequential match ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nypi committed Dec 1, 2021
1 parent c898564 commit d6bfcb9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AsciiMatchPrinter implements MatchListener<Integer> {

private static final char FREE = ' ';
private static final char BLOCK = 'X';
private static final char COIN = '*';
private static final char COIN = '¤';
private static final char VIEW_MASK = '.';
private static final char MINING_MASK = '+';
private static final char ATTACK_MASK = '-';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ private void runMatch(MatchMode mode, List<MatchRequest> matchRequests) {
botKey++;
}

String matchId = MatchId.nextId();
MatchMap map = mapRegistry.randomMap(numBots);
MatchConfig config = buildMatchConfig(mode, map);
List<MatchListener<Integer>> listeners = Arrays.asList(
new AsciiMatchPrinter(),
new MatchFileLogger<>(this.matchLogsFolder)
new MatchFileLogger<>(matchId, this.matchLogsFolder)
);
Match<Integer> match = new Match<>(map, config, botNames, listeners);
new MatchRunner(match, botSessions).run();
Expand All @@ -113,7 +114,7 @@ public void close() throws IOException {
}

public static void main(String[] args) throws IOException {
System.out.println("Запуск сервера...");
System.out.print("¤ ¤ ¤ HyperNull...");
String configPath = args.length > 0
? args[0]
: "hypernull.properties";
Expand All @@ -130,6 +131,7 @@ public static void main(String[] args) throws IOException {
app.close();
ThreadPools.shutdownAll();
})));
System.out.println(" READY ¤ ¤ ¤ ");
app.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.UUID;

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

private final static String LOG_FILE_TEMPLATE = "%s/Match_log_%s.txt";
private final static String LOG_FILE_TEMPLATE = "%s/match_%s.log";

private final UUID matchId;
private final PrintWriter logWriter;

public MatchFileLogger(String logsFolder) {
this.matchId = UUID.randomUUID();
public MatchFileLogger(String matchId, String logsFolder) {
Path path = Paths.get(logsFolder);
try {
Files.createDirectories(path);
Expand Down
27 changes: 27 additions & 0 deletions server/src/main/java/ru/croccode/hypernull/server/MatchId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.croccode.hypernull.server;

import java.util.concurrent.atomic.AtomicInteger;

public final class MatchId {

private final static AtomicInteger COUNTER = new AtomicInteger(0);

private MatchId() {
}

public static String nextId() {
// format: current time (epoch millis)
// + 2 chars of sync match counter reminder
// separated by underscore
int n = 'z' - 'a' + 1;
int mod = n * n;
int k = COUNTER.getAndIncrement() % mod;
if (k < 0)
k += mod;
long now = System.currentTimeMillis();
return now
+ "_"
+ (char)('a' + (k / n))
+ (char)('a' + (k % n));
}
}

0 comments on commit d6bfcb9

Please sign in to comment.