private void discussionTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            discussionTimer.Stop();
            var poll = new CommandPoll(this, false);

            poll.PollEnded += MapVoteEnded;
            StartVote(new CmdMap(), null, "", MapVoteTime, poll);
        }
 public void Dispose()
 {
     Stop();
     spring.UnsubscribeEvents(this);
     pollTimer.Dispose();
     pollTimer  = null;
     spring     = null;
     ActivePoll = null;
 }
 public async void StopVote(Say e = null)
 {
     if (ActivePoll != null)
     {
         await ActivePoll.End();
     }
     if (pollTimer != null)
     {
         pollTimer.Enabled = false;
     }
     ActivePoll = null;
 }
 public void Dispose()
 {
     spring.UnsubscribeEvents(this);
     if (pollTimer != null)
     {
         pollTimer.Enabled = false;
     }
     pollTimer?.Dispose();
     pollTimer  = null;
     spring     = null;
     ActivePoll = null;
 }
 public async Task RegisterVote(Say e, bool vote)
 {
     if (ActivePoll != null)
     {
         if (await ActivePoll.Vote(e, vote))
         {
             pollTimer.Enabled = false;
             ActivePoll        = null;
         }
     }
     else
     {
         await Respond(e, "There is no poll going on, start some first");
     }
 }
        public async Task <bool> StartVote(Func <string, string> eligibilitySelector, List <PollOption> options, Say creator, string topic, CommandPoll poll, int timeout = PollTimeout)
        {
            if (ActivePoll != null)
            {
                await Respond(creator, $"Please wait, another poll already in progress: {ActivePoll.Topic}");

                return(false);
            }
            await poll.Setup(eligibilitySelector, options, creator, topic);

            ActivePoll         = poll;
            pollTimer.Interval = timeout * 1000;
            pollTimer.Enabled  = true;
            return(true);
        }
        private async Task <bool> CreateMultiMapPoll()
        {
            var poll = new CommandPoll(this, false, false, true);

            poll.PollEnded += MapVoteEnded;
            var        options    = new List <PollOption>();
            List <int> pickedMaps = new List <int>();

            pickedMaps.Add(HostedMap?.ResourceID ?? 0);
            using (var db = new ZkDataContext())
            {
                for (int i = 0; i < NumberOfMapChoices; i++)
                {
                    Resource map = null;
                    if (i < NumberOfMapChoices / 2)
                    {
                        map = MapPicker.GetRecommendedMap(GetContext(), (MinimalMapSupportLevel < MapSupportLevel.Supported) ? MapSupportLevel.Supported : MinimalMapSupportLevel, MapRatings.GetMapRanking(Mode).TakeWhile(x => x.Percentile < 0.2).Select(x => x.Map).Where(x => !pickedMaps.Contains(x.ResourceID)).AsQueryable()); //choose at least 50% popular maps
                    }
                    if (map == null)
                    {
                        map = MapPicker.GetRecommendedMap(GetContext(), (MinimalMapSupportLevel < MapSupportLevel.Featured) ? MapSupportLevel.Supported : MinimalMapSupportLevel, db.Resources.Where(x => !pickedMaps.Contains(x.ResourceID)));
                    }
                    pickedMaps.Add(map.ResourceID);
                    options.Add(new PollOption()
                    {
                        Name       = map.InternalName,
                        URL        = $"{GlobalConst.BaseSiteUrl}/Maps/Detail/{map.ResourceID}",
                        ResourceID = map.ResourceID,
                        Action     = async() =>
                        {
                            var cmd = new CmdMap().Create();
                            cmd.Arm(this, null, map.ResourceID.ToString());
                            if (cmd.Access == BattleCommand.AccessType.NotIngame && spring.IsRunning)
                            {
                                return;
                            }
                            if (cmd.Access == BattleCommand.AccessType.Ingame && !spring.IsRunning)
                            {
                                return;
                            }
                            await cmd.ExecuteArmed(this, null);
                        }
                    });
                }
            }
            return(await StartVote(new CmdMap().GetIneligibilityReasonFunc(this), options, null, "Choose the next map", poll, MapVoteTime));
        }
        public async Task StartVote(BattleCommand command, Say e, string args)
        {
            if (ActivePoll != null)
            {
                await Respond(e, "Another poll already in progress, please wait");

                return;
            }
            var poll = new CommandPoll(this);

            if (await poll.Setup(command, e, args))
            {
                ActivePoll         = poll;
                pollTimer.Interval = PollTimeout * 1000;
                pollTimer.Enabled  = true;
            }
        }
        public async Task StartVote(BattleCommand command, Say e, string args, int timeout = PollTimeout, CommandPoll poll = null)
        {
            if (ActivePoll != null)
            {
                await Respond(e, $"Please wait, another poll already in progress: {ActivePoll.question}");

                return;
            }
            if (poll == null)
            {
                poll = new CommandPoll(this);
            }
            if (await poll.Setup(command, e, args))
            {
                ActivePoll         = poll;
                pollTimer.Interval = timeout * 1000;
                pollTimer.Enabled  = true;
            }
        }
        public async Task <bool> StartVote(BattleCommand cmd, Say e, string args, int timeout = PollTimeout, CommandPoll poll = null)
        {
            cmd = cmd.Create();

            if (cmd is CmdMap && string.IsNullOrEmpty(args))
            {
                return(await CreateMultiMapPoll());
            }

            string topic = cmd.Arm(this, e, args);

            if (topic == null)
            {
                return(false);
            }
            Func <string, string> selector = cmd.GetIneligibilityReasonFunc(this);

            if (e != null && selector(e.User) != null)
            {
                return(false);
            }
            var options = new List <PollOption>();

            string url = null;
            string map = null;

            if (cmd is CmdMap)
            {
                url = $"{GlobalConst.BaseSiteUrl}/Maps/Detail/{(cmd as CmdMap).Map.ResourceID}";
                map = (cmd as CmdMap).Map.InternalName;
            }
            poll = poll ?? new CommandPoll(this, true, true, cmd is CmdMap, map, cmd is CmdStart);
            options.Add(new PollOption()
            {
                Name   = "Yes",
                URL    = url,
                Action = async() =>
                {
                    if (cmd.Access == BattleCommand.AccessType.NotIngame && spring.IsRunning)
                    {
                        return;
                    }
                    if (cmd.Access == BattleCommand.AccessType.Ingame && !spring.IsRunning)
                    {
                        return;
                    }
                    await cmd.ExecuteArmed(this, e);
                }
            });
            options.Add(new PollOption()
            {
                Name   = "No",
                Action = async() => { }
            });

            if (await StartVote(selector, options, e, topic, poll))
            {
                await RegisterVote(e, 1);

                return(true);
            }
            return(false);
        }