|
@@ -0,0 +1,81 @@
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using MafiaTelegramBot.DataBase.EntityDao;
|
|
|
|
+using MafiaTelegramBot.Models;
|
|
|
|
+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 readonly List<long> _awardedIdList = new ();
|
|
|
|
+ private Roles _prize;
|
|
|
|
+ private long _chatId;
|
|
|
|
+ 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(long channelId, string buttonText, string message = null, string imageUrl = null, string userName = null)
|
|
|
|
+ {
|
|
|
|
+ _chatId = channelId;
|
|
|
|
+ _buttonText = buttonText;
|
|
|
|
+ var keyboard = new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl(_buttonText + $" ({_count})", _url));
|
|
|
|
+ var result = imageUrl == null
|
|
|
|
+ ? await Bot.SendWithMarkdown2(_chatId, message, keyboard)
|
|
|
|
+ : await Bot.Get().SendPhotoAsync(_chatId, 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 Bot.SendHyperLink(admin.ChatId, $"<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(long id)
|
|
|
|
+ {
|
|
|
|
+ if (!await TryDecreaseCount(id)) return ResultCode.NowYouGetPrizeFromThisGiveaway;
|
|
|
|
+ var player = await UserDao.GetPlayerById(id);
|
|
|
|
+ return ResultCode.CodeOk;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async Task UpdateGiveAwayMessageOrDelete()
|
|
|
|
+ {
|
|
|
|
+ if (_count == 0)
|
|
|
|
+ {
|
|
|
|
+ await Bot.Get().DeleteMessageAsync(_chatId, _messageId);
|
|
|
|
+ OpenedGiveaways.Remove(_id);
|
|
|
|
+ _awardedIdList.Clear();
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ var keyboard = new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl(_buttonText + $" ({_count})", _url));
|
|
|
|
+ await Bot.Get().EditMessageReplyMarkupAsync(_chatId, _messageId, keyboard);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|