Ejemplo n.º 1
0
        internal static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order)
        {
            switch (order.OrderString)
            {
            // Server message
            case "Message":
                Game.AddSystemLine(order.TargetString);
                break;

            // Reports that the target player disconnected
            case "Disconnected":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    client.State = Session.ClientState.Disconnected;
                    var player = world?.FindPlayerByClient(client);
                    if (player != null)
                    {
                        world.OnPlayerDisconnected(player);
                    }
                }

                break;
            }

            case "Chat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client == null)
                {
                    break;
                }

                // Cut chat messages to the hard limit to avoid exploits
                var message = order.TargetString;
                if (message.Length > ChatMessageMaxLength)
                {
                    message = order.TargetString.Substring(0, ChatMessageMaxLength);
                }

                // ExtraData 0 means this is a normal chat order, everything else is team chat
                if (order.ExtraData == 0)
                {
                    var p = world != null?world.FindPlayerByClient(client) : null;

                    var suffix = (p != null && p.WinState == WinState.Lost) ? " (Dead)" : "";
                    suffix = client.IsObserver ? " (Spectator)" : suffix;

                    if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
                    {
                        suffix += " (Ally)";
                    }

                    Game.AddChatLine(client.Name + suffix, client.Color, message);
                    break;
                }

                // We are still in the lobby
                if (world == null)
                {
                    var prefix = order.ExtraData == uint.MaxValue ? "[Spectators] " : "[Team] ";
                    if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
                    {
                        Game.AddChatLine(prefix + client.Name, client.Color, message);
                    }

                    break;
                }

                var player = world.FindPlayerByClient(client);
                var localClientIsObserver = world.IsReplay || (orderManager.LocalClient != null && orderManager.LocalClient.IsObserver) ||
                                            (world.LocalPlayer != null && world.LocalPlayer.WinState != WinState.Undefined);

                // ExtraData gives us the team number, uint.MaxValue means Spectators
                if (order.ExtraData == uint.MaxValue && localClientIsObserver)
                {
                    // Validate before adding the line
                    if (client.IsObserver || (player != null && player.WinState != WinState.Undefined))
                    {
                        Game.AddChatLine("[Spectators] " + client.Name, client.Color, message);
                    }

                    break;
                }

                var valid      = client.Team == order.ExtraData && player != null && player.WinState == WinState.Undefined;
                var isSameTeam = orderManager.LocalClient != null && order.ExtraData == orderManager.LocalClient.Team &&
                                 world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Undefined;

                if (valid && (isSameTeam || world.IsReplay))
                {
                    Game.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message);
                }

                break;
            }

            case "StartGame":
            {
                if (Game.ModData.MapCache[orderManager.LobbyInfo.GlobalSettings.Map].Status != MapStatus.Available)
                {
                    Game.Disconnect();
                    Game.LoadShellMap();

                    // TODO: After adding a startup error dialog, notify the replay load failure.
                    break;
                }

                if (!string.IsNullOrEmpty(order.TargetString))
                {
                    var data = MiniYaml.FromString(order.TargetString);
                    var saveLastOrdersFrame = data.FirstOrDefault(n => n.Key == "SaveLastOrdersFrame");
                    if (saveLastOrdersFrame != null)
                    {
                        orderManager.GameSaveLastFrame =
                            FieldLoader.GetValue <int>("saveLastOrdersFrame", saveLastOrdersFrame.Value.Value);
                    }

                    var saveSyncFrame = data.FirstOrDefault(n => n.Key == "SaveSyncFrame");
                    if (saveSyncFrame != null)
                    {
                        orderManager.GameSaveLastSyncFrame =
                            FieldLoader.GetValue <int>("SaveSyncFrame", saveSyncFrame.Value.Value);
                    }
                }
                else
                {
                    Game.AddSystemLine("The game has started.");
                }

                Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular);
                break;
            }

            case "SaveTraitData":
            {
                var data       = MiniYaml.FromString(order.TargetString)[0];
                var traitIndex = int.Parse(data.Key);

                world?.AddGameSaveTraitData(traitIndex, data.Value);

                break;
            }

            case "GameSaved":
                if (!orderManager.World.IsReplay)
                {
                    Game.AddSystemLine("Game saved");
                }

                foreach (var nsr in orderManager.World.WorldActor.TraitsImplementing <INotifyGameSaved>())
                {
                    nsr.GameSaved(orderManager.World);
                }
                break;

            case "PauseGame":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    var pause = order.TargetString == "Pause";

                    // Prevent injected unpause orders from restarting a finished game
                    if (orderManager.World.PauseStateLocked && !pause)
                    {
                        break;
                    }

                    if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1)
                    {
                        var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name);
                        Game.AddSystemLine(pausetext);
                    }

                    orderManager.World.Paused          = pause;
                    orderManager.World.PredictedPaused = pause;
                }

                break;
            }

            case "HandshakeRequest":
            {
                // Switch to the server's mod if we need and are able to
                var mod     = Game.ModData.Manifest;
                var request = HandshakeRequest.Deserialize(order.TargetString);

                var externalKey = ExternalMod.MakeKey(request.Mod, request.Version);
                if ((request.Mod != mod.Id || request.Version != mod.Metadata.Version) &&
                    Game.ExternalMods.TryGetValue(externalKey, out var external))
                {
                    // The ConnectionFailedLogic will prompt the user to switch mods
                    orderManager.ServerExternalMod = external;
                    orderManager.Connection.Dispose();
                    break;
                }

                Game.Settings.Player.Name = Settings.SanitizedPlayerName(Game.Settings.Player.Name);
                Game.Settings.Save();

                // Otherwise send the handshake with our current settings and let the server reject us
                var info = new Session.Client()
                {
                    Name           = Game.Settings.Player.Name,
                    PreferredColor = Game.Settings.Player.Color,
                    Color          = Game.Settings.Player.Color,
                    Faction        = "Random",
                    SpawnPoint     = 0,
                    Team           = 0,
                    State          = Session.ClientState.Invalid
                };

                var localProfile = Game.LocalPlayerProfile;
                var response     = new HandshakeResponse()
                {
                    Client         = info,
                    Mod            = mod.Id,
                    Version        = mod.Metadata.Version,
                    Password       = orderManager.Password,
                    Fingerprint    = localProfile.Fingerprint,
                    OrdersProtocol = ProtocolVersion.Orders
                };

                if (request.AuthToken != null && response.Fingerprint != null)
                {
                    response.AuthSignature = localProfile.Sign(request.AuthToken);
                }

                orderManager.IssueOrder(new Order("HandshakeResponse", null, false)
                    {
                        Type         = OrderType.Handshake,
                        IsImmediate  = true,
                        TargetString = response.Serialize()
                    });

                break;
            }

            case "ServerError":
            {
                orderManager.ServerError          = order.TargetString;
                orderManager.AuthenticationFailed = false;
                break;
            }

            case "AuthenticationError":
            {
                // The ConnectionFailedLogic will prompt the user for the password
                orderManager.ServerError          = order.TargetString;
                orderManager.AuthenticationFailed = true;
                break;
            }

            case "SyncInfo":
            {
                orderManager.LobbyInfo = Session.Deserialize(order.TargetString);
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbyClients":
            {
                var clients = new List <Session.Client>();
                var nodes   = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "Client")
                    {
                        clients.Add(Session.Client.Deserialize(node.Value));
                    }
                }

                orderManager.LobbyInfo.Clients = clients;
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbySlots":
            {
                var slots = new Dictionary <string, Session.Slot>();
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "Slot")
                    {
                        var slot = Session.Slot.Deserialize(node.Value);
                        slots.Add(slot.PlayerReference, slot);
                    }
                }

                orderManager.LobbyInfo.Slots = slots;
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbyGlobalSettings":
            {
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "GlobalSettings")
                    {
                        orderManager.LobbyInfo.GlobalSettings = Session.Global.Deserialize(node.Value);
                    }
                }

                Game.SyncLobbyInfo();
                break;
            }

            case "SyncClientPings":
            {
                var pings = new List <Session.ClientPing>();
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "ClientPing")
                    {
                        pings.Add(Session.ClientPing.Deserialize(node.Value));
                    }
                }

                orderManager.LobbyInfo.ClientPings = pings;
                break;
            }

            case "Ping":
            {
                orderManager.IssueOrder(Order.FromTargetString("Pong", order.TargetString, true));
                break;
            }

            default:
            {
                if (world == null)
                {
                    break;
                }

                if (order.GroupedActors == null)
                {
                    ResolveOrder(order, world, orderManager, clientId);
                }
                else
                {
                    foreach (var subject in order.GroupedActors)
                    {
                        ResolveOrder(Order.FromGroupedOrder(order, subject), world, orderManager, clientId);
                    }
                }

                break;
            }
            }
        }
Ejemplo n.º 2
0
 public static HandshakeRequest Deserialize(string data)
 {
     var handshake = new HandshakeRequest();
     FieldLoader.Load(handshake, MiniYaml.FromString(data).First().Value);
     return handshake;
 }
Ejemplo n.º 3
0
        public static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order)
        {
            if (world != null)
            {
                if (!world.WorldActor.TraitsImplementing <IValidateOrder>().All(vo =>
                                                                                vo.OrderValidation(orderManager, world, clientId, order)))
                {
                    return;
                }
            }

            switch (order.OrderString)
            {
            case "Chat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    var player = world != null?world.FindPlayerByClient(client) : null;

                    var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : "";
                    suffix = client.IsObserver ? " (Spectator)" : suffix;
                    Game.AddChatLine(client.Color.RGB, client.Name + suffix, order.TargetString);
                }
                else
                {
                    Game.AddChatLine(Color.White, "(player {0})".F(clientId), order.TargetString);
                }
                break;
            }

            case "Message":                     // Server message
                Game.AddChatLine(Color.White, "Server", order.TargetString);
                break;

            case "Disconnected":                     /* reports that the target player disconnected */
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    client.State = Session.ClientState.Disconnected;
                }
                break;
            }

            case "TeamChat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);

                if (client != null)
                {
                    if (world == null)
                    {
                        if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
                        {
                            Game.AddChatLine(client.Color.RGB, client.Name + " (Team)",
                                             order.TargetString);
                        }
                    }
                    else
                    {
                        var player = world.FindPlayerByClient(client);
                        if (player == null)
                        {
                            return;
                        }

                        if (world.LocalPlayer != null && player.Stances[world.LocalPlayer] == Stance.Ally || player.WinState == WinState.Lost)
                        {
                            var suffix = player.WinState == WinState.Lost ? " (Dead)" : " (Team)";
                            Game.AddChatLine(client.Color.RGB, client.Name + suffix, order.TargetString);
                        }
                    }
                }
                break;
            }

            case "StartGame":
            {
                Game.AddChatLine(Color.White, "Server", "The game has started.");
                Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, false);
                break;
            }

            case "PauseGame":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    var pause = order.TargetString == "Pause";
                    if (orderManager.world.Paused != pause && !world.LobbyInfo.IsSinglePlayer)
                    {
                        var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name);
                        Game.AddChatLine(Color.White, "", pausetext);
                    }

                    orderManager.world.Paused          = pause;
                    orderManager.world.PredictedPaused = pause;
                }
                break;
            }

            case "HandshakeRequest":
            {
                var request   = HandshakeRequest.Deserialize(order.TargetString);
                var localMods = orderManager.LobbyInfo.GlobalSettings.Mods.Select(m => "{0}@{1}".F(m, Mod.AllMods[m].Version)).ToArray();

                // Check if mods match
                if (localMods.FirstOrDefault().ToString().Split('@')[0] != request.Mods.FirstOrDefault().ToString().Split('@')[0])
                {
                    throw new InvalidOperationException("Server's mod ({0}) and yours ({1}) don't match".F(localMods.FirstOrDefault().ToString().Split('@')[0], request.Mods.FirstOrDefault().ToString().Split('@')[0]));
                }
                // Check that the map exists on the client
                if (!Game.modData.AvailableMaps.ContainsKey(request.Map))
                {
                    if (Game.Settings.Game.AllowDownloading)
                    {
                        Game.DownloadMap(request.Map);
                    }
                    else
                    {
                        throw new InvalidOperationException("Missing map {0}".F(request.Map));
                    }
                }

                var info = new Session.Client()
                {
                    Name           = Game.Settings.Player.Name,
                    PreferredColor = Game.Settings.Player.Color,
                    Color          = Game.Settings.Player.Color,
                    Country        = "random",
                    SpawnPoint     = 0,
                    Team           = 0,
                    State          = Session.ClientState.NotReady
                };

                var response = new HandshakeResponse()
                {
                    Client   = info,
                    Mods     = localMods,
                    Password = "******"
                };

                orderManager.IssueOrder(Order.HandshakeResponse(response.Serialize()));
                break;
            }

            case "ServerError":
                orderManager.ServerError = order.TargetString;
                break;

            case "SyncInfo":
            {
                orderManager.LobbyInfo = Session.Deserialize(order.TargetString);

                if (orderManager.FramesAhead != orderManager.LobbyInfo.GlobalSettings.OrderLatency &&
                    !orderManager.GameStarted)
                {
                    orderManager.FramesAhead = orderManager.LobbyInfo.GlobalSettings.OrderLatency;
                    Game.Debug("Order lag is now {0} frames.".F(orderManager.LobbyInfo.GlobalSettings.OrderLatency));
                }
                Game.SyncLobbyInfo();
                break;
            }

            case "SetStance":
            {
                if (!Game.orderManager.LobbyInfo.GlobalSettings.FragileAlliances)
                {
                    return;
                }

                var targetPlayer = order.Player.World.Players.FirstOrDefault(p => p.InternalName == order.TargetString);
                var newStance    = (Stance)order.TargetLocation.X;

                SetPlayerStance(world, order.Player, targetPlayer, newStance);

                Game.Debug("{0} has set diplomatic stance vs {1} to {2}".F(
                               order.Player.PlayerName, targetPlayer.PlayerName, newStance));

                // automatically declare war reciprocally
                if (newStance == Stance.Enemy && targetPlayer.Stances[order.Player] == Stance.Ally)
                {
                    SetPlayerStance(world, targetPlayer, order.Player, newStance);
                    Game.Debug("{0} has reciprocated", targetPlayer.PlayerName);
                }

                break;
            }

            case "Ping":
            {
                orderManager.IssueOrder(Order.Pong(order.TargetString));
                break;
            }

            default:
            {
                if (!order.IsImmediate)
                {
                    var self   = order.Subject;
                    var health = self.TraitOrDefault <Health>();
                    if (health == null || !health.IsDead)
                    {
                        foreach (var t in self.TraitsImplementing <IResolveOrder>())
                        {
                            t.ResolveOrder(self, order);
                        }
                    }
                }
                break;
            }
            }
        }
Ejemplo n.º 4
0
        public static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order)
        {
            if (world != null)
            {
                if (!world.WorldActor.TraitsImplementing <IValidateOrder>().All(vo =>
                                                                                vo.OrderValidation(orderManager, world, clientId, order)))
                {
                    return;
                }
            }

            switch (order.OrderString)
            {
            case "Chat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    var player = world != null?world.FindPlayerByClient(client) : null;

                    var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : "";
                    suffix = client.IsObserver ? " (Spectator)" : suffix;

                    if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
                    {
                        suffix += " (Ally)";
                    }

                    Game.AddChatLine(client.Color.RGB, client.Name + suffix, order.TargetString);
                }
                else
                {
                    Game.AddChatLine(Color.White, "(player {0})".F(clientId), order.TargetString);
                }
                break;
            }

            case "Message":                     // Server message
                Game.AddChatLine(Color.White, "Server", order.TargetString);
                break;

            case "Disconnected":                     /* reports that the target player disconnected */
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    client.State = Session.ClientState.Disconnected;
                }
                break;
            }

            case "TeamChat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);

                if (client != null)
                {
                    if (world == null)
                    {
                        if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
                        {
                            Game.AddChatLine(client.Color.RGB, client.Name + " (Team)",
                                             order.TargetString);
                        }
                    }
                    else
                    {
                        var player = world.FindPlayerByClient(client);
                        if (player == null)
                        {
                            return;
                        }

                        if ((world.LocalPlayer != null && player.Stances[world.LocalPlayer] == Stance.Ally) || player.WinState == WinState.Lost)
                        {
                            var suffix = player.WinState == WinState.Lost ? " (Dead)" : " (Team)";
                            Game.AddChatLine(client.Color.RGB, client.Name + suffix, order.TargetString);
                        }
                    }
                }

                break;
            }

            case "StartGame":
            {
                if (Game.ModData.MapCache[orderManager.LobbyInfo.GlobalSettings.Map].Status != MapStatus.Available)
                {
                    Game.Disconnect();
                    Game.LoadShellMap();

                    // TODO: After adding a startup error dialog, notify the replay load failure.
                    break;
                }

                Game.AddChatLine(Color.White, "Server", "The game has started.");
                Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular);
                break;
            }

            case "PauseGame":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    var pause = order.TargetString == "Pause";
                    if (orderManager.World.Paused != pause && !world.LobbyInfo.IsSinglePlayer)
                    {
                        var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name);
                        Game.AddChatLine(Color.White, "", pausetext);
                    }

                    orderManager.World.Paused          = pause;
                    orderManager.World.PredictedPaused = pause;
                }

                break;
            }

            case "HandshakeRequest":
            {
                // Switch to the server's mod if we need and are able to
                var mod     = Game.ModData.Manifest.Mod;
                var request = HandshakeRequest.Deserialize(order.TargetString);

                ModMetadata serverMod;
                if (request.Mod != mod.Id &&
                    ModMetadata.AllMods.TryGetValue(request.Mod, out serverMod) &&
                    serverMod.Version == request.Version)
                {
                    var replay        = orderManager.Connection as ReplayConnection;
                    var launchCommand = replay != null ?
                                        "Launch.Replay=" + replay.Filename :
                                        "Launch.Connect=" + orderManager.Host + ":" + orderManager.Port;

                    Game.ModData.LoadScreen.Display();
                    Game.InitializeMod(request.Mod, new Arguments(launchCommand));

                    break;
                }

                // Otherwise send the handshake with our current settings and let the server reject us
                var info = new Session.Client()
                {
                    Name           = Game.Settings.Player.Name,
                    PreferredColor = Game.Settings.Player.Color,
                    Color          = Game.Settings.Player.Color,
                    Race           = "Random",
                    SpawnPoint     = 0,
                    Team           = 0,
                    State          = Session.ClientState.Invalid
                };

                var response = new HandshakeResponse()
                {
                    Client   = info,
                    Mod      = mod.Id,
                    Version  = mod.Version,
                    Password = orderManager.Password
                };

                orderManager.IssueOrder(Order.HandshakeResponse(response.Serialize()));
                break;
            }

            case "ServerError":
            {
                orderManager.ServerError          = order.TargetString;
                orderManager.AuthenticationFailed = false;
                break;
            }

            case "AuthenticationError":
            {
                orderManager.ServerError          = order.TargetString;
                orderManager.AuthenticationFailed = true;
                break;
            }

            case "SyncInfo":
            {
                orderManager.LobbyInfo = Session.Deserialize(order.TargetString);
                SetOrderLag(orderManager);
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbyClients":
            {
                var clients = new List <Session.Client>();
                var nodes   = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "Client")
                    {
                        clients.Add(Session.Client.Deserialize(node.Value));
                    }
                }

                orderManager.LobbyInfo.Clients = clients;
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbySlots":
            {
                var slots = new Dictionary <string, Session.Slot>();
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "Slot")
                    {
                        var slot = Session.Slot.Deserialize(node.Value);
                        slots.Add(slot.PlayerReference, slot);
                    }
                }

                orderManager.LobbyInfo.Slots = slots;
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbyGlobalSettings":
            {
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "GlobalSettings")
                    {
                        orderManager.LobbyInfo.GlobalSettings = Session.Global.Deserialize(node.Value);
                    }
                }

                SetOrderLag(orderManager);
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncClientPings":
            {
                var pings = new List <Session.ClientPing>();
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "ClientPing")
                    {
                        pings.Add(Session.ClientPing.Deserialize(node.Value));
                    }
                }

                orderManager.LobbyInfo.ClientPings = pings;
                break;
            }

            case "SetStance":
            {
                if (!Game.OrderManager.LobbyInfo.GlobalSettings.FragileAlliances)
                {
                    return;
                }

                var targetPlayer = order.Player.World.Players.FirstOrDefault(p => p.InternalName == order.TargetString);
                var newStance    = (Stance)order.ExtraData;

                order.Player.SetStance(targetPlayer, newStance);

                Game.Debug("{0} has set diplomatic stance vs {1} to {2}",
                           order.Player.PlayerName, targetPlayer.PlayerName, newStance);

                // automatically declare war reciprocally
                if (newStance == Stance.Enemy && targetPlayer.Stances[order.Player] == Stance.Ally)
                {
                    targetPlayer.SetStance(order.Player, newStance);
                    Game.Debug("{0} has reciprocated", targetPlayer.PlayerName);
                }

                break;
            }

            case "Ping":
            {
                orderManager.IssueOrder(Order.Pong(order.TargetString));
                break;
            }

            default:
            {
                if (!order.IsImmediate)
                {
                    var self   = order.Subject;
                    var health = self.TraitOrDefault <Health>();
                    if (health == null || !health.IsDead)
                    {
                        foreach (var t in self.TraitsImplementing <IResolveOrder>())
                        {
                            t.ResolveOrder(self, order);
                        }
                    }
                }

                break;
            }
            }
        }
Ejemplo n.º 5
0
        internal static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order)
        {
            if (world != null)
            {
                if (!world.WorldActor.TraitsImplementing <IValidateOrder>().All(vo =>
                                                                                vo.OrderValidation(orderManager, world, clientId, order)))
                {
                    return;
                }
            }

            switch (order.OrderString)
            {
            case "Chat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);

                // Cut chat messages to the hard limit to avoid exploits
                var message = order.TargetString;
                if (message.Length > ChatMessageMaxLength)
                {
                    message = order.TargetString.Substring(0, ChatMessageMaxLength);
                }

                if (client != null)
                {
                    var player = world != null?world.FindPlayerByClient(client) : null;

                    var suffix = (player != null && player.WinState == WinState.Lost) ? " (Dead)" : "";
                    suffix = client.IsObserver ? " (Spectator)" : suffix;

                    if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
                    {
                        suffix += " (Ally)";
                    }

                    Game.AddChatLine(client.Color.RGB, client.Name + suffix, message);
                }
                else
                {
                    Game.AddChatLine(Color.White, "(player {0})".F(clientId), message);
                }
                break;
            }

            case "Message":                     // Server message
                Game.AddChatLine(Color.White, ServerChatName, order.TargetString);
                break;

            case "Disconnected":                     /* reports that the target player disconnected */
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    client.State = Session.ClientState.Disconnected;
                }
                break;
            }

            case "TeamChat":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);

                if (client != null)
                {
                    if (world == null)
                    {
                        if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
                        {
                            Game.AddChatLine(client.Color.RGB, "[Team] " + client.Name, order.TargetString);
                        }
                    }
                    else
                    {
                        var player = world.FindPlayerByClient(client);
                        if (player != null && player.WinState == WinState.Lost)
                        {
                            Game.AddChatLine(client.Color.RGB, client.Name + " (Dead)", order.TargetString);
                        }
                        else if ((player != null && world.LocalPlayer != null && player.Stances[world.LocalPlayer] == Stance.Ally) || (world.IsReplay && player != null))
                        {
                            Game.AddChatLine(client.Color.RGB, "[Team" + (world.IsReplay ? " " + client.Team : "") + "] " + client.Name, order.TargetString);
                        }
                        else if ((orderManager.LocalClient != null && orderManager.LocalClient.IsObserver && client.IsObserver) || (world.IsReplay && client.IsObserver))
                        {
                            Game.AddChatLine(client.Color.RGB, "[Spectators] " + client.Name, order.TargetString);
                        }
                    }
                }

                break;
            }

            case "StartGame":
            {
                if (Game.ModData.MapCache[orderManager.LobbyInfo.GlobalSettings.Map].Status != MapStatus.Available)
                {
                    Game.Disconnect();
                    Game.LoadShellMap();

                    // TODO: After adding a startup error dialog, notify the replay load failure.
                    break;
                }

                Game.AddChatLine(Color.White, ServerChatName, "The game has started.");
                Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular);
                break;
            }

            case "PauseGame":
            {
                var client = orderManager.LobbyInfo.ClientWithIndex(clientId);
                if (client != null)
                {
                    var pause = order.TargetString == "Pause";

                    // Prevent injected unpause orders from restarting a finished game
                    if (orderManager.World.PauseStateLocked && !pause)
                    {
                        break;
                    }

                    if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1)
                    {
                        var pausetext = "The game is {0} by {1}".F(pause ? "paused" : "un-paused", client.Name);
                        Game.AddChatLine(Color.White, ServerChatName, pausetext);
                    }

                    orderManager.World.Paused          = pause;
                    orderManager.World.PredictedPaused = pause;
                }

                break;
            }

            case "HandshakeRequest":
            {
                // Switch to the server's mod if we need and are able to
                var mod     = Game.ModData.Manifest;
                var request = HandshakeRequest.Deserialize(order.TargetString);

                var         externalKey = ExternalMod.MakeKey(request.Mod, request.Version);
                ExternalMod external;
                if ((request.Mod != mod.Id || request.Version != mod.Metadata.Version) &&
                    Game.ExternalMods.TryGetValue(externalKey, out external))
                {
                    // The ConnectionFailedLogic will prompt the user to switch mods
                    orderManager.ServerExternalMod = external;
                    orderManager.Connection.Dispose();
                    break;
                }

                Game.Settings.Player.Name = Settings.SanitizedPlayerName(Game.Settings.Player.Name);
                Game.Settings.Save();

                // Otherwise send the handshake with our current settings and let the server reject us
                var info = new Session.Client()
                {
                    Name           = Game.Settings.Player.Name,
                    PreferredColor = Game.Settings.Player.Color,
                    Color          = Game.Settings.Player.Color,
                    Faction        = "Random",
                    SpawnPoint     = 0,
                    Team           = 0,
                    State          = Session.ClientState.Invalid
                };

                var response = new HandshakeResponse()
                {
                    Client   = info,
                    Mod      = mod.Id,
                    Version  = mod.Metadata.Version,
                    Password = orderManager.Password
                };

                orderManager.IssueOrder(Order.HandshakeResponse(response.Serialize()));
                break;
            }

            case "ServerError":
            {
                orderManager.ServerError          = order.TargetString;
                orderManager.AuthenticationFailed = false;
                break;
            }

            case "AuthenticationError":
            {
                // The ConnectionFailedLogic will prompt the user for the password
                orderManager.ServerError          = order.TargetString;
                orderManager.AuthenticationFailed = true;
                break;
            }

            case "SyncInfo":
            {
                orderManager.LobbyInfo = Session.Deserialize(order.TargetString);
                SetOrderLag(orderManager);
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbyClients":
            {
                var clients = new List <Session.Client>();
                var nodes   = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "Client")
                    {
                        clients.Add(Session.Client.Deserialize(node.Value));
                    }
                }

                orderManager.LobbyInfo.Clients = clients;
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbySlots":
            {
                var slots = new Dictionary <string, Session.Slot>();
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "Slot")
                    {
                        var slot = Session.Slot.Deserialize(node.Value);
                        slots.Add(slot.PlayerReference, slot);
                    }
                }

                orderManager.LobbyInfo.Slots = slots;
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncLobbyGlobalSettings":
            {
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "GlobalSettings")
                    {
                        orderManager.LobbyInfo.GlobalSettings = Session.Global.Deserialize(node.Value);
                    }
                }

                SetOrderLag(orderManager);
                Game.SyncLobbyInfo();
                break;
            }

            case "SyncClientPings":
            {
                var pings = new List <Session.ClientPing>();
                var nodes = MiniYaml.FromString(order.TargetString);
                foreach (var node in nodes)
                {
                    var strings = node.Key.Split('@');
                    if (strings[0] == "ClientPing")
                    {
                        pings.Add(Session.ClientPing.Deserialize(node.Value));
                    }
                }

                orderManager.LobbyInfo.ClientPings = pings;
                break;
            }

            case "Ping":
            {
                orderManager.IssueOrder(Order.Pong(order.TargetString));
                break;
            }

            default:
            {
                if (!order.IsImmediate)
                {
                    var self = order.Subject;
                    if (!self.IsDead)
                    {
                        foreach (var t in self.TraitsImplementing <IResolveOrder>())
                        {
                            t.ResolveOrder(self, order);
                        }
                    }
                }

                break;
            }
            }
        }
Ejemplo n.º 6
0
        void AcceptConnection()
        {
            Socket newSocket;

            try
            {
                if (!listener.Server.IsBound)
                    return;

                newSocket = listener.AcceptSocket();
            }
            catch (Exception e)
            {
                /* TODO: Could have an exception here when listener 'goes away' when calling AcceptConnection! */
                /* Alternative would be to use locking but the listener doesn't go away without a reason. */
                Log.Write("server", "Accepting the connection failed.", e);
                return;
            }

            var newConn = new Connection { Socket = newSocket };
            try
            {
                newConn.Socket.Blocking = false;
                newConn.Socket.NoDelay = true;

                // assign the player number.
                newConn.PlayerIndex = ChooseFreePlayerIndex();
                SendData(newConn.Socket, BitConverter.GetBytes(ProtocolVersion.Version));
                SendData(newConn.Socket, BitConverter.GetBytes(newConn.PlayerIndex));
                PreConns.Add(newConn);

                // Dispatch a handshake order
                var request = new HandshakeRequest
                {
                    Mod = ModData.Manifest.Id,
                    Version = ModData.Manifest.Metadata.Version,
                    Map = LobbyInfo.GlobalSettings.Map
                };

                DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize());
            }
            catch (Exception e)
            {
                DropClient(newConn);
                Log.Write("server", "Dropping client {0} because handshake failed: {1}", newConn.PlayerIndex.ToString(CultureInfo.InvariantCulture), e);
            }
        }
Ejemplo n.º 7
0
        void AcceptConnection()
        {
            Socket newSocket = null;

            try
            {
                if (!listener.Server.IsBound) return;
                newSocket = listener.AcceptSocket();
            }
            catch
            {
                /* could have an exception here when listener 'goes away' when calling AcceptConnection! */
                /* alternative would be to use locking but the listener doesnt go away without a reason */
                return;
            }

            var newConn = new Connection { socket = newSocket };
            try
            {
                newConn.socket.Blocking = false;
                newConn.socket.NoDelay = true;

                // assign the player number.
                newConn.PlayerIndex = ChooseFreePlayerIndex();
                newConn.socket.Send(BitConverter.GetBytes(ProtocolVersion.Version));
                newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
                preConns.Add(newConn);

                // Dispatch a handshake order
                var request = new HandshakeRequest()
                {
                    Map = lobbyInfo.GlobalSettings.Map,
                    Mods = lobbyInfo.GlobalSettings.Mods.Select(m => "{0}@{1}".F(m,Mod.AllMods[m].Version)).ToArray()
                };
                DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize());
            }
            catch (Exception) { DropClient(newConn); }
        }