Esempio n. 1
0
        private void cmdChatClan(BasePlayer player, string command, string[] args)
        {
            var userId = player.userID.ToString();
            var myClan = FindClanByUser(userId);
            var sb     = new StringBuilder();

            // No arguments: List clans and get help how to create one
            if (args.Length == 0)
            {
                sb.Append("<size=22>Clans</size> " + Version + " by <color=#ce422b>http://playrust.io</color>\n");
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan.")).Append("\n");
                }
                else
                {
                    if (myClan.IsOwner(userId))
                    {
                        sb.Append(_("You are the owner of:"));
                    }
                    else if (myClan.IsModerator(userId))
                    {
                        sb.Append(_("You are a moderator of:"));
                    }
                    else
                    {
                        sb.Append(_("You are a member of:"));
                    }
                    sb.Append(" [").Append(myClan.tag).Append("] ").Append(myClan.description).Append("\n");
                    sb.Append(_("Members online:")).Append(" ");
                    List <string> onlineMembers = new List <string>();
                    int           n             = 0;
                    foreach (var memberId in myClan.members)
                    {
                        var p = BasePlayer.FindByID(Convert.ToUInt64(memberId));
                        if (p != null)
                        {
                            if (n > 0)
                            {
                                sb.Append(", ");
                            }
                            if (myClan.IsOwner(memberId))
                            {
                                sb.Append("<color=#a1ff46>").Append(StripTag(p.displayName, myClan)).Append("</color>");
                            }
                            else if (myClan.IsModerator(memberId))
                            {
                                sb.Append("<color=#74c6ff>").Append(StripTag(p.displayName, myClan)).Append("</color>");
                            }
                            else
                            {
                                sb.Append(p.displayName);
                            }
                            ++n;
                        }
                    }
                    sb.Append("\n");
                    if ((myClan.IsOwner(userId) || myClan.IsModerator(userId)) && myClan.invited.Count > 0)
                    {
                        sb.Append(_("Pending invites:")).Append(" ");
                        int m = 0;
                        foreach (var inviteId in myClan.invited)
                        {
                            var p = BasePlayer.FindByID(Convert.ToUInt64(inviteId));
                            if (p != null)
                            {
                                if (m > 0)
                                {
                                    sb.Append(", ");
                                }
                                sb.Append(p.displayName);
                                ++m;
                            }
                        }
                        sb.Append("\n");
                    }
                }
                sb.Append(_("To learn more about clans, type: <color=\"#ffd479\">/clan help</color>"));
                SendReply(player, "{0}", sb.ToString());
                return;
            }
            switch (args[0])
            {
            case "create":
                if (args.Length != 3)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan create \"TAG\" \"Description\"</color>"));
                    break;
                }
                if (myClan != null)
                {
                    sb.Append(_("You are already a member of a clan."));
                    break;
                }
                if (!tagRe.IsMatch(args[1]))
                {
                    sb.Append(_("Clan tags must be 2 to 6 characters long and may contain standard letters and numbers only"));
                    break;
                }
                args[2] = args[2].Trim();
                if (args[2].Length < 2 || args[2].Length > 30)
                {
                    sb.Append(_("Please provide a short description of your clan."));
                    break;
                }
                if (clans.ContainsKey(args[1]))
                {
                    sb.Append(_("There is already a clan with this tag."));
                    break;
                }
                myClan = Clan.Create(args[1], args[2], userId);
                clans.Add(myClan.tag, myClan);
                SaveData();
                lookup[userId] = myClan;
                SetupPlayer(player);     // Add clan tag
                sb.Append(_("You are now the owner of your new clan:")).Append(" ");
                sb.Append("[").Append(myClan.tag).Append("] ").Append(myClan.description).Append("\n");
                sb.Append(_("To invite new members, type: <color=\"#ffd479\">/clan invite \"Player name\"</color>"));
                break;

            case "invite":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan invite \"Player name\"</color>"));
                    break;
                }
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan."));
                    break;
                }
                if (!myClan.IsOwner(userId) && !myClan.IsModerator(userId))
                {
                    sb.Append(_("You need to be a moderator of your clan to use this command."));
                    break;
                }
                var invPlayer = FindPlayerByPartialName(args[1]);
                if (invPlayer == null)
                {
                    sb.Append(_("No such player or player name not unique:")).Append(" ").Append(args[1]);
                    break;
                }
                var invUserId = invPlayer.userID.ToString();
                if (myClan.members.Contains(invUserId))
                {
                    sb.Append(_("This player is already a member of your clan:")).Append(" ").Append(invPlayer.displayName);
                    break;
                }
                if (myClan.invited.Contains(invUserId))
                {
                    sb.Append(_("This player has already been invited to your clan:")).Append(" ").Append(invPlayer.displayName);
                    break;
                }
                myClan.invited.Add(invUserId);
                SaveData();
                myClan.Broadcast(_("%MEMBER% invited %PLAYER% to the clan.", new Dictionary <string, string>()
                {
                    { "MEMBER", StripTag(player.displayName, myClan) }, { "PLAYER", invPlayer.displayName }
                }));
                invPlayer.SendConsoleCommand("chat.add", "",
                                             _("You have been invited to join the clan:") + " [" + myClan.tag + "] " + myClan.description + "\n" +
                                             _("To join, type: <color=#ffd479>/clan join \"%TAG%\"</color>", new Dictionary <string, string>()
                {
                    { "TAG", myClan.tag }
                }));
                break;

            case "join":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan join \"TAG\"</color>"));
                    break;
                }
                if (myClan != null)
                {
                    sb.Append(_("You are already a member of a clan."));
                    break;
                }
                myClan = FindClan(args[1]);
                if (myClan == null || !myClan.IsInvited(userId))
                {
                    sb.Append(_("You have not been invited to join this clan."));
                    break;
                }
                if (limitMembers >= 0 && myClan.members.Count > limitMembers)
                {
                    sb.Append(_("This clan has already reached the maximum number of members."));
                    break;
                }
                myClan.invited.Remove(userId);
                myClan.members.Add(userId);
                SaveData();
                lookup[userId] = myClan;
                SetupPlayer(player);
                myClan.Broadcast(_("%NAME% has joined the clan!", new Dictionary <string, string>()
                {
                    { "NAME", StripTag(player.displayName, myClan) }
                }));
                foreach (var memberId in myClan.members)
                {
                    if (memberId != userId && IsInstalled() && addClanMatesAsFriends)
                    {
                        AddFriend(memberId, userId);
                        AddFriend(userId, memberId);
                    }
                }
                break;

            case "promote":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan promote \"Player name\"</color>"));
                    break;
                }
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan."));
                    break;
                }
                if (!myClan.IsOwner(userId))
                {
                    sb.Append(_("You need to be the owner of your clan to use this command."));
                    break;
                }
                var promotePlayer = FindPlayerByPartialName(args[1]);
                if (promotePlayer == null)
                {
                    sb.Append(_("No such player or player name not unique:") + " " + args[1]);
                    break;
                }
                var promotePlayerUserId = promotePlayer.userID.ToString();
                if (!myClan.IsMember(promotePlayerUserId))
                {
                    sb.Append(_("This player is not a member of your clan:") + " " + promotePlayer.displayName);
                    break;
                }
                if (myClan.IsModerator(promotePlayerUserId))
                {
                    sb.Append(_("This player is already a moderator of your clan:") + " " + promotePlayer.displayName);
                    break;
                }
                myClan.moderators.Add(promotePlayerUserId);
                SaveData();
                myClan.Broadcast(_("%OWNER% promoted %MEMBER% to moderator.", new Dictionary <string, string>()
                {
                    { "OWNER", StripTag(player.displayName, myClan) }, { "MEMBER", StripTag(promotePlayer.displayName, myClan) }
                }));
                break;

            case "demote":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan demote \"Player name\"</color>"));
                    break;
                }
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan."));
                    break;
                }
                if (!myClan.IsOwner(userId))
                {
                    sb.Append(_("You need to be the owner of your clan to use this command."));
                    break;
                }
                var demotePlayer = FindPlayerByPartialName(args[1]);
                if (demotePlayer == null)
                {
                    sb.Append(_("No such player or player name not unique:") + " " + args[1]);
                    break;
                }
                var demotePlayerUserId = demotePlayer.userID.ToString();
                if (!myClan.IsMember(demotePlayerUserId))
                {
                    sb.Append(_("This player is not a member of your clan:") + " " + demotePlayer.displayName);
                    break;
                }
                if (!myClan.IsModerator(demotePlayerUserId))
                {
                    sb.Append(_("This player is not a moderator of your clan:") + " " + demotePlayer.displayName);
                    break;
                }
                myClan.moderators.Remove(demotePlayerUserId);
                SaveData();
                myClan.Broadcast(player.displayName + " demoted " + demotePlayer.displayName + " to a member");
                break;

            case "leave":
                if (args.Length != 1)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan leave</color>"));
                    break;
                }
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan."));
                    break;
                }
                if (myClan.members.Count == 1)       // Remove the clan once the last member leaves
                {
                    clans.Remove(myClan.tag);
                }
                else
                {
                    myClan.moderators.Remove(userId);
                    myClan.members.Remove(userId);
                    myClan.invited.Remove(userId);
                    if (myClan.IsOwner(userId) && myClan.members.Count > 0)       // Make the first member the new owner
                    {
                        myClan.owner = myClan.members[0];
                    }
                }
                SaveData();
                lookup.Remove(userId);
                SetupPlayer(player);     // Remove clan tag
                sb.Append(_("You have left your current clan."));
                myClan.Broadcast(_("%NAME% has left the clan.", new Dictionary <string, string>()
                {
                    { "NAME", player.displayName }
                }));
                break;

            case "kick":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan kick \"Player name\"</color>"));
                    break;
                }
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan."));
                    break;
                }
                if (!myClan.IsOwner(userId) && !myClan.IsModerator(userId))
                {
                    sb.Append(_("You need to be a moderator of your clan to use this command."));
                    break;
                }
                var kickPlayer = FindPlayerByPartialName(args[1]);
                if (kickPlayer == null)
                {
                    sb.Append(_("No such player or player name not unique:") + " " + args[1]);
                    break;
                }
                var kickPlayerUserId = kickPlayer.userID.ToString();
                if (!myClan.IsMember(kickPlayerUserId) && !myClan.IsInvited(kickPlayerUserId))
                {
                    sb.Append(_("This player is not a member of your clan:") + " " + kickPlayer.displayName);
                    break;
                }
                if (myClan.IsOwner(kickPlayerUserId) || myClan.IsModerator(kickPlayerUserId))
                {
                    sb.Append(_("This player is an owner or moderator and cannot be kicked:") + " " + kickPlayer.displayName);
                    break;
                }
                myClan.members.Remove(kickPlayerUserId);
                myClan.invited.Remove(kickPlayerUserId);
                SaveData();
                lookup.Remove(kickPlayerUserId);
                SetupPlayer(kickPlayer);     // Remove clan tag
                myClan.Broadcast(_("%NAME% kicked %MEMBER% from the clan.", new Dictionary <string, string>()
                {
                    { "NAME", StripTag(player.displayName, myClan) }, { "MEMBER", kickPlayer.displayName }
                }));
                break;

            case "disband":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan disband forever</color>"));
                    break;
                }
                if (myClan == null)
                {
                    sb.Append(_("You are currently not a member of a clan."));
                    break;
                }
                if (!myClan.IsOwner(userId))
                {
                    sb.Append(_("You need to be the owner of your clan to use this command."));
                    break;
                }
                clans.Remove(myClan.tag);
                SaveData();
                foreach (var member in myClan.members)
                {
                    lookup.Remove(member);
                }
                myClan.Broadcast(_("Your current clan has been disbanded forever."));
                SetupPlayers(myClan.members);     // Remove clan tags
                break;

            case "delete":
                if (args.Length != 2)
                {
                    sb.Append(_("Usage: <color=\"#ffd479\">/clan delete \"TAG\"</color>"));
                    break;
                }
                if (player.net.connection.authLevel < 2)
                {
                    sb.Append(_("You need to be a server owner to delete clans."));
                    break;
                }
                Clan clan;
                if (!clans.TryGetValue(args[1], out clan))
                {
                    sb.Append(_("There is no clan with that tag:")).Append(" ").Append(args[1]);
                    break;
                }
                clan.Broadcast(_("Your clan has been deleted by the server owner."));
                clans.Remove(args[1]);
                SaveData();
                foreach (var member in clan.members)
                {
                    lookup.Remove(member);
                }
                SetupPlayers(clan.members);
                sb.Append(_("You have deleted the clan:")).Append(" [").Append(clan.tag).Append("] ").Append(clan.description);
                break;

            default:
                sb.Append(_("Available commands:")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan</color> - Displays relevant information about your current clan")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/c Message...</color> - Sends a message to all online clan members")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan create \"TAG\" \"Description\"</color> - Creates a new clan you own")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan join \"TAG\"</color> - Joins a clan you have been invited to")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan leave</color> - Leaves your current clan")).Append("\n");
                sb.Append(_("<color=#74c6ff>Moderator</color> commands:")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan invite \"Player name\"</color> - Invites a player to your clan")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan kick \"Player name\"</color> - Kicks a member from your clan")).Append("\n");
                sb.Append(_("<color=#a1ff46>Owner</color> commands:")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan promote \"Name\"</color> - Promotes a member to moderator")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan demote \"Name\"</color> - Demotes a moderator to member")).Append("\n");
                sb.Append("  ").Append(_("<color=#ffd479>/clan disband forever</color> - Disbands your clan (no undo)")).Append("\n");
                if (player.net.connection.authLevel >= 2)
                {
                    sb.Append(_("<color=#cd422b>Server owner</color> commands:")).Append("\n");
                    sb.Append("  ").Append(_("<color=#ffd479>/clan delete \"TAG\"</color> - Deletes a clan (no undo)")).Append("\n");
                }
                break;
            }
            SendReply(player, "{0}", sb.ToString().TrimEnd());
        }