Exemplo n.º 1
0
 public void SendSelectedCommand(MessageSelectorCommand data)
 {
     _messenger.SendMessageToServer(MessageTypes.SelectorMessageType, data);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Checks messages which should only be handled if the state is active. Returns true if a message is processed.
        /// </summary>
        /// <param name="?"></param>
        /// <returns>Returns true if a message is processed.</returns>
        bool _checkActiveMessages(MessagePackSerializableObject msg, MessageTypes messageType)
        {
            // All message cases must return true to signify that a message has been processed.

            switch (messageType)
            {
                #region Change ship location

            case (MessageTypes.ChangeShipLocation):
            {
                var data = msg as MessageChangeShipLocation;

                Ship s = _clientShipManager.GetShip(data.ShipID);

                if (s == null)
                {
                    ConsoleManager.WriteLine("Warning: ShipID not found while processing ChangeShipLocation message.", ConsoleMessageType.Warning);
                    break;
                }
                s.Position       = new Vector2(data.PosX, data.PosY);
                s.LinearVelocity = new Vector2(data.VelX, data.VelY);
                s.Rotation       = data.Rotation;

                return(true);
            }

                #endregion

                #region Receieve Position Update

            case (MessageTypes.PositionUpdateData):
            {
                var data = msg as MessagePositionUpdateData;

                if (data == null)
                {
                    return(true);
                }

                data.UpdateDataObjects
                .Where(p => p.TargetType == PositionUpdateTargetType.Ship)
                .ForEach(UpdateShipPosition);


                return(true);
            }

                #endregion

                #region Add/Remove Ships to/from Team

            case (MessageTypes.AddRemoveShipsTeam):
            {
                MessageAddRemoveShipsTeam data = msg as MessageAddRemoveShipsTeam;

                _targetingService.DisableTargetSetting();
                if (data.AddOrRemove)
                {
                    foreach (var ID in data.IDs)
                    {
                        _teamManager.AddTeamToObject(ID, data.TeamID);
                    }
                }
                else
                {
                    foreach (var ID in data.IDs)
                    {
                        _teamManager.RemoveTeamFromObject(ID, data.TeamID);
                    }
                }
                _targetingService.EnableTargetSetting();
                return(true);
            }

                #endregion

                #region Receive New Ship

            // New connection, got a player ID
            case (MessageTypes.ReceiveNewShips):
            {
                var data = msg as MessageReceiveNewShips;
                foreach (var sd in data.Ships)
                {
                    var s = MessageReader.InstantiateShip(sd, _physicsManager.World, _clientShipManager, false);

                    if (s.Pilot.PilotType == PilotType.Player)
                    {
                        _numPlayerPilots++;
                        if (_numPlayerPilots == 1 && IncreaseUpdateInterval != null)         //Added the first player pilot
                        {
                            IncreaseUpdateInterval(this, MessageHandlerID.Value);
                        }
                    }
                }
                return(true);
            }
                #endregion

                #region NumOnlinePlayersChanged (Redis only)
            case MessageTypes.Redis_NumOnlinePlayersChanged:
            {
                var data = msg as MessageEmptyMessage;
                if (data.Data != null)
                {
                    _numPlayerPilots = (int)data.Data;
                    if (_numPlayerPilots >= 1 && IncreaseUpdateInterval != null)     //Added the first player pilot
                    {
                        IncreaseUpdateInterval(this, MessageHandlerID.Value);
                    }
                    else if (_numPlayerPilots == 0 && DecreaseUpdateInterval != null)
                    {
                        DecreaseUpdateInterval(this, MessageHandlerID.Value);
                    }
                }
                return(true);
            }

                #endregion

                #region Receive New Structure
            case MessageTypes.ReceiveNewStructure:
            {
                var           data = msg as MessageReceiveNewStructure;
                StructureData sd   = data.StructureData;
                InstantiateStructure(sd);

                return(true);
            }
                #endregion

                #region ReceiveFloatyAreaObjects

            case MessageTypes.ReceiveFloatyAreaObjects:
            {
                var data = msg as MessageReceiveFloatyAreaObjects;
                _floatyAreaObjectManager.InstantiateFloatyAreaObjects(data);
                break;
            }

                #endregion

                #region KillOrRemoveObjectsFromArea
            case MessageTypes.RemoveKillRevive:
            {
                MessageRemoveKillRevive data = msg as MessageRemoveKillRevive;

                switch (data.ObjectType)
                {
                case RemovableObjectTypes.FloatyAreaObject:
                {
                    _floatyAreaObjectManager.RemoveFloatyAreaObjects(data);
                    break;
                }

                case RemovableObjectTypes.Ship:
                {
                    List <Ship> ships = new List <Ship>();
                    foreach (var ID in data.ObjectIDs)
                    {
                        Ship s = _clientShipManager.GetShip(ID);
                        if (s == null)
                        {
                            ConsoleManager.WriteLine("Error: ShipID not found in MainNetworkingManager while processing MessageTypes.RemoveKillRevive", ConsoleMessageType.Error);
                        }
                        else
                        {
                            ships.Add(s);
                        }
                    }


                    switch (data.ActionType)
                    {
                    case ActionType.Kill:
                    {
                        foreach (Ship s in ships)
                        {
                            s.Kill();
                            if (s != _clientShipManager.PlayerShip)
                            {
                                Debugging.DisposeStack.Push(this.ToString());
                                s.Body.Enabled = false;
                            }
                            _targetingService.DeRegisterObject(s);
                        }
                        break;
                    }

                    case ActionType.Revive:
                    {
                        float healthMultiplier  = data.HealthMultiplier == null ? 1 : (float)data.HealthMultiplier;
                        float shieldsMultiplier = data.ShieldMultiplier == null ? 1 : (float)data.ShieldMultiplier;
                        foreach (Ship s in ships)
                        {
                            s.Revive((int)(s.MaxHealth * healthMultiplier), (int)(s.MaxShields * shieldsMultiplier));
                            _targetingService.RegisterObject(s);
                        }

                        break;
                    }

                    case ActionType.Remove:
                    {
                        foreach (Ship s in ships)
                        {
                            _clientShipManager.RemoveShip(s.Id);
                            if (s.Pilot.PilotType == PilotType.Player)
                            {
                                _numPlayerPilots--;
                                if (_numPlayerPilots == 0 && DecreaseUpdateInterval != null)
                                //Last player pilot has been removed
                                {
                                    DecreaseUpdateInterval(this, MessageHandlerID.Value);
                                }
                            }
                        }
                        break;
                    }
                    }

                    break;
                }


                case RemovableObjectTypes.Structure:
                {
                    switch (data.ActionType)
                    {
                    case ActionType.Kill:
                    {
                        foreach (var ID in data.ObjectIDs)
                        {
                            KillStructure(ID);
                        }
                        break;
                    }

                    case ActionType.Revive:
                    {
                        ConsoleManager.WriteLine("Error: Revive not implemented for structures.", ConsoleMessageType.Error);
                        break;
                    }

                    case ActionType.Remove:
                    {
                        foreach (var ID in data.ObjectIDs)
                        {
                            RemoveStructure(ID);
                        }
                        break;
                    }
                    }

                    break;
                }
                }
                return(true);
            }

                #endregion

                #region ObjectFired

            case MessageTypes.ObjectFired:
            {
                var data = msg as MessageObjectFired;
                ObjectFired(data);

                return(true);
            }

                #endregion

                #region COMMAND Change Ship Type

            case (MessageTypes.ChangeShipType):
            {
                var data = msg as ShipData;
                //This might not work...yolo
                MessageReader.InstantiateShip(data, _physicsManager.World, _clientShipManager, true);
                return(true);
            }
                #endregion

                #region Fire Denial

            case (MessageTypes.FireRequestResponse):
            {
                var data = msg as MessageFireRequestResponse;
                switch (data.FiringObjectType)
                {
                case FiringObjectTypes.Ship:
                {
                    ShipFireResponse(data);
                    break;
                }


                case FiringObjectTypes.Structure:
                {
                    StructureFireResponse(data);
                    break;
                }
                }



                return(true);
            }

                #endregion

                #region SelectorCommandMessage

            case MessageTypes.SelectorMessageType:
            {
                MessageSelectorCommand data = msg as MessageSelectorCommand;
                _selectionManager.RelayNetworkCommand(data);
                return(true);
            }
                #endregion

                #region Set Health

            case (MessageTypes.SetHealth):
            {
                var setHealthData = msg as MessageSetHealth;
                foreach (var d in setHealthData.HealthData)
                {
                    Ship s = _clientShipManager.GetShip(d.ShipID);

                    if (s == null)
                    {
                        ConsoleManager.WriteLine("Warning: ShipID not found while processing SetHealth message.", ConsoleMessageType.Warning);
                        break;
                    }

                    if (!s.IsAlive)
                    {
                        s.Revive((int)d.Health, (int)d.Shields);
                    }

                    s.CurrentHealth          = d.Health;
                    s.Shields.CurrentShields = d.Shields;
                    s.SetCurrentEnergy(d.Energy);

                    for (int i = 0; i < d.DebuffTypesToAdd.Count; i++)
                    {
                        s.Debuffs.AddDebuff(d.DebuffTypesToAdd[i], d.DebuffCountsToAdd[i], TimeKeeper.MsSinceInitialization);
                    }
                }

                return(true);
            }
                #endregion

                #region AddCargoToShip
            case MessageTypes.AddCargoToShip:
            {
                var data = msg as MessageAddCargoToShip;

                Ship s = _clientShipManager.GetShip(data.ShipID);
                if (s == null)
                {
                    ConsoleManager.WriteLine("Warning: ShipID not found while processing AddStatefulCargoToShip message.", ConsoleMessageType.Warning);
                    break;
                }

                foreach (var cargoData in data.StatefulCargoData)
                {
                    var cargo = MessageReader.InstantiateStatefulCargo(cargoData);

                    s.Cargo.AddStatefulCargo(cargo, true);
                    if (cargo.CargoType == StatefulCargoTypes.Module)
                    {
                        s.AddModule((Module)cargo);
                    }
                }

                foreach (var cargoData in data.StatelessCargoData)
                {
                    s.Cargo.AddStatelessCargo(cargoData.CargoType, cargoData.Quantity, true);
                }

                break;
            }

                #endregion

                #region RemoveCargoFromPlayerShip
            case MessageTypes.RemoveCargoFromShip:
            {
                var data = msg as MessageRemoveCargoFromShip;

                Ship s = _clientShipManager.GetShip(data.ShipID);

                if (s == null)
                {
                    ConsoleManager.WriteLine("Warning: ShipID not found while processing RemoveStatefulCargoFromShip message.", ConsoleMessageType.Warning);
                    break;
                }

                foreach (var cargoID in data.StatefulCargoIDs)
                {
                    var removedCargo = s.Cargo.RemoveStatefulCargo(cargoID);

                    if (removedCargo == null)
                    {
                        ConsoleManager.WriteLine("Error: cargo not found while processing message " + MessageTypes.RemoveCargoFromShip + " for ID " + s.Id, ConsoleMessageType.Error);
                    }
                    else if (removedCargo.CargoType == StatefulCargoTypes.Module)
                    {
                        s.RemoveModule(removedCargo.Id);
                    }


                    foreach (var cargoData in data.StatelessCargoData)
                    {
                        s.Cargo.RemoveStatelessCargo(cargoData.CargoType, cargoData.Quantity);
                    }
                }
                break;
            }

                #endregion
            }

            return(false);//No message processed
        }
Exemplo n.º 3
0
        /// <summary>
        /// Relays commands from server to locally simulated IDs.
        /// </summary>
        /// <param name="selectedIDs"></param>
        /// <param name="command"></param>
        /// <param name="data"></param>
        public void RelayNetworkCommand(MessageSelectorCommand commandMessage)
        {
            switch (commandMessage.CommandType)
            {
            case SelectorCommands.AttackTarget:
            {
                AttackTargetData d = commandMessage.CommandData as AttackTargetData;
                int targetID       = (d.TargetID);
                foreach (int id in commandMessage.SelectedIDs)
                {
                    if (_selectedObjects.ContainsKey(id) && _selectedObjects[id] is ICommandable && _selectedObjects[id].IsLocalSim)
                    {
                        ((ICommandable)_selectedObjects[id]).AttackTarget(_targetingManager.GetTargetableObject(targetID));
                    }
                }
                break;
            }

            case SelectorCommands.AttackToPosition:
            {
                AttackToPositionData d = commandMessage.CommandData as AttackToPositionData;
                foreach (int id in commandMessage.SelectedIDs)
                {
                    if (_selectedObjects.ContainsKey(id) && _selectedObjects[id] is ICommandable && _selectedObjects[id].IsLocalSim)
                    {
                        ((ICommandable)_selectedObjects[id]).AttackToPosition(new Vector2(d.XPos, d.YPos));
                    }
                }
                break;
            }

            case SelectorCommands.GoToPosition:
            {
                GoToPositionData d = commandMessage.CommandData as GoToPositionData;
                foreach (int id in commandMessage.SelectedIDs)
                {
                    if (_selectedObjects.ContainsKey(id) && _selectedObjects[id] is ICommandable && _selectedObjects[id].IsLocalSim)
                    {
                        ((ICommandable)_selectedObjects[id]).GoToPosition(new Vector2(d.XPos, d.YPos));
                    }
                }
                break;
            }

            case SelectorCommands.Stop:
            {
                foreach (int id in commandMessage.SelectedIDs)
                {
                    if (_selectedObjects.ContainsKey(id) && _selectedObjects[id] is ICommandable && _selectedObjects[id].IsLocalSim)
                    {
                        ((ICommandable)_selectedObjects[id]).Stop();
                    }
                }
                break;
            }

            case SelectorCommands.HoldPosition:
            {
                foreach (int id in commandMessage.SelectedIDs)
                {
                    if (_selectedObjects.ContainsKey(id) && _selectedObjects[id] is ICommandable && _selectedObjects[id].IsLocalSim)
                    {
                        ((ICommandable)_selectedObjects[id]).HoldPosition();
                    }
                }
                break;
            }
            }
        }