PRoConEvents.MULTIbalancer.InGameCommand C# (CSharp) Method

InGameCommand() private method

private InGameCommand ( String msg, ChatScope scope, int team, int squad, String name ) : void
msg String
scope ChatScope
team int
squad int
name String
return void
        private void InGameCommand(String msg, ChatScope scope, int team, int squad, String name)
        {
            if (EnableLoggingOnlyMode && !fTestMBCommand) {
            ConsoleDebug("EnableLoggingOnlyMode enabled, commands disabled");
            return;
            }
            if (!EnableInGameCommands) {
            ConsoleDebug("EnableInGameCommands is False, commands disabled");
            return;
            }
            CPrivileges p = this.GetAccountPrivileges(name);
            if (!fTestMBCommand && (p == null || !p.CanMovePlayers)) {
            List<String> m = new List<String>();
            m.Add("You are not authorized to use @mb commands! Check your Procon account settings.");
            SayLines(m, name);
            return;
            }

            Match mbCmd = Regex.Match(msg, @"^\s*/?[@!#]mb\s+([\w]+)\s+(.*)$", RegexOptions.IgnoreCase);
            Match mbSubCmd = Regex.Match(msg, @"^\s*/?[@!#]mb\s+(sub|unsub)", RegexOptions.IgnoreCase);
            Match mbHelp = Regex.Match(msg, @"^\s*/?[@!#]mb\s+help\s*$", RegexOptions.IgnoreCase);
            Match mbHelpCmd = Regex.Match(msg, @"^\s*/?[@!#]mb\s+help\s+(add|del|list|new|sub|unsub|scramble)", RegexOptions.IgnoreCase);
            Match mbScrambleCmd = Regex.Match(msg, @"^\s*/?[@!#]mb\s+scramble\s+(on|off|true|false|yes|no|enable|disable)", RegexOptions.IgnoreCase);
            Match mbScramInfoCmd = Regex.Match(msg, @"^\s*/?[@!#]mb\s+scramble\s*$", RegexOptions.IgnoreCase);

            List<String> lines = null;
            PlayerModel player = null;
            int dispersalGroup = 0;
            String nameMatch = String.Empty;
            List<String> list = null;

            if (mbHelp.Success) {
            lines = new List<String>();
            lines.Add("Type '@mb help' and one of the following:");
            lines.Add("add, delete, list, new, subscribe, unsubscribe, scramble");
            SayLines(lines, name);
            return;
            }

            if (mbHelpCmd.Success) {
            lines = new List<String>();
            String which = mbHelpCmd.Groups[1].Value.ToLower();
            switch (which.ToLower()) {
            case "add":
                lines.Add("Add names to a matching name in the disperse, friends, or white list");
                lines.Add("Example: @mb add friends Match Adam");
                break;
            case "del":
                lines.Add("Delete player names from the disperse, friends, or white list");
                lines.Add("Example: @mb del friends Adam Eve");
                break;
            case "list":
                lines.Add("List the disperse, friends or white list");
                lines.Add("Example: @mb list friends");
                break;
            case "new":
                lines.Add("Create a new entry in the disperse, friends, or white list");
                lines.Add("Example: @mb new disperse 2 Name1 Name2 Name3");
                break;
            case "sub":
                lines.Add("Subscribe to all balancer chat messages");
                break;
            case "unsub":
                lines.Add("Unsubscribe from all balancer chat messages");
                break;
            case "scramble":
                lines.Add("Check if teams will be scrambled");
                lines.Add("To scramble teams at end of round, use: @mb scramble on");
                lines.Add("To not scramble teams at end of round, use: @mb scramble off");
                break;
            default:
                break;
            }
            SayLines(lines, name);
            return;
            }

            if (mbSubCmd.Success) {
            lines = new List<String>();
            String which = mbSubCmd.Groups[1].Value.ToLower();
            switch (which.ToLower()) {
            case "sub":
                player = GetPlayer(name);
                if (player != null) {
                    player.Subscribed = true;
                    lines.Add("You will see all balancer chat messages");
                }
                break;
            case "unsub":
                player = GetPlayer(name);
                if (player != null) {
                    player.Subscribed = false;
                    lines.Add("You will no longer see all balancer chat messages");
                }
                break;
            default:
                break;
            }
            SayLines(lines, name);
            return;
            }

            if (mbScramInfoCmd.Success) {
            lines = new List<String>();
            if (OnlyByCommand) {
            if (fScrambleByCommand) {
                lines.Add("Scrambler is ON: Only By Command required and '@mb scramble on' command given");
            } else {
                lines.Add("Scrambler is OFF: Only By Command required and '@mb scramble on' command not given");
            }
            } else {
            if (fScrambleByCommand) {
                lines.Add("Teams WILL be scrambled by command at end of round");
            } else {
                lines.Add("No command used so far, scramble will be by plugin settings");
            }
            }
            SayLines(lines, name);
            return;
            }

            if (mbScrambleCmd.Success) {
            lines = new List<String>();
            /*
            if (!OnlyByCommand) {
            lines.Add("Only By Command setting is False, in-game admin command is disabled");
            return;
            }
            */
            String which = mbScrambleCmd.Groups[1].Value.ToLower();
            switch (which.ToLower()) {
            case "on":
            case "yes":
            case "true":
            case "enable":
                fScrambleByCommand = true;
                lines.Add("Teams WILL be scrambled at end of round");
                break;
            case "off":
            case "no":
            case "false":
            case "disable":
                fScrambleByCommand = false;
                lines.Add("No scrambling of teams at end of round");
                break;
            default:
                break;
            }
            SayLines(lines, name);
            return;
            }

            if (mbCmd.Success) {
            lines = new List<String>();
            String which = mbCmd.Groups[1].Value;
            String tmp = mbCmd.Groups[2].Value;
            IGCommand cmd = IGCommand.None;

            if (Regex.Match(which, @"^add", RegexOptions.IgnoreCase).Success) {
            cmd = IGCommand.Add;
            } else if (Regex.Match(which, @"^del", RegexOptions.IgnoreCase).Success) {
            cmd = IGCommand.Delete;
            } else if (Regex.Match(which, @"^list", RegexOptions.IgnoreCase).Success) {
            cmd = IGCommand.List;
            } else if (Regex.Match(which, @"^new", RegexOptions.IgnoreCase).Success) {
            cmd = IGCommand.New;
            } else {
            lines.Add("Unknown command: " + which + ", try @mb help");
            SayLines(lines, name);
            return;
            }

            String[] args = Regex.Split(tmp, @"\s+");

            if (args.Length == 0) {
            lines.Add("No list (disperse, friends) specified, try @mb help");
            SayLines(lines, name);
            return;
            } else if (cmd != IGCommand.List && args.Length < 2) {
            lines.Add("The command is incomplete: " + msg + ", try @mb help");
            SayLines(lines, name);
            }

            // args[0] should be the name of the list
            String listName = String.Empty;
            if (Regex.Match(args[0], @"^di?s?p?e?r?s?e?", RegexOptions.IgnoreCase).Success) {
            listName = "Dispersal";
            } else if (Regex.Match(args[0], @"^fr?i?e?n?d?s?", RegexOptions.IgnoreCase).Success) {
            listName = "Friends";
            } else if (Regex.Match(args[0], @"^wh?i?t?e?l?i?s?t?", RegexOptions.IgnoreCase).Success) {
            listName = "Whitelist";
            } else {
            lines.Add("Unknown list name: " + args[0] + ", try @mb help");
            SayLines(lines, name);
            return;
            }

            int i = 1;

            if (listName == "Dispersal" && args.Length >= 3) {
            // args[1] may be a dispersal group
            if (args[1] == "1") {
                dispersalGroup = 1;
                ++i;
            } else if (args[1] == "2") {
                dispersalGroup = 2;
                ++i;
            }
            }

            // Next arg may be the match string for add
            if (cmd == IGCommand.Add && listName == "Friends" && i < args.Length) {
            nameMatch = args[i];
            ++i;
            }

            // The rest of the args are the name operands
            List<String> names = new List<String>();
            while (i < args.Length) {
            names.Add(args[i]);
            ++i;
            }

            // Execute the command
            switch (cmd) {
            case IGCommand.Add:
                if (listName == "Dispersal") {
                    if (dispersalGroup != 0) {
                        bool found = false;
                        int groupId = 0;
                        String[] copy = (String[])DisperseEvenlyList.Clone();
                        list = new List<String>();
                        list.AddRange(DisperseEvenlyList);
                        for (int n = 0; n < copy.Length; ++n) {
                            if (Regex.Match(copy[n], @"^[1234]\s+").Success) {
                                // It's a group
                                List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                                if (tokens.Count > 0 && Int32.TryParse(tokens[0], out groupId) && groupId == dispersalGroup) {
                                    found = true;
                                    foreach (String nm in names) {
                                        copy[n] = copy[n] + " " + nm;
                                    }
                                    break;
                                }
                            }
                        }
                        if (found) {
                            list.Clear();
                            list.AddRange(copy);
                            lines.Add("Added " + names.Count + " names to Dispersal Group " + groupId);
                        } else {
                            lines.Add("Can't find Dispersal Group " + groupId + ", add failed!");
                            SayLines(lines, name);
                            return;
                        }
                    } else {
                        foreach (String nm in names) {
                            player = GetPlayer(nm);
                            if (player != null && IsInDispersalList(player, true)) {
                                lines.Add("Duplicate name ^b" + nm + "^n, add failed!");
                                SayLines(lines, name);
                                return;
                            }
                            list.Add(nm);
                        }
                        lines.Add("Added " + names.Count + " names to Disperse Evenly List");
                    }
                    ForceSetPluginVariable("1 - Settings|Disperse Evenly List", list.ToArray());
                } else if (listName == "Friends") {
                    bool found = false;
                    String[] copy = fSettingFriendsList.ToArray();
                    list = new List<String>();
                    list.AddRange(fSettingFriendsList);
                    for (int n = 0; n < copy.Length; ++n) {
                        // Find a line in the list that contains the nameMatch string
                        List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                        if (tokens.Contains(nameMatch)) {
                            found = true;
                            foreach (String nm in names) {
                                copy[n] = copy[n] + " " + nm;
                            }
                            break;
                        }
                    }
                    if (found) {
                        list.Clear();
                        list.AddRange(copy);
                        lines.Add("Added " + names.Count + " names to Friends List");
                    } else {
                        lines.Add("Can't find friend " + nameMatch + " in Friends List, add failed!");
                        SayLines(lines, name);
                        return;
                    }
                    ForceSetPluginVariable("1 - Settings|Friends List", list.ToArray());
                } else if (listName == "Whitelist") {
                    String[] copy = fSettingWhitelist.ToArray();
                    list = new List<String>();
                    list.AddRange(fSettingWhitelist);
                    // Check for duplication
                    foreach (String nm in names) {
                        for (int n = 0; n < copy.Length; ++n) {
                            // Find matches
                            List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                            if (tokens.Contains(nm)) {
                                lines.Add("Duplicate name ^b" + nm + "^n, add failed!");
                                SayLines(lines, name);
                                return;
                            }
                        }
                        // ok to add
                        list.Add(nm);
                    }
                    lines.Add("Added " + names.Count + " names to Whitelist");
                    ForceSetPluginVariable("1 - Settings|Whitelist", list.ToArray());
                }
                break;
            case IGCommand.Delete:
                if (listName == "Dispersal") {
                    if (dispersalGroup != 0) {
                        bool found = false;
                        String remove = String.Empty;
                        int groupId = 0;
                        String[] copy = (String[])DisperseEvenlyList.Clone();
                        list = new List<String>();
                        list.AddRange(DisperseEvenlyList);
                        for (int n = 0; n < copy.Length; ++n) {
                            if (Regex.Match(copy[n], @"^[1234]\s+").Success) {
                                // It's a group
                                List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                                if (tokens.Count > 0 && Int32.TryParse(tokens[0], out groupId) && groupId == dispersalGroup) {
                                    found = true;
                                    foreach (String nm in names) {
                                        if (tokens.Contains(nm)) {
                                            tokens.Remove(nm);
                                        }
                                    }
                                    if (tokens.Count > 1) {
                                        copy[n] = String.Join(" ", tokens.ToArray());
                                    } else {
                                        // Remove the whole item
                                        remove = copy[n];
                                    }
                                    break;
                                }
                            }
                        }
                        if (found) {
                            list.Clear();
                            list.AddRange(copy);
                            if (!String.IsNullOrEmpty(remove)) {
                                list.Remove(remove);
                            }
                            lines.Add("Deleted " + names.Count + " names from Dispersal Group " + groupId);
                        } else {
                            lines.Add("Can't find Dispersal Group " + groupId + ", delete failed!");
                            SayLines(lines, name);
                            return;
                        }
                    } else {
                        foreach (String nm in names) {
                            player = GetPlayer(nm);
                            if (player != null && !IsInDispersalList(player, true)) {
                                lines.Add("Can't find name ^b" + nm + "^n, delete failed!");
                                SayLines(lines, name);
                                return;
                            }
                            list.Remove(nm);
                        }
                        lines.Add("Deleted " + names.Count + " names from Disperse Evenly List");
                    }
                    ForceSetPluginVariable("1 - Settings|Disperse Evenly List", list.ToArray());
                } else if (listName == "Friends") {
                    bool found = false;
                    String remove = String.Empty;
                    String[] copy = fSettingFriendsList.ToArray();
                    list = new List<String>();
                    list.AddRange(fSettingFriendsList);
                    for (int n = 0; n < copy.Length; ++n) {
                        // Find a token in the line that contains a match
                        List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                        foreach (String nm in names) {
                            if (tokens.Contains(nm)) {
                                found = true;
                                tokens.Remove(nm);
                            }
                        }
                        if (tokens.Count > 1) {
                            copy[n] = String.Join(" ", tokens.ToArray());
                        } else {
                            // Remove the whole item
                            remove = copy[n];
                        }
                    }
                    if (found) {
                        list.Clear();
                        list.AddRange(copy);
                        if (!String.IsNullOrEmpty(remove)) list.Remove(remove);
                        lines.Add("Deleted " + names.Count + " names from Friends List");
                    } else {
                        lines.Add("Can't find any matching friends in Friends List, delete failed!");
                        SayLines(lines, name);
                        return;
                    }
                    ForceSetPluginVariable("1 - Settings|Friends List", list.ToArray());
                } else if (listName == "Whitelist") {
                    bool found = false;
                    String[] copy = fSettingWhitelist.ToArray();
                    list = new List<String>();
                    list.AddRange(fSettingWhitelist);
                    // Check for match
                    foreach (String nm in names) {
                        for (int n = 0; n < copy.Length; ++n) {
                            // Find matches
                            List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                            if (tokens.Contains(nm)) {
                                list.Remove(copy[n]);
                                found = true;
                            }
                        }
                    }
                    if (found) {
                        lines.Add("Deleted " + names.Count + " names from Whitelist");
                    } else {
                        lines.Add("Can't find any matching names in Whitelist, delete failed!");
                    }
                    ForceSetPluginVariable("1 - Settings|Whitelist", list.ToArray());
                }
                break;
            case IGCommand.List:
                String buffer = String.Empty;
                bool first = true;
                if (listName == "Dispersal") {
                    for (int j = 1; j <= 4; ++j) {
                        if (fDispersalGroups[j].Count > 0) {
                            String dg = j.ToString() + " " + String.Join(" ", fDispersalGroups[j].ToArray());
                            if (first) {
                                buffer = dg;
                                first = false;
                            } else {
                                buffer = buffer + "; " + dg;
                            }
                        }
                    }
                    foreach (String item in fSettingDisperseEvenlyList) {
                        if (first) {
                            buffer = item;
                            first = false;
                        } else {
                            buffer = buffer + "; " + item;
                        }
                    }
                    lines.Add(buffer);
                } else if (listName == "Friends") {
                    foreach (String item in fSettingFriendsList) {
                        if (first) {
                            buffer = item;
                            first = false;
                        } else {
                            buffer = buffer + "; " + item;
                        }
                    }
                    lines.Add(buffer);
                } else if (listName == "Whitelist") {
                    foreach (String item in fSettingWhitelist) {
                        if (first) {
                            buffer = item;
                            first = false;
                        } else {
                            buffer = buffer + "; " + item;
                        }
                    }
                    lines.Add(buffer);
                }
                break;
            case IGCommand.New:
                if (listName == "Dispersal") {
                    if (dispersalGroup != 0) {
                        int groupId = 0;
                        String[] copy = (String[])DisperseEvenlyList.Clone();
                        list = new List<String>();
                        list.AddRange(DisperseEvenlyList);
                        for (int n = 0; n < copy.Length; ++n) {
                            if (Regex.Match(copy[n], @"^[1234]\s+").Success) {
                                // It's a group
                                List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                                if (tokens.Count > 0 && Int32.TryParse(tokens[0], out groupId) && groupId == dispersalGroup) {
                                    lines.Add("Dispersal Group " + groupId + " already exists, new failed!");
                                    SayLines(lines, name);
                                    return;
                                }
                            }
                        }
                        list.Add(groupId + " " + String.Join(" ", names.ToArray()));
                        lines.Add("Created Dispersal Group " + groupId + " with " + names.Count + " names in Disperse Evenly List");
                    } else {
                        foreach (String nm in names) {
                            player = GetPlayer(nm);
                            if (player != null && IsInDispersalList(player, true)) {
                                lines.Add("Duplicate name ^b" + nm + "^n, new failed!");
                                SayLines(lines, name);
                                return;
                            }
                            list.Add(nm);
                        }
                        lines.Add("Created " + names.Count + " new names in Disperse Evenly List");
                    }
                    ForceSetPluginVariable("1 - Settings|Disperse Evenly List", list.ToArray());
                } else if (listName == "Friends") {
                    if (names.Count < 2) {
                        lines.Add("New friends must have at least 2 names, new failed!");
                        SayLines(lines, name);
                        return;
                    }
                    bool found = false;
                    String[] copy = fSettingFriendsList.ToArray();
                    list = new List<String>();
                    list.AddRange(fSettingFriendsList);
                    for (int n = 0; n < copy.Length; ++n) {
                        // Find a line in the list that contains the nameMatch string
                        List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                        foreach (String nm in names) {
                            if (tokens.Contains(nm)) {
                                found = true;
                                break;
                            }
                        }
                        if (found) break;
                    }
                    if (!found) {
                        list.Add(String.Join(" ", names.ToArray()));
                        lines.Add("Created " + names.Count + " new names in Friends List");
                    } else {
                        lines.Add("Duplicate names in Friends List, new failed!");
                        SayLines(lines, name);
                        return;
                    }
                    ForceSetPluginVariable("1 - Settings|Friends List", list.ToArray());
                } else if (listName == "Whitelist") {
                    String[] copy = fSettingWhitelist.ToArray();
                    list = new List<String>();
                    list.AddRange(fSettingWhitelist);
                    // Check for duplication
                    foreach (String nm in names) {
                        for (int n = 0; n < copy.Length; ++n) {
                            // Find matches
                            List<String> tokens = new List<String>(Regex.Split(copy[n], @"\s+"));
                            if (tokens.Contains(nm)) {
                                lines.Add("Duplicate name ^b" + nm + "^n, new failed!");
                                SayLines(lines, name);
                                return;
                            }
                        }
                        // ok to add
                        list.Add(nm);
                    }
                    lines.Add("Created " + names.Count + " new names in Whitelist");
                    ForceSetPluginVariable("1 - Settings|Whitelist", list.ToArray());
                }
                break;
            default:
                break;
            }

            // Send the results
            SayLines(lines, name);
            return;
            }

            // Unknown command
            lines = new List<String>();
            lines.Add("Unknown command: " + msg);
            SayLines(lines, name);
        }
MULTIbalancer