dolphin_state.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. furi_hal_rtc_get_datetime(&datetime);
  64. return furi_hal_rtc_datetime_to_timestamp(&datetime);
  65. }
  66. bool dolphin_state_is_levelup(uint32_t icounter) {
  67. return (icounter == LEVEL2_THRESHOLD) || (icounter == LEVEL3_THRESHOLD);
  68. }
  69. uint8_t dolphin_get_level(uint32_t icounter) {
  70. if(icounter <= LEVEL2_THRESHOLD) {
  71. return 1;
  72. } else if(icounter <= LEVEL3_THRESHOLD) {
  73. return 2;
  74. } else {
  75. return 3;
  76. }
  77. }
  78. uint32_t dolphin_state_xp_above_last_levelup(uint32_t icounter) {
  79. uint32_t threshold = 0;
  80. if(icounter <= LEVEL2_THRESHOLD) {
  81. threshold = 0;
  82. } else if(icounter <= LEVEL3_THRESHOLD) {
  83. threshold = LEVEL2_THRESHOLD + 1;
  84. } else {
  85. threshold = LEVEL3_THRESHOLD + 1;
  86. }
  87. return icounter - threshold;
  88. }
  89. uint32_t dolphin_state_xp_to_levelup(uint32_t icounter) {
  90. uint32_t threshold = 0;
  91. if(icounter <= LEVEL2_THRESHOLD) {
  92. threshold = LEVEL2_THRESHOLD;
  93. } else if(icounter <= LEVEL3_THRESHOLD) {
  94. threshold = LEVEL3_THRESHOLD;
  95. } else {
  96. threshold = (uint32_t)-1;
  97. }
  98. return threshold - icounter;
  99. }
  100. void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  101. // Special case for testing
  102. if(deed > DolphinDeedMAX) {
  103. if(deed == DolphinDeedTestLeft) {
  104. dolphin_state->data.butthurt =
  105. CLAMP(dolphin_state->data.butthurt + 1, BUTTHURT_MAX, BUTTHURT_MIN);
  106. if(dolphin_state->data.icounter > 0) dolphin_state->data.icounter--;
  107. dolphin_state->data.timestamp = dolphin_state_timestamp();
  108. dolphin_state->dirty = true;
  109. } else if(deed == DolphinDeedTestRight) {
  110. dolphin_state->data.butthurt = BUTTHURT_MIN;
  111. if(dolphin_state->data.icounter < UINT32_MAX) dolphin_state->data.icounter++;
  112. dolphin_state->data.timestamp = dolphin_state_timestamp();
  113. dolphin_state->dirty = true;
  114. }
  115. return;
  116. }
  117. DolphinApp app = dolphin_deed_get_app(deed);
  118. int8_t weight_limit =
  119. dolphin_deed_get_app_limit(app) - dolphin_state->data.icounter_daily_limit[app];
  120. uint8_t deed_weight = CLAMP(dolphin_deed_get_weight(deed), weight_limit, 0);
  121. uint32_t xp_to_levelup = dolphin_state_xp_to_levelup(dolphin_state->data.icounter);
  122. if(xp_to_levelup) {
  123. deed_weight = MIN(xp_to_levelup, deed_weight);
  124. dolphin_state->data.icounter += deed_weight;
  125. dolphin_state->data.icounter_daily_limit[app] += deed_weight;
  126. }
  127. /* decrease butthurt:
  128. * 0 deeds accumulating --> 0 butthurt
  129. * +1....+15 deeds accumulating --> -1 butthurt
  130. * +16...+30 deeds accumulating --> -1 butthurt
  131. * +31...+45 deeds accumulating --> -1 butthurt
  132. * +46...... deeds accumulating --> -1 butthurt
  133. * -4 butthurt per day is maximum
  134. * */
  135. uint8_t butthurt_icounter_level_old = dolphin_state->data.butthurt_daily_limit / 15 +
  136. !!(dolphin_state->data.butthurt_daily_limit % 15);
  137. dolphin_state->data.butthurt_daily_limit =
  138. CLAMP(dolphin_state->data.butthurt_daily_limit + deed_weight, 46, 0);
  139. uint8_t butthurt_icounter_level_new = dolphin_state->data.butthurt_daily_limit / 15 +
  140. !!(dolphin_state->data.butthurt_daily_limit % 15);
  141. int32_t new_butthurt = ((int32_t)dolphin_state->data.butthurt) -
  142. (butthurt_icounter_level_old != butthurt_icounter_level_new);
  143. new_butthurt = CLAMP(new_butthurt, BUTTHURT_MAX, BUTTHURT_MIN);
  144. dolphin_state->data.butthurt = new_butthurt;
  145. dolphin_state->data.timestamp = dolphin_state_timestamp();
  146. dolphin_state->dirty = true;
  147. FURI_LOG_D(
  148. TAG,
  149. "icounter %d, butthurt %d",
  150. dolphin_state->data.icounter,
  151. dolphin_state->data.butthurt);
  152. }
  153. void dolphin_state_butthurted(DolphinState* dolphin_state) {
  154. if(dolphin_state->data.butthurt < BUTTHURT_MAX) {
  155. dolphin_state->data.butthurt++;
  156. dolphin_state->data.timestamp = dolphin_state_timestamp();
  157. dolphin_state->dirty = true;
  158. }
  159. }
  160. void dolphin_state_increase_level(DolphinState* dolphin_state) {
  161. furi_assert(dolphin_state_is_levelup(dolphin_state->data.icounter));
  162. ++dolphin_state->data.icounter;
  163. dolphin_state->dirty = true;
  164. }
  165. void dolphin_state_clear_limits(DolphinState* dolphin_state) {
  166. furi_assert(dolphin_state);
  167. for(int i = 0; i < DolphinAppMAX; ++i) {
  168. dolphin_state->data.icounter_daily_limit[i] = 0;
  169. }
  170. dolphin_state->data.butthurt_daily_limit = 0;
  171. dolphin_state->dirty = true;
  172. }