public void SetPlayerLevel(string playerName, uint level, ulong sender)
        {
            PlayerPermission player;

            if (TryGetPlayerPermission(playerName, out player, sender))
            {
                playerName = player.Player.PlayerName;

                //change level
                var i = Players.IndexOf(player);
                player.Level = level;
                Players[i]   = player;

                //send changes to player
                SendPermissions(player.Player.SteamId);
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Player {0} could not be found.", playerName));
                return;
            }

            MessageClientTextMessage.SendMessage(sender, "Server",
                                                 string.Format("{0}'s level was set to {1}.", playerName, level));
        }
        public void SetGroupName(string groupName, string newName, ulong sender)
        {
            PermissionGroup group;

            if (TryGetGroup(groupName, out group, sender))
            {
                if (Groups.Any(g => g.GroupName.Equals(newName, StringComparison.InvariantCultureIgnoreCase)))
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("There is already a group named {0}.", newName));
                    return;
                }

                groupName = group.GroupName;

                var i = Groups.IndexOf(group);
                group.GroupName = newName;
                Groups[i]       = group;

                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Group {0} was renamed to {1}.", groupName, newName));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Group {0} could not be found.", groupName));
            }
        }
示例#3
0
        public override bool Invoke(ulong steamId, long playerId, string messageText)
        {
            var match = Regex.Match(messageText, @"/pardon\s+(?<Key>.+)", RegexOptions.IgnoreCase);

            if (match.Success)
            {
                var playerName = match.Groups["Key"].Value;

                Player bannedPlayer = ChatCommandLogic.Instance.ServerCfg.Config.ForceBannedPlayers.FirstOrDefault(p => p.PlayerName.Equals(playerName, StringComparison.InvariantCultureIgnoreCase));
                if (bannedPlayer.SteamId != 0)
                {
                    ChatCommandLogic.Instance.ServerCfg.Config.ForceBannedPlayers.Remove(bannedPlayer);
                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Pardoned player {0}", bannedPlayer.PlayerName));
                }
                else
                {
                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Can't find a banned player named {0}", playerName));
                }

                return(true);
            }

            MyAPIGateway.Utilities.SendMessage(steamId, "Pardoning", "Please supply name to pardon from Ban.");
            return(true);
        }
示例#4
0
        public override void ProcessServer()
        {
            switch (Action)
            {
            case ConfigAction.Save:
                ChatCommandLogic.Instance.ServerCfg.Save();
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Config saved.");
                break;

            case ConfigAction.Reload:
                ChatCommandLogic.Instance.ServerCfg.ReloadConfig();
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Config reloaded.");
                break;

            case ConfigAction.AdminLevel:
                ChatCommandLogic.Instance.ServerCfg.UpdateAdminLevel(Config.AdminLevel);
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", string.Format("Updated default admin level to {0}. Please note that you have to use '/cfg save' to save it permanently.", Config.AdminLevel));
                break;

            case ConfigAction.NoGrindIndestructible:
                ChatCommandLogic.Instance.ServerCfg.SetNoGrindIndestructible(Config.NoGrindIndestructible);
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", string.Format("Set NoGrindIndestructible to {0}. ", Config.NoGrindIndestructible));
                break;

            case ConfigAction.Show:
                Config = ChatCommandLogic.Instance.ServerCfg.Config;
                ConnectionHelper.SendMessageToPlayer(SenderSteamId, this);
                break;
            }
        }
        public void CreateCommandHotlist(ulong sender, string param = null)
        {
            List <CommandStruct> commands = new List <CommandStruct>(Commands);

            if (!string.IsNullOrEmpty(param))
            {
                commands =
                    new List <CommandStruct>(
                        Commands.Where(c => c.Name.IndexOf(param, StringComparison.InvariantCultureIgnoreCase) >= 0));

                if (commands.Count == 0)
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("No command matching with {0} could be found.", param));
                    return;
                }
            }

            if (!_commandCache.ContainsKey(sender))
            {
                _commandCache.Add(sender, commands);
            }
            else
            {
                _commandCache[sender] = commands;
            }

            var message = new MessageCommandPermission()
            {
                Commands      = commands,
                CommandAction = CommandActions.List
            };

            ConnectionHelper.SendMessageToPlayer(sender, message);
        }
        public void SetGroupLevel(string groupName, uint level, ulong sender)
        {
            PermissionGroup group;

            if (TryGetGroup(groupName, out group, sender))
            {
                groupName = group.GroupName;

                var i = Groups.IndexOf(group);
                group.Level = level;
                Groups[i]   = group;

                foreach (ulong steamId in group.Members)
                {
                    SendPermissions(steamId);
                }

                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("The level of group {0} was updated to {1}.", groupName, level));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Group {0} could not be found.", groupName));
            }
        }
示例#7
0
        public static void CheckTradeTimeouts()
        {
            var processingTime = DateTime.Now;

            if (EconomyScript.Instance.ServerConfig == null)
            {
                return;
            }

            var expiration = EconomyScript.Instance.ServerConfig.TradeTimeout;

            if (EconomyScript.Instance.Data == null || EconomyScript.Instance.Data.OrderBook.Count == 0)
            {
                return;
            }

            var cancellations = EconomyScript.Instance.Data.OrderBook.Where(order => processingTime - order.Created > expiration &&
                                                                            (order.TradeState == TradeState.Sell || order.TradeState == TradeState.SellDirectPlayer)).ToArray();

            if (cancellations.Length == 0)
            {
                return;
            }

            EconomyScript.Instance.ServerLogger.WriteVerbose("CheckTradeTimeouts: {0} cancellations", cancellations.Length);

            foreach (var order in cancellations)
            {
                switch (order.TradeState)
                {
                case TradeState.Sell:
                    // Change the TradeState first, to prevent other calls into this.
                    order.TradeState = TradeState.SellTimedout;
                    MessageClientTextMessage.SendMessage(order.TraderId, "SELL", "Your offer has timed out. Type '/sell collect' to collect your goods.");
                    break;

                case TradeState.SellDirectPlayer:
                    // Change the TradeState first, to prevent other calls into this.
                    order.TradeState = TradeState.SellTimedout;
                    MessageClientTextMessage.SendMessage(order.TraderId, "SELL", "Your offer has timed out. Type '/sell collect' to collect your goods.");

                    ulong tradePartner;
                    if (ulong.TryParse(order.OptionalId, out tradePartner))
                    {
                        var sellingAccount = EconomyScript.Instance.Data.Accounts.FirstOrDefault(a => a.SteamId == order.TraderId);
                        // If the account is null, then the account may have been cleaned up because it hasn't been used.
                        if (sellingAccount != null)
                        {
                            MessageClientTextMessage.SendMessage(tradePartner, "SELL", "The offer from {0} has now expired.", sellingAccount.NickName);
                        }
                        else
                        {
                            MessageClientTextMessage.SendMessage(tradePartner, "SELL", "An offer has now expired.");
                        }
                    }
                    break;
                }
            }
        }
        public void CreateGroupHotlist(ulong sender, string param = null)
        {
            if (Groups.Count == 0)
            {
                MessageClientTextMessage.SendMessage(sender, "Server", "No groups found.");
                return;
            }

            List <PermissionGroup> groups = new List <PermissionGroup>(Groups);

            if (!string.IsNullOrEmpty(param))
            {
                groups =
                    new List <PermissionGroup>(
                        Groups.Where(g => g.GroupName.IndexOf(param, StringComparison.InvariantCultureIgnoreCase) >= 0));

                if (groups.Count == 0)
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("No group matching with {0} could be found.", param));
                    return;
                }
            }

            if (!_groupCache.ContainsKey(sender))
            {
                _groupCache.Add(sender, groups);
            }
            else
            {
                _groupCache[sender] = groups;
            }

            var memberNames = new List <string>();

            groups = new List <PermissionGroup>(groups.OrderBy(g => g.GroupName));

            foreach (PermissionGroup group in groups)
            {
                List <string> names = new List <string>();
                foreach (ulong steamId in group.Members)
                {
                    names.Add(Players.FirstOrDefault(p => p.Player.SteamId == steamId).Player.PlayerName);
                }

                memberNames.Add(string.Join(", ", names));
            }

            ConnectionHelper.SendMessageToPlayer(sender, new MessageGroupPermission()
            {
                Action      = PermissionGroupAction.List,
                Groups      = groups,
                MemberNames = memberNames
            });
        }
 public override void ProcessServer()
 {
     if (AddPrefab(PrefabName, PositionEntityId, Type))
     {
         MessageClientTextMessage.SendMessage(SenderSteamId, "Success", "The Prefab {0} is spawning.", PrefabName);
     }
     else
     {
         MessageClientTextMessage.SendMessage(SenderSteamId, "Failed", "Could not create the specified prefab.");
     }
 }
 /// <summary>
 /// Sends a message to an specific player.  If steamId is set as 0, then it is sent to the current player.
 /// </summary>
 public static void SendMessage(this IMyUtilities utilities, ulong steamId, string sender, string messageText, params object[] args)
 {
     if (steamId == MyAPIGateway.Multiplayer.ServerId || (MyAPIGateway.Session.Player != null && steamId == MyAPIGateway.Session.Player.SteamUserId))
     {
         utilities.ShowMessage(sender, messageText, args);
     }
     else
     {
         MessageClientTextMessage.SendMessage(steamId, sender, messageText, args);
     }
 }
        public override void ProcessServer()
        {
            bool sync = false;

            switch (Type)
            {
            case ProtectionConfigType.Invert:
                if (ProtectionHandler.Config.ProtectionInverted != Value)
                {
                    sync = true;
                    ProtectionHandler.Config.ProtectionInverted = Value;
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", $"The protection is {(Value ? "inverted" : "normal")} now.");
                }
                break;

            case ProtectionConfigType.Enable:
                if (ProtectionHandler.Config.ProtectionEnabled != Value)
                {
                    sync = true;
                    ProtectionHandler.Config.ProtectionEnabled = Value;
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", $"The protection is {(Value ? "enabled" : "disabled")} now");
                }
                break;

            case ProtectionConfigType.LandingGear:
                if (ProtectionHandler.Config.ProtectionAllowLandingGear != Value)
                {
                    sync = true;
                    ProtectionHandler.Config.ProtectionAllowLandingGear = Value;
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", $"Protection area LandingGear is now {(Value ? "allowed" : "disabled")}.");
                }
                break;
            }

            if (sync)
            {
                ProtectionHandler.Save();
                ConnectionHelper.SendMessageToAllPlayers(new MessageSyncProtection {
                    Config = ProtectionHandler.Config
                });
            }
            else
            {
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "The setting was already set to the specified value.");
            }
        }
        public void CreateGroup(string name, uint level, ulong sender)
        {
            if (Groups.Any(g => g.GroupName.Equals(name, StringComparison.InvariantCultureIgnoreCase)))
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("There is already a group named {0}.", name));
                return;
            }

            Groups.Add(new PermissionGroup()
            {
                GroupName = name,
                Level     = level,
                Members   = new List <ulong>(),
            });

            MessageClientTextMessage.SendMessage(sender, "Server",
                                                 string.Format("Group {0} with level {1} was created.", name, level));
        }
        public override void ProcessServer()
        {
            // TODO: check security

            var definition = MyDefinitionManager.Static.GetDefinition(TypeId, SubtypeName);

            if (definition == null)
            {
                return;
            }

            MyFixedPoint amount = (MyFixedPoint)Amount;

            switch (Type)
            {
            case SyncCreateObjectType.Floating:
                Support.InventoryDrop(Position, amount, definition.Id);
                break;

            case SyncCreateObjectType.Inventory:
            {
                if (!MyAPIGateway.Entities.EntityExists(EntityId))
                {
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Failed", "Cannot find the specified Entity.");
                    return;
                }

                var entity = (MyEntity)MyAPIGateway.Entities.GetEntityById(EntityId);

                if (!Support.InventoryAdd(entity, amount, definition.Id))
                {
                    // Send message to player.
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Failed", "Invalid container or Full container. Could not add the item.");
                }
            }
            break;

            case SyncCreateObjectType.Clear:
                new CommandInventoryClear().ClearInventory(SenderSteamId, EntityId);
                break;
            }
        }
        public void RemovePlayerFromGroup(string groupName, string playerName, ulong sender)
        {
            PermissionGroup group;

            if (TryGetGroup(groupName, out group, sender))
            {
                groupName = group.GroupName;

                PlayerPermission playerPermission;
                if (TryGetPlayerPermission(playerName, out playerPermission, sender))
                {
                    playerName = playerPermission.Player.PlayerName;
                    if (!group.Members.Contains(playerPermission.Player.SteamId))
                    {
                        MessageClientTextMessage.SendMessage(sender, "Server",
                                                             string.Format("Player {0} is not a member of group {1}.", playerName, groupName));
                        return;
                    }

                    var i = Groups.IndexOf(group);
                    group.Members.Remove(playerPermission.Player.SteamId);
                    Groups[i] = group;

                    SendPermissions(playerPermission.Player.SteamId);
                }
                else
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("Player {0} could not be found.", playerName));
                    return;
                }

                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Removed player {0} from group {1}.", playerName, groupName));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Group {0} could not be found.", groupName));
            }
        }
        public override void ProcessServer()
        {
            if (ServerConfig.ServerIsClient && SenderSteamId != MyAPIGateway.Session.Player.SteamUserId) //no one should be able to do that
            {
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Saving the session on a locally hosted server is not allowed.");
                return;
            }

            if (string.IsNullOrEmpty(Name))
            {
                MyAPIGateway.Session.Save();
                ChatCommandLogic.Instance.ServerCfg.SaveLogs();
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Session saved.");
            }
            else
            {
                MyAPIGateway.Session.Save(Name);
                ChatCommandLogic.Instance.ServerCfg.Save(Name);
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", string.Format("Session saved as {0}.", Name));
            }
        }
        public override void ProcessServer()
        {
            //update the fields
            if (FieldsToUpdate.HasFlag(ChangedFields.Content))
            {
                Content = ChatCommandLogic.Instance.ServerCfg.SetMessageOfTheDay(Content);
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "The message of the day was updated. Please note that you have to use '/cfg save' to save it permanently.");
            }

            if (FieldsToUpdate.HasFlag(ChangedFields.HeadLine))
            {
                CommandMessageOfTheDay.HeadLine = HeadLine;
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "The headline of the message of the day was updated. Please note that you have to use '/cfg save' to save it permanently.");
            }

            if (FieldsToUpdate.HasFlag(ChangedFields.ShowInChat))
            {
                CommandMessageOfTheDay.ShowInChat = ShowInChat;
                MessageClientTextMessage.SendMessage(SenderSteamId, "Server", string.Format("The setting motdShowInChat was set to {0}. Please note that you have to use '/cfg save' to save it permanently.", ShowInChat));
            }

            ConnectionHelper.SendMessageToAllPlayers(this);
        }
        public void DeleteGroup(string groupName, ulong sender)
        {
            PermissionGroup group;

            if (TryGetGroup(groupName, out group, sender))
            {
                groupName = group.GroupName;
                Groups.Remove(group);

                foreach (ulong steamId in group.Members)
                {
                    SendPermissions(steamId);
                }

                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Group {0} has been deleted.", groupName));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Group {0} could not be found.", groupName));
            }
        }
        public override void ProcessServer()
        {
            switch (Type)
            {
            case ProtectionAreaMessageType.Add:
                if (ProtectionHandler.AddArea(ProtectionArea))
                {
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Successfully created area.");
                    ConnectionHelper.SendMessageToAllPlayers(new MessageSyncProtection()
                    {
                        Config = ProtectionHandler.Config
                    });
                    ProtectionHandler.Save();
                }
                else
                {
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "An area with that name already exists.");
                }
                break;

            case ProtectionAreaMessageType.Remove:
                if (ProtectionHandler.RemoveArea(ProtectionArea))
                {
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Successfully removed area.");
                    ConnectionHelper.SendMessageToAllPlayers(new MessageSyncProtection()
                    {
                        Config = ProtectionHandler.Config
                    });
                    ProtectionHandler.Save();
                }
                else
                {
                    MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "An area with that name could not be found.");
                }
                break;
            }
        }
        public void UsePlayerLevel(string playerName, bool usePlayerLevel, ulong sender)
        {
            PlayerPermission playerPermission;

            if (TryGetPlayerPermission(playerName, out playerPermission, sender))
            {
                playerName = playerPermission.Player.PlayerName;

                var i = Players.IndexOf(playerPermission);
                playerPermission.UsePlayerLevel = usePlayerLevel;
                Players[i] = playerPermission;

                SendPermissions(playerPermission.Player.SteamId);

                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("{0} uses the {1} level now. Current level: {2}", playerName,
                                                                   usePlayerLevel ? "player" : "group", GetPlayerLevel(playerPermission.Player.SteamId)));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Player {0} could not be found.", playerName));
            }
        }
        private void CommonProcess(ulong steamId, SyncGridChangeType syncType, long entityId, string searchEntity, long playerId, bool switchOn)
        {
            List <IMyCubeGrid> selectedShips = new List <IMyCubeGrid>();

            if (entityId != 0)
            {
                var selectedShip = MyAPIGateway.Entities.GetEntityById(entityId) as IMyCubeGrid;
                if (selectedShip != null)
                {
                    selectedShips.Add(selectedShip);
                }
            }
            else if (!string.IsNullOrEmpty(searchEntity))
            {
                // exact name search.
                var currentShipList = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(currentShipList, e => e is IMyCubeGrid && e.DisplayName.Equals(searchEntity, StringComparison.InvariantCultureIgnoreCase));
                selectedShips.AddRange(currentShipList.Cast <IMyCubeGrid>());

                if (currentShipList.Count == 0)
                {
                    // hotlist search.
                    int index;
                    List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                    if (searchEntity.Substring(0, 1) == "#" && int.TryParse(searchEntity.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                    {
                        selectedShips.Add((IMyCubeGrid)shipCache[index - 1]);
                    }
                }
            }

            if (selectedShips.Count == 0)
            {
                MessageClientTextMessage.SendMessage(steamId, "Server", "No ships selected or found.");
                return;
            }

            switch (syncType)
            {
            case SyncGridChangeType.OwnerClaim:
            {
                var players = new List <IMyPlayer>();
                MyAPIGateway.Players.GetPlayers(players, p => p != null && p.PlayerID == playerId);
                IMyPlayer player = players.FirstOrDefault();

                if (player == null)
                {
                    return;
                }

                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        grid.ChangeGridOwnership(player.PlayerID, MyOwnershipShareModeEnum.All);
                    }

                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Grid {0} Claimed by player {1}.", selectedShip.DisplayName, player.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerRevoke:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        grid.ChangeGridOwnership(0, MyOwnershipShareModeEnum.All);
                    }
                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Grid {0} Revoked of all ownership.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerShareAll:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        var blocks = new List <IMySlimBlock>();
                        grid.GetBlocks(blocks, f => f.FatBlock != null && f.FatBlock.OwnerId != 0);

                        foreach (var block in blocks)
                        {
                            block.FatBlock.ChangeOwner(block.FatBlock.OwnerId, MyOwnershipShareModeEnum.All);
                        }
                    }
                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Grid {0} Shared.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.SwitchOnPower:
            {
                int reactors;
                int batteries;
                TurnOnShips(selectedShips, out reactors, out batteries);
                MyAPIGateway.Utilities.SendMessage(steamId, selectedShips.First().DisplayName, "{0} Reactors, {1} Batteries turned on.", reactors, batteries);
            }
            break;

            case SyncGridChangeType.SwitchOffPower:
            {
                int reactors;
                int batteries;
                TurnOffShips(selectedShips, out reactors, out batteries);
                MyAPIGateway.Utilities.SendMessage(steamId, selectedShips.First().DisplayName, "{0} Reactors, {1} Batteries turned off.", reactors, batteries);
            }
            break;

            case SyncGridChangeType.DeleteShip:
            {
                if (selectedShips.Count == 1)
                {
                    DeleteShip(steamId, selectedShips.First());
                }
                else if (selectedShips.Count > 1)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "deleteship", "{0} Ships match that name.", selectedShips.Count);
                }
            }
            break;

            case SyncGridChangeType.Destructible:
            {
                if (selectedShips.Count == 1)
                {
                    SetDestructible(selectedShips.First(), switchOn, steamId);
                }
            }
            break;

            case SyncGridChangeType.Stop:
            {
                foreach (var selectedShip in selectedShips)
                {
                    MessageSyncEntity.Process(selectedShip, SyncEntityType.Stop);
                    MyAPIGateway.Utilities.SendMessage(steamId, selectedShip.DisplayName, "Is stopping.");
                }
            }
            break;

            case SyncGridChangeType.ScaleDown:
            {
                ScaleShip(steamId, selectedShips.First(), MyCubeSize.Small);
            }
            break;

            case SyncGridChangeType.ScaleUp:
            {
                ScaleShip(steamId, selectedShips.First(), MyCubeSize.Large);
            }
            break;
            }
        }
        public void UpdateCommandSecurity(CommandStruct command, ulong sender)
        {
            var commandStruct =
                Commands.FirstOrDefault(c => c.Name.Equals(command.Name, StringComparison.InvariantCultureIgnoreCase));

            int index;

            if (_commandCache.ContainsKey(sender) && command.Name.Substring(0, 1) == "#" &&
                Int32.TryParse(command.Name.Substring(1), out index) && index > 0 && index <= _commandCache[sender].Count)
            {
                commandStruct =
                    Commands.FirstOrDefault(
                        c =>
                        c.Name.Equals(_commandCache[sender][index - 1].Name,
                                      StringComparison.InvariantCultureIgnoreCase));
            }

            if (string.IsNullOrEmpty(commandStruct.Name))
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Command {0} could not be found.", command.Name));
                return;
            }

            command.Name = commandStruct.Name;

            //update security first
            var i = Commands.IndexOf(commandStruct);

            commandStruct.NeededLevel = command.NeededLevel;
            Commands[i] = commandStruct;

            //then send changes
            List <IMyPlayer> players = new List <IMyPlayer>();

            MyAPIGateway.Players.GetPlayers(players, p => p != null);
            foreach (IMyPlayer player in players)
            {
                var playerPermission = Players.FirstOrDefault(p => p.Player.SteamId == player.SteamUserId);

                if (playerPermission.Player.SteamId == 0)
                {
                    //no player found -> send changes
                    SendPermissionChange(player.SteamUserId, commandStruct);
                    continue;
                }

                //don't send changes to players with exeptional permissions
                if (playerPermission.Extensions.Any(s => s.Equals(commandStruct.Name)) ||
                    playerPermission.Restrictions.Any(s => s.Equals(commandStruct.Name)))
                {
                    continue;
                }

                SendPermissionChange(player.SteamUserId, commandStruct);
            }

            if (commandStruct.NeededLevel == uint.MaxValue)
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("The command '{0}' was disabled.", commandStruct.Name));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("The level of command '{0}' was set to {1}.", commandStruct.Name,
                                                                   commandStruct.NeededLevel));
            }
        }
        public void RestrictRights(string playerName, string commandName, ulong sender)
        {
            PlayerPermission playerPermission;

            if (TryGetPlayerPermission(playerName, out playerPermission, sender))
            {
                playerName = playerPermission.Player.PlayerName;

                var commandStruct =
                    Commands.FirstOrDefault(c => c.Name.Equals(commandName, StringComparison.InvariantCultureIgnoreCase));

                int index;
                if (_commandCache.ContainsKey(sender) && commandName.Substring(0, 1) == "#" &&
                    Int32.TryParse(commandName.Substring(1), out index) && index > 0 &&
                    index <= _commandCache[sender].Count)
                {
                    commandStruct =
                        Commands.FirstOrDefault(
                            c =>
                            c.Name.Equals(_commandCache[sender][index - 1].Name,
                                          StringComparison.InvariantCultureIgnoreCase));
                }

                if (string.IsNullOrEmpty(commandStruct.Name))
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("Command {0} could not be found.", commandName));
                    return;
                }

                commandName = commandStruct.Name;
                var i = Players.IndexOf(playerPermission);

                if (Players[i].Restrictions.Any(s => s.Equals(commandName, StringComparison.InvariantCultureIgnoreCase)))
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("Player {0} already has restricted access to {1}.", playerName, commandName));
                    return;
                }

                if (Players[i].Extensions.Any(s => s.Equals(commandName, StringComparison.InvariantCultureIgnoreCase)))
                {
                    var command =
                        Players[i].Extensions.FirstOrDefault(
                            s => s.Equals(commandName, StringComparison.InvariantCultureIgnoreCase));
                    Players[i].Extensions.Remove(command);
                    SendPermissionChange(playerPermission.Player.SteamId, commandStruct);
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("Player {0} has normal access to {1} from now.", playerName, commandName));
                    return;
                }

                Players[i].Restrictions.Add(commandStruct.Name);

                SendPermissionChange(playerPermission.Player.SteamId, new CommandStruct()
                {
                    Name        = commandStruct.Name,
                    NeededLevel = GetPlayerLevel(playerPermission.Player.SteamId) + 1
                });
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Player {0} has no access to {1} from now.", playerName, commandName));
            }
            else
            {
                MessageClientTextMessage.SendMessage(sender, "Server",
                                                     string.Format("Player {0} could not be found.", playerName));
            }
        }
        public void CreatePlayerHotlist(ulong sender, string param)
        {
            List <PlayerPermission> players = new List <PlayerPermission>(Players);

            var onlinePlayers = new List <IMyPlayer>();

            MyAPIGateway.Players.GetPlayers(onlinePlayers, p => p != null);

            if (onlinePlayers.Count == 0 && Players.Count == 0)
            {
                MessageClientTextMessage.SendMessage(sender, "Server", "No players found.");
                return;
            }

            foreach (IMyPlayer player in onlinePlayers)
            {
                if (players.All(p => p.Player.SteamId != player.SteamUserId))
                {
                    players.Add(new PlayerPermission()
                    {
                        Player = new Player()
                        {
                            PlayerName = player.DisplayName,
                            SteamId    = player.SteamUserId
                        },
                        Level          = GetPlayerLevel(player.SteamUserId),
                        UsePlayerLevel = false
                    });
                }
            }


            if (!string.IsNullOrEmpty(param))
            {
                players =
                    new List <PlayerPermission>(
                        players.Where(
                            p => p.Player.PlayerName.IndexOf(param, StringComparison.InvariantCultureIgnoreCase) >= 0));

                if (players.Count == 0)
                {
                    MessageClientTextMessage.SendMessage(sender, "Server",
                                                         string.Format("No player matching with {0} could be found.", param));
                    return;
                }
            }
            players = new List <PlayerPermission>(players.OrderBy(p => p.Player.PlayerName));
            if (!_playerCache.ContainsKey(sender))
            {
                _playerCache.Add(sender, players);
            }
            else
            {
                _playerCache[sender] = players;
            }

            ConnectionHelper.SendMessageToPlayer(sender, new MessagePlayerPermission()
            {
                Action            = PlayerPermissionAction.List,
                PlayerPermissions = players
            });
        }