1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using MafiaTelegramBot.Controllers;
- using MafiaTelegramBot.DataBase.Entity;
- using MafiaTelegramBot.DataBase.EntityDao;
- using MafiaTelegramBot.Game;
- using MafiaTelegramBot.Resources;
- using Telegram.Bot.Types;
- using Telegram.Bot.Types.InputFiles;
- using Telegram.Bot.Types.ReplyMarkups;
- namespace MafiaTelegramBot.Other
- {
- public class GiveAway
- {
- public static readonly Dictionary<int, GiveAway> OpenedGiveaways = new ();
- private readonly int _id;
- private readonly string _url;
- private int _count;
- private UserEntity _chatInfo;
- private readonly List<long> _awardedIdList = new ();
- private readonly Roles _prize;
- private int _messageId;
- private string _buttonText = "";
- public GiveAway(int count, Roles prize)
- {
- _id = Utilities.Rnd.Next();
- _count = count;
- _prize = prize;
- _url = $"https://t.me/{AppSettings.Name}?start=giveaway_id={_id}";
- OpenedGiveaways.Add(_id, this);
- }
- public async Task<Message> PostGiveAway(UserEntity info, string buttonText, string message = null, string imageUrl = null, string userName = null)
- {
- _buttonText = buttonText;
- _chatInfo = info;
- var keyboard = new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl(_buttonText + $" ({_count})", _url));
- var result = imageUrl == null
- ? await MessageController.SendText(info, message, keyboard)
- : await MessageController.SendImage(info, new InputOnlineFile(imageUrl), message, replyMarkup: keyboard);
- _messageId = result.MessageId;
- if (userName == null) return result;
- var adminId = await UserDao.GetIdByUsername(userName);
- var admin = await UserDao.GetPlayerById(adminId);
- await MessageController.SendTextWithHtml(admin.Info, $"<a href='{_url}'>{strings.giveaway_link}</a>");
- return result;
- }
- private async Task<bool> TryDecreaseCount(long id)
- {
- if (_awardedIdList.Contains(id)) return false;
- _count--;
- await UpdateGiveAwayMessageOrDelete();
- _awardedIdList.Add(id);
- return true;
- }
- public async Task<ResultCode> GivePrizeTo(Player player)
- {
- if (!await TryDecreaseCount(player.Info.Id)) return ResultCode.NowYouGetPrizeFromThisGiveaway;
- if (_prize is Roles.All) return await GivePackToUser(player);
- var firstPrize = _prize;
- if (player.OpenedRoles.TryOpenRole(firstPrize))
- {
- await MessageController.SendText(player.Info, Stickers.Sticker[firstPrize.ToString()]);
- await MessageController.SendText(player.Info,$"{strings.congratulations_you_open_role} {roles.ResourceManager.GetString(firstPrize.ToString())}");
- }
- else await MessageController.SendText(player.Info,$"{strings.you_got_role} {roles.ResourceManager.GetString(firstPrize.ToString())} ({strings.now_opened})");
- return ResultCode.CodeOk;
- }
- private static async Task<ResultCode> GivePackToUser(Player player)
- {
- player.Info.PackCount++;
- await MessageController.SendSticker(player.Info, Stickers.Sticker["Pool"]);
- await MessageController.SendText(player.Info, $"{strings.your_packs} {player.Info.PackCount}", Keyboard.PackKeyboard());
- return ResultCode.CodeOk;
- }
- private async Task UpdateGiveAwayMessageOrDelete()
- {
- if (_count == 0)
- {
- await MessageController.EditMessage(_chatInfo, _messageId, strings.giveaway_ended);
- OpenedGiveaways.Remove(_id);
- _awardedIdList.Clear();
- }
- else
- {
- var keyboard = new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl(_buttonText + $" ({_count})", _url));
- await MessageController.EditReplyMarkup(_chatInfo, _messageId, keyboard);
- }
- }
- }
- }
|