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
{
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)
{
if (!server.LobbyInfo.Slots.ContainsKey(arg))
@@ -56,6 +78,21 @@ namespace OpenRA.Mods.Common.Server
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)
{
var nonBotPlayers = server.LobbyInfo.NonBotPlayers;
@@ -76,15 +113,7 @@ namespace OpenRA.Mods.Common.Server
server.StartGame();
}
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 dict = new Dictionary<string, Func<string, bool>>
{
{ "state",
s =>
static bool State(S server, Connection conn, Session.Client client, string s)
{
var state = Session.ClientState.Invalid;
if (!Enum<Session.ClientState>.TryParse(s, false, out state))
@@ -104,9 +133,8 @@ namespace OpenRA.Mods.Common.Server
return true;
}
},
{ "startgame",
s =>
static bool StartGame(S server, Connection conn, Session.Client client, string s)
{
if (!client.IsAdmin)
{
@@ -130,9 +158,8 @@ namespace OpenRA.Mods.Common.Server
server.StartGame();
return true;
}
},
{ "slot",
s =>
static bool Slot(S server, Connection conn, Session.Client client, string s)
{
if (!server.LobbyInfo.Slots.ContainsKey(s))
{
@@ -161,9 +188,8 @@ namespace OpenRA.Mods.Common.Server
return true;
}
},
{ "allow_spectators",
s =>
static bool AllowSpectators(S server, Connection conn, Session.Client client, string s)
{
if (bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllowSpectators))
{
@@ -176,9 +202,8 @@ namespace OpenRA.Mods.Common.Server
return true;
}
}
},
{ "spectate",
s =>
static bool Specate(S server, Connection conn, Session.Client client, string s)
{
if (server.LobbyInfo.GlobalSettings.AllowSpectators || client.IsAdmin)
{
@@ -193,9 +218,8 @@ namespace OpenRA.Mods.Common.Server
else
return false;
}
},
{ "slot_close",
s =>
static bool SlotClose(S server, Connection conn, Session.Client client, string s)
{
if (!ValidateSlotCommand(server, conn, client, s, true))
return false;
@@ -228,11 +252,11 @@ namespace OpenRA.Mods.Common.Server
server.LobbyInfo.Slots[s].Closed = true;
server.SyncLobbySlots();
return true;
}
},
{ "slot_open",
s =>
static bool SlotOpen(S server, Connection conn, Session.Client client, string s)
{
if (!ValidateSlotCommand(server, conn, client, s, true))
return false;
@@ -255,11 +279,11 @@ namespace OpenRA.Mods.Common.Server
}
server.SyncLobbyClients();
return true;
}
},
{ "slot_bot",
s =>
static bool SlotBot(S server, Connection conn, Session.Client client, string s)
{
var parts = s.Split(' ');
@@ -335,11 +359,11 @@ namespace OpenRA.Mods.Common.Server
S.SyncClientToPlayerReference(bot, server.Map.Players.Players[parts[0]]);
server.SyncLobbyClients();
server.SyncLobbySlots();
return true;
}
},
{ "map",
s =>
static bool Map(S server, Connection conn, Session.Client client, string s)
{
if (!client.IsAdmin)
{
@@ -436,9 +460,8 @@ namespace OpenRA.Mods.Common.Server
return true;
}
},
{ "option",
s =>
static bool Option(S server, Connection conn, Session.Client client, string s)
{
if (!client.IsAdmin)
{
@@ -488,9 +511,8 @@ namespace OpenRA.Mods.Common.Server
return true;
}
},
{ "assignteams",
s =>
static bool AssignTeams(S server, Connection conn, Session.Client client, string s)
{
if (!client.IsAdmin)
{
@@ -527,11 +549,11 @@ namespace OpenRA.Mods.Common.Server
}
server.SyncLobbyClients();
return true;
}
},
{ "kick",
s =>
static bool Kick(S server, Connection conn, Session.Client client, string s)
{
if (!client.IsAdmin)
{
@@ -578,9 +600,8 @@ namespace OpenRA.Mods.Common.Server
return true;
}
},
{ "name",
s =>
static bool Name(S server, Connection conn, Session.Client client, string s)
{
var sanitizedName = Settings.SanitizedPlayerName(s);
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));
client.Name = sanitizedName;
server.SyncLobbyClients();
return true;
}
},
{ "faction",
s =>
static bool Faction(S server, Connection conn, Session.Client client, string s)
{
var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -619,11 +640,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.Faction = parts[1];
server.SyncLobbyClients();
return true;
}
},
{ "team",
s =>
static bool Team(S server, Connection conn, Session.Client client, string s)
{
var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -645,11 +666,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.Team = team;
server.SyncLobbyClients();
return true;
}
},
{ "spawn",
s =>
static bool Spawn(S server, Connection conn, Session.Client client, string s)
{
var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -698,11 +719,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.SpawnPoint = spawnPoint;
server.SyncLobbyClients();
return true;
}
},
{ "color",
s =>
static bool Color(S server, Connection conn, Session.Client client, string s)
{
var parts = s.Split(' ');
var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0]));
@@ -724,11 +745,11 @@ namespace OpenRA.Mods.Common.Server
targetClient.PreferredColor = targetClient.Color;
server.SyncLobbyClients();
return true;
}
},
{ "sync_lobby",
s =>
static bool SyncLobby(S server, Connection conn, Session.Client client, string s)
{
if (!client.IsAdmin)
{
@@ -746,20 +767,9 @@ namespace OpenRA.Mods.Common.Server
server.LobbyInfo = lobbyInfo;
server.SyncLobbyInfo();
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)
{
@@ -893,7 +903,7 @@ namespace OpenRA.Mods.Common.Server
.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)
return null;