mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Working subscription, button only subs to reversi right now
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user