Пример #1
0
 public Data(Game[] _games, List <Vote> _votes, ulong _votingMessageID, ulong _joinMessageID, List <Game> _allGames, WeeklyEventStatus _status, uint _weekIndex)
 {
     games           = _games;
     allGames        = _allGames;
     votes           = _votes;
     votingMessageID = _votingMessageID;
     joinMessageID   = _joinMessageID;
     status          = _status;
     weekIndex       = _weekIndex;
 }
Пример #2
0
        public async Task Initialize(DateTime time)
        {
            LoadConfiguration();
            BotConfiguration.AddConfigurable(this);
            Data loadedData = SerializationIO.LoadObjectFromFile <Data> (Program.dataPath + dataFileName + Program.gitHubIgnoreType);

            votes           = loadedData.votes;
            games           = loadedData.games;
            votingMessageID = loadedData.votingMessageID;
            joinMessageID   = loadedData.joinMessageID;
            allGames        = loadedData.allGames;
            status          = loadedData.status;
            weekIndex       = loadedData.weekIndex;

            if (allGames == null)
            {
                allGames = new List <Game> ();
            }

            while (Utility.GetServer() == null)
            {
                await Task.Delay(1000);
            }

            if (status == WeeklyEventStatus.Waiting && (int)DateTime.Now.DayOfWeek < (int)voteEndDay)
            {
                BeginNewVote();
            }

            Program.discordClient.ReactionAdded += async(message, channel, reaction) => {
                OnReactionChanged(message, channel, reaction, true);
            };
            Program.discordClient.ReactionRemoved += async(message, channel, reaction) => {
                OnReactionChanged(message, channel, reaction, false);
            };
        }
Пример #3
0
        private async Task BeginNewVote()
        {
            status = WeeklyEventStatus.Voting;

            for (int i = 0; i < allGames.Count; i++)
            {
                allGames [i].votes = 0;
            }

            highestGame = null;
            List <Game> possibilities    = allGames.ToList();
            List <Game> highlightedGames = possibilities.FindAll(x => x.highlight);

            possibilities.RemoveAll(x => x.highlight);

            List <Game> selectedGames = new List <Game> ();
            Random      rand          = new Random();

            games = new Game [gamesPerWeek];

            try {
                // Optimmized code is overrated anyways.
                for (int i = 0; i < gamesPerWeek; i++)
                {
                    int index = rand.Next(0, highlightedGames.Count > 0 ? highlightedGames.Count : possibilities.Count);
                    if (highlightedGames.Count > 0)
                    {
                        selectedGames.Add(highlightedGames [index]);
                        highlightedGames.RemoveAt(index);
                    }
                    else
                    {
                        selectedGames.Add(possibilities [index]);
                        possibilities.RemoveAt(index);
                    }
                }
            } catch (Exception e) {
                Logging.DebugLog(Logging.LogType.EXCEPTION, e.StackTrace);
            }
            // Shuffle dat s***e.
            for (int i = 0; i < gamesPerWeek; i++)
            {
                int index = rand.Next(0, selectedGames.Count);
                games [i] = selectedGames [index];
                selectedGames.RemoveAt(index);
            }

            // Well that's the least optimized code I've written in ages, and that says something.

            foreach (Game game in games)
            {
                game.votes = 0;
            }

            votes = new List <Vote> ();
            await UpdateVoteMessage(true);

            SocketGuildChannel mainChannel = Utility.GetMainChannel();

            Program.messageControl.SendMessage(mainChannel as SocketTextChannel, onNewVoteStartedMessage.Replace("{EVERYONEMENTION}",
                                                                                                                 Utility.GetServer().EveryoneRole.Mention).Replace("{ANNOUNCEMENTCHANNEL}", "<#" + Utility.SearchChannel(announcementsChannelName).Id + ">"), true);
            SaveData();
        }
Пример #4
0
        private async void CountVotes()
        {
            try {
                status = WeeklyEventStatus.Waiting;

                highestGame = null;
                int         highestVote       = int.MinValue;
                List <Game> highestVotedGames = new List <Game> ();

                foreach (Game game in games)
                {
                    if (game.votes == highestVote)
                    {
                        highestVotedGames.Add(game);
                    }
                    if (game.votes > highestVote)
                    {
                        highestVote       = game.votes;
                        highestVotedGames = new List <Game> ();
                        highestVotedGames.Add(game);
                    }
                }

                if (highestVotedGames.Count == 1)
                {
                    highestGame = highestVotedGames.FirstOrDefault();
                }
                else
                {
                    SocketChannel announcementChannel = Utility.SearchChannel(announcementsChannelName);
                    string        pollWinner          = string.Empty;

                    MessageControl.Poll poll = new MessageControl.Poll("Tiebreaker Vote!", announcementChannel.Id, 0, DateTime.Now.AddDays(1), 1, delegate(MessageControl.Poll p) {
                        pollWinner = p.winner.name;
                    },
                                                                       highestVotedGames.Select(x => x.name).ToArray()
                                                                       );
                    // What even is formatting.

                    MessageControl.CreatePoll(poll);
                    await poll.AwaitEnd();

                    highestGame = allGames.Find(x => x.name == pollWinner);
                }

                DateTime now      = DateTime.Now;
                DateTime eventDay = new DateTime(now.Year, now.Month, now.Day, eventHour, 0, 0).AddDays(daysBetween);
                DiscordEvents.CreateEvent(EVENT_NAME, eventDay, new TimeSpan(4, 0, 0), Program.discordClient.CurrentUser.Id, highestGame.iconUrl, highestGame.name + " has been chosen by vote!", new TimeSpan(0));

                SocketGuildChannel mainChannel = Utility.GetMainChannel();
                RestUserMessage    joinMessage = await Program.messageControl.AsyncSend(mainChannel as SocketTextChannel, onEventChosenByVoteMessage.Replace("{VOTEDGAME}", highestGame.name), true);

                joinMessageID = joinMessage.Id;
                joinMessage.AddReactionAsync(new Emoji("🗓"));

                Dictionary <ulong, bool> didWin = new Dictionary <ulong, bool> ();

                foreach (Vote vote in votes)
                {
                    if (!didWin.ContainsKey(vote.voterID))
                    {
                        didWin.Add(vote.voterID, false);
                    }

                    if (!didWin [vote.voterID])
                    {
                        if (highestGame.name == games [vote.votedGameID].name)
                        {
                            didWin [vote.voterID] = true;
                        }
                    }
                }

                int count = didWin.Count();
                for (int i = 0; i < count; i++)
                {
                    KeyValuePair <ulong, bool> pair = didWin.ElementAt(i);
                    if (pair.Value)
                    {
                        DiscordEvents.JoinEvent(pair.Key, EVENT_NAME);
                    }
                }

                await UpdateVoteMessage(false);

                SaveData();
            } catch (Exception e) {
                Logging.DebugLog(Logging.LogType.EXCEPTION, e.Message + " - " + e.StackTrace);
            }
        }