Example #1
0
    private static void OnVote(CommandData commandData, List <Vote> voteResults, Map[] maps, bool logResults)
    {
        // converts a string like "$VOTE 2" to an integer 2.
        if (int.TryParse(commandData.Command.Split(' ').ElementAtOrDefault(1), out int voteFor) &&
            1 <= voteFor && voteFor <= 3)    // If the number is a valid map to vote for.
        {
            // Test if the player already voted for a map. If they did, update the map they are voting for.
            for (int i = 0; i < voteResults.Count; i++)
            {
                if (Identity.Compare(commandData.ChatIdentity, voteResults[i].ChatIdentity))
                {
                    // Don't log if the player is voting for the same map again.
                    if (voteResults[i].VotingFor != voteFor)
                    {
                        if (logResults)
                        {
                            Console.WriteLine($"Player #{i} changing their vote to {maps[voteFor - 1]} ({voteFor})");
                        }
                        voteResults[i].VotingFor = voteFor;
                    }
                    return;
                }
            }

            // If they didn't already vote for a map, add their vote to the VoteResults list.
            if (logResults)
            {
                Console.WriteLine($"New vote from player #{voteResults.Count}: {maps[voteFor - 1]} ({voteFor})");
            }
            voteResults.Add(new Vote(voteFor, commandData.ChatIdentity));
        }
    }