Skip to content

Commit 577b2b3

Browse files
committed
Report protocol version and match id
1 parent 99e54c3 commit 577b2b3

File tree

12 files changed

+82
-33
lines changed

12 files changed

+82
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protocol_version {PROTOCOL_VERSION}
9696
end
9797
```
9898

99-
- `PROTOCOL_VERSION` текущая версия протокола
99+
- `PROTOCOL_VERSION` текущая версия протокола (целое число)
100100

101101
### register
102102

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
package ru.croccode.hypernull.message;
22

33
public class Hello extends Message {
4+
5+
private Integer protocolVersion = Messages.PROTOCOL_VERSION;
6+
7+
public Integer getProtocolVersion() {
8+
return protocolVersion;
9+
}
10+
11+
public void setProtocolVersion(Integer protocolVersion) {
12+
this.protocolVersion = protocolVersion;
13+
}
414
}

common/src/main/java/ru/croccode/hypernull/message/MatchStarted.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
public class MatchStarted extends Message {
77

8+
private String matchId;
9+
810
private Integer numRounds;
911

1012
private MatchMode mode;
@@ -23,6 +25,14 @@ public class MatchStarted extends Message {
2325

2426
private Long moveTimeLimit;
2527

28+
public String getMatchId() {
29+
return matchId;
30+
}
31+
32+
public void setMatchId(String matchId) {
33+
this.matchId = matchId;
34+
}
35+
2636
public Integer getNumRounds() {
2737
return numRounds;
2838
}

common/src/main/java/ru/croccode/hypernull/message/Messages.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
public final class Messages {
2323

24+
// current protocol version
25+
public static final int PROTOCOL_VERSION = 1;
26+
2427
private Messages() {
2528
}
2629

@@ -116,13 +119,23 @@ private static void parseParameters(List<String> lines, BiConsumer<String, List<
116119
public static List<String> formatHello(Hello message) {
117120
List<String> lines = new ArrayList<>();
118121
lines.add("hello");
122+
if (message.getProtocolVersion() != null)
123+
lines.add(formatParameter("protocol_version", message.getProtocolVersion()));
119124
lines.add("end");
120125
return lines;
121126
}
122127

123128
public static Hello parseHello(List<String> lines) {
124129
checkMessage(lines, "hello");
125-
return new Hello();
130+
Hello message = new Hello();
131+
parseParameters(lines, (name, values) -> {
132+
switch (name) {
133+
case "protocol_version":
134+
message.setProtocolVersion(Integer.parseInt(values.get(0)));
135+
break;
136+
}
137+
});
138+
return message;
126139
}
127140

128141
// register
@@ -164,6 +177,8 @@ public static Register parseRegister(List<String> lines) {
164177
public static List<String> formatMatchStarted(MatchStarted message) {
165178
List<String> lines = new ArrayList<>();
166179
lines.add("match_started");
180+
if (!Strings.isNullOrEmpty(message.getMatchId()))
181+
lines.add(formatParameter("match_id", message.getMatchId()));
167182
if (message.getNumRounds() != null)
168183
lines.add(formatParameter("num_rounds", message.getNumRounds()));
169184
if (message.getMode() != null)
@@ -192,6 +207,9 @@ public static MatchStarted parseMatchStarted(List<String> lines) {
192207
MatchStarted message = new MatchStarted();
193208
parseParameters(lines, (name, values) -> {
194209
switch (name) {
210+
case "match_id":
211+
message.setMatchId(values.get(0));
212+
break;
195213
case "num_rounds":
196214
message.setNumRounds(Integer.parseInt(values.get(0)));
197215
break;

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ru.croccode.hypernull.geometry.Offset;
1919
import ru.croccode.hypernull.geometry.Point;
2020
import ru.croccode.hypernull.util.Check;
21+
import ru.croccode.hypernull.util.Strings;
2122

2223
// K - bot key type
2324
public class Match<K> {
@@ -27,6 +28,8 @@ public class Match<K> {
2728

2829
private final Random rnd;
2930

31+
private final String id;
32+
3033
private final MatchConfig config;
3134

3235
private final MatchMap map;
@@ -44,25 +47,26 @@ public class Match<K> {
4447
// current round, starting from 1
4548
private int round;
4649

47-
public Match(MatchMap map, MatchConfig config, Map<K, String> botNames) {
48-
this(map, config, botNames, Collections.emptyList());
50+
public Match(String id, MatchMap map, MatchConfig config, Map<K, String> botNames) {
51+
this(id, map, config, botNames, Collections.emptyList());
4952
}
5053

51-
public Match(MatchMap map, MatchConfig config, Map<K, String> botNames,
54+
public Match(String id, MatchMap map, MatchConfig config, Map<K, String> botNames,
5255
List<MatchListener<K>> listeners) {
5356
Check.notNull(map);
5457
Check.notNull(config);
5558
Check.condition(!botNames.isEmpty());
5659

5760
this.rnd = new Random(config.getRandomSeed());
61+
this.id = Strings.emptyToNull(id);
5862
this.config = config;
5963
this.map = map;
6064
this.mapIndex = new MapIndex(map);
6165
if (listeners != null)
6266
this.listeners.addAll(listeners);
6367

6468
// notify: match started
65-
this.listeners.forEach(l -> l.matchStarted(map, config, botNames));
69+
this.listeners.forEach(l -> l.matchStarted(id, map, config, botNames));
6670
// init bots
6771
initBots(botNames);
6872
// spawn initial coins
@@ -73,8 +77,8 @@ public Match(MatchMap map, MatchConfig config, Map<K, String> botNames,
7377
this.listeners.forEach(l -> l.matchRound(round));
7478
}
7579

76-
public int getRound() {
77-
return round;
80+
public String getId() {
81+
return id;
7882
}
7983

8084
public MatchConfig getConfig() {
@@ -93,6 +97,10 @@ public Set<Point> getCoins() {
9397
return Collections.unmodifiableSet(coins);
9498
}
9599

100+
public int getRound() {
101+
return round;
102+
}
103+
96104
public boolean isActive() {
97105
// check: round <= num_rounds
98106
if (round > config.getNumRounds())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public interface MatchListener<K> {
99

10-
void matchStarted(MatchMap map, MatchConfig config, Map<K, String> botNames);
10+
void matchStarted(String id, MatchMap map, MatchConfig config, Map<K, String> botNames);
1111

1212
void matchRound(int round);
1313

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class AsciiMatchPrinter implements MatchListener<Integer> {
2323
private static final char MINING_MASK = '+';
2424
private static final char ATTACK_MASK = '-';
2525

26+
private String matchId;
27+
2628
private MatchMap map;
2729

2830
private MatchConfig config;
@@ -45,7 +47,8 @@ static class BotState {
4547
}
4648

4749
@Override
48-
public void matchStarted(MatchMap map, MatchConfig config, Map<Integer, String> botNames) {
50+
public void matchStarted(String id, MatchMap map, MatchConfig config, Map<Integer, String> botNames) {
51+
this.matchId = id;
4952
this.map = map;
5053
this.config = config;
5154
this.bots = new HashMap<>(botNames.size());
@@ -111,6 +114,8 @@ public void matchOver(Integer botKey) {
111114

112115
private void printState() {
113116
System.out.print("\033[H\033[2J");
117+
if (!Strings.isNullOrEmpty(matchId))
118+
System.out.println("| MATCH " + matchId );
114119
System.out.println("| ROUND " + round );
115120
List<Integer> botKeys = new ArrayList<>(bots.keySet());
116121
botKeys.sort(Comparator.naturalOrder());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void runMatch(MatchMode mode, List<MatchRequest> matchRequests) {
9595
new AsciiMatchPrinter(),
9696
fileLogger
9797
);
98-
Match<Integer> match = new Match<>(map, config, botNames, listeners);
98+
Match<Integer> match = new Match<>(matchId, map, config, botNames, listeners);
9999
new MatchRunner(match, botSessions).run();
100100
}
101101
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public MatchFileLogger(String matchId, String logsFolder) {
3636
}
3737

3838
@Override
39-
public void matchStarted(MatchMap map, MatchConfig config, Map<K, String> botNames) {
39+
public void matchStarted(String id, MatchMap map, MatchConfig config, Map<K, String> botNames) {
4040
write("match");
41+
write("match_id " + id);
4142
write("num_bots " + botNames.size());
4243
write("##MatchConfig");
4344
printAllFields(config);
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.croccode.hypernull.bot;
22

3+
import ru.croccode.hypernull.message.Hello;
34
import ru.croccode.hypernull.message.MatchOver;
45
import ru.croccode.hypernull.message.MatchStarted;
56
import ru.croccode.hypernull.message.Move;
@@ -8,11 +9,11 @@
89

910
public interface Bot {
1011

11-
Register registerAs();
12+
Register onHello(Hello hello);
1213

13-
void matchStarted(MatchStarted matchStarted);
14+
void onMatchStarted(MatchStarted matchStarted);
1415

15-
void matchOver(MatchOver matchOver);
16+
Move onUpdate(Update update);
1617

17-
Move makeMove(Update update);
18+
void onMatchOver(MatchOver matchOver);
1819
}

0 commit comments

Comments
 (0)