Extract LobbyCommand command logic to their own methods.

This commit is contained in:
teinarss
2018-07-05 21:35:37 +00:00
committed by Paul Chote
parent 2471b07e55
commit 23155c039d

View File

@@ -23,6 +23,28 @@ namespace OpenRA.Mods.Common.Server
{ {
public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart, INotifyServerEmpty, IClientJoined public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart, INotifyServerEmpty, IClientJoined
{ {
readonly IDictionary<string, Func<S, Connection, Session.Client, string, bool>> commandHandlers = new Dictionary<string, Func<S, Connection, Session.Client, string, bool>>
{
{ "state", State },
{ "startgame", StartGame },
{ "slot", Slot },
{ "allow_spectators", AllowSpectators },
{ "spectate", Specate },
{ "slot_close", SlotClose },
{ "slot_open", SlotOpen },
{ "slot_bot", SlotBot },
{ "map", Map },
{ "option", Option },
{ "assignteams", AssignTeams },
{ "kick", Kick },
{ "name", Name },
{ "faction", Faction },
{ "team", Team },
{ "spawn", Spawn },
{ "color", Color },
{ "sync_lobby", SyncLobby }
};
static bool ValidateSlotCommand(S server, Connection conn, Session.Client client, string arg, bool requiresHost) static bool ValidateSlotCommand(S server, Connection conn, Session.Client client, string arg, bool requiresHost)
{ {
if (!server.LobbyInfo.Slots.ContainsKey(arg)) if (!server.LobbyInfo.Slots.ContainsKey(arg))
@@ -56,6 +78,21 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
public bool InterpretCommand(S server, Connection conn, Session.Client client, string cmd)
{
if (server == null || conn == null || client == null || !ValidateCommand(server, conn, client, cmd))
return false;
var cmdName = cmd.Split(' ').First();
var cmdValue = cmd.Split(' ').Skip(1).JoinWith(" ");
Func<S, Connection, Session.Client, string, bool> a;
if (!commandHandlers.TryGetValue(cmdName, out a))
return false;
return a(server, conn, client, cmdValue);
}
static void CheckAutoStart(S server) static void CheckAutoStart(S server)
{ {
var nonBotPlayers = server.LobbyInfo.NonBotPlayers; var nonBotPlayers = server.LobbyInfo.NonBotPlayers;
@@ -76,15 +113,7 @@ namespace OpenRA.Mods.Common.Server
server.StartGame(); server.StartGame();
} }
public bool InterpretCommand(S server, Connection conn, Session.Client client, string cmd) static bool State(S server, Connection conn, Session.Client client, string s)
{
if (server == null || conn == null || client == null || !ValidateCommand(server, conn, client, cmd))
return false;
var dict = new Dictionary<string, Func<string, bool>>
{
{ "state",
s =>
{ {
var state = Session.ClientState.Invalid; var state = Session.ClientState.Invalid;
if (!Enum<Session.ClientState>.TryParse(s, false, out state)) if (!Enum<Session.ClientState>.TryParse(s, false, out state))
@@ -104,9 +133,8 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
},
{ "startgame", static bool StartGame(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!client.IsAdmin) if (!client.IsAdmin)
{ {
@@ -130,9 +158,8 @@ namespace OpenRA.Mods.Common.Server
server.StartGame(); server.StartGame();
return true; return true;
} }
},
{ "slot", static bool Slot(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!server.LobbyInfo.Slots.ContainsKey(s)) if (!server.LobbyInfo.Slots.ContainsKey(s))
{ {
@@ -161,9 +188,8 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
},
{ "allow_spectators", static bool AllowSpectators(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllowSpectators)) if (bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllowSpectators))
{ {
@@ -176,9 +202,8 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
} }
},
{ "spectate", static bool Specate(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (server.LobbyInfo.GlobalSettings.AllowSpectators || client.IsAdmin) if (server.LobbyInfo.GlobalSettings.AllowSpectators || client.IsAdmin)
{ {
@@ -193,9 +218,8 @@ namespace OpenRA.Mods.Common.Server
else else
return false; return false;
} }
},
{ "slot_close", static bool SlotClose(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!ValidateSlotCommand(server, conn, client, s, true)) if (!ValidateSlotCommand(server, conn, client, s, true))
return false; return false;
@@ -228,11 +252,11 @@ namespace OpenRA.Mods.Common.Server
server.LobbyInfo.Slots[s].Closed = true; server.LobbyInfo.Slots[s].Closed = true;
server.SyncLobbySlots(); server.SyncLobbySlots();
return true; return true;
} }
},
{ "slot_open", static bool SlotOpen(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!ValidateSlotCommand(server, conn, client, s, true)) if (!ValidateSlotCommand(server, conn, client, s, true))
return false; return false;
@@ -255,11 +279,11 @@ namespace OpenRA.Mods.Common.Server
} }
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "slot_bot", static bool SlotBot(S server, Connection conn, Session.Client client, string s)
s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
@@ -335,11 +359,11 @@ namespace OpenRA.Mods.Common.Server
S.SyncClientToPlayerReference(bot, server.Map.Players.Players[parts[0]]); S.SyncClientToPlayerReference(bot, server.Map.Players.Players[parts[0]]);
server.SyncLobbyClients(); server.SyncLobbyClients();
server.SyncLobbySlots(); server.SyncLobbySlots();
return true; return true;
} }
},
{ "map", static bool Map(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!client.IsAdmin) if (!client.IsAdmin)
{ {
@@ -436,9 +460,8 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
},
{ "option", static bool Option(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!client.IsAdmin) if (!client.IsAdmin)
{ {
@@ -488,9 +511,8 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
},
{ "assignteams", static bool AssignTeams(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!client.IsAdmin) if (!client.IsAdmin)
{ {
@@ -527,11 +549,11 @@ namespace OpenRA.Mods.Common.Server
} }
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "kick", static bool Kick(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!client.IsAdmin) if (!client.IsAdmin)
{ {
@@ -578,9 +600,8 @@ namespace OpenRA.Mods.Common.Server
return true; return true;
} }
},
{ "name", static bool Name(S server, Connection conn, Session.Client client, string s)
s =>
{ {
var sanitizedName = Settings.SanitizedPlayerName(s); var sanitizedName = Settings.SanitizedPlayerName(s);
if (sanitizedName == client.Name) if (sanitizedName == client.Name)
@@ -590,11 +611,11 @@ namespace OpenRA.Mods.Common.Server
server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName)); server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName));
client.Name = sanitizedName; client.Name = sanitizedName;
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "faction", static bool Faction(S server, Connection conn, Session.Client client, string s)
s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -619,11 +640,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.Faction = parts[1]; targetClient.Faction = parts[1];
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "team", static bool Team(S server, Connection conn, Session.Client client, string s)
s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -645,11 +666,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.Team = team; targetClient.Team = team;
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "spawn", static bool Spawn(S server, Connection conn, Session.Client client, string s)
s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -698,11 +719,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.SpawnPoint = spawnPoint; targetClient.SpawnPoint = spawnPoint;
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "color", static bool Color(S server, Connection conn, Session.Client client, string s)
s =>
{ {
var parts = s.Split(' '); var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -724,11 +745,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.PreferredColor = targetClient.Color; targetClient.PreferredColor = targetClient.Color;
server.SyncLobbyClients(); server.SyncLobbyClients();
return true; return true;
} }
},
{ "sync_lobby", static bool SyncLobby(S server, Connection conn, Session.Client client, string s)
s =>
{ {
if (!client.IsAdmin) if (!client.IsAdmin)
{ {
@@ -746,20 +767,9 @@ namespace OpenRA.Mods.Common.Server
server.LobbyInfo = lobbyInfo; server.LobbyInfo = lobbyInfo;
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
} }
}
};
var cmdName = cmd.Split(' ').First();
var cmdValue = cmd.Split(' ').Skip(1).JoinWith(" ");
Func<string, bool> a;
if (!dict.TryGetValue(cmdName, out a))
return false;
return a(cmdValue);
}
public void ServerStarted(S server) public void ServerStarted(S server)
{ {
@@ -893,7 +903,7 @@ namespace OpenRA.Mods.Common.Server
.ToDictionary(ss => ss.PlayerReference, ss => ss); .ToDictionary(ss => ss.PlayerReference, ss => ss);
} }
public PlayerReference PlayerReferenceForSlot(S server, Session.Slot slot) public static PlayerReference PlayerReferenceForSlot(S server, Session.Slot slot)
{ {
if (slot == null) if (slot == null)
return null; return null;