diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 46ca384c3a..daf20e9a46 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -148,12 +148,6 @@ namespace OpenRA.Server throw new InvalidOperationException("Already got 256 players"); } - static int ChooseFreeSlot() - { - return lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null - && !lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index; - } - static void AcceptConnection() { Socket newSocket = null; @@ -189,33 +183,8 @@ namespace OpenRA.Server newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex)); conns.Add(newConn); - var defaults = new GameRules.PlayerSettings(); - - var client = new Session.Client() - { - Index = newConn.PlayerIndex, - Color1 = defaults.Color1, - Color2 = defaults.Color2, - Name = defaults.Name, - Country = "random", - State = Session.ClientState.NotReady, - SpawnPoint = 0, - Team = 0, - Slot = ChooseFreeSlot(), - }; - - var slotData = lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); - if (slotData != null) - SyncClientToPlayerReference(client, Map.Players[slotData.MapPlayer]); - - lobbyInfo.Clients.Add(client); - - Log.Write("server", "Client {0}: Accepted connection from {1}", - newConn.PlayerIndex, newConn.socket.RemoteEndPoint); - - SendChat(newConn, "has joined the game."); - - SyncLobbyInfo(); + foreach (var t in ServerTraits.WithInterface()) + t.ClientJoined(newConn); } catch (Exception e) { DropClient(newConn, e); } } @@ -279,19 +248,6 @@ namespace OpenRA.Server catch (EndOfStreamException) { } catch (NotImplementedException) { } } - - public static void SyncClientToPlayerReference(Session.Client c, PlayerReference pr) - { - if (pr == null) - return; - if (pr.LockColor) - { - c.Color1 = pr.Color; - c.Color2 = pr.Color2; - } - if (pr.LockRace) - c.Country = pr.Race; - } public static void SendChatTo(Connection conn, string text) { @@ -299,12 +255,12 @@ namespace OpenRA.Server new ServerOrder("Chat", text).Serialize()); } - static void SendChat(Connection asConn, string text) + public static void SendChat(Connection asConn, string text) { DispatchOrders(asConn, 0, new ServerOrder("Chat", text).Serialize()); } - static void SendDisconnected(Connection asConn) + public static void SendDisconnected(Connection asConn) { DispatchOrders(asConn, 0, new ServerOrder("Disconnected", "").Serialize()); } diff --git a/OpenRA.Game/ServerTraits/LobbyCommands.cs b/OpenRA.Game/ServerTraits/LobbyCommands.cs index 40ce452952..4bfba3831d 100644 --- a/OpenRA.Game/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Game/ServerTraits/LobbyCommands.cs @@ -16,7 +16,7 @@ using OpenRA.FileFormats; namespace OpenRA.Server.Traits { - public class LobbyCommands : IInterpretCommand, IStartServer + public class LobbyCommands : IInterpretCommand, IStartServer, IClientJoined { public bool InterpretCommand(Connection conn, string cmd) { @@ -72,15 +72,13 @@ namespace OpenRA.Server.Traits { "spectator", s => { - var slotData = Server.lobbyInfo.Slots.Where(ax => ax.Spectator && !Server.lobbyInfo.Clients.Any(l => l.Slot == ax.Index)).FirstOrDefault(); - if (slotData == null) - return true; - - var cl = Server.GetClient(conn); - + var slotData = Server.lobbyInfo.Slots.Where(ax => ax.Spectator && !Server.lobbyInfo.Clients.Any(l => l.Slot == ax.Index)).FirstOrDefault(); + if (slotData == null) + return true; + + var cl = Server.GetClient(conn); cl.Slot = slotData.Index; - - Server.SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null); + SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null); Server.SyncLobbyInfo(); return true; @@ -98,8 +96,7 @@ namespace OpenRA.Server.Traits var cl = Server.GetClient(conn); cl.Slot = slot; - - Server.SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null); + SyncClientToPlayerReference(cl, slotData.MapPlayer != null ? Server.Map.Players[slotData.MapPlayer] : null); Server.SyncLobbyInfo(); return true; @@ -201,8 +198,8 @@ namespace OpenRA.Server.Traits { client.SpawnPoint = 0; var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); - if (slotData != null) - Server.SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]); + if (slotData != null && slotData.MapPlayer != null) + SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]); client.State = Session.ClientState.NotReady; } @@ -266,5 +263,55 @@ namespace OpenRA.Server.Traits Bot = null }); } + + public void ClientJoined(Connection newConn) + { + var defaults = new GameRules.PlayerSettings(); + + var client = new Session.Client() + { + Index = newConn.PlayerIndex, + Color1 = defaults.Color1, + Color2 = defaults.Color2, + Name = defaults.Name, + Country = "random", + State = Session.ClientState.NotReady, + SpawnPoint = 0, + Team = 0, + Slot = ChooseFreeSlot(), + }; + + var slotData = Server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); + if (slotData != null) + SyncClientToPlayerReference(client, Server.Map.Players[slotData.MapPlayer]); + + Server.lobbyInfo.Clients.Add(client); + + Log.Write("server", "Client {0}: Accepted connection from {1}", + newConn.PlayerIndex, newConn.socket.RemoteEndPoint); + + Server.SendChat(newConn, "has joined the game."); + Server.SyncLobbyInfo(); + } + + static int ChooseFreeSlot() + { + return Server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null + && !Server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index; + } + + + public static void SyncClientToPlayerReference(Session.Client c, PlayerReference pr) + { + if (pr == null) + return; + if (pr.LockColor) + { + c.Color1 = pr.Color; + c.Color2 = pr.Color2; + } + if (pr.LockRace) + c.Country = pr.Race; + } } } diff --git a/OpenRA.Game/ServerTraits/TraitInterfaces.cs b/OpenRA.Game/ServerTraits/TraitInterfaces.cs index 5f73f5008e..6a03298d69 100644 --- a/OpenRA.Game/ServerTraits/TraitInterfaces.cs +++ b/OpenRA.Game/ServerTraits/TraitInterfaces.cs @@ -13,7 +13,8 @@ namespace OpenRA.Server.Traits // Returns true if order is handled public interface IInterpretCommand { bool InterpretCommand(Connection conn, string cmd); } public interface IStartServer { void ServerStarted(); } - + public interface IClientJoined { void ClientJoined(Connection conn); } + public class DebugServerTrait : IInterpretCommand { public bool InterpretCommand(Connection conn, string cmd)