dolphin_state.c 6.4 KB

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