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;
import com.google.gson.Gson;
import org.toop.framework.game.players.ServerPlayer;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
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.TurnBasedGameStore;
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.Tournament;
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) {
try {
tournament.init(clientStore.all().toArray(new NettyClient[0]), new RandomShuffle());
new Thread(() -> tournament.start(gameType)).start();
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) {
IO.println("TOUNAMENT WINNER IS: "); // TODO
IO.println("SCORES");
IO.println("-----------------------------------");
for (var a : score.entrySet()) {
String b = "" + a.getKey().name() + ": " + a.getValue();
IO.println(b);
public void endTournament(Map<NettyClient, Integer> score, String gameType) {
List<String> u = new ArrayList<>();
List<Integer> s = new ArrayList<>();
for (var entry : score.entrySet()) {
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.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
@@ -28,8 +27,8 @@ public class BasicTournament implements Tournament {
}
@Override
public void init(NettyClient[] clients, Shuffler shuffler) {
// if (this.clients == null || clients.length < 1) return;
public void init(NettyClient[] clients, Shuffler shuffler) throws IllegalArgumentException {
if (clients.length <= 1) throw new IllegalArgumentException("Not enough clients to initialize a tournament");
for (NettyClient client : clients) {
int INIT_SCORE = 0;
@@ -52,7 +51,11 @@ public class BasicTournament implements Tournament {
}
}
try {
shuffler.shuffle(matchList);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
}
}
public void addScorePoints(NettyClient client) {
@@ -60,8 +63,8 @@ public class BasicTournament implements Tournament {
}
@Override
public boolean start(String gameType) { // TODO, rename to run
// if (this.clients == null || clients.length < 1) return false;
public boolean run(String gameType) throws RuntimeException {
if (matchList.isEmpty()) throw new RuntimeException("No matches to start a tournament with");
if (server.gameTypes().stream().noneMatch(e -> e.equalsIgnoreCase(gameType))) return false;
this.gameType = gameType;
@@ -84,7 +87,7 @@ public class BasicTournament implements Tournament {
match.getRight().clearGame();
}
server.endTournament(end());
server.endTournament(end(), gameType);
}).start();
return true;

View File

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