dolphin_state.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "dolphin_state.h"
  2. #include <internal-storage/internal-storage.h>
  3. #include <furi.h>
  4. #include <math.h>
  5. #define DOLPHIN_STORE_KEY "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. InternalStorage* internal_storage;
  31. DolphinStoreData data;
  32. };
  33. DolphinState* dolphin_state_alloc() {
  34. DolphinState* dolphin_state = furi_alloc(sizeof(DolphinState));
  35. dolphin_state->internal_storage = furi_record_open("internal-storage");
  36. return dolphin_state;
  37. }
  38. void dolphin_state_free(DolphinState* dolphin_state) {
  39. furi_record_close("internal-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 internal-storage");
  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. int ret = internal_storage_write_key(
  61. dolphin_state->internal_storage, DOLPHIN_STORE_KEY, (uint8_t*)&store, sizeof(DolphinStore));
  62. if(ret != sizeof(DolphinStore)) {
  63. FURI_LOG_E("dolphin-state", "Save failed. Storage returned: %d", ret);
  64. return false;
  65. }
  66. FURI_LOG_I("dolphin-state", "Saved");
  67. return true;
  68. }
  69. bool dolphin_state_load(DolphinState* dolphin_state) {
  70. DolphinStore store;
  71. // Read Dolphin State Store
  72. FURI_LOG_I("dolphin-state", "Loading state from internal-storage");
  73. int ret = internal_storage_read_key(
  74. dolphin_state->internal_storage, DOLPHIN_STORE_KEY, (uint8_t*)&store, sizeof(DolphinStore));
  75. if(ret != sizeof(DolphinStore)) {
  76. FURI_LOG_E("dolphin-state", "Load failed. Storage returned: %d", ret);
  77. return false;
  78. }
  79. FURI_LOG_I("dolphin-state", "State loaded, verifying header");
  80. if(store.header.magic == DOLPHIN_STORE_HEADER_MAGIC &&
  81. store.header.version == DOLPHIN_STORE_HEADER_VERSION) {
  82. FURI_LOG_I(
  83. "dolphin-state",
  84. "Magic(%d) and Version(%d) match",
  85. store.header.magic,
  86. store.header.version);
  87. uint8_t checksum = 0;
  88. const uint8_t* source = (const uint8_t*)&store.data;
  89. for(size_t i = 0; i < sizeof(DolphinStoreData); i++) {
  90. checksum += source[i];
  91. }
  92. if(store.header.checksum == checksum) {
  93. FURI_LOG_I("dolphin-state", "Checksum(%d) match", store.header.checksum);
  94. dolphin_state->data = store.data;
  95. return true;
  96. } else {
  97. FURI_LOG_E(
  98. "dolphin-state", "Checksum(%d != %d) mismatch", store.header.checksum, checksum);
  99. }
  100. } else {
  101. FURI_LOG_E(
  102. "dolphin-state",
  103. "Magic(%d != %d) and Version(%d != %d) mismatch",
  104. store.header.magic,
  105. DOLPHIN_STORE_HEADER_MAGIC,
  106. store.header.version,
  107. DOLPHIN_STORE_HEADER_VERSION);
  108. }
  109. return false;
  110. }
  111. void dolphin_state_clear(DolphinState* dolphin_state) {
  112. memset(&dolphin_state->data, 0, sizeof(DolphinStoreData));
  113. }
  114. void dolphin_state_on_deed(DolphinState* dolphin_state, DolphinDeed deed) {
  115. const DolphinDeedWeight* deed_weight = dolphin_deed_weight(deed);
  116. int32_t icounter = dolphin_state->data.icounter + deed_weight->icounter;
  117. if(icounter >= 0) {
  118. dolphin_state->data.icounter = icounter;
  119. }
  120. }
  121. uint32_t dolphin_state_get_icounter(DolphinState* dolphin_state) {
  122. return dolphin_state->data.icounter;
  123. }
  124. uint32_t dolphin_state_get_butthurt(DolphinState* dolphin_state) {
  125. return dolphin_state->data.butthurt;
  126. }
  127. uint32_t dolphin_state_get_level(DolphinState* dolphin_state) {
  128. return 0.5f +
  129. sqrtf(1.0f + 8.0f * ((float)dolphin_state->data.icounter / DOLPHIN_LVL_THRESHOLD)) /
  130. 2.0f;
  131. }
  132. uint32_t dolphin_state_xp_to_levelup(DolphinState* dolphin_state, uint32_t level, bool remaining) {
  133. return (DOLPHIN_LVL_THRESHOLD * level * (level + 1) / 2) -
  134. (remaining ? dolphin_state->data.icounter : 0);
  135. }