Shuffle now changeable, host can now switch tournament gametype

This commit is contained in:
Bas de Jong
2026-01-10 00:06:52 +01:00
parent 6a395cc40b
commit 6b644ed8fa
7 changed files with 66 additions and 43 deletions

View File

@@ -10,6 +10,7 @@ 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;
@@ -265,7 +266,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
}
public void startTournament(Tournament tournament, String gameType) {
tournament.init(clientStore.all().toArray(new NettyClient[0]));
tournament.init(clientStore.all().toArray(new NettyClient[0]), new RandomShuffle());
new Thread(() -> tournament.start(gameType)).start();
}

View File

@@ -28,7 +28,7 @@ public class BasicTournament implements Tournament {
}
@Override
public void init(NettyClient[] clients) {
public void init(NettyClient[] clients, Shuffler shuffler) {
// if (this.clients == null || clients.length < 1) return;
for (NettyClient client : clients) {
@@ -52,21 +52,7 @@ public class BasicTournament implements Tournament {
}
}
shuffle();
}
public void shuffle() { // TODO make shuffle own class so user can use different shuffles
final int SHUFFLE_AMOUNT = matchList.size() * 2;
Random rand = new Random();
for (int i = 0; i <= SHUFFLE_AMOUNT; i++) {
int index = rand.nextInt(matchList.size());
TournamentMatch match = matchList.get(index);
matchList.remove(index);
matchList.addLast(match);
}
shuffler.shuffle(matchList);
}
public void addScorePoints(NettyClient client) {

View File

@@ -0,0 +1,20 @@
package org.toop.framework.networking.server.tournaments;
import java.util.List;
import java.util.Random;
public class RandomShuffle implements Shuffler {
@Override
public <T> void shuffle(List<T> listToShuffle) {
final int SHUFFLE_AMOUNT = listToShuffle.size() * 2;
Random rand = new Random();
for (int i = 0; i <= SHUFFLE_AMOUNT; i++) {
int index = rand.nextInt(listToShuffle.size());
T match = listToShuffle.get(index);
listToShuffle.remove(index);
listToShuffle.addLast(match);
}
}
}

View File

@@ -0,0 +1,7 @@
package org.toop.framework.networking.server.tournaments;
import java.util.List;
public interface Shuffler {
<T> void shuffle(List<T> listToShuffle);
}

View File

@@ -5,7 +5,7 @@ import org.toop.framework.networking.server.client.NettyClient;
import java.util.HashMap;
public interface Tournament {
void init(NettyClient[] clients);
void init(NettyClient[] clients, Shuffler shuffler);
boolean start(String gameType);
HashMap<NettyClient, Integer> end();
}