mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
Starting a tournament now requires to be admin
This commit is contained in:
@@ -31,6 +31,8 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
final private Duration challengeDuration;
|
||||
final private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
private final List<NettyClient> admins = new ArrayList<>();
|
||||
|
||||
public Server(
|
||||
Duration challengeDuration,
|
||||
TurnBasedGameTypeStore turnBasedGameTypeStore,
|
||||
@@ -49,11 +51,13 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
|
||||
@Override
|
||||
public void addClient(NettyClient client) {
|
||||
if (admins.isEmpty()) admins.addLast(client);
|
||||
clientStore.add(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeClient(NettyClient client) {
|
||||
admins.remove(client);
|
||||
clientStore.remove(client.id());
|
||||
}
|
||||
|
||||
@@ -265,19 +269,25 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void startTournament(String gameType) {
|
||||
public void startTournament(String gameType, NettyClient requestor) {
|
||||
if (!admins.contains(requestor)) {
|
||||
requestor.send("ERR you do not have the privileges to start a tournament");
|
||||
return;
|
||||
}
|
||||
|
||||
Tournament tournament = new BasicTournament(new TournamentBuilder(
|
||||
this,
|
||||
new BasicTournamentRunner(),
|
||||
new RoundRobinMatchMaker(onlineUsers()),
|
||||
new BasicScoreSystem(onlineUsers())
|
||||
));
|
||||
|
||||
try {
|
||||
new Thread(() -> tournament.run(gameType)).start();
|
||||
} catch (IllegalArgumentException e) {
|
||||
getUser("host").send("ERR not enough clients to start a tournament");
|
||||
admins.forEach(c -> c.send("ERR not enough clients to start a tournament"));
|
||||
} catch (RuntimeException e) {
|
||||
getUser("host").send("ERR no matches could be created to start a tournament with");
|
||||
admins.forEach(c -> c.send("ERR no matches could be created to start a tournament with"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.toop.framework.game.players.ServerPlayer;
|
||||
import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
||||
import org.toop.framework.networking.server.Server;
|
||||
import org.toop.framework.networking.server.client.Client;
|
||||
import org.toop.framework.networking.server.client.NettyClient;
|
||||
import org.toop.framework.networking.server.parsing.ParsedMessage;
|
||||
import org.toop.framework.networking.server.tournaments.*;
|
||||
import org.toop.framework.utils.Utils;
|
||||
@@ -11,9 +12,9 @@ import org.toop.framework.utils.Utils;
|
||||
public class MessageHandler implements Handler<ParsedMessage> {
|
||||
|
||||
private final Server server;
|
||||
private final Client<OnlineTurnBasedGame, ServerPlayer> client;
|
||||
private final NettyClient client;
|
||||
|
||||
public MessageHandler(Server server, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
public MessageHandler(Server server, NettyClient client) {
|
||||
this.server = server;
|
||||
this.client = client;
|
||||
}
|
||||
@@ -43,27 +44,27 @@ public class MessageHandler implements Handler<ParsedMessage> {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void handleLogin(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleLogin(ParsedMessage p, NettyClient client) {
|
||||
if (!hasArgs(p.args())) return;
|
||||
|
||||
client.setName(p.args()[0]);
|
||||
}
|
||||
|
||||
private void handleSubscribe(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleSubscribe(ParsedMessage p, NettyClient client) {
|
||||
if (!hasArgs(p.args())) return;
|
||||
|
||||
server.subscribeClient(client.name(), p.args()[0]);
|
||||
}
|
||||
|
||||
private void handleHelp(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleHelp(ParsedMessage p, NettyClient client) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void handleMessage(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleMessage(ParsedMessage p, NettyClient client) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void handleGet(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleGet(ParsedMessage p, NettyClient client) {
|
||||
if (!hasArgs(p.args())) return;
|
||||
|
||||
switch (p.args()[0]) {
|
||||
@@ -78,7 +79,7 @@ public class MessageHandler implements Handler<ParsedMessage> {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleChallenge(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleChallenge(ParsedMessage p, NettyClient client) {
|
||||
if (!hasArgs(p.args())) return;
|
||||
if (p.args().length < 2) return;
|
||||
|
||||
@@ -103,20 +104,18 @@ public class MessageHandler implements Handler<ParsedMessage> {
|
||||
server.challengeClient(client.name(), p.args()[0], p.args()[1]);
|
||||
}
|
||||
|
||||
private void handleMove(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleMove(ParsedMessage p, NettyClient client) {
|
||||
if(!hasArgs(p.args())) return;
|
||||
|
||||
// TODO check if not number
|
||||
client.player().setMove(1L << Integer.parseInt(p.args()[0]));
|
||||
}
|
||||
|
||||
private void handleTournament(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
||||
private void handleTournament(ParsedMessage p, NettyClient client) {
|
||||
if(!hasArgs(p.args())) return;
|
||||
|
||||
if (!client.name().equalsIgnoreCase("host")) return;
|
||||
|
||||
if (p.args()[0].equalsIgnoreCase("start") && p.args().length > 1) {
|
||||
server.startTournament(p.args()[1]);
|
||||
server.startTournament(p.args()[1], client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user