Beispiel #1
0
        /// <summary>
        /// Gets the result and displays these results to the user
        /// </summary>
        /// <param name="sender">The handle to the button</param>
        /// <param name="e">The extra messages</param>
        private void CountVotesButton_Click(object sender, EventArgs e)
        {
            // Make sure there's data to calculate
            if (this.VotesGridView.Columns.Count > 1)
            {
                // Reset the lists to remove any existing data
                allVotes   = new VotesList();
                candidates = new List <string>();

                // Goes through and adds the candidates
                foreach (DataGridViewColumn col in this.VotesGridView.Columns)
                {
                    candidates.Add(col.Name);
                }

                // Goes through and adds the votes
                for (int i = 0; i < VotesGridView.Rows.Count - 1; i++)
                {
                    Vote vote = new Vote(); // Holds the current vote

                    // Goes through each cell and matches the candidate with the vote
                    foreach (DataGridViewCell cell in VotesGridView.Rows[i].Cells)
                    {
                        // If the cell is empty, then add a -1, this is important for counting invalid votes
                        if (cell.Value == null)
                        {
                            vote.Add(VotesGridView.Columns[cell.ColumnIndex].Name, -1);
                        }
                        else
                        {
                            int n;

                            // Checks for bad input
                            if (int.TryParse(cell.Value.ToString(), out n))
                            {
                                vote.Add(VotesGridView.Columns[cell.ColumnIndex].Name, n);
                            }

                            // If there's bad input
                            else
                            {
                                MessageBox.Show("Value must be a number");
                                return;
                            }
                        }
                    }

                    // Add the vote
                    allVotes.Add(vote);
                }

                Result finalResult = allVotes.calculateResult(candidates);  // Calculate the result

                // Checks for a result
                if (finalResult != null)
                {
                    // Displays the number of invalid votes
                    this.InvalidVotesLabel.Text = "" + finalResult.NumberOfInvalidVotes;

                    string winnerText = ""; // The text that displays which candidate won the vote

                    // If there's won winner, add the candidate to the string
                    if (finalResult.Winners.Count == 1)
                    {
                        winnerText = finalResult.Winners[0];
                    }

                    // If there's a tie, add each candidate to the string with corresponding English grammar
                    else
                    {
                        for (int i = 0; i < finalResult.Winners.Count; i++)
                        {
                            if (i == finalResult.Winners.Count - 1)
                            {
                                winnerText += finalResult.Winners[i] + " tie";
                            }
                            else
                            {
                                winnerText += finalResult.Winners[i] + " and ";
                            }
                        }
                    }

                    // Display the winner(s) and the chart
                    WinnerLabel.Text = winnerText;
                    Chart chart = new Chart(finalResult);
                    chart.ShowDialog();
                }
            }

            // If there isn't enough candidates
            else
            {
                MessageBox.Show("Must have at least 2 candidates", "Error");
            }
        }
Beispiel #2
0
                public async Task AddModule(string name, string what, string how)
                {
                    try
                    {
                        if (!Privileg.CheckById(Context.User.Id, Privileg.owner))
                        {
                            await Context.Channel.SendMessageAsync(embed : Classes.Embed.New(Context.Message.Author, Field.CreateFieldBuilder("warning", "You are not my god!"), Colors.warning));

                            Log.Warning($"command - data vote add - user:{Context.User.Id} channel:{Context.Channel.Id} privileg to low");
                            return;
                        }

                        Log.Information($"command - data vote add - start user:{Context.User.Id} channel:{Context.Channel.Id} command:{Context.Message.Content}");

                        try
                        {
                            await Context.Channel.SendMessageAsync(embed : Classes.Embed.New(Context.Message.Author, Field.CreateFieldBuilder("vote", $"rows affected: {Vote.Add(new Vote(name, what, how))}"), Colors.information));
                        }
                        catch (Exception e)
                        {
                            await Context.Channel.SendMessageAsync(embed : Classes.Embed.New(Context.Message.Author, Field.CreateFieldBuilder("error", e.Message), Colors.error));

                            throw e;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error($"command - data vote add - user:{Context.User.Id} channel:{Context.Channel.Id} error:{ex.Message}");
                    }
                }