storage.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 ENTITY_UP:
  105. strncpy(direction_str, "0", sizeof(direction_str));
  106. break;
  107. case ENTITY_DOWN:
  108. strncpy(direction_str, "1", sizeof(direction_str));
  109. break;
  110. case ENTITY_LEFT:
  111. strncpy(direction_str, "2", sizeof(direction_str));
  112. break;
  113. case ENTITY_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 ENTITY_IDLE:
  131. strncpy(state_str, "0", sizeof(state_str));
  132. break;
  133. case ENTITY_MOVING:
  134. strncpy(state_str, "1", sizeof(state_str));
  135. break;
  136. case ENTITY_ATTACKING:
  137. strncpy(state_str, "2", sizeof(state_str));
  138. break;
  139. case ENTITY_ATTACKED:
  140. strncpy(state_str, "3", sizeof(state_str));
  141. break;
  142. case ENTITY_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 ENTITY_UP:
  259. furi_string_cat_str(json, "\"up\",");
  260. break;
  261. case ENTITY_DOWN:
  262. furi_string_cat_str(json, "\"down\",");
  263. break;
  264. case ENTITY_LEFT:
  265. furi_string_cat_str(json, "\"left\",");
  266. break;
  267. case ENTITY_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 ENTITY_IDLE:
  277. furi_string_cat_str(json, "\"idle\",");
  278. break;
  279. case ENTITY_MOVING:
  280. furi_string_cat_str(json, "\"moving\",");
  281. break;
  282. case ENTITY_ATTACKING:
  283. furi_string_cat_str(json, "\"attacking\",");
  284. break;
  285. case ENTITY_ATTACKED:
  286. furi_string_cat_str(json, "\"attacked\",");
  287. break;
  288. case ENTITY_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. // create new JSON with username key (of just username), and game_stats key (of the all of the data)
  317. FuriString *json_data = furi_string_alloc();
  318. if (!json_data)
  319. {
  320. FURI_LOG_E(TAG, "Failed to allocate JSON string");
  321. furi_string_free(json);
  322. return false;
  323. }
  324. furi_string_cat_str(json_data, "{\"username\":\"");
  325. furi_string_cat_str(json_data, player_context->username);
  326. furi_string_cat_str(json_data, "\",\"game_stats\":");
  327. furi_string_cat(json_data, json);
  328. furi_string_cat_str(json_data, "}");
  329. furi_string_free(json);
  330. // save the json_data to the API
  331. if (!flipper_http_request(fhttp, POST, "https://www.flipsocial.net/api/user/update-game-stats/", "{\"Content-Type\": \"application/json\"}", furi_string_get_cstr(json_data)))
  332. {
  333. FURI_LOG_E(TAG, "Failed to save player context to API");
  334. furi_string_free(json_data);
  335. return false;
  336. }
  337. fhttp->state = RECEIVING;
  338. while (fhttp->state != IDLE)
  339. {
  340. furi_delay_ms(100);
  341. }
  342. furi_string_free(json_data);
  343. flipper_http_free(fhttp);
  344. return true;
  345. }
  346. // Helper function to load an integer
  347. static bool load_number(const char *path_name, int *value)
  348. {
  349. if (!path_name || !value)
  350. {
  351. FURI_LOG_E(TAG, "Invalid arguments to load_number");
  352. return false;
  353. }
  354. char buffer[64];
  355. if (!load_char(path_name, buffer, sizeof(buffer)))
  356. {
  357. FURI_LOG_E(TAG, "Failed to load number from path: %s", path_name);
  358. return false;
  359. }
  360. *value = atoi(buffer);
  361. return true;
  362. }
  363. // Helper function to load a float
  364. static bool load_float(const char *path_name, float *value)
  365. {
  366. if (!path_name || !value)
  367. {
  368. FURI_LOG_E(TAG, "Invalid arguments to load_float");
  369. return false;
  370. }
  371. char buffer[64];
  372. if (!load_char(path_name, buffer, sizeof(buffer)))
  373. {
  374. FURI_LOG_E(TAG, "Failed to load float from path: %s", path_name);
  375. return false;
  376. }
  377. // check if the string is a valid float
  378. char *endptr;
  379. *value = strtof(buffer, &endptr);
  380. if (endptr == buffer)
  381. {
  382. FURI_LOG_E(TAG, "Failed to parse float from path: %s", path_name);
  383. return false;
  384. }
  385. return true;
  386. }
  387. // Helper function to load an int8_t
  388. static bool load_int8(const char *path_name, int8_t *value)
  389. {
  390. if (!path_name || !value)
  391. {
  392. FURI_LOG_E(TAG, "Invalid arguments to load_int8");
  393. return false;
  394. }
  395. char buffer[64];
  396. if (!load_char(path_name, buffer, sizeof(buffer)))
  397. {
  398. FURI_LOG_E(TAG, "Failed to load int8 from path: %s", path_name);
  399. return false;
  400. }
  401. long temp = strtol(buffer, NULL, 10);
  402. if (temp < INT8_MIN || temp > INT8_MAX)
  403. {
  404. FURI_LOG_E(TAG, "Value out of range for int8: %ld", temp);
  405. return false;
  406. }
  407. // check if the string is a valid int8
  408. char *endptr;
  409. *value = (int8_t)strtol(buffer, &endptr, 10);
  410. if (endptr == buffer)
  411. {
  412. FURI_LOG_E(TAG, "Failed to parse int8 from path: %s", path_name);
  413. return false;
  414. }
  415. return true;
  416. }
  417. // Helper function to load a uint32_t
  418. static bool load_uint32(const char *path_name, uint32_t *value)
  419. {
  420. if (!path_name || !value)
  421. {
  422. FURI_LOG_E(TAG, "Invalid arguments to load_uint32");
  423. return false;
  424. }
  425. char buffer[64];
  426. if (!load_char(path_name, buffer, sizeof(buffer)))
  427. {
  428. FURI_LOG_E(TAG, "Failed to load uint32 from path: %s", path_name);
  429. return false;
  430. }
  431. // check if the string is a valid uint32
  432. char *endptr;
  433. *value = strtoul(buffer, &endptr, 10);
  434. if (endptr == buffer)
  435. {
  436. FURI_LOG_E(TAG, "Failed to parse uint32 from path: %s", path_name);
  437. return false;
  438. }
  439. return true;
  440. }
  441. bool load_player_context(PlayerContext *player_context)
  442. {
  443. if (!player_context)
  444. {
  445. FURI_LOG_E(TAG, "Invalid player context");
  446. return false;
  447. }
  448. // 1. Username (String)
  449. if (!load_char("player/username", player_context->username, sizeof(player_context->username)))
  450. {
  451. FURI_LOG_E(TAG, "No data or parse error for username. Using default: 'Unknown'");
  452. memset(player_context->username, 0, sizeof(player_context->username));
  453. strncpy(player_context->username, "Unknown", sizeof(player_context->username) - 1);
  454. }
  455. // 2. Level (uint32_t)
  456. {
  457. uint32_t temp = 1; // Default
  458. if (!load_char("player/level", (char *)&temp, sizeof(temp)))
  459. {
  460. FURI_LOG_E(TAG, "No data or parse error for level. Using default: 1");
  461. }
  462. else
  463. {
  464. // char buffer[64];
  465. if (load_uint32("player/level", &temp))
  466. {
  467. player_context->level = temp;
  468. }
  469. else
  470. {
  471. FURI_LOG_E(TAG, "Failed to parse level. Using default: 1");
  472. player_context->level = 1;
  473. }
  474. }
  475. player_context->level = temp;
  476. }
  477. // 3. XP (uint32_t)
  478. {
  479. uint32_t temp = 0; // Default
  480. if (!load_uint32("player/xp", &temp))
  481. {
  482. FURI_LOG_E(TAG, "No data or parse error for xp. Using default: 0");
  483. temp = 0;
  484. }
  485. player_context->xp = temp;
  486. }
  487. // 4. Health (uint32_t)
  488. {
  489. uint32_t temp = 100; // Default
  490. if (!load_uint32("player/health", &temp))
  491. {
  492. FURI_LOG_E(TAG, "No data or parse error for health. Using default: 100");
  493. temp = 100;
  494. }
  495. player_context->health = temp;
  496. }
  497. // 5. Strength (uint32_t)
  498. {
  499. uint32_t temp = 10; // Default
  500. if (!load_uint32("player/strength", &temp))
  501. {
  502. FURI_LOG_E(TAG, "No data or parse error for strength. Using default: 10");
  503. temp = 10;
  504. }
  505. player_context->strength = temp;
  506. }
  507. // 6. Max Health (uint32_t)
  508. {
  509. uint32_t temp = 100; // Default
  510. if (!load_uint32("player/max_health", &temp))
  511. {
  512. FURI_LOG_E(TAG, "No data or parse error for max_health. Using default: 100");
  513. temp = 100;
  514. }
  515. player_context->max_health = temp;
  516. }
  517. // 7. Health Regen (uint32_t)
  518. {
  519. uint32_t temp = 1; // Default
  520. if (!load_uint32("player/health_regen", &temp))
  521. {
  522. FURI_LOG_E(TAG, "No data or parse error for health_regen. Using default: 1");
  523. temp = 1;
  524. }
  525. player_context->health_regen = temp;
  526. }
  527. // 8. Elapsed Health Regen (float)
  528. {
  529. float temp = 0.0f; // Default
  530. if (!load_float("player/elapsed_health_regen", &temp))
  531. {
  532. FURI_LOG_E(TAG, "No data or parse error for elapsed_health_regen. Using default: 0.0f");
  533. temp = 0.0f;
  534. }
  535. player_context->elapsed_health_regen = temp;
  536. }
  537. // 9. Attack Timer (float)
  538. {
  539. float temp = 0.1f; // Default
  540. if (!load_float("player/attack_timer", &temp))
  541. {
  542. FURI_LOG_E(TAG, "No data or parse error for attack_timer. Using default: 0.1f");
  543. temp = 0.1f;
  544. }
  545. player_context->attack_timer = temp;
  546. }
  547. // 10. Elapsed Attack Timer (float)
  548. {
  549. float temp = 0.0f; // Default
  550. if (!load_float("player/elapsed_attack_timer", &temp))
  551. {
  552. FURI_LOG_E(TAG, "No data or parse error for elapsed_attack_timer. Using default: 0.0f");
  553. temp = 0.0f;
  554. }
  555. player_context->elapsed_attack_timer = temp;
  556. }
  557. // 11. Direction (enum PlayerDirection)
  558. {
  559. int direction_int = 3; // Default to ENTITY_RIGHT
  560. if (!load_number("player/direction", &direction_int))
  561. {
  562. FURI_LOG_E(TAG, "No data or parse error for direction. Defaulting to ENTITY_RIGHT");
  563. direction_int = 3;
  564. }
  565. switch (direction_int)
  566. {
  567. case 0:
  568. player_context->direction = ENTITY_UP;
  569. break;
  570. case 1:
  571. player_context->direction = ENTITY_DOWN;
  572. break;
  573. case 2:
  574. player_context->direction = ENTITY_LEFT;
  575. break;
  576. case 3:
  577. player_context->direction = ENTITY_RIGHT;
  578. break;
  579. default:
  580. FURI_LOG_E(TAG, "Invalid direction value: %d. Defaulting to ENTITY_RIGHT", direction_int);
  581. player_context->direction = ENTITY_RIGHT;
  582. break;
  583. }
  584. }
  585. // 12. State (enum PlayerState)
  586. {
  587. int state_int = 0; // Default to ENTITY_IDLE
  588. if (!load_number("player/state", &state_int))
  589. {
  590. FURI_LOG_E(TAG, "No data or parse error for state. Defaulting to ENTITY_IDLE");
  591. state_int = 0;
  592. }
  593. switch (state_int)
  594. {
  595. case 0:
  596. player_context->state = ENTITY_IDLE;
  597. break;
  598. case 1:
  599. player_context->state = ENTITY_MOVING;
  600. break;
  601. case 2:
  602. player_context->state = ENTITY_ATTACKING;
  603. break;
  604. case 3:
  605. player_context->state = ENTITY_ATTACKED;
  606. break;
  607. case 4:
  608. player_context->state = ENTITY_DEAD;
  609. break;
  610. default:
  611. FURI_LOG_E(TAG, "Invalid state value: %d. Defaulting to ENTITY_IDLE", state_int);
  612. player_context->state = ENTITY_IDLE;
  613. break;
  614. }
  615. }
  616. // 13. Start Position X (float)
  617. {
  618. float temp = 192.0f; // Default
  619. if (!load_float("player/start_position_x", &temp))
  620. {
  621. FURI_LOG_E(TAG, "No data or parse error for start_position_x. Using default: 192.0f");
  622. temp = 192.0f;
  623. }
  624. player_context->start_position.x = temp;
  625. }
  626. // 14. Start Position Y (float)
  627. {
  628. float temp = 96.0f; // Default
  629. if (!load_float("player/start_position_y", &temp))
  630. {
  631. FURI_LOG_E(TAG, "No data or parse error for start_position_y. Using default: 96.0f");
  632. temp = 96.0f;
  633. }
  634. player_context->start_position.y = temp;
  635. }
  636. // 15. dx (int8_t)
  637. {
  638. int8_t temp = 1; // Default
  639. if (!load_int8("player/dx", &temp))
  640. {
  641. FURI_LOG_E(TAG, "No data or parse error for dx. Using default: 1");
  642. temp = 1;
  643. }
  644. player_context->dx = temp;
  645. }
  646. // 16. dy (int8_t)
  647. {
  648. int8_t temp = 0; // Default
  649. if (!load_int8("player/dy", &temp))
  650. {
  651. FURI_LOG_E(TAG, "No data or parse error for dy. Using default: 0");
  652. temp = 0;
  653. }
  654. player_context->dy = temp;
  655. }
  656. return true;
  657. }
  658. // loads from STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json
  659. // then gets each key-value pair and saves it as it's own file so it can be loaded separately using
  660. // load_player_context
  661. bool set_player_context()
  662. {
  663. char file_path[256];
  664. snprintf(file_path, sizeof(file_path),
  665. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  666. FuriString *player_stats = flipper_http_load_from_file(file_path);
  667. if (!player_stats)
  668. {
  669. FURI_LOG_E(TAG, "Failed to load player stats from file: %s", file_path);
  670. return false;
  671. }
  672. // Get the key one-by-one and save it to a separate file
  673. // 1. Username (String)
  674. FuriString *username = get_json_value_furi("username", player_stats);
  675. if (username)
  676. {
  677. save_char("player/username", furi_string_get_cstr(username));
  678. furi_string_free(username);
  679. }
  680. // 2. Level (uint32_t)
  681. FuriString *level = get_json_value_furi("level", player_stats);
  682. if (level)
  683. {
  684. save_uint32("player/level", atoi(furi_string_get_cstr(level)));
  685. furi_string_free(level);
  686. }
  687. // 3. XP (uint32_t)
  688. FuriString *xp = get_json_value_furi("xp", player_stats);
  689. if (xp)
  690. {
  691. save_uint32("player/xp", atoi(furi_string_get_cstr(xp)));
  692. furi_string_free(xp);
  693. }
  694. // 4. Health (uint32_t)
  695. FuriString *health = get_json_value_furi("health", player_stats);
  696. if (health)
  697. {
  698. save_uint32("player/health", atoi(furi_string_get_cstr(health)));
  699. furi_string_free(health);
  700. }
  701. // 5. Strength (uint32_t)
  702. FuriString *strength = get_json_value_furi("strength", player_stats);
  703. if (strength)
  704. {
  705. save_uint32("player/strength", atoi(furi_string_get_cstr(strength)));
  706. furi_string_free(strength);
  707. }
  708. // 6. Max Health (uint32_t)
  709. FuriString *max_health = get_json_value_furi("max_health", player_stats);
  710. if (max_health)
  711. {
  712. save_uint32("player/max_health", atoi(furi_string_get_cstr(max_health)));
  713. furi_string_free(max_health);
  714. }
  715. // 7. Health Regen (uint32_t)
  716. FuriString *health_regen = get_json_value_furi("health_regen", player_stats);
  717. if (health_regen)
  718. {
  719. save_uint32("player/health_regen", atoi(furi_string_get_cstr(health_regen)));
  720. furi_string_free(health_regen);
  721. }
  722. // 8. Elapsed Health Regen (float)
  723. FuriString *elapsed_health_regen = get_json_value_furi("elapsed_health_regen", player_stats);
  724. if (elapsed_health_regen)
  725. {
  726. save_float("player/elapsed_health_regen", strtof(furi_string_get_cstr(elapsed_health_regen), NULL));
  727. furi_string_free(elapsed_health_regen);
  728. }
  729. // 9. Attack Timer (float)
  730. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  731. if (attack_timer)
  732. {
  733. save_float("player/attack_timer", strtof(furi_string_get_cstr(attack_timer), NULL));
  734. furi_string_free(attack_timer);
  735. }
  736. // 10. Elapsed Attack Timer (float)
  737. FuriString *elapsed_attack_timer = get_json_value_furi("elapsed_attack_timer", player_stats);
  738. if (elapsed_attack_timer)
  739. {
  740. save_float("player/elapsed_attack_timer", strtof(furi_string_get_cstr(elapsed_attack_timer), NULL));
  741. furi_string_free(elapsed_attack_timer);
  742. }
  743. // 11. Direction (enum PlayerDirection)
  744. FuriString *direction = get_json_value_furi("direction", player_stats);
  745. if (direction)
  746. {
  747. save_char("player/direction", furi_string_get_cstr(direction));
  748. furi_string_free(direction);
  749. }
  750. // 12. State (enum PlayerState)
  751. FuriString *state = get_json_value_furi("state", player_stats);
  752. if (state)
  753. {
  754. save_char("player/state", furi_string_get_cstr(state));
  755. furi_string_free(state);
  756. }
  757. // 13. Start Position X (float)
  758. FuriString *start_position_x = get_json_value_furi("start_position_x", player_stats);
  759. if (start_position_x)
  760. {
  761. save_float("player/start_position_x", strtof(furi_string_get_cstr(start_position_x), NULL));
  762. furi_string_free(start_position_x);
  763. }
  764. // 14. Start Position Y (float)
  765. FuriString *start_position_y = get_json_value_furi("start_position_y", player_stats);
  766. if (start_position_y)
  767. {
  768. save_float("player/start_position_y", strtof(furi_string_get_cstr(start_position_y), NULL));
  769. furi_string_free(start_position_y);
  770. }
  771. // 15. dx (int8_t)
  772. FuriString *dx = get_json_value_furi("dx", player_stats);
  773. if (dx)
  774. {
  775. save_int8("player/dx", atoi(furi_string_get_cstr(dx)));
  776. furi_string_free(dx);
  777. }
  778. // 16. dy (int8_t)
  779. FuriString *dy = get_json_value_furi("dy", player_stats);
  780. if (dy)
  781. {
  782. save_int8("player/dy", atoi(furi_string_get_cstr(dy)));
  783. furi_string_free(dy);
  784. }
  785. furi_string_free(player_stats);
  786. return true;
  787. }
  788. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  789. {
  790. furi_string_replace_str(string, needle, "", 0);
  791. }
  792. static FuriString *json_data(const FuriString *world_data, const char *key)
  793. {
  794. size_t json_data_pos = furi_string_search_str(world_data, key, 0);
  795. if (json_data_pos == FURI_STRING_FAILURE)
  796. {
  797. FURI_LOG_E("Game", "Failed to find json_data in world data");
  798. return NULL;
  799. }
  800. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  801. if (bracket_start == FURI_STRING_FAILURE)
  802. {
  803. FURI_LOG_E("Game", "Failed to find start of json_data array");
  804. return NULL;
  805. }
  806. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  807. if (bracket_end == FURI_STRING_FAILURE)
  808. {
  809. FURI_LOG_E("Game", "Failed to find end of json_data array");
  810. return NULL;
  811. }
  812. FuriString *json_data_str = furi_string_alloc();
  813. if (!json_data_str)
  814. {
  815. FURI_LOG_E("Game", "Failed to allocate json_data string");
  816. return NULL;
  817. }
  818. furi_string_cat_str(json_data_str, "{\"");
  819. furi_string_cat_str(json_data_str, key);
  820. furi_string_cat_str(json_data_str, "\":");
  821. {
  822. FuriString *temp_sub = furi_string_alloc();
  823. furi_string_set_strn(
  824. temp_sub,
  825. furi_string_get_cstr(world_data) + bracket_start,
  826. (bracket_end + 1) - bracket_start);
  827. furi_string_cat(json_data_str, temp_sub);
  828. furi_string_free(temp_sub);
  829. }
  830. furi_string_cat_str(json_data_str, "}");
  831. return json_data_str;
  832. }
  833. bool separate_world_data(char *id, FuriString *world_data)
  834. {
  835. if (!id || !world_data)
  836. {
  837. FURI_LOG_E("Game", "Invalid parameters");
  838. return false;
  839. }
  840. FuriString *file_json_data = json_data(world_data, "json_data");
  841. if (!file_json_data || furi_string_size(file_json_data) == 0)
  842. {
  843. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  844. return false;
  845. }
  846. // Save file_json_data to disk
  847. char directory_path[256];
  848. snprintf(directory_path, sizeof(directory_path),
  849. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  850. Storage *storage = furi_record_open(RECORD_STORAGE);
  851. storage_common_mkdir(storage, directory_path);
  852. File *file = storage_file_alloc(storage);
  853. char file_path[256];
  854. snprintf(file_path, sizeof(file_path),
  855. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  856. id, id);
  857. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  858. {
  859. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  860. storage_file_free(file);
  861. furi_record_close(RECORD_STORAGE);
  862. furi_string_free(file_json_data);
  863. return false;
  864. }
  865. size_t data_size = furi_string_size(file_json_data);
  866. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  867. {
  868. FURI_LOG_E("Game", "Failed to write json_data");
  869. }
  870. storage_file_close(file);
  871. furi_string_replace_at(file_json_data, 0, 1, "");
  872. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  873. // include the comma at the end of the json_data array
  874. furi_string_cat_str(file_json_data, ",");
  875. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  876. furi_string_free(file_json_data);
  877. // save npc_data to disk
  878. FuriString *file_npc_data = json_data(world_data, "npc_data");
  879. if (!file_npc_data)
  880. {
  881. FURI_LOG_E("Game", "Failed to get npc data");
  882. return false;
  883. }
  884. snprintf(file_path, sizeof(file_path),
  885. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
  886. id, id);
  887. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  888. {
  889. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  890. storage_file_free(file);
  891. furi_record_close(RECORD_STORAGE);
  892. furi_string_free(file_npc_data);
  893. return false;
  894. }
  895. data_size = furi_string_size(file_npc_data);
  896. if (storage_file_write(file, furi_string_get_cstr(file_npc_data), data_size) != data_size)
  897. {
  898. FURI_LOG_E("Game", "Failed to write npc_data");
  899. }
  900. storage_file_close(file);
  901. furi_string_replace_at(file_npc_data, 0, 1, "");
  902. furi_string_replace_at(file_npc_data, furi_string_size(file_npc_data) - 1, 1, "");
  903. // include the comma at the end of the npc_data array
  904. furi_string_cat_str(file_npc_data, ",");
  905. furi_string_remove_str(world_data, furi_string_get_cstr(file_npc_data));
  906. furi_string_free(file_npc_data);
  907. // Save enemy_data to disk
  908. FuriString *file_enemy_data = json_data(world_data, "enemy_data");
  909. if (!file_enemy_data)
  910. {
  911. FURI_LOG_E("Game", "Failed to get enemy data");
  912. return false;
  913. }
  914. snprintf(file_path, sizeof(file_path),
  915. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  916. id, id);
  917. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  918. {
  919. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  920. storage_file_free(file);
  921. furi_record_close(RECORD_STORAGE);
  922. furi_string_free(file_enemy_data);
  923. return false;
  924. }
  925. data_size = furi_string_size(file_enemy_data);
  926. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  927. {
  928. FURI_LOG_E("Game", "Failed to write enemy_data");
  929. }
  930. furi_string_free(file_enemy_data);
  931. // Clean up
  932. storage_file_close(file);
  933. storage_file_free(file);
  934. furi_record_close(RECORD_STORAGE);
  935. return true;
  936. }