dolphin_state.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "dolphin_state.h"
  2. #include <storage/storage.h>
  3. #include <furi.h>
  4. #include <math.h>
  5. #include <toolbox/saved_struct.h>
  6. #define DOLPHIN_STORE_PATH "/int/dolphin.state"
  7. #define DOLPHIN_STORE_HEADER_MAGIC 0xD0
  8. #define DOLPHIN_STORE_HEADER_VERSION 0x01
  9. #define DOLPHIN_LVL_THRESHOLD 20.0f
  10. typedef struct {
  11. uint32_t limit_ibutton;
  12. uint32_t limit_nfc;
  13. uint32_t limit_ir;
  14. uint32_t limit_rfid;
  15. uint32_t flags;
  16. uint32_t icounter;
  17. uint32_t butthurt;
  18. uint64_t timestamp;
  19. } DolphinStoreData;
  20. struct DolphinState {
  21. DolphinStoreData data;
  22. bool dirty;
  23. };
  24. DolphinState* dolphin_state_alloc() {
  25. return furi_alloc(sizeof(DolphinState));
  26. }
  27. void dolphin_state_free(DolphinState* dolphin_state) {
  28. free(dolphin_state);
  29. }
  30. bool dolphin_state_save(DolphinState* dolphin_state) {
  31. return saved_struct_save(
  32. DOLPHIN_STORE_PATH,
  33. &dolphin_state->data,
  34. sizeof(DolphinStoreData),
  35. DOLPHIN_STORE_HEADER_MAGIC,
  36. DOLPHIN_STORE_HEADER_VERSION);
  37. }
  38. bool dolphin_state_load(DolphinState* dolphin_state) {
  39. bool loaded = saved_struct_load(
  40. DOLPHIN_STORE_PATH,
  41. &dolphin_state->data,
  42. sizeof(DolphinStoreData),
  43. DOLPHIN_STORE_HEADER_MAGIC,
  44. DOLPHIN_STORE_HEADER_VERSION);
  45. if(!loaded) {
  46. FURI_LOG_W("dolphin-state", "Reset dolphin-state");
  47. memset(dolphin_state, 0, sizeof(*dolphin_state));
  48. dolphin_state_save(dolphin_state);
  49. }
  50. return true;
  51. }
  52. uint64_t dolphin_state_timestamp() {
  53. RTC_TimeTypeDef time;
  54. RTC_DateTypeDef date;
  55. struct tm current;
  56. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  57. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  58. current.tm_year = date.Year + 100;
  59. current.tm_mday = date.Date;
  60. current.tm_mon = date.Month - 1;
  61. current.tm_hour = time.Hours;
  62. current.tm_min = time.Minutes;
  63. current.tm_sec = time.Seconds;
  64. return mktime(&current);
  65. }
  66. void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  67. const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
  68. int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
  69. int32_t butthurt = dolphin_state->data.butthurt;
  70. if(icounter >= 0) {
  71. dolphin_state->data.icounter = icounter;
  72. dolphin_state->data.butthurt = MAX(butthurt - deed_weight->icounter, 0);
  73. dolphin_state->data.timestamp = dolphin_state_timestamp();
  74. }
  75. dolphin_state->dirty = true;
  76. }
  77. void dolphin_state_butthurted(DolphinState* dolphin_state) {
  78. dolphin_state->data.butthurt++;
  79. dolphin_state->data.timestamp = dolphin_state_timestamp();
  80. dolphin_state->dirty = true;
  81. }
  82. uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state) {
  83. return dolphin_state->data.icounter;
  84. }
  85. uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state) {
  86. return dolphin_state->data.butthurt;
  87. }
  88. uint64_t dolphin_state_get_timestamp(DolphinState* dolphin_state) {
  89. return dolphin_state->data.timestamp;
  90. }
  91. uint32_t dolphin_state_get_level(uint32_t icounter) {
  92. return 0.5f + sqrtf(1.0f + 8.0f * ((float)icounter / DOLPHIN_LVL_THRESHOLD)) / 2.0f;
  93. }
  94. uint32_t dolphin_state_xp_to_levelup(uint32_t icounter, uint32_t level, bool remaining) {
  95. return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) - (remaining ? icounter : 0);
  96. }