mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Async tournament runner
This commit is contained in:
@@ -295,7 +295,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
|
||||
Tournament tournament = new BasicTournament(new TournamentBuilder(
|
||||
this,
|
||||
new BasicTournamentRunner(),
|
||||
new AsyncTournamentRunner(),
|
||||
new RoundRobinMatchMaker(tournamentUsers),
|
||||
new BasicScoreSystem(tournamentUsers)
|
||||
));
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import org.toop.framework.networking.server.Server;
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
import org.toop.framework.networking.server.tournaments.matchmakers.MatchMaker;
|
||||
import org.toop.framework.networking.server.tournaments.scoresystems.ScoreSystem;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class AsyncTournamentRunner implements TournamentRunner {
|
||||
|
||||
@Override
|
||||
public void run(
|
||||
Server server,
|
||||
MatchMaker matchMaker,
|
||||
ScoreSystem scoreSystem,
|
||||
String gameType
|
||||
) {
|
||||
|
||||
ExecutorService matchExecutor =
|
||||
Executors.newFixedThreadPool(
|
||||
Runtime.getRuntime().availableProcessors()
|
||||
);
|
||||
|
||||
Queue<TournamentMatch> pendingMatches = new ConcurrentLinkedQueue<>();
|
||||
matchMaker.forEach(pendingMatches::add);
|
||||
|
||||
Set<NettyClient> busyPlayers = ConcurrentHashMap.newKeySet();
|
||||
List<CompletableFuture<Void>> runningMatches = new CopyOnWriteArrayList<>();
|
||||
|
||||
try {
|
||||
while (!pendingMatches.isEmpty() || !runningMatches.isEmpty()) {
|
||||
|
||||
Iterator<TournamentMatch> it = pendingMatches.iterator();
|
||||
while (it.hasNext()) {
|
||||
TournamentMatch match = it.next();
|
||||
|
||||
NettyClient a = match.getClient0();
|
||||
NettyClient b = match.getClient1();
|
||||
|
||||
// TODO game != null doesn't work here, fix later
|
||||
if (busyPlayers.contains(a) || busyPlayers.contains(b)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
busyPlayers.add(a);
|
||||
busyPlayers.add(b);
|
||||
it.remove();
|
||||
|
||||
CompletableFuture<Void> f =
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
var game = server.startGame(gameType, a, b);
|
||||
int result = game.result().join();
|
||||
|
||||
switch (result) {
|
||||
case 0 -> scoreSystem.addScore(a);
|
||||
case 1 -> scoreSystem.addScore(b);
|
||||
}
|
||||
} finally {
|
||||
a.clearGame();
|
||||
b.clearGame();
|
||||
busyPlayers.remove(a);
|
||||
busyPlayers.remove(b);
|
||||
}
|
||||
}, matchExecutor);
|
||||
|
||||
runningMatches.add(f);
|
||||
|
||||
f.whenComplete((_, _) -> runningMatches.remove(f));
|
||||
}
|
||||
|
||||
Thread.sleep(10);
|
||||
}
|
||||
|
||||
server.endTournament(scoreSystem.getScore(), gameType);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
matchExecutor.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.networking.server.OnlineGame;
|
||||
import org.toop.framework.networking.server.Server;
|
||||
import org.toop.framework.networking.server.tournaments.matchmakers.MatchMaker;
|
||||
import org.toop.framework.networking.server.tournaments.scoresystems.ScoreSystem;
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package org.toop.framework.networking.server.tournaments.matchmakers;
|
||||
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
import org.toop.framework.networking.server.tournaments.TournamentMatch;
|
||||
|
||||
public interface MatchMaker extends Iterable<TournamentMatch> {}
|
||||
import java.util.List;
|
||||
|
||||
public interface MatchMaker extends Iterable<TournamentMatch> {
|
||||
List<NettyClient> getPlayers();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@ public class RoundRobinMatchMaker implements MatchMaker {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NettyClient> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<TournamentMatch> iterator() {
|
||||
return new Iterator<>() {
|
||||
|
||||
@@ -2,13 +2,13 @@ package org.toop.framework.networking.server.tournaments.scoresystems;
|
||||
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class BasicScoreSystem implements ScoreSystem {
|
||||
|
||||
private final Map<NettyClient, Integer> scores = new HashMap<>();
|
||||
private final Map<NettyClient, Integer> scores = new ConcurrentHashMap<>();
|
||||
|
||||
public BasicScoreSystem(List<NettyClient> store) {
|
||||
for (NettyClient c : store) {
|
||||
|
||||
Reference in New Issue
Block a user