UserSettings.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using System.Linq;
  6. namespace CardCollector.DataBase.Entity
  7. {
  8. [Table("user_settings")]
  9. public class UserSettings
  10. {
  11. [Key, Column("id"), MaxLength(127)] public long UserId { get; set; }
  12. [Column("settings"), MaxLength(512)] public string Settings {
  13. get {
  14. return Utilities.ToJson(settings.ToDictionary(item => (int) item.Key, item => Convert.ToInt32(item.Value)));
  15. }
  16. set
  17. {
  18. var dict = Utilities.FromJson<Dictionary<int, bool>>(value);
  19. settings = dict.ToDictionary(item => (UserSettingsEnum) item.Key, item => Convert.ToBoolean(item.Value));
  20. }
  21. }
  22. [NotMapped] private Dictionary<UserSettingsEnum, bool> settings = new ();
  23. private bool GetProperty(UserSettingsEnum property)
  24. {
  25. if (!settings.ContainsKey(property))
  26. settings.Add(property, true);
  27. return settings[property];
  28. }
  29. private void SetProperty(UserSettingsEnum property, bool value)
  30. {
  31. if (!settings.ContainsKey(property))
  32. settings.Add(property, true);
  33. settings[property] = value;
  34. }
  35. public void SwitchProperty(UserSettingsEnum property)
  36. {
  37. if (!settings.ContainsKey(property))
  38. settings.Add(property, true);
  39. settings[property] = !settings[property];
  40. }
  41. public void InitProperties()
  42. {
  43. var props = Enum.GetValues<UserSettingsEnum>();
  44. foreach (var prop in props) settings.Add(prop, true);
  45. }
  46. public bool this[UserSettingsEnum key]
  47. {
  48. get => GetProperty(key);
  49. set => SetProperty(key, value);
  50. }
  51. }
  52. public enum UserSettingsEnum
  53. {
  54. DailyTasks,
  55. ExpGain,
  56. StickerEffects,
  57. DailyTaskProgress,
  58. PiggyBankCapacity,
  59. DailyExpTop,
  60. }
  61. }