Skip to content

Commit 4143867

Browse files
committed
Drop previous match requests from the bot on a new connection; Run matches in parallel
1 parent 8a0fcdb commit 4143867

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,19 @@ public HyperNull(Properties properties) throws IOException {
6363
public void run() {
6464
List<MatchMode> modes = Arrays.asList(MatchMode.FRIENDLY, MatchMode.DEATHMATCH);
6565
while (true) {
66-
List<MatchRequest> matchRequests = Collections.emptyList();
6766
Collections.shuffle(modes);
67+
boolean matchStarted = false;
6868
for (MatchMode mode : modes) {
69-
matchRequests = server.pollRequests(
69+
List<MatchRequest> matchRequests = server.pollRequests(
7070
mode,
7171
mode == MatchMode.FRIENDLY ? MIN_FRIENDLY_BOTS : MIN_DEATHMATCH_BOTS,
7272
mode == MatchMode.FRIENDLY ? MAX_FRIENDLY_BOTS : MAX_DEATHMATCH_BOTS);
7373
if (!matchRequests.isEmpty()) {
7474
runMatch(mode, matchRequests);
75+
matchStarted = true;
7576
}
7677
}
77-
if (matchRequests.isEmpty()) {
78+
if (!matchStarted) {
7879
try {
7980
Thread.sleep(1_000L);
8081
} catch (InterruptedException e) {
@@ -118,7 +119,7 @@ private void runMatch(MatchMode mode, List<MatchRequest> matchRequests) {
118119
.map(r -> r.getBotName() + " [" + r.getMode() + "]")
119120
.collect(Collectors.joining(", "));
120121
System.out.println("Starting match " + matchId + ": " + bots);
121-
new MatchRunner(match, botSessions).run();
122+
ThreadPools.matchPool().submit(new MatchRunner(match, botSessions));
122123
}
123124
}
124125

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.util.Collections;
1010
import java.util.Deque;
1111
import java.util.HashMap;
12+
import java.util.Iterator;
1213
import java.util.List;
1314
import java.util.Map;
15+
import java.util.Objects;
1416
import java.util.concurrent.locks.Lock;
1517
import java.util.concurrent.locks.ReentrantLock;
1618

@@ -95,6 +97,21 @@ private void sessionOpened(SocketSession session) {
9597
private void offerRequest(MatchRequest request) {
9698
lock.lock();
9799
try {
100+
// discard previous match requests of the bot (by name)
101+
queues.values().forEach(q -> {
102+
Iterator<MatchRequest> it = q.iterator();
103+
while (it.hasNext()) {
104+
MatchRequest prev = it.next();
105+
if (!Objects.equals(prev.getBotName(), request.getBotName()))
106+
continue;
107+
// close request session
108+
try {
109+
prev.getSession().close();
110+
} catch (IOException ignore) {
111+
}
112+
it.remove();
113+
}
114+
});
98115
queues
99116
.computeIfAbsent(request.getMode(), k -> new ArrayDeque<>())
100117
.offer(request);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@
55

66
public final class ThreadPools {
77

8+
private static final int MAX_PARALLEL_MATCHES = 20;
9+
810
private static final ExecutorService DEFAULT_POOL = Executors.newCachedThreadPool();
911

12+
private static final ExecutorService MATCH_POOL = Executors.newFixedThreadPool(MAX_PARALLEL_MATCHES);
13+
1014
private ThreadPools() {
1115
}
1216

1317
public static ExecutorService defaultPool() {
1418
return DEFAULT_POOL;
1519
}
1620

21+
public static ExecutorService matchPool() {
22+
return MATCH_POOL;
23+
}
24+
1725
public static void shutdownAll() {
26+
MATCH_POOL.shutdownNow();
1827
DEFAULT_POOL.shutdownNow();
1928
}
2029
}

0 commit comments

Comments
 (0)