storage.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. #include <game/storage.h>
  2. static bool save_uint32(const char *path_name, uint32_t value)
  3. {
  4. char buffer[32];
  5. snprintf(buffer, sizeof(buffer), "%lu", value);
  6. return save_char(path_name, buffer);
  7. }
  8. // Helper function to save an int8_t
  9. static bool save_int8(const char *path_name, int8_t value)
  10. {
  11. char buffer[32];
  12. snprintf(buffer, sizeof(buffer), "%d", value);
  13. return save_char(path_name, buffer);
  14. }
  15. // Helper function to save a float
  16. static bool save_float(const char *path_name, float value)
  17. {
  18. char buffer[32];
  19. snprintf(buffer, sizeof(buffer), "%.6f", (double)value); // Limit to 6 decimal places
  20. return save_char(path_name, buffer);
  21. }
  22. bool save_player_context(PlayerContext *player_context)
  23. {
  24. if (!player_context)
  25. {
  26. FURI_LOG_E(TAG, "Invalid player context");
  27. return false;
  28. }
  29. // ensure the folders exist
  30. char directory_path[128];
  31. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  32. Storage *storage = furi_record_open(RECORD_STORAGE);
  33. storage_common_mkdir(storage, directory_path);
  34. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  35. storage_common_mkdir(storage, directory_path);
  36. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player");
  37. storage_common_mkdir(storage, directory_path);
  38. furi_record_close(RECORD_STORAGE);
  39. // 1. Username (String)
  40. if (!save_char("player/username", player_context->username))
  41. {
  42. FURI_LOG_E(TAG, "Failed to save player username");
  43. return false;
  44. }
  45. // 2. Level (uint32_t)
  46. if (!save_uint32("player/level", player_context->level))
  47. {
  48. FURI_LOG_E(TAG, "Failed to save player level");
  49. return false;
  50. }
  51. // 3. XP (uint32_t)
  52. if (!save_uint32("player/xp", player_context->xp))
  53. {
  54. FURI_LOG_E(TAG, "Failed to save player xp");
  55. return false;
  56. }
  57. // 4. Health (uint32_t)
  58. if (!save_uint32("player/health", player_context->health))
  59. {
  60. FURI_LOG_E(TAG, "Failed to save player health");
  61. return false;
  62. }
  63. // 5. Strength (uint32_t)
  64. if (!save_uint32("player/strength", player_context->strength))
  65. {
  66. FURI_LOG_E(TAG, "Failed to save player strength");
  67. return false;
  68. }
  69. // 6. Max Health (uint32_t)
  70. if (!save_uint32("player/max_health", player_context->max_health))
  71. {
  72. FURI_LOG_E(TAG, "Failed to save player max health");
  73. return false;
  74. }
  75. // 7. Health Regen (uint32_t)
  76. if (!save_uint32("player/health_regen", player_context->health_regen))
  77. {
  78. FURI_LOG_E(TAG, "Failed to save player health regen");
  79. return false;
  80. }
  81. // 8. Elapsed Health Regen (float)
  82. if (!save_float("player/elapsed_health_regen", player_context->elapsed_health_regen))
  83. {
  84. FURI_LOG_E(TAG, "Failed to save player elapsed health regen");
  85. return false;
  86. }
  87. // 9. Attack Timer (float)
  88. if (!save_float("player/attack_timer", player_context->attack_timer))
  89. {
  90. FURI_LOG_E(TAG, "Failed to save player attack timer");
  91. return false;
  92. }
  93. // 10. Elapsed Attack Timer (float)
  94. if (!save_float("player/elapsed_attack_timer", player_context->elapsed_attack_timer))
  95. {
  96. FURI_LOG_E(TAG, "Failed to save player elapsed attack timer");
  97. return false;
  98. }
  99. // 11. Direction (enum PlayerDirection)
  100. {
  101. char direction_str[2];
  102. switch (player_context->direction)
  103. {
  104. case PLAYER_UP:
  105. strncpy(direction_str, "0", sizeof(direction_str));
  106. break;
  107. case PLAYER_DOWN:
  108. strncpy(direction_str, "1", sizeof(direction_str));
  109. break;
  110. case PLAYER_LEFT:
  111. strncpy(direction_str, "2", sizeof(direction_str));
  112. break;
  113. case PLAYER_RIGHT:
  114. default:
  115. strncpy(direction_str, "3", sizeof(direction_str));
  116. break;
  117. }
  118. direction_str[1] = '\0'; // Ensure null termination
  119. if (!save_char("player/direction", direction_str))
  120. {
  121. FURI_LOG_E(TAG, "Failed to save player direction");
  122. return false;
  123. }
  124. }
  125. // 12. State (enum PlayerState)
  126. {
  127. char state_str[2];
  128. switch (player_context->state)
  129. {
  130. case PLAYER_IDLE:
  131. strncpy(state_str, "0", sizeof(state_str));
  132. break;
  133. case PLAYER_MOVING:
  134. strncpy(state_str, "1", sizeof(state_str));
  135. break;
  136. case PLAYER_ATTACKING:
  137. strncpy(state_str, "2", sizeof(state_str));
  138. break;
  139. case PLAYER_ATTACKED:
  140. strncpy(state_str, "3", sizeof(state_str));
  141. break;
  142. case PLAYER_DEAD:
  143. strncpy(state_str, "4", sizeof(state_str));
  144. break;
  145. default:
  146. strncpy(state_str, "5", sizeof(state_str)); // Assuming '5' for unknown states
  147. break;
  148. }
  149. state_str[1] = '\0'; // Ensure null termination
  150. if (!save_char("player/state", state_str))
  151. {
  152. FURI_LOG_E(TAG, "Failed to save player state");
  153. return false;
  154. }
  155. }
  156. // 13. Start Position X (float)
  157. if (!save_float("player/start_position_x", player_context->start_position.x))
  158. {
  159. FURI_LOG_E(TAG, "Failed to save player start position x");
  160. return false;
  161. }
  162. // 14. Start Position Y (float)
  163. if (!save_float("player/start_position_y", player_context->start_position.y))
  164. {
  165. FURI_LOG_E(TAG, "Failed to save player start position y");
  166. return false;
  167. }
  168. // 15. dx (int8_t)
  169. if (!save_int8("player/dx", player_context->dx))
  170. {
  171. FURI_LOG_E(TAG, "Failed to save player dx");
  172. return false;
  173. }
  174. // 16. dy (int8_t)
  175. if (!save_int8("player/dy", player_context->dy))
  176. {
  177. FURI_LOG_E(TAG, "Failed to save player dy");
  178. return false;
  179. }
  180. return true;
  181. }
  182. bool save_player_context_api(PlayerContext *player_context)
  183. {
  184. if (!player_context)
  185. {
  186. FURI_LOG_E(TAG, "Invalid player context");
  187. return false;
  188. }
  189. FlipperHTTP *fhttp = flipper_http_alloc();
  190. if (!fhttp)
  191. {
  192. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  193. return false;
  194. }
  195. // create JSON for all the player context data
  196. FuriString *json = furi_string_alloc();
  197. if (!json)
  198. {
  199. FURI_LOG_E(TAG, "Failed to allocate JSON string");
  200. return false;
  201. }
  202. // opening brace
  203. furi_string_cat_str(json, "{");
  204. // 1. Username (String)
  205. furi_string_cat_str(json, "\"username\":\"");
  206. furi_string_cat_str(json, player_context->username);
  207. furi_string_cat_str(json, "\",");
  208. // 2. Level (uint32_t)
  209. furi_string_cat_str(json, "\"level\":");
  210. char buffer[32];
  211. snprintf(buffer, sizeof(buffer), "%lu", player_context->level);
  212. furi_string_cat_str(json, buffer);
  213. furi_string_cat_str(json, ",");
  214. // 3. XP (uint32_t)
  215. furi_string_cat_str(json, "\"xp\":");
  216. snprintf(buffer, sizeof(buffer), "%lu", player_context->xp);
  217. furi_string_cat_str(json, buffer);
  218. furi_string_cat_str(json, ",");
  219. // 4. Health (uint32_t)
  220. furi_string_cat_str(json, "\"health\":");
  221. snprintf(buffer, sizeof(buffer), "%lu", player_context->health);
  222. furi_string_cat_str(json, buffer);
  223. furi_string_cat_str(json, ",");
  224. // 5. Strength (uint32_t)
  225. furi_string_cat_str(json, "\"strength\":");
  226. snprintf(buffer, sizeof(buffer), "%lu", player_context->strength);
  227. furi_string_cat_str(json, buffer);
  228. furi_string_cat_str(json, ",");
  229. // 6. Max Health (uint32_t)
  230. furi_string_cat_str(json, "\"max_health\":");
  231. snprintf(buffer, sizeof(buffer), "%lu", player_context->max_health);
  232. furi_string_cat_str(json, buffer);
  233. furi_string_cat_str(json, ",");
  234. // 7. Health Regen (uint32_t)
  235. furi_string_cat_str(json, "\"health_regen\":");
  236. snprintf(buffer, sizeof(buffer), "%lu", player_context->health_regen);
  237. furi_string_cat_str(json, buffer);
  238. furi_string_cat_str(json, ",");
  239. // 8. Elapsed Health Regen (float)
  240. furi_string_cat_str(json, "\"elapsed_health_regen\":");
  241. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->elapsed_health_regen);
  242. furi_string_cat_str(json, buffer);
  243. furi_string_cat_str(json, ",");
  244. // 9. Attack Timer (float)
  245. furi_string_cat_str(json, "\"attack_timer\":");
  246. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->attack_timer);
  247. furi_string_cat_str(json, buffer);
  248. furi_string_cat_str(json, ",");
  249. // 10. Elapsed Attack Timer (float)
  250. furi_string_cat_str(json, "\"elapsed_attack_timer\":");
  251. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->elapsed_attack_timer);
  252. furi_string_cat_str(json, buffer);
  253. furi_string_cat_str(json, ",");
  254. // 11. Direction (enum PlayerDirection)
  255. furi_string_cat_str(json, "\"direction\":");
  256. switch (player_context->direction)
  257. {
  258. case PLAYER_UP:
  259. furi_string_cat_str(json, "\"up\",");
  260. break;
  261. case PLAYER_DOWN:
  262. furi_string_cat_str(json, "\"down\",");
  263. break;
  264. case PLAYER_LEFT:
  265. furi_string_cat_str(json, "\"left\",");
  266. break;
  267. case PLAYER_RIGHT:
  268. default:
  269. furi_string_cat_str(json, "\"right\",");
  270. break;
  271. }
  272. // 12. State (enum PlayerState)
  273. furi_string_cat_str(json, "\"state\":");
  274. switch (player_context->state)
  275. {
  276. case PLAYER_IDLE:
  277. furi_string_cat_str(json, "\"idle\",");
  278. break;
  279. case PLAYER_MOVING:
  280. furi_string_cat_str(json, "\"moving\",");
  281. break;
  282. case PLAYER_ATTACKING:
  283. furi_string_cat_str(json, "\"attacking\",");
  284. break;
  285. case PLAYER_ATTACKED:
  286. furi_string_cat_str(json, "\"attacked\",");
  287. break;
  288. case PLAYER_DEAD:
  289. furi_string_cat_str(json, "\"dead\",");
  290. break;
  291. default:
  292. furi_string_cat_str(json, "\"unknown\",");
  293. break;
  294. }
  295. // 13. Start Position X (float)
  296. furi_string_cat_str(json, "\"start_position_x\":");
  297. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->start_position.x);
  298. furi_string_cat_str(json, buffer);
  299. furi_string_cat_str(json, ",");
  300. // 14. Start Position Y (float)
  301. furi_string_cat_str(json, "\"start_position_y\":");
  302. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->start_position.y);
  303. furi_string_cat_str(json, buffer);
  304. furi_string_cat_str(json, ",");
  305. // 15. dx (int8_t)
  306. furi_string_cat_str(json, "\"dx\":");
  307. snprintf(buffer, sizeof(buffer), "%d", player_context->dx);
  308. furi_string_cat_str(json, buffer);
  309. furi_string_cat_str(json, ",");
  310. // 16. dy (int8_t)
  311. furi_string_cat_str(json, "\"dy\":");
  312. snprintf(buffer, sizeof(buffer), "%d", player_context->dy);
  313. furi_string_cat_str(json, buffer);
  314. // closing brace
  315. furi_string_cat_str(json, "}");
  316. // save the json to API
  317. // create new JSON with username key (of just username), and game_stats key (of the all of the data)
  318. FuriString *json_data = furi_string_alloc();
  319. if (!json_data)
  320. {
  321. FURI_LOG_E(TAG, "Failed to allocate JSON string");
  322. furi_string_free(json);
  323. return false;
  324. }
  325. furi_string_cat_str(json_data, "{\"username\":\"");
  326. furi_string_cat_str(json_data, player_context->username);
  327. furi_string_cat_str(json_data, "\",\"game_stats\":");
  328. furi_string_cat(json_data, json);
  329. furi_string_cat_str(json_data, "}");
  330. furi_string_free(json);
  331. // save the json_data to the API
  332. if (!flipper_http_post_request_with_headers(fhttp, "https://www.flipsocial.net/api/user/update-game-stats/", "{\"Content-Type\":\"application/json\"}", furi_string_get_cstr(json_data)))
  333. {
  334. FURI_LOG_E(TAG, "Failed to save player context to API");
  335. furi_string_free(json_data);
  336. return false;
  337. }
  338. fhttp->state = RECEIVING;
  339. while (fhttp->state != IDLE)
  340. {
  341. furi_delay_ms(100);
  342. }
  343. furi_string_free(json_data);
  344. flipper_http_free(fhttp);
  345. return true;
  346. }
  347. // Helper function to load an integer
  348. static bool load_number(const char *path_name, int *value)
  349. {
  350. if (!path_name || !value)
  351. {
  352. FURI_LOG_E(TAG, "Invalid arguments to load_number");
  353. return false;
  354. }
  355. char buffer[64];
  356. if (!load_char(path_name, buffer, sizeof(buffer)))
  357. {
  358. FURI_LOG_E(TAG, "Failed to load number from path: %s", path_name);
  359. return false;
  360. }
  361. *value = atoi(buffer);
  362. return true;
  363. }
  364. // Helper function to load a float
  365. static bool load_float(const char *path_name, float *value)
  366. {
  367. if (!path_name || !value)
  368. {
  369. FURI_LOG_E(TAG, "Invalid arguments to load_float");
  370. return false;
  371. }
  372. char buffer[64];
  373. if (!load_char(path_name, buffer, sizeof(buffer)))
  374. {
  375. FURI_LOG_E(TAG, "Failed to load float from path: %s", path_name);
  376. return false;
  377. }
  378. // check if the string is a valid float
  379. char *endptr;
  380. *value = strtof(buffer, &endptr);
  381. if (endptr == buffer)
  382. {
  383. FURI_LOG_E(TAG, "Failed to parse float from path: %s", path_name);
  384. return false;
  385. }
  386. return true;
  387. }
  388. // Helper function to load an int8_t
  389. static bool load_int8(const char *path_name, int8_t *value)
  390. {
  391. if (!path_name || !value)
  392. {
  393. FURI_LOG_E(TAG, "Invalid arguments to load_int8");
  394. return false;
  395. }
  396. char buffer[64];
  397. if (!load_char(path_name, buffer, sizeof(buffer)))
  398. {
  399. FURI_LOG_E(TAG, "Failed to load int8 from path: %s", path_name);
  400. return false;
  401. }
  402. long temp = strtol(buffer, NULL, 10);
  403. if (temp < INT8_MIN || temp > INT8_MAX)
  404. {
  405. FURI_LOG_E(TAG, "Value out of range for int8: %ld", temp);
  406. return false;
  407. }
  408. // check if the string is a valid int8
  409. char *endptr;
  410. *value = (int8_t)strtol(buffer, &endptr, 10);
  411. if (endptr == buffer)
  412. {
  413. FURI_LOG_E(TAG, "Failed to parse int8 from path: %s", path_name);
  414. return false;
  415. }
  416. return true;
  417. }
  418. // Helper function to load a uint32_t
  419. static bool load_uint32(const char *path_name, uint32_t *value)
  420. {
  421. if (!path_name || !value)
  422. {
  423. FURI_LOG_E(TAG, "Invalid arguments to load_uint32");
  424. return false;
  425. }
  426. char buffer[64];
  427. if (!load_char(path_name, buffer, sizeof(buffer)))
  428. {
  429. FURI_LOG_E(TAG, "Failed to load uint32 from path: %s", path_name);
  430. return false;
  431. }
  432. // check if the string is a valid uint32
  433. char *endptr;
  434. *value = strtoul(buffer, &endptr, 10);
  435. if (endptr == buffer)
  436. {
  437. FURI_LOG_E(TAG, "Failed to parse uint32 from path: %s", path_name);
  438. return false;
  439. }
  440. return true;
  441. }
  442. bool load_player_context(PlayerContext *player_context)
  443. {
  444. if (!player_context)
  445. {
  446. FURI_LOG_E(TAG, "Invalid player context");
  447. return false;
  448. }
  449. // 1. Username (String)
  450. if (!load_char("player/username", player_context->username, sizeof(player_context->username)))
  451. {
  452. FURI_LOG_E(TAG, "No data or parse error for username. Using default: 'Unknown'");
  453. memset(player_context->username, 0, sizeof(player_context->username));
  454. strncpy(player_context->username, "Unknown", sizeof(player_context->username) - 1);
  455. }
  456. // 2. Level (uint32_t)
  457. {
  458. uint32_t temp = 1; // Default
  459. if (!load_char("player/level", (char *)&temp, sizeof(temp)))
  460. {
  461. FURI_LOG_E(TAG, "No data or parse error for level. Using default: 1");
  462. }
  463. else
  464. {
  465. // char buffer[64];
  466. if (load_uint32("player/level", &temp))
  467. {
  468. player_context->level = temp;
  469. }
  470. else
  471. {
  472. FURI_LOG_E(TAG, "Failed to parse level. Using default: 1");
  473. player_context->level = 1;
  474. }
  475. }
  476. player_context->level = temp;
  477. }
  478. // 3. XP (uint32_t)
  479. {
  480. uint32_t temp = 0; // Default
  481. if (!load_uint32("player/xp", &temp))
  482. {
  483. FURI_LOG_E(TAG, "No data or parse error for xp. Using default: 0");
  484. temp = 0;
  485. }
  486. player_context->xp = temp;
  487. }
  488. // 4. Health (uint32_t)
  489. {
  490. uint32_t temp = 100; // Default
  491. if (!load_uint32("player/health", &temp))
  492. {
  493. FURI_LOG_E(TAG, "No data or parse error for health. Using default: 100");
  494. temp = 100;
  495. }
  496. player_context->health = temp;
  497. }
  498. // 5. Strength (uint32_t)
  499. {
  500. uint32_t temp = 10; // Default
  501. if (!load_uint32("player/strength", &temp))
  502. {
  503. FURI_LOG_E(TAG, "No data or parse error for strength. Using default: 10");
  504. temp = 10;
  505. }
  506. player_context->strength = temp;
  507. }
  508. // 6. Max Health (uint32_t)
  509. {
  510. uint32_t temp = 100; // Default
  511. if (!load_uint32("player/max_health", &temp))
  512. {
  513. FURI_LOG_E(TAG, "No data or parse error for max_health. Using default: 100");
  514. temp = 100;
  515. }
  516. player_context->max_health = temp;
  517. }
  518. // 7. Health Regen (uint32_t)
  519. {
  520. uint32_t temp = 1; // Default
  521. if (!load_uint32("player/health_regen", &temp))
  522. {
  523. FURI_LOG_E(TAG, "No data or parse error for health_regen. Using default: 1");
  524. temp = 1;
  525. }
  526. player_context->health_regen = temp;
  527. }
  528. // 8. Elapsed Health Regen (float)
  529. {
  530. float temp = 0.0f; // Default
  531. if (!load_float("player/elapsed_health_regen", &temp))
  532. {
  533. FURI_LOG_E(TAG, "No data or parse error for elapsed_health_regen. Using default: 0.0f");
  534. temp = 0.0f;
  535. }
  536. player_context->elapsed_health_regen = temp;
  537. }
  538. // 9. Attack Timer (float)
  539. {
  540. float temp = 0.1f; // Default
  541. if (!load_float("player/attack_timer", &temp))
  542. {
  543. FURI_LOG_E(TAG, "No data or parse error for attack_timer. Using default: 0.1f");
  544. temp = 0.1f;
  545. }
  546. player_context->attack_timer = temp;
  547. }
  548. // 10. Elapsed Attack Timer (float)
  549. {
  550. float temp = 0.0f; // Default
  551. if (!load_float("player/elapsed_attack_timer", &temp))
  552. {
  553. FURI_LOG_E(TAG, "No data or parse error for elapsed_attack_timer. Using default: 0.0f");
  554. temp = 0.0f;
  555. }
  556. player_context->elapsed_attack_timer = temp;
  557. }
  558. // 11. Direction (enum PlayerDirection)
  559. {
  560. int direction_int = 3; // Default to PLAYER_RIGHT
  561. if (!load_number("player/direction", &direction_int))
  562. {
  563. FURI_LOG_E(TAG, "No data or parse error for direction. Defaulting to PLAYER_RIGHT");
  564. direction_int = 3;
  565. }
  566. switch (direction_int)
  567. {
  568. case 0:
  569. player_context->direction = PLAYER_UP;
  570. break;
  571. case 1:
  572. player_context->direction = PLAYER_DOWN;
  573. break;
  574. case 2:
  575. player_context->direction = PLAYER_LEFT;
  576. break;
  577. case 3:
  578. player_context->direction = PLAYER_RIGHT;
  579. break;
  580. default:
  581. FURI_LOG_E(TAG, "Invalid direction value: %d. Defaulting to PLAYER_RIGHT", direction_int);
  582. player_context->direction = PLAYER_RIGHT;
  583. break;
  584. }
  585. }
  586. // 12. State (enum PlayerState)
  587. {
  588. int state_int = 0; // Default to PLAYER_IDLE
  589. if (!load_number("player/state", &state_int))
  590. {
  591. FURI_LOG_E(TAG, "No data or parse error for state. Defaulting to PLAYER_IDLE");
  592. state_int = 0;
  593. }
  594. switch (state_int)
  595. {
  596. case 0:
  597. player_context->state = PLAYER_IDLE;
  598. break;
  599. case 1:
  600. player_context->state = PLAYER_MOVING;
  601. break;
  602. case 2:
  603. player_context->state = PLAYER_ATTACKING;
  604. break;
  605. case 3:
  606. player_context->state = PLAYER_ATTACKED;
  607. break;
  608. case 4:
  609. player_context->state = PLAYER_DEAD;
  610. break;
  611. default:
  612. FURI_LOG_E(TAG, "Invalid state value: %d. Defaulting to PLAYER_IDLE", state_int);
  613. player_context->state = PLAYER_IDLE;
  614. break;
  615. }
  616. }
  617. // 13. Start Position X (float)
  618. {
  619. float temp = 192.0f; // Default
  620. if (!load_float("player/start_position_x", &temp))
  621. {
  622. FURI_LOG_E(TAG, "No data or parse error for start_position_x. Using default: 192.0f");
  623. temp = 192.0f;
  624. }
  625. player_context->start_position.x = temp;
  626. }
  627. // 14. Start Position Y (float)
  628. {
  629. float temp = 96.0f; // Default
  630. if (!load_float("player/start_position_y", &temp))
  631. {
  632. FURI_LOG_E(TAG, "No data or parse error for start_position_y. Using default: 96.0f");
  633. temp = 96.0f;
  634. }
  635. player_context->start_position.y = temp;
  636. }
  637. // 15. dx (int8_t)
  638. {
  639. int8_t temp = 1; // Default
  640. if (!load_int8("player/dx", &temp))
  641. {
  642. FURI_LOG_E(TAG, "No data or parse error for dx. Using default: 1");
  643. temp = 1;
  644. }
  645. player_context->dx = temp;
  646. }
  647. // 16. dy (int8_t)
  648. {
  649. int8_t temp = 0; // Default
  650. if (!load_int8("player/dy", &temp))
  651. {
  652. FURI_LOG_E(TAG, "No data or parse error for dy. Using default: 0");
  653. temp = 0;
  654. }
  655. player_context->dy = temp;
  656. }
  657. return true;
  658. }
  659. // loads from STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json
  660. // then gets each key-value pair and saves it as it's own file so it can be loaded separately using
  661. // load_player_context
  662. bool set_player_context()
  663. {
  664. char file_path[256];
  665. snprintf(file_path, sizeof(file_path),
  666. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  667. FuriString *player_stats = flipper_http_load_from_file(file_path);
  668. if (!player_stats)
  669. {
  670. FURI_LOG_E(TAG, "Failed to load player stats from file: %s", file_path);
  671. return false;
  672. }
  673. // Get the key one-by-one and save it to a separate file
  674. // 1. Username (String)
  675. FuriString *username = get_json_value_furi("username", player_stats);
  676. if (username)
  677. {
  678. save_char("player/username", furi_string_get_cstr(username));
  679. furi_string_free(username);
  680. }
  681. // 2. Level (uint32_t)
  682. FuriString *level = get_json_value_furi("level", player_stats);
  683. if (level)
  684. {
  685. save_uint32("player/level", atoi(furi_string_get_cstr(level)));
  686. furi_string_free(level);
  687. }
  688. // 3. XP (uint32_t)
  689. FuriString *xp = get_json_value_furi("xp", player_stats);
  690. if (xp)
  691. {
  692. save_uint32("player/xp", atoi(furi_string_get_cstr(xp)));
  693. furi_string_free(xp);
  694. }
  695. // 4. Health (uint32_t)
  696. FuriString *health = get_json_value_furi("health", player_stats);
  697. if (health)
  698. {
  699. save_uint32("player/health", atoi(furi_string_get_cstr(health)));
  700. furi_string_free(health);
  701. }
  702. // 5. Strength (uint32_t)
  703. FuriString *strength = get_json_value_furi("strength", player_stats);
  704. if (strength)
  705. {
  706. save_uint32("player/strength", atoi(furi_string_get_cstr(strength)));
  707. furi_string_free(strength);
  708. }
  709. // 6. Max Health (uint32_t)
  710. FuriString *max_health = get_json_value_furi("max_health", player_stats);
  711. if (max_health)
  712. {
  713. save_uint32("player/max_health", atoi(furi_string_get_cstr(max_health)));
  714. furi_string_free(max_health);
  715. }
  716. // 7. Health Regen (uint32_t)
  717. FuriString *health_regen = get_json_value_furi("health_regen", player_stats);
  718. if (health_regen)
  719. {
  720. save_uint32("player/health_regen", atoi(furi_string_get_cstr(health_regen)));
  721. furi_string_free(health_regen);
  722. }
  723. // 8. Elapsed Health Regen (float)
  724. FuriString *elapsed_health_regen = get_json_value_furi("elapsed_health_regen", player_stats);
  725. if (elapsed_health_regen)
  726. {
  727. save_float("player/elapsed_health_regen", strtof(furi_string_get_cstr(elapsed_health_regen), NULL));
  728. furi_string_free(elapsed_health_regen);
  729. }
  730. // 9. Attack Timer (float)
  731. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  732. if (attack_timer)
  733. {
  734. save_float("player/attack_timer", strtof(furi_string_get_cstr(attack_timer), NULL));
  735. furi_string_free(attack_timer);
  736. }
  737. // 10. Elapsed Attack Timer (float)
  738. FuriString *elapsed_attack_timer = get_json_value_furi("elapsed_attack_timer", player_stats);
  739. if (elapsed_attack_timer)
  740. {
  741. save_float("player/elapsed_attack_timer", strtof(furi_string_get_cstr(elapsed_attack_timer), NULL));
  742. furi_string_free(elapsed_attack_timer);
  743. }
  744. // 11. Direction (enum PlayerDirection)
  745. FuriString *direction = get_json_value_furi("direction", player_stats);
  746. if (direction)
  747. {
  748. save_char("player/direction", furi_string_get_cstr(direction));
  749. furi_string_free(direction);
  750. }
  751. // 12. State (enum PlayerState)
  752. FuriString *state = get_json_value_furi("state", player_stats);
  753. if (state)
  754. {
  755. save_char("player/state", furi_string_get_cstr(state));
  756. furi_string_free(state);
  757. }
  758. // 13. Start Position X (float)
  759. FuriString *start_position_x = get_json_value_furi("start_position_x", player_stats);
  760. if (start_position_x)
  761. {
  762. save_float("player/start_position_x", strtof(furi_string_get_cstr(start_position_x), NULL));
  763. furi_string_free(start_position_x);
  764. }
  765. // 14. Start Position Y (float)
  766. FuriString *start_position_y = get_json_value_furi("start_position_y", player_stats);
  767. if (start_position_y)
  768. {
  769. save_float("player/start_position_y", strtof(furi_string_get_cstr(start_position_y), NULL));
  770. furi_string_free(start_position_y);
  771. }
  772. // 15. dx (int8_t)
  773. FuriString *dx = get_json_value_furi("dx", player_stats);
  774. if (dx)
  775. {
  776. save_int8("player/dx", atoi(furi_string_get_cstr(dx)));
  777. furi_string_free(dx);
  778. }
  779. // 16. dy (int8_t)
  780. FuriString *dy = get_json_value_furi("dy", player_stats);
  781. if (dy)
  782. {
  783. save_int8("player/dy", atoi(furi_string_get_cstr(dy)));
  784. furi_string_free(dy);
  785. }
  786. furi_string_free(player_stats);
  787. return true;
  788. }
  789. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  790. {
  791. furi_string_replace_str(string, needle, "", 0);
  792. }
  793. static FuriString *enemy_data(const FuriString *world_data)
  794. {
  795. size_t enemy_data_pos = furi_string_search_str(world_data, "enemy_data", 0);
  796. if (enemy_data_pos == FURI_STRING_FAILURE)
  797. {
  798. FURI_LOG_E("Game", "Failed to find enemy_data in world data");
  799. return NULL;
  800. }
  801. size_t bracket_start = furi_string_search_char(world_data, '[', enemy_data_pos);
  802. if (bracket_start == FURI_STRING_FAILURE)
  803. {
  804. FURI_LOG_E("Game", "Failed to find start of enemy_data array");
  805. return NULL;
  806. }
  807. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  808. if (bracket_end == FURI_STRING_FAILURE)
  809. {
  810. FURI_LOG_E("Game", "Failed to find end of enemy_data array");
  811. return NULL;
  812. }
  813. FuriString *enemy_data_str = furi_string_alloc();
  814. if (!enemy_data_str)
  815. {
  816. FURI_LOG_E("Game", "Failed to allocate enemy_data string");
  817. return NULL;
  818. }
  819. furi_string_cat_str(enemy_data_str, "{\"enemy_data\":");
  820. {
  821. FuriString *temp_sub = furi_string_alloc();
  822. furi_string_set_strn(
  823. temp_sub,
  824. furi_string_get_cstr(world_data) + bracket_start,
  825. (bracket_end + 1) - bracket_start);
  826. furi_string_cat(enemy_data_str, temp_sub);
  827. furi_string_free(temp_sub);
  828. }
  829. furi_string_cat_str(enemy_data_str, "}");
  830. return enemy_data_str;
  831. }
  832. static FuriString *json_data(const FuriString *world_data)
  833. {
  834. size_t json_data_pos = furi_string_search_str(world_data, "json_data", 0);
  835. if (json_data_pos == FURI_STRING_FAILURE)
  836. {
  837. FURI_LOG_E("Game", "Failed to find json_data in world data");
  838. return NULL;
  839. }
  840. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  841. if (bracket_start == FURI_STRING_FAILURE)
  842. {
  843. FURI_LOG_E("Game", "Failed to find start of json_data array");
  844. return NULL;
  845. }
  846. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  847. if (bracket_end == FURI_STRING_FAILURE)
  848. {
  849. FURI_LOG_E("Game", "Failed to find end of json_data array");
  850. return NULL;
  851. }
  852. FuriString *json_data_str = furi_string_alloc();
  853. if (!json_data_str)
  854. {
  855. FURI_LOG_E("Game", "Failed to allocate json_data string");
  856. return NULL;
  857. }
  858. furi_string_cat_str(json_data_str, "{\"json_data\":");
  859. {
  860. FuriString *temp_sub = furi_string_alloc();
  861. furi_string_set_strn(
  862. temp_sub,
  863. furi_string_get_cstr(world_data) + bracket_start,
  864. (bracket_end + 1) - bracket_start);
  865. furi_string_cat(json_data_str, temp_sub);
  866. furi_string_free(temp_sub);
  867. }
  868. furi_string_cat_str(json_data_str, "}");
  869. return json_data_str;
  870. }
  871. bool separate_world_data(char *id, FuriString *world_data)
  872. {
  873. if (!id || !world_data)
  874. {
  875. FURI_LOG_E("Game", "Invalid parameters");
  876. return false;
  877. }
  878. FuriString *file_json_data = json_data(world_data);
  879. if (!file_json_data || furi_string_size(file_json_data) == 0)
  880. {
  881. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  882. return false;
  883. }
  884. // Save file_json_data to disk
  885. char directory_path[256];
  886. snprintf(directory_path, sizeof(directory_path),
  887. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  888. Storage *storage = furi_record_open(RECORD_STORAGE);
  889. storage_common_mkdir(storage, directory_path);
  890. File *file = storage_file_alloc(storage);
  891. char file_path[256];
  892. snprintf(file_path, sizeof(file_path),
  893. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  894. id, id);
  895. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  896. {
  897. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  898. storage_file_free(file);
  899. furi_record_close(RECORD_STORAGE);
  900. furi_string_free(file_json_data);
  901. return false;
  902. }
  903. size_t data_size = furi_string_size(file_json_data);
  904. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  905. {
  906. FURI_LOG_E("Game", "Failed to write json_data");
  907. }
  908. storage_file_close(file);
  909. furi_string_replace_at(file_json_data, 0, 1, "");
  910. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  911. // include the comma at the end of the json_data array
  912. furi_string_cat_str(file_json_data, ",");
  913. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  914. furi_string_free(file_json_data);
  915. FuriString *file_enemy_data = enemy_data(world_data);
  916. if (!file_enemy_data)
  917. {
  918. FURI_LOG_E("Game", "Failed to get enemy data");
  919. return false;
  920. }
  921. snprintf(file_path, sizeof(file_path),
  922. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  923. id, id);
  924. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  925. {
  926. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  927. storage_file_free(file);
  928. furi_record_close(RECORD_STORAGE);
  929. furi_string_free(file_enemy_data);
  930. return false;
  931. }
  932. data_size = furi_string_size(file_enemy_data);
  933. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  934. {
  935. FURI_LOG_E("Game", "Failed to write enemy_data");
  936. }
  937. // Clean up
  938. furi_string_free(file_enemy_data);
  939. storage_file_close(file);
  940. storage_file_free(file);
  941. furi_record_close(RECORD_STORAGE);
  942. return true;
  943. }