mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +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;
|
||||
}
|
||||
|
||||
primary = new ServerView(user, this::sendChallenge, () -> {
|
||||
GlobalEventBus.get().post(new NetworkEvents.SendCommand(clientId, "tournament", "start", "tic-tac-toe"));
|
||||
}, clientId);
|
||||
primary = new ServerView(user, this::sendChallenge, user, clientId);
|
||||
|
||||
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
|
||||
|
||||
a.unsubscribe("connecting");
|
||||
@@ -339,7 +338,8 @@ public final class Server {
|
||||
|
||||
private void gamesListFromServerHandler(NetworkEvents.GamelistResponse event) {
|
||||
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);
|
||||
primary.updateGameList(gl);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.toop.app.widget.Primitive;
|
||||
import org.toop.app.widget.complex.ViewWidget;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.function.Consumer;
|
||||
@@ -16,41 +17,42 @@ import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ListView;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.framework.eventbus.GlobalEventBus;
|
||||
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||
|
||||
public final class ServerView extends ViewWidget {
|
||||
private final String user;
|
||||
private final Consumer<String> onPlayerClicked;
|
||||
private final Runnable tournamentRequest;
|
||||
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 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.onPlayerClicked = onPlayerClicked;
|
||||
this.tournamentRequest = tournamentRequest;
|
||||
this.clientId = clientId;
|
||||
|
||||
this.gameList = new ComboBox<>();
|
||||
this.gameListSub = new ComboBox<>();
|
||||
this.gameListTour = new ComboBox<>();
|
||||
this.listView = new ListView<>();
|
||||
|
||||
setupLayout();
|
||||
setupLayout(userName);
|
||||
}
|
||||
|
||||
private void setupLayout() {
|
||||
private void setupLayout(String userName) {
|
||||
var playerHeader = Primitive.header(user, false);
|
||||
|
||||
subscribeButton = Primitive.button(
|
||||
"subscribe",
|
||||
() -> new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, gameList.getValue())).postEvent(),
|
||||
() -> new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, gameListSub.getValue())).postEvent(),
|
||||
false,
|
||||
true
|
||||
); // TODO localize
|
||||
|
||||
var subscribe = Primitive.hbox(gameList, subscribeButton);
|
||||
var subscribe = Primitive.hbox(gameListSub, subscribeButton);
|
||||
|
||||
var playerListSection = Primitive.vbox(
|
||||
playerHeader,
|
||||
@@ -61,16 +63,19 @@ public final class ServerView extends ViewWidget {
|
||||
|
||||
add(Pos.CENTER, playerListSection);
|
||||
|
||||
var tournamentButton = Primitive.vbox(
|
||||
Primitive.button(
|
||||
"tournament",
|
||||
tournamentRequest,
|
||||
false,
|
||||
false
|
||||
)
|
||||
);
|
||||
if (userName.equals("host")) {
|
||||
var tournamentButton = Primitive.hbox(
|
||||
gameListTour,
|
||||
Primitive.button(
|
||||
"tournament",
|
||||
() -> GlobalEventBus.get().post(new NetworkEvents.SendCommand(clientId, "tournament", "start", gameListTour.getValue())),
|
||||
false,
|
||||
false
|
||||
)
|
||||
);
|
||||
|
||||
add(Pos.BOTTOM_CENTER, tournamentButton);
|
||||
add(Pos.BOTTOM_CENTER, tournamentButton);
|
||||
}
|
||||
|
||||
var disconnectButton = Primitive.button(
|
||||
"disconnect", () -> transitionPrevious(), false);
|
||||
@@ -91,9 +96,13 @@ public final class ServerView extends ViewWidget {
|
||||
|
||||
public void updateGameList(List<String> games) {
|
||||
Platform.runLater(() -> {
|
||||
gameList.getItems().clear();
|
||||
gameList.setItems(FXCollections.observableArrayList(games));
|
||||
gameList.getSelectionModel().select(0);
|
||||
gameListSub.getItems().clear();
|
||||
gameListSub.setItems(FXCollections.observableArrayList(games));
|
||||
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.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