Esempio n. 1
0
        public async Task <bool> Vote(Say e, bool vote)
        {
            string reason;

            if (command.GetRunPermissions(battle, e.User, out reason) >= BattleCommand.RunPermission.Vote && !ended)
            {
                if (command.IsSpectator(battle, e.User, null))
                {
                    return(false);
                }

                userVotes[e.User] = vote;
                var yes = userVotes.Count(x => x.Value == true);
                var no  = userVotes.Count(x => x.Value == false);

                if (yes >= winCount)
                {
                    ended = true;
                }

                await battle.SayBattle(string.Format("Poll: {0} [!y={1}/{3}, !n={2}/{3}]", question, yes, no, winCount));

                if (yes >= winCount)
                {
                    ended = true;
                    await battle.SayBattle($"Poll: {question} [END:SUCCESS]");

                    if (command.Access == BattleCommand.AccessType.NotIngame && battle.spring.IsRunning)
                    {
                        return(true);
                    }
                    if (command.Access == BattleCommand.AccessType.Ingame && !battle.spring.IsRunning)
                    {
                        return(true);
                    }
                    await command.ExecuteArmed(battle, Creator);

                    return(true);
                }
                else if (no >= winCount)
                {
                    await End();

                    return(true);
                }
            }
            else
            {
                await battle.Respond(e, reason);

                return(false);
            }
            return(false);
        }
        private async Task <bool> CheckEnd(bool timeout)
        {
            var yes = userVotes.Count(x => x.Value == true);
            var no  = userVotes.Count(x => x.Value == false);

            if (yes >= winCount || timeout && !AbsoluteMajorityVote && yes > no)
            {
                Ended = true;
                await battle.SayBattle($"Poll: {question} [END:SUCCESS]");

                if (command.Access == BattleCommand.AccessType.NotIngame && battle.spring.IsRunning)
                {
                    return(true);
                }
                if (command.Access == BattleCommand.AccessType.Ingame && !battle.spring.IsRunning)
                {
                    return(true);
                }
                await command.ExecuteArmed(battle, Creator);

                Outcome = new PollOutcome()
                {
                    Success = true
                };
                return(true);
            }
            else if (no >= winCount || timeout)
            {
                Ended = true;
                await battle.SayBattle($"Poll: {question} [END:FAILED]");

                Outcome = new PollOutcome()
                {
                    Success = false
                };
                return(true);
            }
            return(false);
        }
Esempio n. 3
0
 public override async Task ExecuteArmed(ServerBattle battle, Say e)
 {
     await commandToRun.ExecuteArmed(battle, e);
 }
        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);
        }