CountQuery.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Threading.Tasks;
  2. using CardCollector.Controllers;
  3. using CardCollector.DataBase.Entity;
  4. using CardCollector.Resources;
  5. using CardCollector.Session.Modules;
  6. using Telegram.Bot.Types;
  7. namespace CardCollector.Commands.CallbackQuery
  8. {
  9. public class CountQuery : CallbackQuery
  10. {
  11. protected override string CommandText => Command.count;
  12. public override async Task Execute()
  13. {
  14. var (stickerCount, maxCount) = User.Session.State switch
  15. {
  16. UserState.CollectionMenu when User.Session.GetModule<CollectionModule>() is {} module =>
  17. (module.Count, User.Stickers[module.SelectedSticker.Md5Hash].Count),
  18. UserState.ProductMenu when User.Session.GetModule<AuctionModule>() is {} module =>
  19. (module.Count, module.MaxCount),
  20. UserState.CombineMenu when User.Session.GetModule<CombineModule>() is {} module =>
  21. (module.Count, User.Stickers[module.SelectedSticker.Md5Hash].Count),
  22. _ => (0, 0)
  23. };
  24. var changed = false;
  25. if (CallbackData.Contains(Text.minus) && (stickerCount < maxCount || maxCount == -1))
  26. {
  27. stickerCount++;
  28. changed = true;
  29. }
  30. else if (CallbackData.Contains(Text.plus) && stickerCount > 1)
  31. {
  32. stickerCount--;
  33. changed = true;
  34. }
  35. switch(User.Session.State)
  36. {
  37. case UserState.CollectionMenu:
  38. User.Session.GetModule<CollectionModule>().Count = stickerCount;
  39. break;
  40. case UserState.ProductMenu:
  41. User.Session.GetModule<AuctionModule>().Count = stickerCount;
  42. break;
  43. case UserState.CombineMenu:
  44. User.Session.GetModule<CombineModule>().Count = stickerCount;
  45. break;
  46. }
  47. if (changed) await MessageController.EditReplyMarkup(User, CallbackMessageId, Keyboard.GetStickerKeyboard(User.Session));
  48. else await MessageController.AnswerCallbackQuery(User, Update.CallbackQuery!.Id, Messages.cant_change_count);
  49. }
  50. public CountQuery() { }
  51. public CountQuery(UserEntity user, Update update) : base(user, update) { }
  52. }
  53. }