mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Tournament refactor for better naming and easier to understand code
This commit is contained in:
2
.idea/inspectionProfiles/Project_Default.xml
generated
2
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -2,7 +2,7 @@
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,org.toop.framework.audio.AudioPlayer,play,java.util.Map,remove,java.util.concurrent.Executors,newSingleThreadScheduledExecutor" />
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,org.toop.framework.audio.AudioPlayer,play,java.util.Map,remove,java.util.concurrent.Executors,newSingleThreadScheduledExecutor|newFixedThreadPool|newSingleThreadExecutor" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="WriteOnlyObject" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
|
||||
@@ -11,6 +11,8 @@ import org.toop.framework.networking.server.stores.SubscriptionStore;
|
||||
import org.toop.framework.networking.server.stores.TurnBasedGameStore;
|
||||
import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore;
|
||||
import org.toop.framework.networking.server.tournaments.*;
|
||||
import org.toop.framework.networking.server.tournaments.matchmakers.RoundRobinMatchMaker;
|
||||
import org.toop.framework.networking.server.tournaments.scoresystems.BasicScoreSystem;
|
||||
import org.toop.framework.utils.ImmutablePair;
|
||||
|
||||
import java.util.*;
|
||||
@@ -263,18 +265,15 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void startTournament(Tournament tournament, String gameType) {
|
||||
try {
|
||||
var tb = new TournamentBuilder();
|
||||
var cTournament = tb.create(
|
||||
tournament,
|
||||
onlineUsers(),
|
||||
public void startTournament(String gameType) {
|
||||
Tournament tournament = new BasicTournament(new TournamentBuilder(
|
||||
this,
|
||||
new BasicScoreManager(new HashMap<>()),
|
||||
new BasicMatchManager(),
|
||||
new RandomShuffle()
|
||||
);
|
||||
new Thread(() -> cTournament.run(gameType)).start();
|
||||
new BasicTournamentRunner(),
|
||||
new RoundRobinMatchMaker(onlineUsers()),
|
||||
new BasicScoreSystem(onlineUsers())
|
||||
));
|
||||
try {
|
||||
new Thread(() -> tournament.run(gameType)).start();
|
||||
} catch (IllegalArgumentException e) {
|
||||
getUser("host").send("ERR not enough clients to start a tournament");
|
||||
} catch (RuntimeException e) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
||||
import org.toop.framework.networking.server.Server;
|
||||
import org.toop.framework.networking.server.client.Client;
|
||||
import org.toop.framework.networking.server.parsing.ParsedMessage;
|
||||
import org.toop.framework.networking.server.tournaments.BasicTournament;
|
||||
import org.toop.framework.networking.server.tournaments.*;
|
||||
import org.toop.framework.utils.Utils;
|
||||
|
||||
public class MessageHandler implements Handler<ParsedMessage> {
|
||||
@@ -116,7 +116,7 @@ public class MessageHandler implements Handler<ParsedMessage> {
|
||||
if (!client.name().equalsIgnoreCase("host")) return;
|
||||
|
||||
if (p.args()[0].equalsIgnoreCase("start") && p.args().length > 1) {
|
||||
server.startTournament(new BasicTournament(), p.args()[1]);
|
||||
server.startTournament(p.args()[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
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.client.NettyClient;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class BasicMatchManager implements MatchManager {
|
||||
|
||||
private final HashMap<NettyClient, List<NettyClient>> matches = new HashMap<>(); // TODO store
|
||||
private final ArrayList<TournamentMatch> matchOrder = new ArrayList<>(); // TODO store
|
||||
private int matchIndex = 0;
|
||||
|
||||
public BasicMatchManager() {}
|
||||
|
||||
@Override
|
||||
public void addClient(NettyClient client) {
|
||||
matches.putIfAbsent(client, new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NettyClient> getClients() {
|
||||
return matches.keySet().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createMatches() {
|
||||
for (var match : matches.entrySet()) {
|
||||
for (var matchNoSelf : matches.keySet()) {
|
||||
if (match.getKey() == matchNoSelf) continue;
|
||||
|
||||
match.getValue().addLast(matchNoSelf);
|
||||
}
|
||||
}
|
||||
|
||||
for (var client : matches.entrySet()) {
|
||||
for (var opponent : client.getValue()) {
|
||||
matchOrder.addLast(new TournamentMatch(client.getKey(), opponent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TournamentMatch next() {
|
||||
matchIndex++;
|
||||
if (matchIndex >= matchOrder.size()) return null;
|
||||
|
||||
return matchOrder.get(matchIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Server server, ScoreManager scoreManager, String gameType) {
|
||||
new Thread(() -> {
|
||||
for (var match : matchOrder) {
|
||||
final CompletableFuture<Void> finished = new CompletableFuture<>();
|
||||
|
||||
AtomicReference<OnlineGame<TurnBasedGame>> game = new AtomicReference<>();
|
||||
new Thread(() -> game.set(server.startGame(gameType, finished, match.getLeft(), match.getRight()))).start(); // TODO can possibly create a race condition
|
||||
|
||||
finished.join();
|
||||
switch (game.get().game().getWinner()) {
|
||||
case 0 -> scoreManager.addScore(match.getLeft());
|
||||
case 1 -> scoreManager.addScore(match.getRight());
|
||||
default -> {}
|
||||
}
|
||||
|
||||
match.getLeft().clearGame();
|
||||
match.getRight().clearGame();
|
||||
}
|
||||
|
||||
server.endTournament(scoreManager.getScore(), gameType);
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shuffle(Shuffler shuffler) {
|
||||
shuffler.shuffle(matchOrder);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class BasicScoreManager implements ScoreManager {
|
||||
|
||||
private final Map<NettyClient, Integer> scores;
|
||||
|
||||
public BasicScoreManager(Map<NettyClient, Integer> store) {
|
||||
scores = store;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addClient(NettyClient client) {
|
||||
scores.putIfAbsent(client, getInitScore());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addScore(NettyClient client) {
|
||||
int clientScore = scores.get(client);
|
||||
scores.put(client, clientScore + getWinPointAmount());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<NettyClient, Integer> getScore() {
|
||||
return scores;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,29 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
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;
|
||||
|
||||
public class BasicTournament implements Tournament {
|
||||
private Server server;
|
||||
private final Server server;
|
||||
|
||||
private ScoreManager scoreManager;
|
||||
private MatchManager matchManager;
|
||||
private final ScoreSystem scoreSystem;
|
||||
private final TournamentRunner tournamentRunner;
|
||||
private final MatchMaker matchMaker;
|
||||
|
||||
public BasicTournament() {}
|
||||
|
||||
public void init(TournamentBuilder builder) {
|
||||
public BasicTournament(TournamentBuilder builder) {
|
||||
server = builder.server;
|
||||
scoreManager = builder.scoreManager;
|
||||
matchManager = builder.matchManager;
|
||||
scoreSystem = builder.scoreSystem;
|
||||
tournamentRunner = builder.tournamentRunner;
|
||||
matchMaker = builder.matchMaker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean run(String gameType) throws RuntimeException {
|
||||
if (server.gameTypes().stream().noneMatch(e -> e.equalsIgnoreCase(gameType))) return false;
|
||||
|
||||
matchManager.run(server, scoreManager, gameType);
|
||||
tournamentRunner.run(server, matchMaker, scoreSystem, gameType);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public HashMap<NettyClient, Integer> end() {
|
||||
//
|
||||
// for (var match : matchList) {
|
||||
// match.getLeft().clearGame(); // TODO send msg to client
|
||||
// match.getRight().clearGame(); // TODO send msg to client
|
||||
// }
|
||||
//
|
||||
// gameType = null;
|
||||
// matchList = null;
|
||||
// matches.clear();
|
||||
//
|
||||
// HashMap<NettyClient, Integer> retScore = new HashMap<>(score);
|
||||
//
|
||||
// score.clear();
|
||||
//
|
||||
// clients = null;
|
||||
//
|
||||
// return retScore;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class BasicTournamentRunner implements TournamentRunner {
|
||||
|
||||
public BasicTournamentRunner() {}
|
||||
|
||||
@Override
|
||||
public void run(Server server, MatchMaker matchMaker, ScoreSystem scoreSystem, String gameType) {
|
||||
ExecutorService threadPool = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
threadPool.execute(() -> {
|
||||
for (var match : matchMaker) {
|
||||
final CompletableFuture<Void> finished = new CompletableFuture<>();
|
||||
|
||||
// Play game and await the results
|
||||
OnlineGame<TurnBasedGame> game = server.startGame(gameType, finished, match.getClient0(), match.getClient1()); // TODO can possibly create a race condition
|
||||
finished.join();
|
||||
// End
|
||||
|
||||
// Get result and calculate new score
|
||||
switch (game.game().getWinner()) {
|
||||
case 0 -> scoreSystem.addScore(match.getClient0());
|
||||
case 1 -> scoreSystem.addScore(match.getClient1());
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
|
||||
match.getClient0().clearGame();
|
||||
match.getClient1().clearGame();
|
||||
}
|
||||
|
||||
server.endTournament(scoreSystem.getScore(), gameType);
|
||||
});
|
||||
} finally {
|
||||
threadPool.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import org.toop.framework.networking.server.Server;
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MatchManager {
|
||||
void addClient(NettyClient client);
|
||||
List<NettyClient> getClients();
|
||||
void createMatches();
|
||||
TournamentMatch next();
|
||||
void run(Server server, ScoreManager scoreManager, String gameType);
|
||||
void shuffle(Shuffler shuffler);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import org.toop.framework.networking.server.client.NettyClient;
|
||||
import java.util.HashMap;
|
||||
|
||||
public interface Tournament {
|
||||
void init(TournamentBuilder builder);
|
||||
// void init(TournamentBuilder builder);
|
||||
boolean run(String gameType);
|
||||
// HashMap<NettyClient, Integer> end();
|
||||
}
|
||||
|
||||
@@ -1,39 +1,24 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import org.toop.framework.networking.server.Server;
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
|
||||
import java.util.List;
|
||||
import org.toop.framework.networking.server.tournaments.matchmakers.MatchMaker;
|
||||
import org.toop.framework.networking.server.tournaments.scoresystems.ScoreSystem;
|
||||
|
||||
public class TournamentBuilder {
|
||||
public Server server;
|
||||
public ScoreManager scoreManager;
|
||||
public MatchManager matchManager;
|
||||
public ScoreSystem scoreSystem;
|
||||
public TournamentRunner tournamentRunner;
|
||||
public MatchMaker matchMaker;
|
||||
|
||||
public TournamentBuilder() {}
|
||||
|
||||
public Tournament create(
|
||||
Tournament tournament,
|
||||
List<NettyClient> clients,
|
||||
public TournamentBuilder(
|
||||
Server server,
|
||||
ScoreManager scoreManager,
|
||||
MatchManager matchManager,
|
||||
Shuffler shuffler
|
||||
TournamentRunner tournamentRunner,
|
||||
MatchMaker matchMaker,
|
||||
ScoreSystem scoreSystem
|
||||
) {
|
||||
|
||||
this.server = server;
|
||||
this.scoreManager = scoreManager;
|
||||
this.matchManager = matchManager;
|
||||
|
||||
for (var client : clients) {
|
||||
matchManager.addClient(client);
|
||||
scoreManager.addClient(client);
|
||||
}
|
||||
|
||||
matchManager.createMatches();
|
||||
matchManager.shuffle(shuffler);
|
||||
|
||||
tournament.init(this);
|
||||
return tournament;
|
||||
this.tournamentRunner = tournamentRunner;
|
||||
this.matchMaker = matchMaker;
|
||||
this.scoreSystem = scoreSystem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,12 @@ public class TournamentMatch extends ImmutablePair<NettyClient, NettyClient> {
|
||||
public TournamentMatch(NettyClient a, NettyClient b) {
|
||||
super(a, b);
|
||||
}
|
||||
|
||||
NettyClient getClient0() {
|
||||
return getLeft();
|
||||
}
|
||||
|
||||
NettyClient getClient1() {
|
||||
return getRight();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
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;
|
||||
|
||||
public interface TournamentRunner {
|
||||
void run(Server server, MatchMaker matchMaker, ScoreSystem scoreSystem, String gameType);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.toop.framework.networking.server.tournaments.matchmakers;
|
||||
|
||||
import org.toop.framework.networking.server.tournaments.TournamentMatch;
|
||||
|
||||
public interface MatchMaker extends Iterable<TournamentMatch> {}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.toop.framework.networking.server.tournaments.matchmakers;
|
||||
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
import org.toop.framework.networking.server.tournaments.TournamentMatch;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class RoundRobinMatchMaker implements MatchMaker {
|
||||
|
||||
private final List<NettyClient> players;
|
||||
|
||||
public RoundRobinMatchMaker(List<NettyClient> players) {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<TournamentMatch> iterator() {
|
||||
return new Iterator<>() {
|
||||
|
||||
private int i = 0;
|
||||
private int j = 1;
|
||||
private boolean reverse = false;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return players.size() > 1
|
||||
&& i < players.size() - 1
|
||||
&& j < players.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TournamentMatch next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
NettyClient home = players.get(i);
|
||||
NettyClient away = players.get(j);
|
||||
|
||||
TournamentMatch match = reverse ? new TournamentMatch(away, home) : new TournamentMatch(home, away);
|
||||
|
||||
advance();
|
||||
return match;
|
||||
}
|
||||
|
||||
private void advance() {
|
||||
j++;
|
||||
if (j >= players.size()) {
|
||||
i++;
|
||||
j = i + 1;
|
||||
|
||||
if (i >= players.size() - 1) {
|
||||
if (!reverse) {
|
||||
reverse = true;
|
||||
i = 0;
|
||||
j = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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;
|
||||
|
||||
public class BasicScoreSystem implements ScoreSystem {
|
||||
|
||||
private final Map<NettyClient, Integer> scores = new HashMap<>();
|
||||
|
||||
public BasicScoreSystem(List<NettyClient> store) {
|
||||
for (NettyClient c : store) {
|
||||
scores.putIfAbsent(c, getInitScore());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addScore(NettyClient client) {
|
||||
int clientScore = scores.get(client);
|
||||
scores.put(client, clientScore + getWinPointAmount());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<NettyClient, Integer> getScore() {
|
||||
return scores;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
package org.toop.framework.networking.server.tournaments.scoresystems;
|
||||
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface ScoreManager {
|
||||
void addClient(NettyClient client);
|
||||
public interface ScoreSystem {
|
||||
void addScore(NettyClient client);
|
||||
Map<NettyClient, Integer> getScore();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
package org.toop.framework.networking.server.tournaments.shufflers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
package org.toop.framework.networking.server.tournaments.shufflers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
Reference in New Issue
Block a user