Session.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using CardCollector.Commands;
  6. using CardCollector.Commands.CallbackQuery;
  7. using CardCollector.Controllers;
  8. using CardCollector.DataBase.Entity;
  9. using CardCollector.Resources;
  10. namespace CardCollector.Session
  11. {
  12. public class UserSession
  13. {
  14. /* Ссылка на пользователя */
  15. public readonly UserEntity user;
  16. /* Дата и время последней актвности пользователя */
  17. private DateTime _lastAccess = DateTime.Now;
  18. /* Текущее состояние пользователя */
  19. public UserState State = UserState.Default;
  20. /* Подключаемые модули */
  21. private readonly Dictionary<Type, Module> Modules = new();
  22. /* Сообщения в чате пользователя */
  23. public readonly List<int> StickerMessages = new();
  24. public readonly List<int> Messages = new();
  25. /* Последовательность вызова списка меню */
  26. private readonly Stack<MenuInformation> MenuStack = new();
  27. private Type PreviousCommandType;
  28. private Type CurrentCommandType;
  29. public UserSession(UserEntity user)
  30. {
  31. this.user = user;
  32. }
  33. public T InitNewModule<T>() where T : Module
  34. {
  35. if (!Modules.ContainsKey(typeof(T))) Modules.Add(typeof(T), Activator.CreateInstance<T>());
  36. return (T) Modules[typeof(T)];
  37. }
  38. public T GetModule<T>() where T : Module
  39. {
  40. try {
  41. return (T) Modules[typeof(T)];
  42. } catch {
  43. return InitNewModule<T>();
  44. }
  45. }
  46. public void ResetModule<T>() where T : Module
  47. {
  48. try { Modules[typeof(T)].Reset(); }
  49. catch (Exception) { /**/ }
  50. }
  51. public void DeleteModule<T>() where T : Module
  52. {
  53. Modules.Remove(typeof(T));
  54. }
  55. public void UpdateLastAccess()
  56. {
  57. _lastAccess = DateTime.Now;
  58. }
  59. public int GetLastAccessInterval()
  60. {
  61. return (int) (DateTime.Now - _lastAccess).TotalMinutes;
  62. }
  63. public async Task ClearMessages()
  64. {
  65. foreach (var messageId in Messages.ToList())
  66. await MessageController.DeleteMessage(user, messageId, false);
  67. foreach (var messageId in StickerMessages.ToList())
  68. await MessageController.DeleteMessage(user, messageId, false);
  69. StickerMessages.Clear();
  70. Messages.Clear();
  71. }
  72. public async Task ClearStickers()
  73. {
  74. foreach (var messageId in StickerMessages.ToList())
  75. await MessageController.DeleteMessage(user, messageId, false);
  76. StickerMessages.Clear();
  77. }
  78. public async Task EndSession()
  79. {
  80. await ClearMessages();
  81. State = UserState.Default;
  82. foreach (var module in Modules.Values) module.Reset();
  83. Modules.Clear();
  84. MenuStack.Clear();
  85. }
  86. public bool TryGetPreviousMenu(out MenuInformation menu)
  87. {
  88. while (MenuStack.TryPeek(out menu) && CurrentCommandType == menu.GetMenuType()) {
  89. MenuStack.TryPop(out _);
  90. }
  91. return MenuStack.TryPeek(out menu);
  92. }
  93. public void AddMenuToStack(UpdateModel menu)
  94. {
  95. var menuInfo = new MenuInformation(menu, State);
  96. if (!MenuStack.Contains(menuInfo, new MenuComparer())) MenuStack.Push(menuInfo);
  97. }
  98. public void SetCurrentCommand(Type commandType)
  99. {
  100. PreviousCommandType = CurrentCommandType;
  101. if(commandType != typeof(Back)) CurrentCommandType = commandType;
  102. }
  103. public void UndoCurrentCommand()
  104. {
  105. CurrentCommandType = PreviousCommandType;
  106. }
  107. public void ClearMenuStack()
  108. {
  109. MenuStack.Clear();
  110. foreach (var module in Modules.Values) module.Reset();
  111. }
  112. }
  113. public class MenuComparer : IEqualityComparer<MenuInformation>
  114. {
  115. public bool Equals(MenuInformation x, MenuInformation y)
  116. {
  117. return x?.GetMenuType() == y?.GetMenuType();
  118. }
  119. public int GetHashCode(MenuInformation obj)
  120. {
  121. return base.GetHashCode();
  122. }
  123. }
  124. }