Added result comeback with a draw

This commit is contained in:
Bas de Jong
2026-01-11 00:53:31 +01:00
parent 28791fcc8a
commit c77499c36d
5 changed files with 37 additions and 24 deletions

View File

@@ -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) {}

View File

@@ -6,7 +6,7 @@ import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public interface GameServer<GAMETYPE, CLIENT, CHALLENGEIDTYPE> { 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 addClient(CLIENT client);
void removeClient(CLIENT client); void removeClient(CLIENT client);

View File

@@ -17,24 +17,20 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
private TurnBasedGame game; private TurnBasedGame game;
private ServerThreadBehaviour gameThread; 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.game = game;
this.gameThread = new ServerThreadBehaviour( this.gameThread = new ServerThreadBehaviour(
game, game,
(pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()), (pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()),
(pair) -> notifyGameEnd(pair.getLeft(), pair.getRight()) (pair) -> notifyGameEnd(pair.getLeft(), pair.getRight())
); );
this.futureOrNull = futureOrNull; this.resultFuture = resultFuture;
this.clients = clients; this.clients = clients;
this.admins = admins; this.admins = admins;
} }
public OnlineTurnBasedGame(NettyClient[] admins, TurnBasedGame game, NettyClient... clients) {
this(admins, game, null, clients);
}
private void notifyMoveMade(String speler, int move){ private void notifyMoveMade(String speler, int move){
for (NettyClient admin : admins) { for (NettyClient admin : admins) {
admin.send(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"<reactie spel op zet>\"}", speler, move)); 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 { } else {
Arrays.stream(admins).forEach(a -> a.send("SVR GAME END")); 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[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 // Remove game from clients
@@ -65,8 +61,9 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
client.clearGame(); client.clearGame();
} }
if (futureOrNull != null) { if (resultFuture != null) {
futureOrNull.complete(null); if (state.equals(GameState.DRAW)) resultFuture.complete(-1); // Return -1 if draw
else resultFuture.complete(winner); // Return number for winner's index
} }
} }

View File

@@ -110,7 +110,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
public void acceptChallenge(Long challengeId) { public void acceptChallenge(Long challengeId) {
for (var challenge : gameChallenges) { for (var challenge : gameChallenges) {
if (challenge.id() == challengeId) { if (challenge.id() == challengeId) {
startGame(challenge.acceptChallenge(), null, challenge.getUsers()); startGame(challenge.acceptChallenge(), challenge.getUsers());
break; break;
} }
} }
@@ -132,12 +132,23 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
} }
@Override @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; if (!gameTypesStore.all().containsKey(gameType)) return null;
try { try {
ServerPlayer[] players = new ServerPlayer[clients.length]; 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++) { for (int i = 0; i < clients.length; i++) {
players[i] = new ServerPlayer(clients[i]); players[i] = new ServerPlayer(clients[i]);
@@ -156,7 +167,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
gameType, gameType,
clients[0].name())); clients[0].name()));
game.start(); game.start();
return game; return grfReturn;
} catch (Exception e) { } catch (Exception e) {
IO.println("ERROR: Failed to start OnlineTurnBasedGame"); IO.println("ERROR: Failed to start OnlineTurnBasedGame");
e.printStackTrace(); e.printStackTrace();

View File

@@ -18,19 +18,17 @@ public class BasicTournamentRunner implements TournamentRunner {
try { try {
threadPool.execute(() -> { threadPool.execute(() -> {
for (var match : matchMaker) { for (var match : matchMaker) {
final CompletableFuture<Void> finished = new CompletableFuture<>();
// Play game and await the results // Play game and await the results
OnlineGame<TurnBasedGame> game = server.startGame(gameType, finished, match.getClient0(), match.getClient1()); // TODO can possibly create a race condition var game = server.startGame(gameType, match.getClient0(), match.getClient1());
finished.join(); int result = game.result().join();
// End // End
// Get result and calculate new score // Get result and calculate new score
switch (game.game().getWinner()) { switch (result) {
case 0 -> scoreSystem.addScore(match.getClient0()); case 0 -> scoreSystem.addScore(match.getClient0());
case 1 -> scoreSystem.addScore(match.getClient1()); case 1 -> scoreSystem.addScore(match.getClient1());
default -> { case -1 -> {} // Draw
} default -> {}
} }
match.getClient0().clearGame(); match.getClient0().clearGame();