コード例 #1
0
        // Load config from JSON file
        public static void Load()
        {
            JSONNode configJson;

            if (!JSON.Deserialize(ConfigFilePath, out configJson, false))
            {
                Log.Write($"{CONFIG_FILE} not found, no friendly lists defined");
                return;
            }

            Log.Write($"Loading friendly list from {CONFIG_FILE}");
            try {
                JSONNode playersJson;
                configJson.TryGetAs("players", out playersJson);
                foreach (JSONNode record in playersJson.LoopArray())
                {
                    Players.Player owner;
                    string         error;
                    if (!PlayerHelper.TryGetPlayer(record.GetAs <string>("owner"), out owner, out error, true))
                    {
                        continue;
                    }
                    List <Players.Player> friends = new List <Players.Player>();
                    JSONNode friendsJson;
                    record.TryGetAs("friendly", out friendsJson);
                    foreach (JSONNode friendJson in friendsJson.LoopArray())
                    {
                        Players.Player target;
                        if (PlayerHelper.TryGetPlayer(friendJson.GetAs <string>(), out target, out error, true))
                        {
                            friends.Add(target);
                        }
                    }
                    friendlyPlayers[owner] = friends;
                }
            } catch (Exception e) {
                Log.Write($"Error parsing {CONFIG_FILE}: {e.Message}");
            }
            return;
        }
コード例 #2
0
        public bool TryDoCommand(Players.Player causedBy, string chattext, List <string> splits)
        {
            if (!splits[0].Equals("/friendly"))
            {
                return(false);
            }

            var m = Regex.Match(chattext, @"/friendly (?<action>[^ ]+) ?(?<player>['].+[']|[^ ]+)?$");

            if (!m.Success)
            {
                return(SyntaxError(causedBy));
            }
            string action = m.Groups["action"].Value;

            if (!(action.Equals("add") || action.Equals("list") || action.Equals("remove")))
            {
                return(SyntaxError(causedBy));
            }

            // list does not require a target player
            if (action.Equals("list"))
            {
                string playerNames;
                if (PlayerTracker.GetFriendlyList(causedBy, out playerNames))
                {
                    Chat.Send(causedBy, $"Friendly players: {playerNames}");
                }
                else
                {
                    Chat.Send(causedBy, "No friendly players defined. Guards shoot everyone.");
                }
                return(true);
            }

            string playerName = m.Groups["player"].Value;
            string error;

            Players.Player target;
            if (!PlayerHelper.TryGetPlayer(playerName, out target, out error, true))
            {
                Chat.Send(causedBy, $"Could not find player {playerName}: {error}");
                return(true);
            }

            if (action.Equals("add"))
            {
                if (!PlayerTracker.AddFriendly(causedBy, target))
                {
                    Chat.Send(causedBy, $"Player {target.Name} is already on the friendly list");
                    return(true);
                }
                else
                {
                    Chat.Send(causedBy, $"Added {target.Name} as friendly");
                }
            }
            else if (action.Equals("remove"))
            {
                if (!PlayerTracker.RemoveFriendly(causedBy, target))
                {
                    Chat.Send(causedBy, $"Player {target.Name} was not on the friendly list");
                }
                else
                {
                    Chat.Send(causedBy, $"Removed {target.Name} from friendly list");
                }
            }

            return(true);
        }