dolphin_state.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "dolphin_state.h"
  2. #include "dolphin/helpers/dolphin_deed.h"
  3. #include <stdint.h>
  4. #include <storage/storage.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <math.h>
  8. #include <toolbox/saved_struct.h>
  9. #define TAG "DolphinState"
  10. #define DOLPHIN_STATE_PATH "/int/dolphin.state"
  11. #define DOLPHIN_STATE_HEADER_MAGIC 0xD0
  12. #define DOLPHIN_STATE_HEADER_VERSION 0x01
  13. #define LEVEL2_THRESHOLD 735
  14. #define LEVEL3_THRESHOLD 2940
  15. #define BUTTHURT_MAX 14
  16. #define BUTTHURT_MIN 0
  17. DolphinState* dolphin_state_alloc() {
  18. return malloc(sizeof(DolphinState));
  19. }
  20. void dolphin_state_free(DolphinState* dolphin_state) {
  21. free(dolphin_state);
  22. }
  23. bool dolphin_state_save(DolphinState* dolphin_state) {
  24. if(!dolphin_state->dirty) {
  25. return true;
  26. }
  27. bool result = saved_struct_save(
  28. DOLPHIN_STATE_PATH,
  29. &dolphin_state->data,
  30. sizeof(DolphinStoreData),
  31. DOLPHIN_STATE_HEADER_MAGIC,
  32. DOLPHIN_STATE_HEADER_VERSION);
  33. if(result) {
  34. FURI_LOG_I(TAG, "State saved");
  35. dolphin_state->dirty = false;
  36. } else {
  37. FURI_LOG_E(TAG, "Failed to save state");
  38. }
  39. return result;
  40. }
  41. bool dolphin_state_load(DolphinState* dolphin_state) {
  42. bool success = saved_struct_load(
  43. DOLPHIN_STATE_PATH,
  44. &dolphin_state->data,
  45. sizeof(DolphinStoreData),
  46. DOLPHIN_STATE_HEADER_MAGIC,
  47. DOLPHIN_STATE_HEADER_VERSION);
  48. if(success) {
  49. if((dolphin_state->data.butthurt > BUTTHURT_MAX) ||
  50. (dolphin_state->data.butthurt < BUTTHURT_MIN)) {
  51. success = false;
  52. }
  53. }
  54. if(!success) {
  55. FURI_LOG_W(TAG, "Reset dolphin-state");
  56. memset(dolphin_state, 0, sizeof(*dolphin_state));
  57. dolphin_state->dirty = true;
  58. }
  59. return success;
  60. }
  61. uint64_t dolphin_state_timestamp() {
  62. FuriHalRtcDateTime datetime;
  63. struct tm current;
  64. furi_hal_rtc_get_datetime(&datetime);
  65. current.tm_year = datetime.year - 1900;
  66. current.tm_mday = datetime.day;
  67. current.tm_mon = datetime.month - 1;
  68. current.tm_hour = datetime.hour;
  69. current.tm_min = datetime.minute;
  70. current.tm_sec = datetime.second;
  71. return mktime(&current);
  72. }
  73. bool dolphin_state_is_levelup(uint32_t icounter) {
  74. return (icounter == LEVEL2_THRESHOLD) || (icounter == LEVEL3_THRESHOLD);
  75. }
  76. uint8_t dolphin_get_level(uint32_t icounter) {
  77. if(icounter <= LEVEL2_THRESHOLD) {
  78. return 1;
  79. } else if(icounter <= LEVEL3_THRESHOLD) {
  80. return 2;
  81. } else {
  82. return 3;
  83. }
  84. }
  85. uint32_t dolphin_state_xp_above_last_levelup(uint32_t icounter) {
  86. uint32_t threshold = 0;
  87. if(icounter <= LEVEL2_THRESHOLD) {
  88. threshold = 0;
  89. } else if(icounter <= LEVEL3_THRESHOLD) {
  90. threshold = LEVEL2_THRESHOLD + 1;
  91. } else {
  92. threshold = LEVEL3_THRESHOLD + 1;
  93. }
  94. return icounter - threshold;
  95. }
  96. uint32_t dolphin_state_xp_to_levelup(uint32_t icounter) {
  97. uint32_t threshold = 0;
  98. if(icounter <= LEVEL2_THRESHOLD) {
  99. threshold = LEVEL2_THRESHOLD;
  100. } else if(icounter <= LEVEL3_THRESHOLD) {
  101. threshold = LEVEL3_THRESHOLD;
  102. } else {
  103. threshold = (uint32_t)-1;
  104. }
  105. return threshold - icounter;
  106. }
  107. void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  108. DolphinApp app = dolphin_deed_get_app(deed);
  109. int8_t weight_limit =
  110. dolphin_deed_get_app_limit(app) - dolphin_state->data.icounter_daily_limit[app];
  111. uint8_t deed_weight = CLAMP(dolphin_deed_get_weight(deed), weight_limit, 0);
  112. uint8_t xp_to_levelup = dolphin_state_xp_to_levelup(dolphin_state->data.icounter);
  113. if(xp_to_levelup) {
  114. deed_weight = MIN(xp_to_levelup, deed_weight);
  115. dolphin_state->data.icounter += deed_weight;
  116. dolphin_state->data.icounter_daily_limit[app] += deed_weight;
  117. }
  118. /* decrease butthurt:
  119. * 0 deeds accumulating --> 0 butthurt
  120. * +1....+15 deeds accumulating --> -1 butthurt
  121. * +16...+30 deeds accumulating --> -1 butthurt
  122. * +31...+45 deeds accumulating --> -1 butthurt
  123. * +46...... deeds accumulating --> -1 butthurt
  124. * -4 butthurt per day is maximum
  125. * */
  126. uint8_t butthurt_icounter_level_old = dolphin_state->data.butthurt_daily_limit / 15 +
  127. !!(dolphin_state->data.butthurt_daily_limit % 15);
  128. dolphin_state->data.butthurt_daily_limit =
  129. CLAMP(dolphin_state->data.butthurt_daily_limit + deed_weight, 46, 0);
  130. uint8_t butthurt_icounter_level_new = dolphin_state->data.butthurt_daily_limit / 15 +
  131. !!(dolphin_state->data.butthurt_daily_limit % 15);
  132. int32_t new_butthurt = ((int32_t)dolphin_state->data.butthurt) -
  133. (butthurt_icounter_level_old != butthurt_icounter_level_new);
  134. new_butthurt = CLAMP(new_butthurt, BUTTHURT_MAX, BUTTHURT_MIN);
  135. dolphin_state->data.butthurt = new_butthurt;
  136. dolphin_state->data.timestamp = dolphin_state_timestamp();
  137. dolphin_state->dirty = true;
  138. FURI_LOG_D(
  139. TAG,
  140. "icounter %d, butthurt %d",
  141. dolphin_state->data.icounter,
  142. dolphin_state->data.butthurt);
  143. }
  144. void dolphin_state_butthurted(DolphinState* dolphin_state) {
  145. if(dolphin_state->data.butthurt < BUTTHURT_MAX) {
  146. dolphin_state->data.butthurt++;
  147. dolphin_state->data.timestamp = dolphin_state_timestamp();
  148. dolphin_state->dirty = true;
  149. }
  150. }
  151. void dolphin_state_increase_level(DolphinState* dolphin_state) {
  152. furi_assert(dolphin_state_is_levelup(dolphin_state->data.icounter));
  153. ++dolphin_state->data.icounter;
  154. dolphin_state->dirty = true;
  155. }
  156. void dolphin_state_clear_limits(DolphinState* dolphin_state) {
  157. furi_assert(dolphin_state);
  158. for(int i = 0; i < DolphinAppMAX; ++i) {
  159. dolphin_state->data.icounter_daily_limit[i] = 0;
  160. }
  161. dolphin_state->data.butthurt_daily_limit = 0;
  162. dolphin_state->dirty = true;
  163. }