Extensions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using CardCollector.DataBase.Entity;
  6. using CardCollector.DataBase.EntityDao;
  7. using CardCollector.Others;
  8. using CardCollector.Resources;
  9. using Telegram.Bot.Types.InlineQueryResults;
  10. namespace CardCollector
  11. {
  12. public static class Extensions
  13. {
  14. /* Преобразует список стикеров в список результатов для телеграм */
  15. public static IEnumerable<InlineQueryResult> ToTelegramResults
  16. (this IEnumerable<StickerEntity> list, string command, bool asMessage = true)
  17. {
  18. var result = new List<InlineQueryResult>();
  19. foreach (var item in list)
  20. {
  21. result.Add( asMessage
  22. ? new InlineQueryResultCachedSticker($"{(Constants.UNLIMITED_ALL_STICKERS ? Command.unlimited_stickers : "")}" +
  23. $"{command}={item.Md5Hash}", item.Id)
  24. {InputMessageContent = new InputTextMessageContent(Text.select)}
  25. : new InlineQueryResultCachedSticker($"{(Constants.UNLIMITED_ALL_STICKERS ? Command.unlimited_stickers : "")}" +
  26. $"{command}={item.Md5Hash}", item.Id));
  27. /* Ограничение Telegram API по количеству результатов в 50 шт. */
  28. if (result.Count > 49) return result;
  29. }
  30. return result;
  31. }
  32. /* Преобразует список продавцов в список результатов для телеграм */
  33. public static IEnumerable<InlineQueryResult> ToTelegramResults
  34. (this IEnumerable<TraderInformation> list, string command)
  35. {
  36. var result = new List<InlineQueryResult>();
  37. foreach (var item in list)
  38. {
  39. result.Add(new InlineQueryResultArticle($"{command}={item.Id}",
  40. $"{item.Username} {item.Quantity}{Text.items}", new InputTextMessageContent(Text.buy))
  41. { Description = $"{item.PriceCoins}{Text.coin}/{item.PriceGems}{Text.gem} {Text.per} 1{Text.items}" });
  42. /* Ограничение Telegram API по количеству результатов в 50 шт. */
  43. if (result.Count > 49) return result;
  44. }
  45. return result;
  46. }
  47. /* Возвращает все стикеры системы */
  48. public static async Task<IEnumerable<StickerEntity>> ToStickers
  49. (this Dictionary<string, UserStickerRelationEntity> dict, string filter)
  50. {
  51. var result = new List<StickerEntity>();
  52. foreach (var relation in dict.Values.Where(i => i.Count > 0))
  53. {
  54. var sticker = await StickerDao.GetStickerByHash(relation.StickerId);
  55. if (sticker.Title.Contains(filter, StringComparison.CurrentCultureIgnoreCase)) result.Add(sticker);
  56. }
  57. return result;
  58. }
  59. public static IEnumerable<StickerEntity> ApplyTo (this Dictionary<string, object> dict,
  60. IEnumerable<StickerEntity> list, UserState state)
  61. {
  62. /* Фильтруем по автору */
  63. if (dict[Command.author] is string author && author != "")
  64. list = list.Where(item => item.Author.Contains(author));
  65. /* Фильтруем по тиру */
  66. if (dict[Command.tier] is int tier && tier != -1)
  67. list = list.Where(item => item.Tier.Equals(tier));
  68. /* Фильтруем по эмоции */
  69. if (dict[Command.emoji] is string emoji && emoji != "")
  70. list = list.Where(item => item.Emoji.Contains(emoji));
  71. /* Если пользвователь не находится в меню коллекции, то фильтруем по цене */
  72. if (state is not UserState.CollectionMenu){
  73. /* Фильтруем по цене монет ОТ */
  74. if (dict[Command.price_coins_from] is int PCF && PCF != 0)
  75. list = list.Where(item => item.PriceCoins >= PCF);
  76. /* Фильтруем по цене монет ДО */
  77. if (dict[Command.price_coins_to] is int PCT && PCT != 0)
  78. list = list.Where(item => item.PriceCoins <= PCT);
  79. /* Фильтруем по цене алмазов ОТ */
  80. if (dict[Command.price_gems_from] is int PGF && PGF != 0)
  81. list = list.Where(item => item.PriceGems >= PGF);
  82. /* Фильтруем по цене адмазов ДО */
  83. if (dict[Command.price_gems_to] is int PGT && PGT != 0)
  84. list = list.Where(item => item.PriceGems <= PGT);
  85. }
  86. /* Сортируем список, если тип сортировки установлен */
  87. if (dict[Command.sort] is not string sort || sort == SortingTypes.None) return list;
  88. {
  89. /* Сортируем по автору */
  90. if (sort== SortingTypes.ByAuthor)
  91. list = list.OrderBy(item => item.Author);
  92. /* Сортируем по названию */
  93. if (sort == SortingTypes.ByTitle)
  94. list = list.OrderBy(item => item.Title);
  95. /* Сортируем по увеличению тира */
  96. if (sort == SortingTypes.ByTierIncrease)
  97. list = list.OrderBy(item => item.Tier);
  98. /* Сортируем по уменьшению тира */
  99. if (sort == SortingTypes.ByTierDecrease)
  100. list = list.OrderByDescending(item => item.Tier);
  101. }
  102. return list;
  103. }
  104. public static string ToMessage(this Dictionary<string, object> dict, UserState state)
  105. {
  106. var text = $"{Messages.current_filters}\n" +
  107. $"{Messages.author} {(dict[Command.author] is string author and not "" ? author : Messages.all)}\n" +
  108. $"{Messages.tier} {(dict[Command.tier] is int tier and not -1 ? new string('⭐', tier) : Messages.all)}\n" +
  109. $"{Messages.emoji} {(dict[Command.emoji] is string emoji and not "" ? emoji : Messages.all)}\n";
  110. if (state != UserState.CollectionMenu)
  111. text += $"{Messages.price} 💰 {dict[Command.price_coins_from]} -" +
  112. $" {(dict[Command.price_coins_to] is int c and not 0 ? c : "∞")}\n" +
  113. $"{Messages.price} 💎 {dict[Command.price_gems_from]} -" +
  114. $" {(dict[Command.price_gems_to] is int g and not 0 ? g : "∞")}\n";
  115. text += $"{Messages.sorting} {dict[Command.sort]}\n\n{Messages.select_filter}";
  116. return text;
  117. }
  118. }
  119. }