Working subscription, button only subs to reversi right now

This commit is contained in:
lieght
2025-12-13 23:20:28 +01:00
parent c2f1df7143
commit 8cb0a86d4e
5 changed files with 47 additions and 18 deletions

View File

@@ -118,7 +118,7 @@ public final class Server {
return;
}
primary = new ServerView(user, this::sendChallenge);
primary = new ServerView(user, this::sendChallenge, clientId);
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
a.unsubscribe("connecting");

View File

@@ -10,16 +10,20 @@ import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import org.toop.framework.eventbus.EventFlow;
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 long clientId;
private final ListView<Button> listView;
public ServerView(String user, Consumer<String> onPlayerClicked) {
public ServerView(String user, Consumer<String> onPlayerClicked, long clientId) {
this.user = user;
this.onPlayerClicked = onPlayerClicked;
this.clientId = clientId;
this.listView = new ListView<>();
@@ -29,9 +33,16 @@ public final class ServerView extends ViewWidget {
private void setupLayout() {
var playerHeader = Primitive.header(user, false);
Button subscribeButton = Primitive.button(
"subscribe",
() -> new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, "reversi")).postEvent(),
false
); // TODO localize
var playerListSection = Primitive.vbox(
playerHeader,
Primitive.separator(),
subscribeButton,
listView
);

View File

@@ -110,7 +110,9 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
@Override
public void subscribeClient(String clientName, String gameTypeKey) {
if (!gameTypesStore.all().containsKey(gameTypeKey)) return;
if (!gameTypesStore.all().containsKey(gameTypeKey)) {
return;
}
subscriptions.forEach((_, clientNames) -> clientNames.remove(clientName));
subscriptions.computeIfAbsent(
@@ -126,8 +128,14 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
@Override
public void startGame(String gameType, NettyClient... clients) {
IO.println("------------------------------------------");
IO.println("USERS: " + clients.length + " " + Arrays.stream(clients).toList().toString());
if (!gameTypesStore.all().containsKey(gameType)) return;
IO.println("------------------------------------------");
try {
ServerPlayer[] players = new ServerPlayer[clients.length];
var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), clients);
@@ -150,7 +158,10 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
gameType,
clients[0].name()));
game.start();
} catch (Exception ignored) {}
} catch (Exception e) {
IO.println("ERROR: Failed to start OnlineTurnBasedGame");
e.printStackTrace();
}
}
@Override
@@ -188,25 +199,29 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
private void checkSubscriptions() {
if (subscriptions.isEmpty()) return;
List<String> keys = subscriptions.keySet().stream().toList();
List<String> keys = List.copyOf(subscriptions.keySet());
Random ran = new Random();
for (String key : keys) {
var userNames = subscriptions.get(key);
List<String> userNames = subscriptions.get(key);
if (userNames.size() < 2) continue;
Random ran = new Random();
while (userNames.size() > 1) {
int left = ran.nextInt(userNames.size());
int right;
do {
right = ran.nextInt(userNames.size());
} while (left == right);
var left = ran.nextInt(userNames.size());
var right = ran.nextInt(userNames.size());
String userLeft = userNames.get(left);
String userRight = userNames.get(right);
while (left == right) left = ran.nextInt(userNames.size());
int first = Math.max(left, right);
int second = Math.min(left, right);
userNames.remove(first);
userNames.remove(second);
subscriptions.get(key).remove(userNames.get(left));
subscriptions.get(key).remove(userNames.get(right));
startGame(key, getUser(left), getUser(right));
startGame(key, getUser(userLeft), getUser(userRight));
}
}
}

View File

@@ -22,7 +22,6 @@ import org.toop.framework.networking.server.stores.TurnBasedGameStore;
import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

View File

@@ -34,7 +34,11 @@ public class MessageHandler implements Handler<ParsedMessage> {
// DO NOT INVERT
private boolean hasArgs(String... args) {
return (args.length >= 1);
if (args.length < 1) {
client.send("ERR not enough arguments");
return false;
}
return true;
}
private void handleLogin(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
@@ -46,7 +50,7 @@ public class MessageHandler implements Handler<ParsedMessage> {
private void handleSubscribe(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
if (!hasArgs(p.args())) return;
server.subscribeClient(p.args()[0], client.name());
server.subscribeClient(client.name(), p.args()[0]);
}
private void handleHelp(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {