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:
@@ -117,9 +117,8 @@ public final class Server {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
primary = new ServerView(user, this::sendChallenge, () -> {
|
primary = new ServerView(user, this::sendChallenge, user, clientId);
|
||||||
GlobalEventBus.get().post(new NetworkEvents.SendCommand(clientId, "tournament", "start", "tic-tac-toe"));
|
|
||||||
}, clientId);
|
|
||||||
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
|
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
|
||||||
|
|
||||||
a.unsubscribe("connecting");
|
a.unsubscribe("connecting");
|
||||||
@@ -339,7 +338,8 @@ public final class Server {
|
|||||||
|
|
||||||
private void gamesListFromServerHandler(NetworkEvents.GamelistResponse event) {
|
private void gamesListFromServerHandler(NetworkEvents.GamelistResponse event) {
|
||||||
gameList.clear();
|
gameList.clear();
|
||||||
var gl = List.of(event.gamelist());
|
var gl = new java.util.ArrayList<>(List.of(event.gamelist()));
|
||||||
|
gl.sort(String::compareTo);
|
||||||
gameList.addAll(gl);
|
gameList.addAll(gl);
|
||||||
primary.updateGameList(gl);
|
primary.updateGameList(gl);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import org.toop.app.widget.Primitive;
|
|||||||
import org.toop.app.widget.complex.ViewWidget;
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -16,41 +17,42 @@ import javafx.geometry.Pos;
|
|||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
import org.toop.framework.networking.connection.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
|
|
||||||
public final class ServerView extends ViewWidget {
|
public final class ServerView extends ViewWidget {
|
||||||
private final String user;
|
private final String user;
|
||||||
private final Consumer<String> onPlayerClicked;
|
private final Consumer<String> onPlayerClicked;
|
||||||
private final Runnable tournamentRequest;
|
|
||||||
private final long clientId;
|
private final long clientId;
|
||||||
|
|
||||||
private final ComboBox<String> gameList;
|
private final ComboBox<String> gameListSub;
|
||||||
|
private final ComboBox<String> gameListTour;
|
||||||
private final ListView<Button> listView;
|
private final ListView<Button> listView;
|
||||||
private Button subscribeButton;
|
private Button subscribeButton;
|
||||||
|
|
||||||
public ServerView(String user, Consumer<String> onPlayerClicked, Runnable tournamentRequest, long clientId) {
|
public ServerView(String user, Consumer<String> onPlayerClicked, String userName, long clientId) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.onPlayerClicked = onPlayerClicked;
|
this.onPlayerClicked = onPlayerClicked;
|
||||||
this.tournamentRequest = tournamentRequest;
|
|
||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
|
|
||||||
this.gameList = new ComboBox<>();
|
this.gameListSub = new ComboBox<>();
|
||||||
|
this.gameListTour = new ComboBox<>();
|
||||||
this.listView = new ListView<>();
|
this.listView = new ListView<>();
|
||||||
|
|
||||||
setupLayout();
|
setupLayout(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupLayout() {
|
private void setupLayout(String userName) {
|
||||||
var playerHeader = Primitive.header(user, false);
|
var playerHeader = Primitive.header(user, false);
|
||||||
|
|
||||||
subscribeButton = Primitive.button(
|
subscribeButton = Primitive.button(
|
||||||
"subscribe",
|
"subscribe",
|
||||||
() -> new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, gameList.getValue())).postEvent(),
|
() -> new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, gameListSub.getValue())).postEvent(),
|
||||||
false,
|
false,
|
||||||
true
|
true
|
||||||
); // TODO localize
|
); // TODO localize
|
||||||
|
|
||||||
var subscribe = Primitive.hbox(gameList, subscribeButton);
|
var subscribe = Primitive.hbox(gameListSub, subscribeButton);
|
||||||
|
|
||||||
var playerListSection = Primitive.vbox(
|
var playerListSection = Primitive.vbox(
|
||||||
playerHeader,
|
playerHeader,
|
||||||
@@ -61,16 +63,19 @@ public final class ServerView extends ViewWidget {
|
|||||||
|
|
||||||
add(Pos.CENTER, playerListSection);
|
add(Pos.CENTER, playerListSection);
|
||||||
|
|
||||||
var tournamentButton = Primitive.vbox(
|
if (userName.equals("host")) {
|
||||||
|
var tournamentButton = Primitive.hbox(
|
||||||
|
gameListTour,
|
||||||
Primitive.button(
|
Primitive.button(
|
||||||
"tournament",
|
"tournament",
|
||||||
tournamentRequest,
|
() -> GlobalEventBus.get().post(new NetworkEvents.SendCommand(clientId, "tournament", "start", gameListTour.getValue())),
|
||||||
false,
|
false,
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
add(Pos.BOTTOM_CENTER, tournamentButton);
|
add(Pos.BOTTOM_CENTER, tournamentButton);
|
||||||
|
}
|
||||||
|
|
||||||
var disconnectButton = Primitive.button(
|
var disconnectButton = Primitive.button(
|
||||||
"disconnect", () -> transitionPrevious(), false);
|
"disconnect", () -> transitionPrevious(), false);
|
||||||
@@ -91,9 +96,13 @@ public final class ServerView extends ViewWidget {
|
|||||||
|
|
||||||
public void updateGameList(List<String> games) {
|
public void updateGameList(List<String> games) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
gameList.getItems().clear();
|
gameListSub.getItems().clear();
|
||||||
gameList.setItems(FXCollections.observableArrayList(games));
|
gameListSub.setItems(FXCollections.observableArrayList(games));
|
||||||
gameList.getSelectionModel().select(0);
|
gameListSub.getSelectionModel().select(0);
|
||||||
|
|
||||||
|
gameListTour.getItems().clear();
|
||||||
|
gameListTour.setItems(FXCollections.observableArrayList(games));
|
||||||
|
gameListTour.getSelectionModel().select(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.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.BasicTournament;
|
||||||
|
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;
|
||||||
|
|
||||||
@@ -265,7 +266,7 @@ 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]));
|
tournament.init(clientStore.all().toArray(new NettyClient[0]), new RandomShuffle());
|
||||||
new Thread(() -> tournament.start(gameType)).start();
|
new Thread(() -> tournament.start(gameType)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class BasicTournament implements Tournament {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(NettyClient[] clients) {
|
public void init(NettyClient[] clients, Shuffler shuffler) {
|
||||||
// if (this.clients == null || clients.length < 1) return;
|
// if (this.clients == null || clients.length < 1) return;
|
||||||
|
|
||||||
for (NettyClient client : clients) {
|
for (NettyClient client : clients) {
|
||||||
@@ -52,21 +52,7 @@ public class BasicTournament implements Tournament {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shuffle();
|
shuffler.shuffle(matchList);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addScorePoints(NettyClient client) {
|
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;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public interface Tournament {
|
public interface Tournament {
|
||||||
void init(NettyClient[] clients);
|
void init(NettyClient[] clients, Shuffler shuffler);
|
||||||
boolean start(String gameType);
|
boolean start(String gameType);
|
||||||
HashMap<NettyClient, Integer> end();
|
HashMap<NettyClient, Integer> end();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user