Tournament results are now send back to the clients connected to the server

This commit is contained in:
Bas de Jong
2026-01-10 02:14:23 +01:00
parent 6b644ed8fa
commit 75963a891b
3 changed files with 44 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
package org.toop.framework.networking.server; package org.toop.framework.networking.server;
import com.google.gson.Gson;
import org.toop.framework.game.players.ServerPlayer; import org.toop.framework.game.players.ServerPlayer;
import org.toop.framework.gameFramework.model.game.TurnBasedGame; import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.networking.server.challenges.gamechallenge.GameChallenge; import org.toop.framework.networking.server.challenges.gamechallenge.GameChallenge;
@@ -9,7 +10,6 @@ import org.toop.framework.networking.server.stores.ClientStore;
import org.toop.framework.networking.server.stores.SubscriptionStore; import org.toop.framework.networking.server.stores.SubscriptionStore;
import org.toop.framework.networking.server.stores.TurnBasedGameStore; import org.toop.framework.networking.server.stores.TurnBasedGameStore;
import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore; import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore;
import org.toop.framework.networking.server.tournaments.BasicTournament;
import org.toop.framework.networking.server.tournaments.RandomShuffle; import org.toop.framework.networking.server.tournaments.RandomShuffle;
import org.toop.framework.networking.server.tournaments.Tournament; import org.toop.framework.networking.server.tournaments.Tournament;
import org.toop.framework.utils.ImmutablePair; import org.toop.framework.utils.ImmutablePair;
@@ -266,17 +266,40 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
} }
public void startTournament(Tournament tournament, String gameType) { public void startTournament(Tournament tournament, String gameType) {
tournament.init(clientStore.all().toArray(new NettyClient[0]), new RandomShuffle()); try {
new Thread(() -> tournament.start(gameType)).start(); tournament.init(clientStore.all().toArray(new NettyClient[0]), new RandomShuffle());
new Thread(() -> tournament.run(gameType)).start();
} catch (IllegalArgumentException e) {
getUser("host").send("ERR not enough clients to start a tournament");
} catch (RuntimeException e) {
getUser("host").send("ERR no matches could be created to start a tournament with");
}
} }
public void endTournament(Map<NettyClient, Integer> score) { public void endTournament(Map<NettyClient, Integer> score, String gameType) {
IO.println("TOUNAMENT WINNER IS: "); // TODO
IO.println("SCORES"); List<String> u = new ArrayList<>();
IO.println("-----------------------------------"); List<Integer> s = new ArrayList<>();
for (var a : score.entrySet()) {
String b = "" + a.getKey().name() + ": " + a.getValue(); for (var entry : score.entrySet()) {
IO.println(b); u.add(entry.getKey().name());
s.add(entry.getValue());
}
Gson gson = new Gson();
String users = gson.toJson(u);
String scores = gson.toJson(s);
String msg = String.format(
"SVR RESULTS {GAMETYPE: \"%s\", USERS: %s, SCORES: %s}",
gameType,
users,
scores
);
for (var user : onlineUsers()) {
user.send(msg);
} }
} }
} }

View File

@@ -8,7 +8,6 @@ import org.toop.framework.networking.server.client.NettyClient;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@@ -28,8 +27,8 @@ public class BasicTournament implements Tournament {
} }
@Override @Override
public void init(NettyClient[] clients, Shuffler shuffler) { public void init(NettyClient[] clients, Shuffler shuffler) throws IllegalArgumentException {
// if (this.clients == null || clients.length < 1) return; if (clients.length <= 1) throw new IllegalArgumentException("Not enough clients to initialize a tournament");
for (NettyClient client : clients) { for (NettyClient client : clients) {
int INIT_SCORE = 0; int INIT_SCORE = 0;
@@ -52,7 +51,11 @@ public class BasicTournament implements Tournament {
} }
} }
shuffler.shuffle(matchList); try {
shuffler.shuffle(matchList);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
}
} }
public void addScorePoints(NettyClient client) { public void addScorePoints(NettyClient client) {
@@ -60,8 +63,8 @@ public class BasicTournament implements Tournament {
} }
@Override @Override
public boolean start(String gameType) { // TODO, rename to run public boolean run(String gameType) throws RuntimeException {
// if (this.clients == null || clients.length < 1) return false; if (matchList.isEmpty()) throw new RuntimeException("No matches to start a tournament with");
if (server.gameTypes().stream().noneMatch(e -> e.equalsIgnoreCase(gameType))) return false; if (server.gameTypes().stream().noneMatch(e -> e.equalsIgnoreCase(gameType))) return false;
this.gameType = gameType; this.gameType = gameType;
@@ -84,7 +87,7 @@ public class BasicTournament implements Tournament {
match.getRight().clearGame(); match.getRight().clearGame();
} }
server.endTournament(end()); server.endTournament(end(), gameType);
}).start(); }).start();
return true; return true;

View File

@@ -6,6 +6,6 @@ import java.util.HashMap;
public interface Tournament { public interface Tournament {
void init(NettyClient[] clients, Shuffler shuffler); void init(NettyClient[] clients, Shuffler shuffler);
boolean start(String gameType); boolean run(String gameType);
HashMap<NettyClient, Integer> end(); HashMap<NettyClient, Integer> end();
} }