mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Compare commits
4 Commits
28791fcc8a
...
cc7acf9f0c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc7acf9f0c | ||
|
|
955cb6109c | ||
|
|
013dd90705 | ||
|
|
c77499c36d |
@@ -0,0 +1,7 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public record GameResultFuture(OnlineGame<TurnBasedGame> game, CompletableFuture<Integer> result) {}
|
||||
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface GameServer<GAMETYPE, CLIENT, CHALLENGEIDTYPE> {
|
||||
OnlineGame<TurnBasedGame> startGame(String gameType, CompletableFuture<Void> futureOrNull, CLIENT... clients);
|
||||
GameResultFuture startGame(String gameType, CLIENT... clients);
|
||||
|
||||
void addClient(CLIENT client);
|
||||
void removeClient(CLIENT client);
|
||||
|
||||
@@ -17,24 +17,20 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
|
||||
private TurnBasedGame game;
|
||||
private ServerThreadBehaviour gameThread;
|
||||
|
||||
private final CompletableFuture<Void> futureOrNull;
|
||||
private final CompletableFuture<Integer> resultFuture;
|
||||
|
||||
public OnlineTurnBasedGame(NettyClient[] admins, TurnBasedGame game, CompletableFuture<Void> futureOrNull, NettyClient... clients) {
|
||||
public OnlineTurnBasedGame(NettyClient[] admins, TurnBasedGame game, CompletableFuture<Integer> resultFuture, NettyClient... clients) {
|
||||
this.game = game;
|
||||
this.gameThread = new ServerThreadBehaviour(
|
||||
game,
|
||||
(pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()),
|
||||
(pair) -> notifyGameEnd(pair.getLeft(), pair.getRight())
|
||||
);
|
||||
this.futureOrNull = futureOrNull;
|
||||
this.resultFuture = resultFuture;
|
||||
this.clients = clients;
|
||||
this.admins = admins;
|
||||
}
|
||||
|
||||
public OnlineTurnBasedGame(NettyClient[] admins, TurnBasedGame game, NettyClient... clients) {
|
||||
this(admins, game, null, clients);
|
||||
}
|
||||
|
||||
private void notifyMoveMade(String speler, int move){
|
||||
for (NettyClient admin : admins) {
|
||||
admin.send(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"<reactie spel op zet>\"}", speler, move));
|
||||
@@ -56,7 +52,7 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
|
||||
} else {
|
||||
Arrays.stream(admins).forEach(a -> a.send("SVR GAME END"));
|
||||
clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}"));
|
||||
clients[1-winner].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}"));
|
||||
clients[(winner+1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}"));
|
||||
}
|
||||
|
||||
// Remove game from clients
|
||||
@@ -65,8 +61,9 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
|
||||
client.clearGame();
|
||||
}
|
||||
|
||||
if (futureOrNull != null) {
|
||||
futureOrNull.complete(null);
|
||||
if (resultFuture != null) {
|
||||
if (state.equals(GameState.DRAW)) resultFuture.complete(-1); // Return -1 if draw
|
||||
else resultFuture.complete(winner); // Return number for winner's index
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ 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.networking.server.tournaments.shufflers.RandomShuffle;
|
||||
import org.toop.framework.utils.ImmutablePair;
|
||||
|
||||
import java.util.*;
|
||||
@@ -110,7 +111,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
public void acceptChallenge(Long challengeId) {
|
||||
for (var challenge : gameChallenges) {
|
||||
if (challenge.id() == challengeId) {
|
||||
startGame(challenge.acceptChallenge(), null, challenge.getUsers());
|
||||
startGame(challenge.acceptChallenge(), challenge.getUsers());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -132,12 +133,23 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OnlineGame<TurnBasedGame> startGame(String gameType, CompletableFuture<Void> futureOrNull, NettyClient... clients) {
|
||||
public GameResultFuture startGame(String gameType, NettyClient... clients) {
|
||||
if (!gameTypesStore.all().containsKey(gameType)) return null;
|
||||
|
||||
try {
|
||||
|
||||
ServerPlayer[] players = new ServerPlayer[clients.length];
|
||||
var game = new OnlineTurnBasedGame(getAdmins().toArray(NettyClient[]::new), gameTypesStore.create(gameType), futureOrNull, clients);
|
||||
|
||||
var gameResult = new CompletableFuture<Integer>();
|
||||
|
||||
var game = new OnlineTurnBasedGame(
|
||||
getAdmins().toArray(NettyClient[]::new),
|
||||
gameTypesStore.create(gameType),
|
||||
gameResult,
|
||||
clients
|
||||
);
|
||||
|
||||
var grfReturn = new GameResultFuture(game, gameResult);
|
||||
|
||||
for (int i = 0; i < clients.length; i++) {
|
||||
players[i] = new ServerPlayer(clients[i]);
|
||||
@@ -156,7 +168,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
gameType,
|
||||
clients[0].name()));
|
||||
game.start();
|
||||
return game;
|
||||
return grfReturn;
|
||||
} catch (Exception e) {
|
||||
IO.println("ERROR: Failed to start OnlineTurnBasedGame");
|
||||
e.printStackTrace();
|
||||
@@ -273,7 +285,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void startTournament(String gameType, NettyClient requestor) {
|
||||
public void startTournament(String gameType, NettyClient requestor, boolean shuffle) {
|
||||
if (!admins.contains(requestor)) {
|
||||
requestor.send("ERR you do not have the privileges to start a tournament");
|
||||
return;
|
||||
@@ -282,10 +294,15 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
var tournamentUsers = new ArrayList<>(onlineUsers());
|
||||
tournamentUsers.removeIf(admins::contains);
|
||||
|
||||
var matchMaker = new RoundRobinMatchMaker(tournamentUsers);
|
||||
if (shuffle) {
|
||||
matchMaker.shuffle(new RandomShuffle()); // Remove if not wanting to shuffle
|
||||
}
|
||||
|
||||
Tournament tournament = new BasicTournament(new TournamentBuilder(
|
||||
this,
|
||||
new BasicTournamentRunner(),
|
||||
new RoundRobinMatchMaker(tournamentUsers),
|
||||
new AsyncTournamentRunner(),
|
||||
matchMaker,
|
||||
new BasicScoreSystem(tournamentUsers)
|
||||
));
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ public class MessageHandler implements Handler<ParsedMessage> {
|
||||
if(!hasArgs(p.args())) return;
|
||||
|
||||
if (p.args()[0].equalsIgnoreCase("start") && p.args().length > 1) {
|
||||
server.startTournament(p.args()[1], client);
|
||||
server.startTournament(p.args()[1], client, false); // TODO add shuffle to msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import org.toop.framework.networking.server.GameResultFuture;
|
||||
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 {
|
||||
GameResultFuture game = server.startGame(gameType, a, b);
|
||||
scoreSystem.matchEndAwait(game);
|
||||
} finally {
|
||||
a.clearGame();
|
||||
b.clearGame();
|
||||
busyPlayers.remove(a);
|
||||
busyPlayers.remove(b);
|
||||
}
|
||||
}, matchExecutor);
|
||||
|
||||
runningMatches.add(f);
|
||||
|
||||
f.whenComplete((_, _) -> runningMatches.remove(f));
|
||||
}
|
||||
|
||||
Thread.sleep(10); // Safety
|
||||
}
|
||||
|
||||
server.endTournament(scoreSystem.getScore(), gameType);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
matchExecutor.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
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.GameResultFuture;
|
||||
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;
|
||||
@@ -9,29 +8,15 @@ 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<>();
|
||||
|
||||
for (TournamentMatch match : matchMaker) {
|
||||
// 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 -> {
|
||||
}
|
||||
}
|
||||
GameResultFuture game = server.startGame(gameType, match.getClient0(), match.getClient1());
|
||||
scoreSystem.matchEndAwait(game);
|
||||
|
||||
match.getClient0().clearGame();
|
||||
match.getClient1().clearGame();
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
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 org.toop.framework.networking.server.tournaments.shufflers.Shuffler;
|
||||
|
||||
public interface MatchMaker extends Iterable<TournamentMatch> {}
|
||||
import java.util.List;
|
||||
|
||||
public interface MatchMaker extends Iterable<TournamentMatch> {
|
||||
List<NettyClient> getPlayers();
|
||||
void shuffle(Shuffler shuffler);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ 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 org.toop.framework.networking.server.tournaments.shufflers.Shuffler;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -15,6 +16,16 @@ public class RoundRobinMatchMaker implements MatchMaker {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shuffle(Shuffler shuffler) {
|
||||
shuffler.shuffle(players);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NettyClient> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<TournamentMatch> iterator() {
|
||||
return new Iterator<>() {
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package org.toop.framework.networking.server.tournaments.scoresystems;
|
||||
|
||||
import org.toop.framework.networking.server.GameResultFuture;
|
||||
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) {
|
||||
@@ -17,7 +18,19 @@ public class BasicScoreSystem implements ScoreSystem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addScore(NettyClient client) {
|
||||
public void matchEndAwait(GameResultFuture result) {
|
||||
|
||||
if (result.game().users().length < 2) return;
|
||||
|
||||
switch (result.result().join()) {
|
||||
case 0 -> givePoints(result.game().users()[0]);
|
||||
case 1 -> givePoints(result.game().users()[1]);
|
||||
case -1 -> {} // Draw
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
private void givePoints(NettyClient client) {
|
||||
int clientScore = scores.get(client);
|
||||
scores.put(client, clientScore + getWinPointAmount());
|
||||
}
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package org.toop.framework.networking.server.tournaments.scoresystems;
|
||||
|
||||
import org.toop.framework.networking.server.GameResultFuture;
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
import org.toop.framework.networking.server.tournaments.TournamentMatch;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface ScoreSystem {
|
||||
void addScore(NettyClient client);
|
||||
void matchEndAwait(GameResultFuture result);
|
||||
Map<NettyClient, Integer> getScore();
|
||||
|
||||
default int getWinPointAmount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
default int getInitScore() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user