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;
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);

View File

@@ -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));
@@ -44,7 +40,7 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
}
}
private void notifyGameEnd(GameState state, int winner){
private void notifyGameEnd(GameState state, int winner) {
if (state == GameState.DRAW) {
Arrays.stream(admins).forEach(a -> a.send(
String.format("SVR GAME END")
@@ -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
}
}
@@ -86,7 +83,7 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
}
@Override
public void start(){
public void start() {
this.gameThread.start();
}
}

View File

@@ -110,7 +110,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 +132,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 +167,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();

View File

@@ -18,19 +18,17 @@ public class BasicTournamentRunner implements TournamentRunner {
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();
var game = server.startGame(gameType, match.getClient0(), match.getClient1());
int result = game.result().join();
// End
// Get result and calculate new score
switch (game.game().getWinner()) {
switch (result) {
case 0 -> scoreSystem.addScore(match.getClient0());
case 1 -> scoreSystem.addScore(match.getClient1());
default -> {
}
case -1 -> {} // Draw
default -> {}
}
match.getClient0().clearGame();