dolphin_state.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "dolphin_state.h"
  2. #include <storage/storage.h>
  3. #include <furi.h>
  4. #include <math.h>
  5. #define DOLPHIN_STORE_KEY "/int/dolphin.state"
  6. #define DOLPHIN_STORE_HEADER_MAGIC 0xD0
  7. #define DOLPHIN_STORE_HEADER_VERSION 0x01
  8. #define DOLPHIN_LVL_THRESHOLD 20.0f
  9. typedef struct {
  10. uint8_t magic;
  11. uint8_t version;
  12. uint8_t checksum;
  13. uint8_t flags;
  14. uint32_t timestamp;
  15. } DolphinStoreHeader;
  16. typedef struct {
  17. uint32_t limit_ibutton;
  18. uint32_t limit_nfc;
  19. uint32_t limit_ir;
  20. uint32_t limit_rfid;
  21. uint32_t flags;
  22. uint32_t icounter;
  23. uint32_t butthurt;
  24. } DolphinStoreData;
  25. typedef struct {
  26. DolphinStoreHeader header;
  27. DolphinStoreData data;
  28. } DolphinStore;
  29. struct DolphinState {
  30. Storage* fs_api;
  31. DolphinStoreData data;
  32. };
  33. DolphinState* dolphin_state_alloc() {
  34. DolphinState* dolphin_state = furi_alloc(sizeof(DolphinState));
  35. dolphin_state->fs_api = furi_record_open("storage");
  36. return dolphin_state;
  37. }
  38. void dolphin_state_free(DolphinState* dolphin_state) {
  39. furi_record_close("storage");
  40. free(dolphin_state);
  41. }
  42. bool dolphin_state_save(DolphinState* dolphin_state) {
  43. DolphinStore store;
  44. FURI_LOG_I("dolphin-state", "Saving state to \"%s\"", DOLPHIN_STORE_KEY);
  45. // Calculate checksum
  46. uint8_t* source = (uint8_t*)&dolphin_state->data;
  47. uint8_t checksum = 0;
  48. for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
  49. checksum += source[i];
  50. }
  51. // Set header
  52. store.header.magic = DOLPHIN_STORE_HEADER_MAGIC;
  53. store.header.version = DOLPHIN_STORE_HEADER_VERSION;
  54. store.header.checksum = checksum;
  55. store.header.flags = 0;
  56. store.header.timestamp = 0;
  57. // Set data
  58. store.data = dolphin_state->data;
  59. // Store
  60. File* file = storage_file_alloc(dolphin_state->fs_api);
  61. bool save_result = storage_file_open(file, DOLPHIN_STORE_KEY, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  62. if(save_result) {
  63. uint16_t bytes_count = storage_file_write(file, &store, sizeof(DolphinStore));
  64. if(bytes_count != sizeof(DolphinStore)) {
  65. save_result = false;
  66. }
  67. }
  68. if(!save_result) {
  69. FURI_LOG_E(
  70. "dolphin-state",
  71. "Save failed. Storage returned: %s",
  72. storage_file_get_error_desc(file));
  73. }
  74. storage_file_close(file);
  75. storage_file_free(file);
  76. FURI_LOG_I("dolphin-state", "Saved");
  77. return save_result;
  78. }
  79. bool dolphin_state_load(DolphinState* dolphin_state) {
  80. DolphinStore store;
  81. // Read Dolphin State Store
  82. FURI_LOG_I("dolphin-state", "Loading state from \"%s\"", DOLPHIN_STORE_KEY);
  83. File* file = storage_file_alloc(dolphin_state->fs_api);
  84. bool load_result = storage_file_open(file, DOLPHIN_STORE_KEY, FSAM_READ, FSOM_OPEN_EXISTING);
  85. if(load_result) {
  86. uint16_t bytes_count = storage_file_read(file, &store, sizeof(DolphinStore));
  87. if(bytes_count != sizeof(DolphinStore)) {
  88. load_result = false;
  89. }
  90. }
  91. if(!load_result) {
  92. FURI_LOG_E(
  93. "dolphin-state",
  94. "Load failed. Storage returned: %s",
  95. storage_file_get_error_desc(file));
  96. } else {
  97. FURI_LOG_I("dolphin-state", "State loaded, verifying header");
  98. if(store.header.magic == DOLPHIN_STORE_HEADER_MAGIC &&
  99. store.header.version == DOLPHIN_STORE_HEADER_VERSION) {
  100. FURI_LOG_I(
  101. "dolphin-state",
  102. "Magic(%d) and Version(%d) match",
  103. store.header.magic,
  104. store.header.version);
  105. uint8_t checksum = 0;
  106. const uint8_t* source = (const uint8_t*)&store.data;
  107. for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
  108. checksum += source[i];
  109. }
  110. if(store.header.checksum == checksum) {
  111. FURI_LOG_I("dolphin-state", "Checksum(%d) match", store.header.checksum);
  112. dolphin_state->data = store.data;
  113. } else {
  114. FURI_LOG_E(
  115. "dolphin-state",
  116. "Checksum(%d != %d) mismatch",
  117. store.header.checksum,
  118. checksum);
  119. load_result = false;
  120. }
  121. } else {
  122. FURI_LOG_E(
  123. "dolphin-state",
  124. "Magic(%d != %d) and Version(%d != %d) mismatch",
  125. store.header.magic,
  126. DOLPHIN_STORE_HEADER_MAGIC,
  127. store.header.version,
  128. DOLPHIN_STORE_HEADER_VERSION);
  129. load_result = false;
  130. }
  131. }
  132. storage_file_close(file);
  133. storage_file_free(file);
  134. return load_result;
  135. }
  136. void dolphin_state_clear(DolphinState* dolphin_state) {
  137. memset(&dolphin_state->data, 0, sizeof(DolphinStoreData));
  138. }
  139. void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  140. const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
  141. int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
  142. if(icounter >= 0) {
  143. dolphin_state->data.icounter = icounter;
  144. }
  145. }
  146. uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state) {
  147. return dolphin_state->data.icounter;
  148. }
  149. uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state) {
  150. return dolphin_state->data.butthurt;
  151. }
  152. uint32_t dolphin_state_get_level(DolphinState* dolphin_state) {
  153. return 0.5f +
  154. sqrtf(1.0f + 8.0f * ((float)dolphin_state->data.icounter / DOLPHIN_LVL_THRESHOLD)) /
  155. 2.0f;
  156. }
  157. uint32_t dolphin_state_xp_to_levelup(DolphinState* dolphin_state, uint32_t level, bool remaining) {
  158. return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) -
  159. (remaining ? dolphin_state->data.icounter : 0);
  160. }