UserEntity.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using CardCollector.DataBase.EntityDao;
  8. using CardCollector.Resources;
  9. using CardCollector.Session;
  10. using Telegram.Bot.Types;
  11. namespace CardCollector.DataBase.Entity
  12. {
  13. /* Этот класс представляет собой строку таблицы users и описывает объект пользователя */
  14. [Table("users")]
  15. public class UserEntity
  16. {
  17. /* Id пользователя */
  18. [Key]
  19. [Column("id"), MaxLength(127)] public long Id { get; set; }
  20. /* Id чата */
  21. [Column("chat_id"), MaxLength(127)] public long ChatId { get; set; }
  22. /* Имя пользователя */
  23. [Column("username"), MaxLength(256)] public string Username { get; set; }
  24. /* Заблокирован ли пользователь */
  25. [Column("is_blocked"), MaxLength(11)] public bool IsBlocked { get; set; }
  26. /* Уровень привилегий пользователя */
  27. [Column("privilege_level"), MaxLength(32)] public int PrivilegeLevel { get; set; } = 0;
  28. /* Счет пользователя */
  29. [NotMapped] public CashEntity Cash { get; set; }
  30. /* Стикеры пользователя */
  31. [NotMapped] public Dictionary<string, UserStickerRelationEntity> Stickers { get; set; }
  32. /* Данные, хранящиеся в рамках одной сессии */
  33. [NotMapped] public readonly UserSession Session;
  34. /* Удаляет из чата все сообщения, добавленные в список выше */
  35. public async Task ClearChat()
  36. {
  37. await Session.ClearMessages();
  38. }
  39. /* Возвращает стикеры пользователя */
  40. public async Task<IEnumerable<StickerEntity>> GetStickersList(string filter, int tier = -1)
  41. {
  42. if (Constants.UNLIMITED_ALL_STICKERS) return await StickerDao.GetAll(filter);
  43. var result = Stickers.Values
  44. .Where(relation => relation.Count > 0)
  45. .Select(rel => StickerDao.GetStickerByHash(rel.ShortHash).Result)
  46. .Where(sticker => sticker.Title.Contains(filter, StringComparison.CurrentCultureIgnoreCase));
  47. if (tier != -1) result = result.Where(sticker => sticker.Tier == tier);
  48. return result;
  49. }
  50. public UserEntity()
  51. {
  52. Session = new UserSession(this);
  53. }
  54. }
  55. }