Exemplo n.º 1
0
        public async Task <(string, bool, string)> Handle(CallbackQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            var callback = data.Data.Split(':');

            if (callback[0] != "adjust")
            {
                return(null, false, null);
            }

            if (!PollEx.TryGetPollId(callback.ElementAtOrDefault(1), out var pollId, out var format))
            {
                return("Poll is publishing. Try later.", true, null);
            }

            if (!int.TryParse(callback.ElementAtOrDefault(2) ?? "", NumberStyles.Integer, CultureInfo.InvariantCulture, out var offset))
            {
                return("", false, null);
            }

            var poll = (await myRaidService.GetOrCreatePollAndMessage(new PollMessage(data)
            {
                PollId = pollId
            }, myUrlHelper, format, cancellationToken))?.Poll;

            if (poll == null)
            {
                return("Poll is not found.", true, null);
            }

            var user = data.From;

            if (!await myChatInfo.CandEditPoll(poll.Owner, user.Id, cancellationToken))
            {
                return("You can't edit the poll.", true, null);
            }

            poll.Time = poll.Time?.AddMinutes(offset);
            if (poll.Time > poll.Raid?.RaidBossEndTime)
            {
                return($"Raid will end in {poll.Time:t}.", true, null);
            }

            var changed = await myContext.SaveChangesAsync(cancellationToken) > 0;

            if (changed)
            {
                await myRaidService.UpdatePoll(poll, myUrlHelper, cancellationToken);
            }

            return($"Poll is scheduled at {poll.Time:t}.", false, null);
        }
Exemplo n.º 2
0
        public async Task <(string, bool, string)> Handle(CallbackQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            var callback = data.Data.Split(':');

            if (callback[0] != "cancel")
            {
                return(null, false, null);
            }

            if (!PollEx.TryGetPollId(callback.ElementAtOrDefault(1), out var pollId, out var format))
            {
                return("Poll is publishing. Try later.", true, null);
            }

            var poll = (await myRaidService.GetOrCreatePollAndMessage(new PollMessage(data)
            {
                PollId = pollId
            }, myUrlHelper, format, cancellationToken))?.Poll;

            if (poll == null)
            {
                return("Poll is not found.", true, null);
            }

            var user = data.From;

            if (!await myChatInfo.CandEditPoll(poll.Owner, user.Id, cancellationToken))
            {
                return("You can't cancel the poll.", true, null);
            }

            poll.Cancelled = true;
            var changed = await myContext.SaveChangesAsync(cancellationToken) > 0;

            if (changed)
            {
                await myRaidService.UpdatePoll(poll, myUrlHelper, cancellationToken);
            }

            return(changed ? "Poll is canceled." : "Poll is already canceled.", false, null);
        }
        public async Task <(string, bool, string)> Handle(CallbackQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            var callback = data.Data.Split(':');

            if (callback[0] != "set")
            {
                return(null, false, null);
            }

            if (!await myChatInfo.CandEditPoll(data.Message.Chat, data.From?.Id, cancellationToken))
            {
                return("You can't edit the poll.", true, null);
            }

            var chatId      = data.Message.Chat.Id;
            var settingsSet = myContext.Set <Settings>();

            var allSettings = await settingsSet.GetSettings(chatId).AsTracking().ToListAsync(cancellationToken);

            Settings settings = null;

            switch (callback.ElementAtOrDefault(1))
            {
            case "list":
                return(await Return(await SettingsList(chatId, cancellationToken)));

            case var identifier when int.TryParse(identifier, out var id):
                settings = allSettings.FirstOrDefault(setting => setting.Id == id);

                break;
            }

            if (settings == null)
            {
                // create a new
                settings = new Settings {
                    Chat = chatId, Order = allSettings.Select(_ => _.Order).DefaultIfEmpty(-1).Max() + 1, Format = VoteEnum.Standard
                };
                await settingsSet.AddAsync(settings, cancellationToken);
            }

            string message = null;

            switch (callback.ElementAtOrDefault(2) ?? "")
            {
            case "default":
                settings.Order = allSettings.Select(_ => _.Order).DefaultIfEmpty(1).Min() - 1;
                await myContext.SaveChangesAsync(cancellationToken);

                return(await Return(await SettingsList(chatId, cancellationToken), "Default poll format is changed."));

            case "delete":
                settingsSet.Remove(settings);
                await myContext.SaveChangesAsync(cancellationToken);

                return(await Return(await SettingsList(chatId, cancellationToken), "Poll format is deleted."));

            case var format when FlagEnums.TryParseFlags(format, out VoteEnum toggleVotes, EnumFormat.DecimalValue):
                settings.Format = FlagEnums.ToggleFlags(settings.Format, toggleVotes);

                // adjust ⁺¹
                if (settings.Format.HasAnyFlags(VoteEnum.Plus) && !settings.Format.HasAnyFlags(VoteEnum.Countable))
                {
                    settings.Format = settings.Format.RemoveFlags(VoteEnum.Plus);
                }

                if (await myContext.SaveChangesAsync(cancellationToken) > 0)
                {
                    message = "Poll format is changed.";
                }
                goto default;

            default:
                var buttons = new []
                {
                    VoteEnum.Team,
                    VoteEnum.TeamHarmony,
                    VoteEnum.Plus1,
                    VoteEnum.Remotely,
                    VoteEnum.Invitation,
                    VoteEnum.MayBe,
                    VoteEnum.Yes,
                    VoteEnum.Thumbs,
                    VoteEnum.HarryPotter,
                    VoteEnum.Cancel,
                    VoteEnum.Share
                }
                .Select(format => new []
                {
                    InlineKeyboardButton.WithCallbackData($"{(settings.Format.HasAllFlags(format) ? '☑' : '☐')} {format.Format(new StringBuilder())}", $"{ID}:{settings.Id}:{format:D}")
                });

                if (allSettings.FirstOrDefault() is Settings existingDefault && existingDefault != settings)
                {
                    buttons = buttons.Append(new[]
                    {
                        InlineKeyboardButton.WithCallbackData("Make as a default", $"{ID}:{settings.Id}:default")
                    });
                }

                buttons = buttons
                          .Append(new [] { InlineKeyboardButton.WithCallbackData("Delete", $"{ID}:{settings.Id}:delete") })
                          .Append(new [] { InlineKeyboardButton.WithCallbackData("Back", $"{ID}:list") });

                return(await Return((
                                        settings.Format.Format(new StringBuilder("Selected poll format:").AppendLine()).ToTextMessageContent(),
                                        new InlineKeyboardMarkup(buttons)), message));
            }

            async Task <(string, bool, string)> Return((InputTextMessageContent content, InlineKeyboardMarkup replyMarkup) pass, string notification = "")
            {
                await myTelegramBotClient.EditMessageTextAsync(data.Message.Chat, data.Message.MessageId,
                                                               pass.content.MessageText, pass.content.ParseMode, pass.content.DisableWebPagePreview, pass.replyMarkup, cancellationToken);

                return(notification, false, null);
            }
        }