示例#1
0
文件: VoteForm.cs 项目: ransty/voting
        /**
         * Calculate the current round when the button COUNT is pressed.
         * Takes no parameters as it uses the Vote object that is stored when importing.
         **/
        private int cal()// Did some one win, who cuts
        {
            barChartToolStripMenuItem.Enabled = true;
            switch (cand.Length)
            {
            case 1:
                candidateController.candLabel1.Text = cand[0];
                break;

            case 2:
                candidateController.candLabel2.Text = cand[1];
                goto case 1;

            case 3:
                candidateController.candLabel3.Text = cand[2];
                goto case 2;

            case 4:
                candidateController.candLabel4.Text = cand[3];
                goto case 3;

            case 5:
                candidateController.candLabel5.Text = cand[4];
                goto case 4;

            case 6:
                candidateController.candLabel6.Text = cand[5];
                goto case 5;

            case 7:
                candidateController.candLabel7.Text = cand[6];
                goto case 6;

            case 8:
                candidateController.candLabel8.Text = cand[7];
                goto case 7;

            case 9:
                candidateController.candLabel9.Text = cand[8];
                goto case 8;

            case 10:
                candidateController.candLabel10.Text = cand[9];
                goto case 9;

            default:
                break;
            }

            int[]    totalForCand = new int[cand.Length];
            double[] percent      = new double[cand.Length];
            int      totalVotes   = VotingRoll.Count;

            //for  each vote
            for (int i = 0; i < VotingRoll.Count; i++)
            {
                Vote Current = VotingRoll.ElementAt(i);
                if (Current.getValid())
                {
                    //Get the first vote
                    for (int index = 0; index < Current.Votes.Length; index++)
                    {
                        if (Current.Votes[index] == 1)
                        {
                            totalForCand[index] += 1;
                        }
                    }
                }
            }

            switch (cand.Length)
            {
            case 1:
                candidateController.scoreLabel1.Text = totalForCand[0].ToString();
                break;

            case 2:
                candidateController.scoreLabel2.Text = totalForCand[1].ToString();
                goto case 1;

            case 3:
                candidateController.scoreLabel3.Text = totalForCand[2].ToString();
                goto case 2;

            case 4:
                candidateController.scoreLabel4.Text = totalForCand[3].ToString();
                goto case 3;

            case 5:
                candidateController.scoreLabel5.Text = totalForCand[4].ToString();
                goto case 4;

            case 6:
                candidateController.scoreLabel6.Text = totalForCand[5].ToString();
                goto case 5;

            case 7:
                candidateController.scoreLabel7.Text = totalForCand[6].ToString();
                goto case 6;

            case 8:
                candidateController.scoreLabel8.Text = totalForCand[7].ToString();
                goto case 7;

            case 9:
                candidateController.scoreLabel9.Text = totalForCand[8].ToString();
                goto case 8;

            case 10:
                candidateController.scoreLabel10.Text = totalForCand[9].ToString();
                goto case 9;

            default:
                break;
            }

            //
            for (int i = 0; i < totalForCand.Length; i++)
            {
                percent[i] = (totalForCand[i] * 100) / totalVotes;
                if (percent[i] > 50)
                {
                    VotingOver = true;

                    WinningCand = cand[i];
                }
            }

            int lowest   = totalForCand[0];
            int lowIndex = 0;

            // cut people out if noone is a winner
            for (int i = 0; i < totalForCand.Length; i++)
            {
                if (totalForCand[i] < 0)
                {
                    continue;
                }

                if (totalForCand[i] < lowest && !CuttedCand.Contains(i))
                {
                    lowest   = totalForCand[i];
                    lowIndex = i;
                }
                //
                //Console.WriteLine("Candidate " + cand[i] + " has the total percentage of Votes of: " + percent[i] + "%");
            }
            // results for round x
            String[] results = new string[cand.Length + 1]; // Round , score/ cut ++++
            results[0] = rounds.Count.ToString();

            for (int loop = 0; loop < cand.Length; loop++)
            {
                if (CuttedCand.Contains(loop) || loop == lowIndex)
                {//P if cut
                    results[loop + 1] = "P";
                }
                else
                {
                    results[loop + 1] = totalForCand[loop].ToString();
                }
            }
            rounds.Add(results);
            currentVotes = new int[cand.Length];
            currentVotes = totalForCand;
            return(lowIndex);
        }
示例#2
0
        private void Vote(CommandArgs args)
        {
            if (args.Parameters.Count < 2)
            {
                args.Player.SendErrorMessage("Invalid syntax!" +
                                             " Proper syntax: /voting [kick/mute/ban/yes/no/info/cancel] [vote (name or index)]");
                return;
            }

            var voteIndex = args.Parameters[1];
            Vote vote;

            switch (args.Parameters[0])
            {
                #region Yes

                case "yes":
                    vote = Votes.GetVote(voteIndex);
                    if (vote == null)
                    {
                        args.Player.SendErrorMessage("Vote not found");
                        return;
                    }
                    if (!vote.voters.Contains(args.Player) && vote.active)
                    {
                        args.Player.SendSuccessMessage("You have voted yes to {0} {1}", vote.voteType, vote.votedplayer.Name);
                        vote.voters.Add(args.Player);
                        vote.votedyes.Add(args.Player);
                        return;
                    }
                    if (vote.voters.Contains(args.Player))
                    {
                        args.Player.SendErrorMessage("You have already voted for this vote!");
                        return;
                    }

                    args.Player.SendInfoMessage("A vote is not running at this time.");
                    return;

                    #endregion

                #region No

                case "no":
                    vote = Votes.GetVote(voteIndex);
                    if (vote == null)
                    {
                        args.Player.SendErrorMessage("Vote not found!");
                        return;
                    }
                    if (!vote.voters.Contains(args.Player) && vote.active)
                    {
                        args.Player.SendSuccessMessage("You have voted no to {0} {1}", vote.voteType, vote.votedplayer.Name);
                        vote.voters.Add(args.Player);
                        vote.votedno.Add(args.Player);
                        return;
                    }
                    if (vote.voters.Contains(args.Player))
                    {
                        args.Player.SendErrorMessage("You have already voted for this vote!");
                        return;
                    }

                    args.Player.SendInfoMessage("A vote is not running at this time.");
                    return;

                    #endregion

                #region Kick

                case "kick":
                    if (config.CanPlayersVoteKick)
                    {
                        if (TShock.Utils.ActivePlayers() >= config.AmountofPlayersForVotesToTakeEffect)
                        {
                            if (args.Parameters.Count > 1)
                            {
                                var plStr = args.Parameters.Count > 3
                                    ? String.Join(" ",
                                        args.Parameters.GetRange(1, args.Parameters.Count - 2)).ToLower()
                                    : String.Join(" ",
                                        args.Parameters.GetRange(1, args.Parameters.Count - 1)).ToLower();

                                var players = TShock.Utils.FindPlayer(plStr);

                                if (players.Count == 0)
                                {
                                    args.Player.SendErrorMessage("No users matched your query '{0}'", plStr);
                                    return;
                                }

                                if (players.Count > 1)
                                {
                                    TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
                                    return;
                                }

                                var users = new List<User>();

                                foreach (
                                    var u in TShock.Users.GetUsers().FindAll(u => u.Name.ToLower().StartsWith(plStr)))
                                {
                                    if (u.Name.ToLower() == plStr)
                                    {
                                        users = new List<User> {u};
                                        break;
                                    }
                                    if (u.Name.ToLower().StartsWith(plStr))
                                        users.Add(u);
                                }

                                if (users.Count == 0)
                                    args.Player.SendErrorMessage("No users matched your query '{0}'", plStr);

                                else if (users.Count > 1)
                                    TShock.Utils.SendMultipleMatchError(args.Player, users.Select(p => p.Name));

                                var user = users[0];

                                if (Votes.ContainsKey(user.Name))
                                {
                                    args.Player.SendErrorMessage("Someone has already started a vote to {0} {1}",
                                        Votes[user.Name].voteType.ToString(), user.Name);
                                    return;
                                }

                                vote = new Vote
                                {
                                    active = true,
                                    votedplayer = user,
                                    voteType = VoteType.kick,
                                    timestarted = DateTime.Now,
                                    votestarter = args.Player
                                };
                                vote.votedyes.Add(args.Player);
                                vote.voters.Add(args.Player);

                                Votes.Add(user.Name, vote);

                                TShock.Utils.Broadcast(string.Format("{0} has started a vote to kick {1}",
                                    args.Player.Name, user.Name),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                                _timers.Start();
                            }
                            else
                                args.Player.SendErrorMessage("Error! Please use /voting kick <playername>");
                        }
                        else
                            args.Player.SendErrorMessage(
                                "There are not enough players online to start a vote! {0} more players required.",
                                config.AmountofPlayersForVotesToTakeEffect - TShock.Utils.ActivePlayers());
                    }
                    else
                        args.Player.SendErrorMessage("Vote kicking has been disabled by the server owner.");

                    break;

                    #endregion

                #region Mute

                case "mute":
                    if (config.CanPlayersVoteMute)
                    {
                        if (TShock.Utils.ActivePlayers() >= config.AmountofPlayersForVotesToTakeEffect)
                        {
                            if (args.Parameters.Count > 1)
                            {
                                var plStr = args.Parameters.Count > 3
                                    ? String.Join(" ",
                                        args.Parameters.GetRange(1, args.Parameters.Count - 2)).ToLower()
                                    : String.Join(" ",
                                        args.Parameters.GetRange(1, args.Parameters.Count - 1)).ToLower();

                                var players = TShock.Utils.FindPlayer(plStr);

                                if (players.Count == 0)
                                {
                                    args.Player.SendErrorMessage("No users matched your query '{0}'", plStr);
                                    return;
                                }

                                if (players.Count > 1)
                                {
                                    TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
                                    return;
                                }

                                var users = new List<User>();

                                foreach (
                                    var u in TShock.Users.GetUsers().FindAll(u => u.Name.ToLower().StartsWith(plStr)))
                                {
                                    if (u.Name.ToLower() == plStr)
                                    {
                                        users = new List<User> { u };
                                        break;
                                    }
                                    if (u.Name.ToLower().StartsWith(plStr))
                                        users.Add(u);
                                }

                                if (users.Count == 0)
                                    args.Player.SendErrorMessage("No users matched your query '{0}'", plStr);

                                else if (users.Count > 1)
                                    TShock.Utils.SendMultipleMatchError(args.Player, users.Select(p => p.Name));

                                var user = users[0];

                                if (Votes.ContainsKey(user.Name))
                                {
                                    args.Player.SendErrorMessage("Someone has already started a vote to {0} {1}",
                                        Votes[user.Name].voteType.ToString(), user.Name);
                                    return;
                                }

                                vote = new Vote
                                {
                                    active = true,
                                    votedplayer = user,
                                    voteType = VoteType.mute,
                                    timestarted = DateTime.Now,
                                    votestarter = args.Player
                                };
                                vote.votedyes.Add(args.Player);
                                vote.voters.Add(args.Player);

                                Votes.Add(user.Name, vote);

                                TShock.Utils.Broadcast(string.Format("{0} has started a vote to mute {1}",
                                    args.Player.Name, user.Name),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                                _timers.Start();
                            }
                            else
                                args.Player.SendErrorMessage("Error! Please use /voting mute <playername>");
                        }
                        else
                            args.Player.SendErrorMessage(
                                "There are not enough players online to start a vote! {0} more players required.",
                                config.AmountofPlayersForVotesToTakeEffect - TShock.Utils.ActivePlayers());
                    }
                    else
                        args.Player.SendErrorMessage("Vote muting has been disabled by the server owner.");

                    break;

                #endregion

                #region Ban

                case "ban":
                    if (config.CanPlayersVoteBan)
                    {
                        if (TShock.Utils.ActivePlayers() >= config.AmountofPlayersForVotesToTakeEffect)
                        {
                            if (args.Parameters.Count > 1)
                            {
                                var plStr = args.Parameters.Count > 3
                                    ? String.Join(" ",
                                        args.Parameters.GetRange(1, args.Parameters.Count - 2)).ToLower()
                                    : String.Join(" ",
                                        args.Parameters.GetRange(1, args.Parameters.Count - 1)).ToLower();

                                var players = TShock.Utils.FindPlayer(plStr);

                                if (players.Count == 0)
                                {
                                    args.Player.SendErrorMessage("No users matched your query '{0}'", plStr);
                                    return;
                                }

                                if (players.Count > 1)
                                {
                                    TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
                                    return;
                                }

                                var users = new List<User>();

                                foreach (
                                    var u in TShock.Users.GetUsers().FindAll(u => u.Name.ToLower().StartsWith(plStr)))
                                {
                                    if (u.Name.ToLower() == plStr)
                                    {
                                        users = new List<User> { u };
                                        break;
                                    }
                                    if (u.Name.ToLower().StartsWith(plStr))
                                        users.Add(u);
                                }

                                if (users.Count == 0)
                                    args.Player.SendErrorMessage("No users matched your query '{0}'", plStr);

                                else if (users.Count > 1)
                                    TShock.Utils.SendMultipleMatchError(args.Player, users.Select(p => p.Name));

                                var user = users[0];

                                if (Votes.ContainsKey(user.Name))
                                {
                                    args.Player.SendErrorMessage("Someone has already started a vote to {0} {1}",
                                        Votes[user.Name].voteType.ToString(), user.Name);
                                    return;
                                }

                                vote = new Vote
                                {
                                    active = true,
                                    votedplayer = user,
                                    voteType = VoteType.ban,
                                    timestarted = DateTime.Now,
                                    votestarter = args.Player
                                };
                                vote.votedyes.Add(args.Player);
                                vote.voters.Add(args.Player);

                                Votes.Add(user.Name, vote);

                                TShock.Utils.Broadcast(string.Format("{0} has started a vote to ban {1}",
                                    args.Player.Name, user.Name),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                                    Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                                _timers.Start();
                            }
                            else
                                args.Player.SendErrorMessage("Error! Please use /voting ban <playername>");
                        }
                        else
                            args.Player.SendErrorMessage(
                                "There are not enough players online to start a vote! {0} more players required.",
                                config.AmountofPlayersForVotesToTakeEffect - TShock.Utils.ActivePlayers());
                    }
                    else
                        args.Player.SendErrorMessage("Vote banning has been disabled by the server owner.");

                    break;

                #endregion

                #region Info

                case "info":
                    vote = Votes.GetVote(voteIndex);
                    if (vote == null)
                    {
                        args.Player.SendErrorMessage("Vote not found!");
                        return;
                    }

                    args.Player.SendInfoMessage("Vote {0} targetting {1}",
                        vote.voteType.ToString(), vote.votedplayer.Name);
                    args.Player.SendInfoMessage("Players voted: {0}. Yes:No - {1}:{2}",
                        vote.voters.Count, vote.votedyes.Count, vote.votedno.Count);

                    break;

                    #endregion

                #region Cancel

                case "cancel":
                    if (args.Player.Group.HasPermission("caw.cancelvotekick"))
                    {
                        vote = Votes.GetVote(voteIndex);
                        if (vote == null)
                        {
                            args.Player.SendErrorMessage("Vote not found!");
                            return;
                        }
                        var reason = args.Parameters.Count > 3
                            ? string.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count))
                            : "cancelled";
                        vote.Kill(string.Format("Vote to {0} {1} ended by {2} ({3})",
                            vote.voteType, vote.votedplayer.Name, args.Player.Name, reason));
                    }
                    else
                        args.Player.SendErrorMessage("You do not have permission to use that command!");
                    break;

                    #endregion
            }
        }
示例#3
0
        private void DoVote(Vote vote)
        {
            var active = TShock.Utils.ActivePlayers();
            var percent = active/(double) Voting.config.AmountofPlayersForVotesToTakeEffect;

            if (vote.votedyes.Count > vote.votedno.Count && vote.votedyes.Count >= percent)
            {
                switch (vote.voteType)
                {
                    case VoteType.ban:
                        var ips = JsonConvert.DeserializeObject<List<string>>(vote.votedplayer.KnownIps);
                        var ip = ips[ips.Count - 1];
                        TShock.Bans.AddBan(ip, vote.votedplayer.Name, vote.votedplayer.UUID,
                            "vote banned", false, vote.votestarter.Name);
                        TShock.Utils.Kick(TShock.Utils.FindPlayer(vote.votedplayer.Name)[0],
                            "vote banned", true, false, vote.votestarter.Name);

                        TShock.Utils.Broadcast(string.Format("{0} was banned (Yes:No - {1}:{2})",
                            vote.votedplayer.Name, vote.votedyes.Count, vote.votedno.Count),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    case VoteType.kick:
                        if (TShock.Utils.FindPlayer(vote.votedplayer.Name).Count == 1)
                            TShock.Utils.Kick(TShock.Utils.FindPlayer(vote.votedplayer.Name)[0],
                                "vote kicked", true, false, vote.votestarter.Name);

                        TShock.Utils.Broadcast(string.Format("{0} was kicked (Yes:No - {1}:{2})",
                            vote.votedplayer.Name, vote.votedyes.Count, vote.votedno.Count),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    case VoteType.mute:
                    {
                        if (TShock.Utils.FindPlayer(vote.votedplayer.Name).Count == 1)
                            TShock.Utils.FindPlayer(vote.votedplayer.Name)[0].mute = true;

                        TShock.Utils.Broadcast(string.Format("{0} was muted (Yes:No - {1}:{2})",
                            vote.votedplayer.Name, vote.votedyes.Count, vote.votedno.Count),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    }
                }
            }

            if (vote.votedno.Count > vote.votedyes.Count && vote.votedno.Count > percent)
            {
                switch (vote.voteType)
                {
                    case VoteType.ban:

                        TShock.Utils.Broadcast(string.Format("{0} was not banned (Yes:No - {1}:{2})",
                            vote.votedplayer.Name, vote.votedyes.Count, vote.votedno.Count),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    case VoteType.kick:

                        TShock.Utils.Broadcast(string.Format("{0} was not kicked (Yes:No - {1}:{2})",
                            vote.votedplayer.Name, vote.votedyes.Count, vote.votedno.Count),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    case VoteType.mute:
                    {
                        TShock.Utils.Broadcast(string.Format("{0} was not muted (Yes:No - {1}:{2})",
                            vote.votedplayer.Name, vote.votedyes.Count, vote.votedno.Count),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    }
                }
            }

            if (vote.votedno.Count < percent && vote.votedyes.Count < percent)
            {
                switch (vote.voteType)
                {
                    case VoteType.ban:

                        TShock.Utils.Broadcast(string.Format("{0} was not banned (Not enough votes)",
                            vote.votedplayer.Name),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    case VoteType.kick:

                        TShock.Utils.Broadcast(string.Format("{0} was not kicked (Not enough votes)",
                            vote.votedplayer.Name),
                            Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                            Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                        break;
                    case VoteType.mute:
                        {
                            TShock.Utils.Broadcast(string.Format("{0} was not muted (Not enough votes)",
                                vote.votedplayer.Name),
                                Convert.ToByte(TShock.Config.BroadcastRGB[0]),
                                Convert.ToByte(TShock.Config.BroadcastRGB[1]),
                                Convert.ToByte(TShock.Config.BroadcastRGB[2]));

                            break;
                        }
                }
            }
            vote.Kill();
        }