From 4db569da94ca3028776a88a1ab7ee66b2cb8cbaa Mon Sep 17 00:00:00 2001 From: teinarss Date: Mon, 2 Jul 2018 18:31:11 +0200 Subject: [PATCH] Cleanup in LobbyCommands --- .../ServerTraits/LobbyCommands.cs | 1270 ++++++++--------- 1 file changed, 611 insertions(+), 659 deletions(-) diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index 81b4f64463e1..691987a162f2 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -23,6 +23,30 @@ namespace OpenRA.Mods.Common.Server { public class LobbyCommands : ServerTrait, IInterpretCommand, INotifyServerStart, INotifyServerEmpty, IClientJoined { + readonly IDictionary> commandHandlers = new Dictionary> + { + { "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 }, + { "make_admin", MakeAdmin }, + { "make_spectator", MakeSpectator }, + { "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,844 +80,752 @@ public static bool ValidateCommand(S server, Connection conn, Session.Client cli return true; } - static void CheckAutoStart(S server) - { - var nonBotPlayers = server.LobbyInfo.NonBotPlayers; - - // Are all players and admin (could be spectating) ready? - if (nonBotPlayers.Any(c => c.State != Session.ClientState.Ready) || - server.LobbyInfo.Clients.First(c => c.IsAdmin).State != Session.ClientState.Ready) - return; - - // Does server have at least 2 human players? - if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && nonBotPlayers.Count() < 2) - return; - - // Are the map conditions satisfied? - if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null)) - return; - - 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> - { - { "state", State(server, conn, client) }, - { "startgame", StartGame(server, conn, client) }, - { "slot", Slot(server, conn, client) }, - { "allow_spectators", AllowSpectators(server, conn) }, - { "spectate", Specate(server, client) }, - { "slot_close", SlotClose(server, conn, client) }, - { "slot_open", SlotOpen(server, conn, client) }, - { "slot_bot", SlotBot(server, conn, client) }, - { "map", Map(server, conn, client) }, - { "option", Option(server, conn, client) }, - { "assignteams", AssignTeams(server, conn, client) }, - { "kick", Kick(server, conn, client) }, - { "make_admin", MakeAdmin(server, conn, client) }, - { "make_spectator", MakeSpectator(server, conn, client) }, - { "name", Name(server, conn, client) }, - { "faction", Faction(server, conn, client) }, - { "team", Team(server, client) }, - { "spawn", Spawn(server, conn, client) }, - { "color", Color(server, conn, client) }, - { "sync_lobby", SyncLobby(server, conn, client) } - }; - var cmdName = cmd.Split(' ').First(); var cmdValue = cmd.Split(' ').Skip(1).JoinWith(" "); - Func a; - if (!dict.TryGetValue(cmdName, out a)) + Func a; + if (!commandHandlers.TryGetValue(cmdName, out a)) return false; - return a(cmdValue); + return a(server, conn, client, cmdValue); } - Func MakeSpectator(S server, Connection conn, Session.Client client) + public void ServerStarted(S server) { - return s => - { - if (!client.IsAdmin) - { - server.SendOrderTo(conn, "Message", "Only the host can move players to spectators."); - return true; - } - - int targetId; - Exts.TryParseIntegerInvariant(s, out targetId); - var targetConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == targetId); + // Remote maps are not supported for the initial map + var uid = server.LobbyInfo.GlobalSettings.Map; + server.Map = server.ModData.MapCache[uid]; + if (server.Map.Status != MapStatus.Available) + throw new InvalidOperationException("Map {0} not found".F(uid)); - if (targetConn == null) - { - server.SendOrderTo(conn, "Message", "No-one in that slot."); - return true; - } + server.LobbyInfo.Slots = server.Map.Players.Players + .Select(p => MakeSlotFromPlayerReference(p.Value)) + .Where(s => s != null) + .ToDictionary(s => s.PlayerReference, s => s); - var targetClient = server.GetClient(targetConn); - targetClient.Slot = null; - targetClient.SpawnPoint = 0; - targetClient.Team = 0; - targetClient.Color = HSLColor.FromRGB(255, 255, 255); - server.SendMessage("{0} moved {1} to spectators.".F(client.Name, targetClient.Name)); - Log.Write("server", "{0} moved {1} to spectators.".F(client.Name, targetClient.Name)); - server.SyncLobbyClients(); - CheckAutoStart(server); - return true; - }; + LoadMapSettings(server, server.LobbyInfo.GlobalSettings, server.Map.Rules); } - static Func SyncLobby(S server, Connection conn, Session.Client client) + static bool State(S server, Connection conn, Session.Client client, string s) { - return s => + var state = Session.ClientState.Invalid; + if (!Enum.TryParse(s, false, out state)) { - if (!client.IsAdmin) - { - server.SendOrderTo(conn, "Message", "Only the host can set lobby info"); - return true; - } + server.SendOrderTo(conn, "Message", "Malformed state command"); + return true; + } - var lobbyInfo = Session.Deserialize(s); - if (lobbyInfo == null) - { - server.SendOrderTo(conn, "Message", "Invalid Lobby Info Sent"); - return true; - } + client.State = state; - server.LobbyInfo = lobbyInfo; + Log.Write("server", "Player @{0} is {1}", + conn.Socket.RemoteEndPoint, client.State); - server.SyncLobbyInfo(); - return true; - }; + server.SyncLobbyClients(); + + CheckAutoStart(server); + + return true; } - static Func Color(S server, Connection conn, Session.Client client) + static bool StartGame(S server, Connection conn, Session.Client client, string s) { - return s => + if (!client.IsAdmin) { - var parts = s.Split(' '); - var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); - - // Only the host can change other client's info - if (targetClient.Index != client.Index && !client.IsAdmin) - return true; - - // Spectator or map has disabled color changes - if (targetClient.Slot == null || server.LobbyInfo.Slots[targetClient.Slot].LockColor) - return true; - - // Validate if color is allowed and get an alternative it isn't - var newColor = FieldLoader.GetValue("(value)", parts[1]); - targetClient.Color = SanitizePlayerColor(server, newColor, targetClient.Index, conn); + server.SendOrderTo(conn, "Message", "Only the host can start the game."); + return true; + } - // Only update player's preferred color if new color is valid - if (newColor == targetClient.Color) - targetClient.PreferredColor = targetClient.Color; + if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && + server.LobbyInfo.ClientInSlot(sl.Key) == null)) + { + server.SendOrderTo(conn, "Message", "Unable to start the game until required slots are full."); + return true; + } - server.SyncLobbyClients(); + if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && server.LobbyInfo.NonBotPlayers.Count() < 2) + { + server.SendOrderTo(conn, "Message", server.TwoHumansRequiredText); return true; - }; + } + + server.StartGame(); + return true; } - Func Spawn(S server, Connection conn, Session.Client client) + static bool Slot(S server, Connection conn, Session.Client client, string s) { - return s => + if (!server.LobbyInfo.Slots.ContainsKey(s)) { - var parts = s.Split(' '); - var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); + Log.Write("server", "Invalid slot: {0}", s); + return false; + } - // Only the host can change other client's info - if (targetClient.Index != client.Index && !client.IsAdmin) - return true; + var slot = server.LobbyInfo.Slots[s]; - // Spectators don't need a spawnpoint - if (targetClient.Slot == null) - return true; + if (slot.Closed || server.LobbyInfo.ClientInSlot(s) != null) + return false; - // Map has disabled spawn changes - if (server.LobbyInfo.Slots[targetClient.Slot].LockSpawn) - return true; + // If the previous slot had a locked spawn then we must not carry that to the new slot + var oldSlot = client.Slot != null ? server.LobbyInfo.Slots[client.Slot] : null; + if (oldSlot != null && oldSlot.LockSpawn) + client.SpawnPoint = 0; - int spawnPoint; - if (!Exts.TryParseIntegerInvariant(parts[1], out spawnPoint) - || spawnPoint < 0 || spawnPoint > server.Map.SpawnPoints.Length) - { - Log.Write("server", "Invalid spawn point: {0}", parts[1]); - return true; - } + client.Slot = s; + S.SyncClientToPlayerReference(client, server.Map.Players.Players[s]); - if (server.LobbyInfo.Clients.Where(cc => cc != client).Any(cc => (cc.SpawnPoint == spawnPoint) && (cc.SpawnPoint != 0))) - { - server.SendOrderTo(conn, "Message", "You cannot occupy the same spawn point as another player."); - return true; - } - - // Check if any other slot has locked the requested spawn - if (spawnPoint > 0) - { - var spawnLockedByAnotherSlot = server.LobbyInfo.Slots.Where(ss => ss.Value.LockSpawn).Any(ss => - { - var pr = PlayerReferenceForSlot(server, ss.Value); - return pr != null && pr.Spawn == spawnPoint; - }); + if (!slot.LockColor) + client.PreferredColor = client.Color = SanitizePlayerColor(server, client.Color, client.Index, conn); - if (spawnLockedByAnotherSlot) - { - server.SendOrderTo(conn, "Message", "The spawn point is locked to another player slot."); - return true; - } - } + server.SyncLobbyClients(); + CheckAutoStart(server); - targetClient.SpawnPoint = spawnPoint; - server.SyncLobbyClients(); - return true; - }; + return true; } - static Func Team(S server, Session.Client client) + static bool AllowSpectators(S server, Connection conn, Session.Client client, string s) { - return s => + if (bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllowSpectators)) { - var parts = s.Split(' '); - var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); - - // Only the host can change other client's info - if (targetClient.Index != client.Index && !client.IsAdmin) - return true; - - // Map has disabled team changes - if (server.LobbyInfo.Slots[targetClient.Slot].LockTeam) - return true; - - int team; - if (!Exts.TryParseIntegerInvariant(parts[1], out team)) - { - Log.Write("server", "Invalid team: {0}", s); - return false; - } - - targetClient.Team = team; - server.SyncLobbyClients(); + server.SyncLobbyGlobalSettings(); return true; - }; - } - - static Func Faction(S server, Connection conn, Session.Client client) - { - return s => + } + else { - var parts = s.Split(' '); - var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); - - // Only the host can change other client's info - if (targetClient.Index != client.Index && !client.IsAdmin) - return true; - - // Map has disabled faction changes - if (server.LobbyInfo.Slots[targetClient.Slot].LockFaction) - return true; - - var factions = server.Map.Rules.Actors["world"].TraitInfos() - .Where(f => f.Selectable).Select(f => f.InternalName); - - if (!factions.Contains(parts[1])) - { - server.SendOrderTo(conn, "Message", "Invalid faction selected: {0}".F(parts[1])); - server.SendOrderTo(conn, "Message", "Supported values: {0}".F(factions.JoinWith(", "))); - return true; - } - - targetClient.Faction = parts[1]; - server.SyncLobbyClients(); + server.SendOrderTo(conn, "Message", "Malformed allow_spectate command"); return true; - }; + } } - static Func Name(S server, Connection conn, Session.Client client) + static bool Specate(S server, Connection conn, Session.Client client, string s) { - return s => + if (server.LobbyInfo.GlobalSettings.AllowSpectators || client.IsAdmin) { - var sanitizedName = Settings.SanitizedPlayerName(s); - if (sanitizedName == client.Name) - return true; - - Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, sanitizedName); - server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName)); - client.Name = sanitizedName; + client.Slot = null; + client.SpawnPoint = 0; + client.Team = 0; + client.Color = HSLColor.FromRGB(255, 255, 255); server.SyncLobbyClients(); + CheckAutoStart(server); return true; - }; + } + else + return false; } - static Func MakeAdmin(S server, Connection conn, Session.Client client) + static bool SlotClose(S server, Connection conn, Session.Client client, string s) { - return s => + if (!ValidateSlotCommand(server, conn, client, s, true)) + return false; + + // kick any player that's in the slot + var occupant = server.LobbyInfo.ClientInSlot(s); + if (occupant != null) { - if (!client.IsAdmin) + if (occupant.Bot != null) { - server.SendOrderTo(conn, "Message", "Only the host can promote players to admin."); - return true; + server.LobbyInfo.Clients.Remove(occupant); + server.SyncLobbyClients(); + var ping = server.LobbyInfo.PingFromClient(occupant); + if (ping != null) + { + server.LobbyInfo.ClientPings.Remove(ping); + server.SyncClientPing(); + } } - - int newAdminId; - Exts.TryParseIntegerInvariant(s, out newAdminId); - var newAdminConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == newAdminId); - - if (newAdminConn == null) + else { - server.SendOrderTo(conn, "Message", "No-one in that slot."); - return true; + var occupantConn = server.Conns.FirstOrDefault(c => c.PlayerIndex == occupant.Index); + if (occupantConn != null) + { + server.SendOrderTo(occupantConn, "ServerError", "Your slot was closed by the host."); + server.DropClient(occupantConn); + } } + } - var newAdminClient = server.GetClient(newAdminConn); - client.IsAdmin = false; - newAdminClient.IsAdmin = true; - server.SendMessage("{0} is now the admin.".F(newAdminClient.Name)); - Log.Write("server", "{0} is now the admin.".F(newAdminClient.Name)); - server.SyncLobbyClients(); - return true; - }; + server.LobbyInfo.Slots[s].Closed = true; + server.SyncLobbySlots(); + + return true; } - static Func Kick(S server, Connection conn, Session.Client client) + static bool SlotOpen(S server, Connection conn, Session.Client client, string s) { - return s => + if (!ValidateSlotCommand(server, conn, client, s, true)) + return false; + + var slot = server.LobbyInfo.Slots[s]; + slot.Closed = false; + server.SyncLobbySlots(); + + // Slot may have a bot in it + var occupant = server.LobbyInfo.ClientInSlot(s); + if (occupant != null && occupant.Bot != null) { - if (!client.IsAdmin) + server.LobbyInfo.Clients.Remove(occupant); + var ping = server.LobbyInfo.PingFromClient(occupant); + if (ping != null) { - server.SendOrderTo(conn, "Message", "Only the host can kick players."); - return true; + server.LobbyInfo.ClientPings.Remove(ping); + server.SyncClientPing(); } + } - var split = s.Split(' '); - if (split.Length < 2) - { - server.SendOrderTo(conn, "Message", "Malformed kick command"); - return true; - } + server.SyncLobbyClients(); - int kickClientID; - Exts.TryParseIntegerInvariant(split[0], out kickClientID); + return true; + } - var kickConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == kickClientID); - if (kickConn == null) - { - server.SendOrderTo(conn, "Message", "No-one in that slot."); - return true; - } + static bool SlotBot(S server, Connection conn, Session.Client client, string s) + { + var parts = s.Split(' '); - var kickClient = server.GetClient(kickConn); + if (parts.Length < 3) + { + server.SendOrderTo(conn, "Message", "Malformed slot_bot command"); + return true; + } - Log.Write("server", "Kicking client {0}.", kickClientID); - server.SendMessage("{0} kicked {1} from the server.".F(client.Name, kickClient.Name)); - server.SendOrderTo(kickConn, "ServerError", "You have been kicked from the server."); - server.DropClient(kickConn); + if (!ValidateSlotCommand(server, conn, client, parts[0], true)) + return false; - bool tempBan; - bool.TryParse(split[1], out tempBan); + var slot = server.LobbyInfo.Slots[parts[0]]; + var bot = server.LobbyInfo.ClientInSlot(parts[0]); + int controllerClientIndex; + if (!Exts.TryParseIntegerInvariant(parts[1], out controllerClientIndex)) + { + Log.Write("server", "Invalid bot controller client index: {0}", parts[1]); + return false; + } - if (tempBan) - { - Log.Write("server", "Temporarily banning client {0} ({1}).", kickClientID, kickClient.IpAddress); - server.SendMessage("{0} temporarily banned {1} from the server.".F(client.Name, kickClient.Name)); - server.TempBans.Add(kickClient.IpAddress); - } + // Invalid slot + if (bot != null && bot.Bot == null) + { + server.SendOrderTo(conn, "Message", "Can't add bots to a slot with another client."); + return true; + } - server.SyncLobbyClients(); - server.SyncLobbySlots(); + var botType = parts[2]; + var botInfo = server.Map.Rules.Actors["player"].TraitInfos() + .FirstOrDefault(b => b.Type == botType); + if (botInfo == null) + { + server.SendOrderTo(conn, "Message", "Invalid bot type."); return true; - }; - } + } - static Func AssignTeams(S server, Connection conn, Session.Client client) - { - return s => + slot.Closed = false; + if (bot == null) { - if (!client.IsAdmin) - { - server.SendOrderTo(conn, "Message", "Only the host can set that option."); - return true; - } + // Create a new bot + bot = new Session.Client() + { + Index = server.ChooseFreePlayerIndex(), + Name = botInfo.Name, + Bot = botType, + Slot = parts[0], + Faction = "Random", + SpawnPoint = 0, + Team = 0, + State = Session.ClientState.NotReady, + BotControllerClientIndex = controllerClientIndex + }; - int teamCount; - if (!Exts.TryParseIntegerInvariant(s, out teamCount)) - { - server.SendOrderTo(conn, "Message", "Number of teams could not be parsed: {0}".F(s)); - return true; - } + // Pick a random color for the bot + var validator = server.ModData.Manifest.Get(); + var tileset = server.Map.Rules.TileSet; + var terrainColors = tileset.TerrainInfo.Where(ti => ti.RestrictPlayerColor).Select(ti => ti.Color); + var playerColors = server.LobbyInfo.Clients.Select(c => c.Color.RGB) + .Concat(server.Map.Players.Players.Values.Select(p => p.Color.RGB)); + bot.Color = bot.PreferredColor = validator.RandomPresetColor(server.Random, terrainColors, playerColors); - var maxTeams = (server.LobbyInfo.Clients.Count(c => c.Slot != null) + 1) / 2; - teamCount = teamCount.Clamp(0, maxTeams); - var clients = server.LobbyInfo.Slots - .Select(slot => server.LobbyInfo.ClientInSlot(slot.Key)) - .Where(c => c != null && !server.LobbyInfo.Slots[c.Slot].LockTeam); + server.LobbyInfo.Clients.Add(bot); + } + else + { + // Change the type of the existing bot + bot.Name = botInfo.Name; + bot.Bot = botType; + } - var assigned = 0; - var clientCount = clients.Count(); - foreach (var player in clients) - { - // Free for all - if (teamCount == 0) - player.Team = 0; - - // Humans vs Bots - else if (teamCount == 1) - player.Team = player.Bot == null ? 1 : 2; - else - player.Team = assigned++ * teamCount / clientCount + 1; - } + S.SyncClientToPlayerReference(bot, server.Map.Players.Players[parts[0]]); + server.SyncLobbyClients(); + server.SyncLobbySlots(); - server.SyncLobbyClients(); - return true; - }; + return true; } - static Func Option(S server, Connection conn, Session.Client client) + static bool Map(S server, Connection conn, Session.Client client, string s) { - return s => + if (!client.IsAdmin) { - if (!client.IsAdmin) - { - server.SendOrderTo(conn, "Message", "Only the host can change the configuration."); - return true; + server.SendOrderTo(conn, "Message", "Only the host can change the map."); + return true; + } + + var lastMap = server.LobbyInfo.GlobalSettings.Map; + Action selectMap = map => + { + // Make sure the map hasn't changed in the meantime + if (server.LobbyInfo.GlobalSettings.Map != lastMap) + return; + + server.LobbyInfo.GlobalSettings.Map = map.Uid; + + var oldSlots = server.LobbyInfo.Slots.Keys.ToArray(); + server.Map = server.ModData.MapCache[server.LobbyInfo.GlobalSettings.Map]; + + server.LobbyInfo.Slots = server.Map.Players.Players + .Select(p => MakeSlotFromPlayerReference(p.Value)) + .Where(ss => ss != null) + .ToDictionary(ss => ss.PlayerReference, ss => ss); + + LoadMapSettings(server, server.LobbyInfo.GlobalSettings, server.Map.Rules); + + // Reset client states + foreach (var c in server.LobbyInfo.Clients) + c.State = Session.ClientState.Invalid; + + // Reassign players into new slots based on their old slots: + // - Observers remain as observers + // - Players who now lack a slot are made observers + // - Bots who now lack a slot are dropped + // - Bots who are not defined in the map rules are dropped + var botTypes = server.Map.Rules.Actors["player"].TraitInfos().Select(t => t.Type); + var slots = server.LobbyInfo.Slots.Keys.ToArray(); + var i = 0; + foreach (var os in oldSlots) + { + var c = server.LobbyInfo.ClientInSlot(os); + if (c == null) + continue; + + c.SpawnPoint = 0; + c.Slot = i < slots.Length ? slots[i++] : null; + if (c.Slot != null) + { + // Remove Bot from slot if slot forbids bots + if (c.Bot != null && (!server.Map.Players.Players[c.Slot].AllowBots || !botTypes.Contains(c.Bot))) + server.LobbyInfo.Clients.Remove(c); + S.SyncClientToPlayerReference(c, server.Map.Players.Players[c.Slot]); + } + else if (c.Bot != null) + server.LobbyInfo.Clients.Remove(c); } - var allOptions = server.Map.Rules.Actors["player"].TraitInfos() - .Concat(server.Map.Rules.Actors["world"].TraitInfos()) - .SelectMany(t => t.LobbyOptions(server.Map.Rules)); + // Validate if color is allowed and get an alternative if it isn't + foreach (var c in server.LobbyInfo.Clients) + if (c.Slot != null && !server.LobbyInfo.Slots[c.Slot].LockColor) + c.Color = c.PreferredColor = SanitizePlayerColor(server, c.Color, c.Index, conn); - // Overwrite keys with duplicate ids - var options = new Dictionary(); - foreach (var o in allOptions) - options[o.Id] = o; + server.SyncLobbyInfo(); - var split = s.Split(' '); - LobbyOption option; - if (split.Length < 2 || !options.TryGetValue(split[0], out option) || - !option.Values.ContainsKey(split[1])) - { - server.SendOrderTo(conn, "Message", "Invalid configuration command."); - return true; - } + server.SendMessage("{0} changed the map to {1}.".F(client.Name, server.Map.Title)); - if (option.IsLocked) - { - server.SendOrderTo(conn, "Message", "{0} cannot be changed.".F(option.Name)); - return true; - } + if (server.Map.DefinesUnsafeCustomRules) + server.SendMessage("This map contains custom rules. Game experience may change."); - var oo = server.LobbyInfo.GlobalSettings.LobbyOptions[option.Id]; - if (oo.Value == split[1]) - return true; + if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer) + server.SendMessage(server.TwoHumansRequiredText); + else if (server.Map.Players.Players.Where(p => p.Value.Playable).All(p => !p.Value.AllowBots)) + server.SendMessage("Bots have been disabled on this map."); - oo.Value = oo.PreferredValue = split[1]; + var briefing = MissionBriefingOrDefault(server); + if (briefing != null) + server.SendMessage(briefing); + }; - if (option.Id == "gamespeed") - { - var speed = server.ModData.Manifest.Get().Speeds[oo.Value]; - server.LobbyInfo.GlobalSettings.Timestep = speed.Timestep; - server.LobbyInfo.GlobalSettings.OrderLatency = speed.OrderLatency; - } + Action queryFailed = () => + server.SendOrderTo(conn, "Message", "Map was not found on server."); - server.SyncLobbyGlobalSettings(); - server.SendMessage(option.ValueChangedMessage(client.Name, split[1])); + var m = server.ModData.MapCache[s]; + if (m.Status == MapStatus.Available || m.Status == MapStatus.DownloadAvailable) + selectMap(m); + else if (server.Settings.QueryMapRepository) + { + server.SendOrderTo(conn, "Message", "Searching for map on the Resource Center..."); + var mapRepository = server.ModData.Manifest.Get().MapRepository; + server.ModData.MapCache.QueryRemoteMapDetails(mapRepository, new[] { s }, selectMap, queryFailed); + } + else + queryFailed(); - return true; - }; + return true; } - static Func Map(S server, Connection conn, Session.Client client) + static bool Option(S server, Connection conn, Session.Client client, string s) { - return s => + if (!client.IsAdmin) { - if (!client.IsAdmin) - { - server.SendOrderTo(conn, "Message", "Only the host can change the map."); - return true; - } + server.SendOrderTo(conn, "Message", "Only the host can change the configuration."); + return true; + } - var lastMap = server.LobbyInfo.GlobalSettings.Map; - Action selectMap = map => - { - // Make sure the map hasn't changed in the meantime - if (server.LobbyInfo.GlobalSettings.Map != lastMap) - return; - - server.LobbyInfo.GlobalSettings.Map = map.Uid; - - var oldSlots = server.LobbyInfo.Slots.Keys.ToArray(); - server.Map = server.ModData.MapCache[server.LobbyInfo.GlobalSettings.Map]; - - server.LobbyInfo.Slots = server.Map.Players.Players - .Select(p => MakeSlotFromPlayerReference(p.Value)) - .Where(ss => ss != null) - .ToDictionary(ss => ss.PlayerReference, ss => ss); - - LoadMapSettings(server, server.LobbyInfo.GlobalSettings, server.Map.Rules); - - // Reset client states - foreach (var c in server.LobbyInfo.Clients) - c.State = Session.ClientState.Invalid; - - // Reassign players into new slots based on their old slots: - // - Observers remain as observers - // - Players who now lack a slot are made observers - // - Bots who now lack a slot are dropped - // - Bots who are not defined in the map rules are dropped - var botTypes = server.Map.Rules.Actors["player"].TraitInfos().Select(t => t.Type); - var slots = server.LobbyInfo.Slots.Keys.ToArray(); - var i = 0; - foreach (var os in oldSlots) - { - var c = server.LobbyInfo.ClientInSlot(os); - if (c == null) - continue; - - c.SpawnPoint = 0; - c.Slot = i < slots.Length ? slots[i++] : null; - if (c.Slot != null) - { - // Remove Bot from slot if slot forbids bots - if (c.Bot != null && (!server.Map.Players.Players[c.Slot].AllowBots || !botTypes.Contains(c.Bot))) - server.LobbyInfo.Clients.Remove(c); - S.SyncClientToPlayerReference(c, server.Map.Players.Players[c.Slot]); - } - else if (c.Bot != null) - server.LobbyInfo.Clients.Remove(c); - } + var allOptions = server.Map.Rules.Actors["player"].TraitInfos() + .Concat(server.Map.Rules.Actors["world"].TraitInfos()) + .SelectMany(t => t.LobbyOptions(server.Map.Rules)); - // Validate if color is allowed and get an alternative if it isn't - foreach (var c in server.LobbyInfo.Clients) - if (c.Slot != null && !server.LobbyInfo.Slots[c.Slot].LockColor) - c.Color = c.PreferredColor = SanitizePlayerColor(server, c.Color, c.Index, conn); + // Overwrite keys with duplicate ids + var options = new Dictionary(); + foreach (var o in allOptions) + options[o.Id] = o; - server.SyncLobbyInfo(); + var split = s.Split(' '); + LobbyOption option; + if (split.Length < 2 || !options.TryGetValue(split[0], out option) || + !option.Values.ContainsKey(split[1])) + { + server.SendOrderTo(conn, "Message", "Invalid configuration command."); + return true; + } - server.SendMessage("{0} changed the map to {1}.".F(client.Name, server.Map.Title)); + if (option.IsLocked) + { + server.SendOrderTo(conn, "Message", "{0} cannot be changed.".F(option.Name)); + return true; + } - if (server.Map.DefinesUnsafeCustomRules) - server.SendMessage("This map contains custom rules. Game experience may change."); + var oo = server.LobbyInfo.GlobalSettings.LobbyOptions[option.Id]; + if (oo.Value == split[1]) + return true; - if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer) - server.SendMessage(server.TwoHumansRequiredText); - else if (server.Map.Players.Players.Where(p => p.Value.Playable).All(p => !p.Value.AllowBots)) - server.SendMessage("Bots have been disabled on this map."); + oo.Value = oo.PreferredValue = split[1]; - var briefing = MissionBriefingOrDefault(server); - if (briefing != null) - server.SendMessage(briefing); - }; + if (option.Id == "gamespeed") + { + var speed = server.ModData.Manifest.Get().Speeds[oo.Value]; + server.LobbyInfo.GlobalSettings.Timestep = speed.Timestep; + server.LobbyInfo.GlobalSettings.OrderLatency = speed.OrderLatency; + } - Action queryFailed = () => - server.SendOrderTo(conn, "Message", "Map was not found on server."); + server.SyncLobbyGlobalSettings(); + server.SendMessage(option.ValueChangedMessage(client.Name, split[1])); - var m = server.ModData.MapCache[s]; - if (m.Status == MapStatus.Available || m.Status == MapStatus.DownloadAvailable) - selectMap(m); - else if (server.Settings.QueryMapRepository) - { - server.SendOrderTo(conn, "Message", "Searching for map on the Resource Center..."); - var mapRepository = server.ModData.Manifest.Get().MapRepository; - server.ModData.MapCache.QueryRemoteMapDetails(mapRepository, new[] { s }, selectMap, queryFailed); - } - else - queryFailed(); + return true; + } + static bool AssignTeams(S server, Connection conn, Session.Client client, string s) + { + if (!client.IsAdmin) + { + server.SendOrderTo(conn, "Message", "Only the host can set that option."); return true; - }; + } + + int teamCount; + if (!Exts.TryParseIntegerInvariant(s, out teamCount)) + { + server.SendOrderTo(conn, "Message", "Number of teams could not be parsed: {0}".F(s)); + return true; + } + + var maxTeams = (server.LobbyInfo.Clients.Count(c => c.Slot != null) + 1) / 2; + teamCount = teamCount.Clamp(0, maxTeams); + var clients = server.LobbyInfo.Slots + .Select(slot => server.LobbyInfo.ClientInSlot(slot.Key)) + .Where(c => c != null && !server.LobbyInfo.Slots[c.Slot].LockTeam); + + var assigned = 0; + var clientCount = clients.Count(); + foreach (var player in clients) + { + // Free for all + if (teamCount == 0) + player.Team = 0; + + // Humans vs Bots + else if (teamCount == 1) + player.Team = player.Bot == null ? 1 : 2; + else + player.Team = assigned++ * teamCount / clientCount + 1; + } + + server.SyncLobbyClients(); + + return true; } - static Func SlotBot(S server, Connection conn, Session.Client client) + static bool Kick(S server, Connection conn, Session.Client client, string s) { - return s => + if (!client.IsAdmin) { - var parts = s.Split(' '); + server.SendOrderTo(conn, "Message", "Only the host can kick players."); + return true; + } - if (parts.Length < 3) - { - server.SendOrderTo(conn, "Message", "Malformed slot_bot command"); - return true; - } + var split = s.Split(' '); + if (split.Length < 2) + { + server.SendOrderTo(conn, "Message", "Malformed kick command"); + return true; + } - if (!ValidateSlotCommand(server, conn, client, parts[0], true)) - return false; + int kickClientID; + Exts.TryParseIntegerInvariant(split[0], out kickClientID); - var slot = server.LobbyInfo.Slots[parts[0]]; - var bot = server.LobbyInfo.ClientInSlot(parts[0]); - int controllerClientIndex; - if (!Exts.TryParseIntegerInvariant(parts[1], out controllerClientIndex)) - { - Log.Write("server", "Invalid bot controller client index: {0}", parts[1]); - return false; - } + var kickConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == kickClientID); + if (kickConn == null) + { + server.SendOrderTo(conn, "Message", "No-one in that slot."); + return true; + } - // Invalid slot - if (bot != null && bot.Bot == null) - { - server.SendOrderTo(conn, "Message", "Can't add bots to a slot with another client."); - return true; - } + var kickClient = server.GetClient(kickConn); - var botType = parts[2]; - var botInfo = server.Map.Rules.Actors["player"].TraitInfos() - .FirstOrDefault(b => b.Type == botType); + Log.Write("server", "Kicking client {0}.", kickClientID); + server.SendMessage("{0} kicked {1} from the server.".F(client.Name, kickClient.Name)); + server.SendOrderTo(kickConn, "ServerError", "You have been kicked from the server."); + server.DropClient(kickConn); - if (botInfo == null) - { - server.SendOrderTo(conn, "Message", "Invalid bot type."); - return true; - } + bool tempBan; + bool.TryParse(split[1], out tempBan); - slot.Closed = false; - if (bot == null) - { - // Create a new bot - bot = new Session.Client() - { - Index = server.ChooseFreePlayerIndex(), - Name = botInfo.Name, - Bot = botType, - Slot = parts[0], - Faction = "Random", - SpawnPoint = 0, - Team = 0, - State = Session.ClientState.NotReady, - BotControllerClientIndex = controllerClientIndex - }; - - // Pick a random color for the bot - var validator = server.ModData.Manifest.Get(); - var tileset = server.Map.Rules.TileSet; - var terrainColors = tileset.TerrainInfo.Where(ti => ti.RestrictPlayerColor).Select(ti => ti.Color); - var playerColors = server.LobbyInfo.Clients.Select(c => c.Color.RGB) - .Concat(server.Map.Players.Players.Values.Select(p => p.Color.RGB)); - bot.Color = bot.PreferredColor = validator.RandomPresetColor(server.Random, terrainColors, playerColors); - - server.LobbyInfo.Clients.Add(bot); - } - else - { - // Change the type of the existing bot - bot.Name = botInfo.Name; - bot.Bot = botType; - } + if (tempBan) + { + Log.Write("server", "Temporarily banning client {0} ({1}).", kickClientID, kickClient.IpAddress); + server.SendMessage("{0} temporarily banned {1} from the server.".F(client.Name, kickClient.Name)); + server.TempBans.Add(kickClient.IpAddress); + } - S.SyncClientToPlayerReference(bot, server.Map.Players.Players[parts[0]]); - server.SyncLobbyClients(); - server.SyncLobbySlots(); - return true; - }; + server.SyncLobbyClients(); + server.SyncLobbySlots(); + + return true; } - static Func SlotOpen(S server, Connection conn, Session.Client client) + static bool MakeAdmin(S server, Connection conn, Session.Client client, string s) { - return s => + if (!client.IsAdmin) { - if (!ValidateSlotCommand(server, conn, client, s, true)) - return false; + server.SendOrderTo(conn, "Message", "Only the host can promote players to admin."); + return true; + } - var slot = server.LobbyInfo.Slots[s]; - slot.Closed = false; - server.SyncLobbySlots(); + int newAdminId; + Exts.TryParseIntegerInvariant(s, out newAdminId); + var newAdminConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == newAdminId); - // Slot may have a bot in it - var occupant = server.LobbyInfo.ClientInSlot(s); - if (occupant != null && occupant.Bot != null) - { - server.LobbyInfo.Clients.Remove(occupant); - var ping = server.LobbyInfo.PingFromClient(occupant); - if (ping != null) - { - server.LobbyInfo.ClientPings.Remove(ping); - server.SyncClientPing(); - } - } - - server.SyncLobbyClients(); + if (newAdminConn == null) + { + server.SendOrderTo(conn, "Message", "No-one in that slot."); return true; - }; + } + + var newAdminClient = server.GetClient(newAdminConn); + client.IsAdmin = false; + newAdminClient.IsAdmin = true; + server.SendMessage("{0} is now the admin.".F(newAdminClient.Name)); + Log.Write("server", "{0} is now the admin.".F(newAdminClient.Name)); + server.SyncLobbyClients(); + + return true; } - static Func SlotClose(S server, Connection conn, Session.Client client) + static bool MakeSpectator(S server, Connection conn, Session.Client client, string s) { - return s => + if (!client.IsAdmin) { - if (!ValidateSlotCommand(server, conn, client, s, true)) - return false; + server.SendOrderTo(conn, "Message", "Only the host can move players to spectators."); + return true; + } - // kick any player that's in the slot - var occupant = server.LobbyInfo.ClientInSlot(s); - if (occupant != null) - { - if (occupant.Bot != null) - { - server.LobbyInfo.Clients.Remove(occupant); - server.SyncLobbyClients(); - var ping = server.LobbyInfo.PingFromClient(occupant); - if (ping != null) - { - server.LobbyInfo.ClientPings.Remove(ping); - server.SyncClientPing(); - } - } - else - { - var occupantConn = server.Conns.FirstOrDefault(c => c.PlayerIndex == occupant.Index); - if (occupantConn != null) - { - server.SendOrderTo(occupantConn, "ServerError", "Your slot was closed by the host."); - server.DropClient(occupantConn); - } - } - } + int targetId; + Exts.TryParseIntegerInvariant(s, out targetId); + var targetConn = server.Conns.SingleOrDefault(c => server.GetClient(c) != null && server.GetClient(c).Index == targetId); - server.LobbyInfo.Slots[s].Closed = true; - server.SyncLobbySlots(); + if (targetConn == null) + { + server.SendOrderTo(conn, "Message", "No-one in that slot."); return true; - }; + } + + var targetClient = server.GetClient(targetConn); + targetClient.Slot = null; + targetClient.SpawnPoint = 0; + targetClient.Team = 0; + targetClient.Color = HSLColor.FromRGB(255, 255, 255); + server.SendMessage("{0} moved {1} to spectators.".F(client.Name, targetClient.Name)); + Log.Write("server", "{0} moved {1} to spectators.".F(client.Name, targetClient.Name)); + server.SyncLobbyClients(); + CheckAutoStart(server); + + return true; } - static Func Specate(S server, Session.Client client) + static bool Name(S server, Connection conn, Session.Client client, string s) { - return s => - { - if (server.LobbyInfo.GlobalSettings.AllowSpectators || client.IsAdmin) - { - client.Slot = null; - client.SpawnPoint = 0; - client.Team = 0; - client.Color = HSLColor.FromRGB(255, 255, 255); - server.SyncLobbyClients(); - CheckAutoStart(server); - return true; - } - else - return false; - }; + var sanitizedName = Settings.SanitizedPlayerName(s); + if (sanitizedName == client.Name) + return true; + + Log.Write("server", "Player@{0} is now known as {1}.", conn.Socket.RemoteEndPoint, sanitizedName); + server.SendMessage("{0} is now known as {1}.".F(client.Name, sanitizedName)); + client.Name = sanitizedName; + server.SyncLobbyClients(); + + return true; } - static Func AllowSpectators(S server, Connection conn) + static bool Faction(S server, Connection conn, Session.Client client, string s) { - return s => + var parts = s.Split(' '); + var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); + + // Only the host can change other client's info + if (targetClient.Index != client.Index && !client.IsAdmin) + return true; + + // Map has disabled faction changes + if (server.LobbyInfo.Slots[targetClient.Slot].LockFaction) + return true; + + var factions = server.Map.Rules.Actors["world"].TraitInfos() + .Where(f => f.Selectable).Select(f => f.InternalName); + + if (!factions.Contains(parts[1])) { - if (bool.TryParse(s, out server.LobbyInfo.GlobalSettings.AllowSpectators)) - { - server.SyncLobbyGlobalSettings(); - return true; - } - else - { - server.SendOrderTo(conn, "Message", "Malformed allow_spectate command"); - return true; - } - }; + server.SendOrderTo(conn, "Message", "Invalid faction selected: {0}".F(parts[1])); + server.SendOrderTo(conn, "Message", "Supported values: {0}".F(factions.JoinWith(", "))); + return true; + } + + targetClient.Faction = parts[1]; + server.SyncLobbyClients(); + + return true; } - static Func Slot(S server, Connection conn, Session.Client client) + static bool Team(S server, Connection conn, Session.Client client, string s) { - return s => + var parts = s.Split(' '); + var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); + + // Only the host can change other client's info + if (targetClient.Index != client.Index && !client.IsAdmin) + return true; + + // Map has disabled team changes + if (server.LobbyInfo.Slots[targetClient.Slot].LockTeam) + return true; + + int team; + if (!Exts.TryParseIntegerInvariant(parts[1], out team)) { - if (!server.LobbyInfo.Slots.ContainsKey(s)) - { - Log.Write("server", "Invalid slot: {0}", s); - return false; - } + Log.Write("server", "Invalid team: {0}", s); + return false; + } - var slot = server.LobbyInfo.Slots[s]; + targetClient.Team = team; + server.SyncLobbyClients(); - if (slot.Closed || server.LobbyInfo.ClientInSlot(s) != null) - return false; + return true; + } - // If the previous slot had a locked spawn then we must not carry that to the new slot - var oldSlot = client.Slot != null ? server.LobbyInfo.Slots[client.Slot] : null; - if (oldSlot != null && oldSlot.LockSpawn) - client.SpawnPoint = 0; + 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])); - client.Slot = s; - S.SyncClientToPlayerReference(client, server.Map.Players.Players[s]); + // Only the host can change other client's info + if (targetClient.Index != client.Index && !client.IsAdmin) + return true; - if (!slot.LockColor) - client.PreferredColor = client.Color = SanitizePlayerColor(server, client.Color, client.Index, conn); + // Spectators don't need a spawnpoint + if (targetClient.Slot == null) + return true; - server.SyncLobbyClients(); - CheckAutoStart(server); + // Map has disabled spawn changes + if (server.LobbyInfo.Slots[targetClient.Slot].LockSpawn) + return true; + int spawnPoint; + if (!Exts.TryParseIntegerInvariant(parts[1], out spawnPoint) + || spawnPoint < 0 || spawnPoint > server.Map.SpawnPoints.Length) + { + Log.Write("server", "Invalid spawn point: {0}", parts[1]); return true; - }; - } + } - static Func StartGame(S server, Connection conn, Session.Client client) - { - return s => + if (server.LobbyInfo.Clients.Where(cc => cc != client).Any(cc => (cc.SpawnPoint == spawnPoint) && (cc.SpawnPoint != 0))) { - if (!client.IsAdmin) - { - server.SendOrderTo(conn, "Message", "Only the host can start the game."); - return true; - } + server.SendOrderTo(conn, "Message", "You cannot occupy the same spawn point as another player."); + return true; + } - if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && - server.LobbyInfo.ClientInSlot(sl.Key) == null)) + // Check if any other slot has locked the requested spawn + if (spawnPoint > 0) + { + var spawnLockedByAnotherSlot = server.LobbyInfo.Slots.Where(ss => ss.Value.LockSpawn).Any(ss => { - server.SendOrderTo(conn, "Message", "Unable to start the game until required slots are full."); - return true; - } + var pr = PlayerReferenceForSlot(server, ss.Value); + return pr != null && pr.Spawn == spawnPoint; + }); - if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && server.LobbyInfo.NonBotPlayers.Count() < 2) + if (spawnLockedByAnotherSlot) { - server.SendOrderTo(conn, "Message", server.TwoHumansRequiredText); + server.SendOrderTo(conn, "Message", "The spawn point is locked to another player slot."); return true; } + } - server.StartGame(); - return true; - }; + targetClient.SpawnPoint = spawnPoint; + server.SyncLobbyClients(); + + return true; } - static Func State(S server, Connection conn, Session.Client client) + static bool Color(S server, Connection conn, Session.Client client, string s) { - return s => - { - var state = Session.ClientState.Invalid; - if (!Enum.TryParse(s, false, out state)) - { - server.SendOrderTo(conn, "Message", "Malformed state command"); - return true; - } + var parts = s.Split(' '); + var targetClient = server.LobbyInfo.ClientWithIndex(Exts.ParseIntegerInvariant(parts[0])); - client.State = state; + // Only the host can change other client's info + if (targetClient.Index != client.Index && !client.IsAdmin) + return true; - Log.Write("server", "Player @{0} is {1}", - conn.Socket.RemoteEndPoint, client.State); + // Spectator or map has disabled color changes + if (targetClient.Slot == null || server.LobbyInfo.Slots[targetClient.Slot].LockColor) + return true; - server.SyncLobbyClients(); + // Validate if color is allowed and get an alternative it isn't + var newColor = FieldLoader.GetValue("(value)", parts[1]); + targetClient.Color = SanitizePlayerColor(server, newColor, targetClient.Index, conn); - CheckAutoStart(server); + // Only update player's preferred color if new color is valid + if (newColor == targetClient.Color) + targetClient.PreferredColor = targetClient.Color; - return true; - }; + server.SyncLobbyClients(); + + return true; } - public void ServerStarted(S server) + static bool SyncLobby(S server, Connection conn, Session.Client client, string s) { - // Remote maps are not supported for the initial map - var uid = server.LobbyInfo.GlobalSettings.Map; - server.Map = server.ModData.MapCache[uid]; - if (server.Map.Status != MapStatus.Available) - throw new InvalidOperationException("Map {0} not found".F(uid)); + if (!client.IsAdmin) + { + server.SendOrderTo(conn, "Message", "Only the host can set lobby info"); + return true; + } - server.LobbyInfo.Slots = server.Map.Players.Players - .Select(p => MakeSlotFromPlayerReference(p.Value)) - .Where(s => s != null) - .ToDictionary(s => s.PlayerReference, s => s); + var lobbyInfo = Session.Deserialize(s); + if (lobbyInfo == null) + { + server.SendOrderTo(conn, "Message", "Invalid Lobby Info Sent"); + return true; + } - LoadMapSettings(server, server.LobbyInfo.GlobalSettings, server.Map.Rules); + server.LobbyInfo = lobbyInfo; + + server.SyncLobbyInfo(); + + return true; } static Session.Slot MakeSlotFromPlayerReference(PlayerReference pr) @@ -953,6 +885,26 @@ public static void LoadMapSettings(S server, Session.Global gs, Ruleset rules) } } + static void CheckAutoStart(S server) + { + var nonBotPlayers = server.LobbyInfo.NonBotPlayers; + + // Are all players and admin (could be spectating) ready? + if (nonBotPlayers.Any(c => c.State != Session.ClientState.Ready) || + server.LobbyInfo.Clients.First(c => c.IsAdmin).State != Session.ClientState.Ready) + return; + + // Does server have at least 2 human players? + if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && nonBotPlayers.Count() < 2) + return; + + // Are the map conditions satisfied? + if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null)) + return; + + server.StartGame(); + } + static HSLColor SanitizePlayerColor(S server, HSLColor askedColor, int playerIndex, Connection connectionToEcho = null) { var validator = server.ModData.Manifest.Get(); @@ -1012,7 +964,7 @@ void INotifyServerEmpty.ServerEmpty(S 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;