mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Shuffle now changeable, host can now switch tournament gametype
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.toop.framework.networking.server.tournaments;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Shuffler {
|
||||
<T> void shuffle(List<T> listToShuffle);
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user