storage.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. static FuriString *player_context_json(PlayerContext *player_context, bool websocket)
  183. {
  184. FuriString *json = furi_string_alloc();
  185. if (!json)
  186. {
  187. FURI_LOG_E(TAG, "Failed to allocate JSON string");
  188. return NULL;
  189. }
  190. furi_string_cat_str(json, "{");
  191. if (websocket)
  192. {
  193. // Minimal JSON for WebSocket (abbreviated, <128 characters)
  194. // "u": username
  195. furi_string_cat_str(json, "\"u\":\"");
  196. furi_string_cat_str(json, player_context->username);
  197. furi_string_cat_str(json, "\",");
  198. // "xp": experience
  199. furi_string_cat_str(json, "\"xp\":");
  200. char buffer[32];
  201. snprintf(buffer, sizeof(buffer), "%lu", player_context->xp);
  202. furi_string_cat_str(json, buffer);
  203. furi_string_cat_str(json, ",");
  204. // "h": health
  205. furi_string_cat_str(json, "\"h\":");
  206. snprintf(buffer, sizeof(buffer), "%lu", player_context->health);
  207. furi_string_cat_str(json, buffer);
  208. furi_string_cat_str(json, ",");
  209. // "ehr": elapsed health regen (1 decimal)
  210. furi_string_cat_str(json, "\"ehr\":");
  211. snprintf(buffer, sizeof(buffer), "%.1f", (double)player_context->elapsed_health_regen);
  212. furi_string_cat_str(json, buffer);
  213. furi_string_cat_str(json, ",");
  214. // "eat": elapsed attack timer (1 decimal)
  215. furi_string_cat_str(json, "\"eat\":");
  216. snprintf(buffer, sizeof(buffer), "%.1f", (double)player_context->elapsed_attack_timer);
  217. furi_string_cat_str(json, buffer);
  218. furi_string_cat_str(json, ",");
  219. // "d": direction (numeric code)
  220. furi_string_cat_str(json, "\"d\":");
  221. snprintf(buffer, sizeof(buffer), "%d", player_context->direction);
  222. furi_string_cat_str(json, buffer);
  223. furi_string_cat_str(json, ",");
  224. // "s": state (numeric code)
  225. furi_string_cat_str(json, "\"s\":");
  226. snprintf(buffer, sizeof(buffer), "%d", player_context->state);
  227. furi_string_cat_str(json, buffer);
  228. furi_string_cat_str(json, ",");
  229. // "sp": start position object with x and y (1 decimal)
  230. furi_string_cat_str(json, "\"sp\":{");
  231. furi_string_cat_str(json, "\"x\":");
  232. snprintf(buffer, sizeof(buffer), "%.1f", (double)player_context->start_position.x);
  233. furi_string_cat_str(json, buffer);
  234. furi_string_cat_str(json, ",\"y\":");
  235. snprintf(buffer, sizeof(buffer), "%.1f", (double)player_context->start_position.y);
  236. furi_string_cat_str(json, buffer);
  237. furi_string_cat_str(json, "}");
  238. }
  239. else
  240. {
  241. // Full JSON output (unchanged)
  242. // 1. Username
  243. furi_string_cat_str(json, "\"username\":\"");
  244. furi_string_cat_str(json, player_context->username);
  245. furi_string_cat_str(json, "\",");
  246. // 2. Level
  247. furi_string_cat_str(json, "\"level\":");
  248. char buffer[32];
  249. snprintf(buffer, sizeof(buffer), "%lu", player_context->level);
  250. furi_string_cat_str(json, buffer);
  251. furi_string_cat_str(json, ",");
  252. // 3. XP
  253. furi_string_cat_str(json, "\"xp\":");
  254. snprintf(buffer, sizeof(buffer), "%lu", player_context->xp);
  255. furi_string_cat_str(json, buffer);
  256. furi_string_cat_str(json, ",");
  257. // 4. Health
  258. furi_string_cat_str(json, "\"health\":");
  259. snprintf(buffer, sizeof(buffer), "%lu", player_context->health);
  260. furi_string_cat_str(json, buffer);
  261. furi_string_cat_str(json, ",");
  262. // 5. Strength
  263. furi_string_cat_str(json, "\"strength\":");
  264. snprintf(buffer, sizeof(buffer), "%lu", player_context->strength);
  265. furi_string_cat_str(json, buffer);
  266. furi_string_cat_str(json, ",");
  267. // 6. Max Health
  268. furi_string_cat_str(json, "\"max_health\":");
  269. snprintf(buffer, sizeof(buffer), "%lu", player_context->max_health);
  270. furi_string_cat_str(json, buffer);
  271. furi_string_cat_str(json, ",");
  272. // 7. Health Regen
  273. furi_string_cat_str(json, "\"health_regen\":");
  274. snprintf(buffer, sizeof(buffer), "%u", player_context->health_regen);
  275. furi_string_cat_str(json, buffer);
  276. furi_string_cat_str(json, ",");
  277. // 8. Elapsed Health Regen
  278. furi_string_cat_str(json, "\"elapsed_health_regen\":");
  279. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->elapsed_health_regen);
  280. furi_string_cat_str(json, buffer);
  281. furi_string_cat_str(json, ",");
  282. // 9. Attack Timer
  283. furi_string_cat_str(json, "\"attack_timer\":");
  284. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->attack_timer);
  285. furi_string_cat_str(json, buffer);
  286. furi_string_cat_str(json, ",");
  287. // 10. Elapsed Attack Timer
  288. furi_string_cat_str(json, "\"elapsed_attack_timer\":");
  289. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->elapsed_attack_timer);
  290. furi_string_cat_str(json, buffer);
  291. furi_string_cat_str(json, ",");
  292. // 11. Direction (string representation)
  293. furi_string_cat_str(json, "\"direction\":");
  294. switch (player_context->direction)
  295. {
  296. case ENTITY_UP:
  297. furi_string_cat_str(json, "\"up\",");
  298. break;
  299. case ENTITY_DOWN:
  300. furi_string_cat_str(json, "\"down\",");
  301. break;
  302. case ENTITY_LEFT:
  303. furi_string_cat_str(json, "\"left\",");
  304. break;
  305. case ENTITY_RIGHT:
  306. default:
  307. furi_string_cat_str(json, "\"right\",");
  308. break;
  309. }
  310. // 12. State (string representation)
  311. furi_string_cat_str(json, "\"state\":");
  312. switch (player_context->state)
  313. {
  314. case ENTITY_IDLE:
  315. furi_string_cat_str(json, "\"idle\",");
  316. break;
  317. case ENTITY_MOVING:
  318. furi_string_cat_str(json, "\"moving\",");
  319. break;
  320. case ENTITY_ATTACKING:
  321. furi_string_cat_str(json, "\"attacking\",");
  322. break;
  323. case ENTITY_ATTACKED:
  324. furi_string_cat_str(json, "\"attacked\",");
  325. break;
  326. case ENTITY_DEAD:
  327. furi_string_cat_str(json, "\"dead\",");
  328. break;
  329. default:
  330. furi_string_cat_str(json, "\"unknown\",");
  331. break;
  332. }
  333. // 13. Start Position X
  334. furi_string_cat_str(json, "\"start_position_x\":");
  335. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->start_position.x);
  336. furi_string_cat_str(json, buffer);
  337. furi_string_cat_str(json, ",");
  338. // 14. Start Position Y
  339. furi_string_cat_str(json, "\"start_position_y\":");
  340. snprintf(buffer, sizeof(buffer), "%.6f", (double)player_context->start_position.y);
  341. furi_string_cat_str(json, buffer);
  342. furi_string_cat_str(json, ",");
  343. // 15. dx
  344. furi_string_cat_str(json, "\"dx\":");
  345. snprintf(buffer, sizeof(buffer), "%d", player_context->dx);
  346. furi_string_cat_str(json, buffer);
  347. furi_string_cat_str(json, ",");
  348. // 16. dy
  349. furi_string_cat_str(json, "\"dy\":");
  350. snprintf(buffer, sizeof(buffer), "%d", player_context->dy);
  351. furi_string_cat_str(json, buffer);
  352. }
  353. furi_string_cat_str(json, "}");
  354. // For websocket, output only the minimal JSON (without extra wrapping)
  355. FuriString *json_data = furi_string_alloc();
  356. if (!json_data)
  357. {
  358. FURI_LOG_E(TAG, "Failed to allocate JSON string");
  359. furi_string_free(json);
  360. return NULL;
  361. }
  362. if (websocket)
  363. {
  364. furi_string_cat(json_data, json);
  365. }
  366. else
  367. {
  368. furi_string_cat_str(json_data, "{\"username\":\"");
  369. furi_string_cat_str(json_data, player_context->username);
  370. furi_string_cat_str(json_data, "\",\"game_stats\":");
  371. furi_string_cat(json_data, json);
  372. furi_string_cat_str(json_data, "}");
  373. }
  374. furi_string_free(json);
  375. return json_data;
  376. }
  377. bool save_player_context_api(PlayerContext *player_context, FlipperHTTP *fhttp)
  378. {
  379. if (!player_context)
  380. {
  381. FURI_LOG_E(TAG, "Invalid player context");
  382. return false;
  383. }
  384. if (!fhttp)
  385. {
  386. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  387. return false;
  388. }
  389. FuriString *json_data = player_context_json(player_context, false);
  390. if (!json_data)
  391. {
  392. FURI_LOG_E(TAG, "Failed to create JSON data");
  393. return false;
  394. }
  395. // save the json_data to the API
  396. if (!flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/update-game-stats/", "{\"Content-Type\": \"application/json\"}", furi_string_get_cstr(json_data)))
  397. {
  398. FURI_LOG_E(TAG, "Failed to save player context to API");
  399. furi_string_free(json_data);
  400. return false;
  401. }
  402. fhttp->state = RECEIVING;
  403. while (fhttp->state != IDLE)
  404. {
  405. furi_delay_ms(100);
  406. }
  407. furi_string_free(json_data);
  408. return true;
  409. }
  410. bool websocket_player_context(PlayerContext *player_context, FlipperHTTP *fhttp)
  411. {
  412. if (!player_context)
  413. {
  414. FURI_LOG_E(TAG, "Invalid player context");
  415. return false;
  416. }
  417. if (!fhttp)
  418. {
  419. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  420. return false;
  421. }
  422. // create JSON for all the player context data
  423. FuriString *json = player_context_json(player_context, true);
  424. if (!json)
  425. {
  426. FURI_LOG_E(TAG, "Failed to create JSON data");
  427. return false;
  428. }
  429. // websocket session is already started, so just send json to esp32
  430. if (!flipper_http_send_data(fhttp, furi_string_get_cstr(json)))
  431. {
  432. FURI_LOG_E(TAG, "Failed to send player context to websocket");
  433. furi_string_free(json);
  434. return false;
  435. }
  436. furi_string_free(json);
  437. return true;
  438. }
  439. bool remove_player_from_lobby(FlipperHTTP *fhttp)
  440. {
  441. if (!fhttp)
  442. {
  443. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  444. return false;
  445. }
  446. char username[32];
  447. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  448. {
  449. FURI_LOG_E(TAG, "Failed to load data/Flip-Social-Username");
  450. return false;
  451. }
  452. char lobby_name[32];
  453. if (!load_char("pvp_lobby_name", lobby_name, sizeof(lobby_name)))
  454. {
  455. FURI_LOG_E(TAG, "Failed to load data/pvp_lobby_name");
  456. return false;
  457. }
  458. char url[128];
  459. char payload[128];
  460. snprintf(payload, sizeof(payload), "{\"username\":\"%s\", \"game_id\":\"%s\"}", username, lobby_name);
  461. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/remove/");
  462. fhttp->state = IDLE;
  463. if (!flipper_http_request(fhttp, POST, url, "{\"Content-Type\":\"application/json\"}", payload))
  464. {
  465. FURI_LOG_E(TAG, "Failed to remove player from lobby");
  466. return false;
  467. }
  468. fhttp->state = RECEIVING;
  469. while (fhttp->state != IDLE)
  470. {
  471. furi_delay_ms(100);
  472. }
  473. return true;
  474. }
  475. // Helper function to load an integer
  476. static bool load_number(const char *path_name, int *value)
  477. {
  478. if (!path_name || !value)
  479. {
  480. FURI_LOG_E(TAG, "Invalid arguments to load_number");
  481. return false;
  482. }
  483. char buffer[64];
  484. if (!load_char(path_name, buffer, sizeof(buffer)))
  485. {
  486. FURI_LOG_E(TAG, "Failed to load number from path: %s", path_name);
  487. return false;
  488. }
  489. *value = atoi(buffer);
  490. return true;
  491. }
  492. // Helper function to load a float
  493. static bool load_float(const char *path_name, float *value)
  494. {
  495. if (!path_name || !value)
  496. {
  497. FURI_LOG_E(TAG, "Invalid arguments to load_float");
  498. return false;
  499. }
  500. char buffer[64];
  501. if (!load_char(path_name, buffer, sizeof(buffer)))
  502. {
  503. FURI_LOG_E(TAG, "Failed to load float from path: %s", path_name);
  504. return false;
  505. }
  506. // check if the string is a valid float
  507. char *endptr;
  508. *value = strtof(buffer, &endptr);
  509. if (endptr == buffer)
  510. {
  511. FURI_LOG_E(TAG, "Failed to parse float from path: %s", path_name);
  512. return false;
  513. }
  514. return true;
  515. }
  516. // Helper function to load an int8_t
  517. static bool load_int8(const char *path_name, int8_t *value)
  518. {
  519. if (!path_name || !value)
  520. {
  521. FURI_LOG_E(TAG, "Invalid arguments to load_int8");
  522. return false;
  523. }
  524. char buffer[64];
  525. if (!load_char(path_name, buffer, sizeof(buffer)))
  526. {
  527. FURI_LOG_E(TAG, "Failed to load int8 from path: %s", path_name);
  528. return false;
  529. }
  530. long temp = strtol(buffer, NULL, 10);
  531. if (temp < INT8_MIN || temp > INT8_MAX)
  532. {
  533. FURI_LOG_E(TAG, "Value out of range for int8: %ld", temp);
  534. return false;
  535. }
  536. // check if the string is a valid int8
  537. char *endptr;
  538. *value = (int8_t)strtol(buffer, &endptr, 10);
  539. if (endptr == buffer)
  540. {
  541. FURI_LOG_E(TAG, "Failed to parse int8 from path: %s", path_name);
  542. return false;
  543. }
  544. return true;
  545. }
  546. // Helper function to load a uint32_t
  547. static bool load_uint32(const char *path_name, uint32_t *value)
  548. {
  549. if (!path_name || !value)
  550. {
  551. FURI_LOG_E(TAG, "Invalid arguments to load_uint32");
  552. return false;
  553. }
  554. char buffer[64];
  555. if (!load_char(path_name, buffer, sizeof(buffer)))
  556. {
  557. FURI_LOG_E(TAG, "Failed to load uint32 from path: %s", path_name);
  558. return false;
  559. }
  560. // check if the string is a valid uint32
  561. char *endptr;
  562. *value = strtoul(buffer, &endptr, 10);
  563. if (endptr == buffer)
  564. {
  565. FURI_LOG_E(TAG, "Failed to parse uint32 from path: %s", path_name);
  566. return false;
  567. }
  568. return true;
  569. }
  570. bool load_player_context(PlayerContext *player_context)
  571. {
  572. if (!player_context)
  573. {
  574. FURI_LOG_E(TAG, "Invalid player context");
  575. return false;
  576. }
  577. // 1. Username (String)
  578. if (!load_char("player/username", player_context->username, sizeof(player_context->username)))
  579. {
  580. FURI_LOG_E(TAG, "No data or parse error for username. Using default: 'Unknown'");
  581. memset(player_context->username, 0, sizeof(player_context->username));
  582. strncpy(player_context->username, "Unknown", sizeof(player_context->username) - 1);
  583. }
  584. // 2. Level (uint32_t)
  585. {
  586. uint32_t temp = 1; // Default
  587. if (!load_char("player/level", (char *)&temp, sizeof(temp)))
  588. {
  589. FURI_LOG_E(TAG, "No data or parse error for level. Using default: 1");
  590. }
  591. else
  592. {
  593. // char buffer[64];
  594. if (load_uint32("player/level", &temp))
  595. {
  596. player_context->level = temp;
  597. }
  598. else
  599. {
  600. FURI_LOG_E(TAG, "Failed to parse level. Using default: 1");
  601. player_context->level = 1;
  602. }
  603. }
  604. player_context->level = temp;
  605. }
  606. // 3. XP (uint32_t)
  607. {
  608. uint32_t temp = 0; // Default
  609. if (!load_uint32("player/xp", &temp))
  610. {
  611. FURI_LOG_E(TAG, "No data or parse error for xp. Using default: 0");
  612. temp = 0;
  613. }
  614. player_context->xp = temp;
  615. }
  616. // 4. Health (uint32_t)
  617. {
  618. uint32_t temp = 100; // Default
  619. if (!load_uint32("player/health", &temp))
  620. {
  621. FURI_LOG_E(TAG, "No data or parse error for health. Using default: 100");
  622. temp = 100;
  623. }
  624. player_context->health = temp;
  625. }
  626. // 5. Strength (uint32_t)
  627. {
  628. uint32_t temp = 10; // Default
  629. if (!load_uint32("player/strength", &temp))
  630. {
  631. FURI_LOG_E(TAG, "No data or parse error for strength. Using default: 10");
  632. temp = 10;
  633. }
  634. player_context->strength = temp;
  635. }
  636. // 6. Max Health (uint32_t)
  637. {
  638. uint32_t temp = 100; // Default
  639. if (!load_uint32("player/max_health", &temp))
  640. {
  641. FURI_LOG_E(TAG, "No data or parse error for max_health. Using default: 100");
  642. temp = 100;
  643. }
  644. player_context->max_health = temp;
  645. }
  646. // 7. Health Regen (uint32_t)
  647. {
  648. uint32_t temp = 1; // Default
  649. if (!load_uint32("player/health_regen", &temp))
  650. {
  651. FURI_LOG_E(TAG, "No data or parse error for health_regen. Using default: 1");
  652. temp = 1;
  653. }
  654. player_context->health_regen = temp;
  655. }
  656. // 8. Elapsed Health Regen (float)
  657. {
  658. float temp = 0.0f; // Default
  659. if (!load_float("player/elapsed_health_regen", &temp))
  660. {
  661. FURI_LOG_E(TAG, "No data or parse error for elapsed_health_regen. Using default: 0.0f");
  662. temp = 0.0f;
  663. }
  664. player_context->elapsed_health_regen = temp;
  665. }
  666. // 9. Attack Timer (float)
  667. {
  668. float temp = 0.1f; // Default
  669. if (!load_float("player/attack_timer", &temp))
  670. {
  671. FURI_LOG_E(TAG, "No data or parse error for attack_timer. Using default: 0.1f");
  672. temp = 0.1f;
  673. }
  674. player_context->attack_timer = temp;
  675. }
  676. // 10. Elapsed Attack Timer (float)
  677. {
  678. float temp = 0.0f; // Default
  679. if (!load_float("player/elapsed_attack_timer", &temp))
  680. {
  681. FURI_LOG_E(TAG, "No data or parse error for elapsed_attack_timer. Using default: 0.0f");
  682. temp = 0.0f;
  683. }
  684. player_context->elapsed_attack_timer = temp;
  685. }
  686. // 11. Direction (enum PlayerDirection)
  687. {
  688. int direction_int = 3; // Default to ENTITY_RIGHT
  689. if (!load_number("player/direction", &direction_int))
  690. {
  691. FURI_LOG_E(TAG, "No data or parse error for direction. Defaulting to ENTITY_RIGHT");
  692. direction_int = 3;
  693. }
  694. switch (direction_int)
  695. {
  696. case 0:
  697. player_context->direction = ENTITY_UP;
  698. break;
  699. case 1:
  700. player_context->direction = ENTITY_DOWN;
  701. break;
  702. case 2:
  703. player_context->direction = ENTITY_LEFT;
  704. break;
  705. case 3:
  706. player_context->direction = ENTITY_RIGHT;
  707. break;
  708. default:
  709. FURI_LOG_E(TAG, "Invalid direction value: %d. Defaulting to ENTITY_RIGHT", direction_int);
  710. player_context->direction = ENTITY_RIGHT;
  711. break;
  712. }
  713. }
  714. // 12. State (enum PlayerState)
  715. {
  716. int state_int = 0; // Default to ENTITY_IDLE
  717. if (!load_number("player/state", &state_int))
  718. {
  719. FURI_LOG_E(TAG, "No data or parse error for state. Defaulting to ENTITY_IDLE");
  720. state_int = 0;
  721. }
  722. switch (state_int)
  723. {
  724. case 0:
  725. player_context->state = ENTITY_IDLE;
  726. break;
  727. case 1:
  728. player_context->state = ENTITY_MOVING;
  729. break;
  730. case 2:
  731. player_context->state = ENTITY_ATTACKING;
  732. break;
  733. case 3:
  734. player_context->state = ENTITY_ATTACKED;
  735. break;
  736. case 4:
  737. player_context->state = ENTITY_DEAD;
  738. break;
  739. default:
  740. FURI_LOG_E(TAG, "Invalid state value: %d. Defaulting to ENTITY_IDLE", state_int);
  741. player_context->state = ENTITY_IDLE;
  742. break;
  743. }
  744. }
  745. // 13. Start Position X (float)
  746. {
  747. float temp = 192.0f; // Default
  748. if (!load_float("player/start_position_x", &temp))
  749. {
  750. FURI_LOG_E(TAG, "No data or parse error for start_position_x. Using default: 192.0f");
  751. temp = 192.0f;
  752. }
  753. player_context->start_position.x = temp;
  754. }
  755. // 14. Start Position Y (float)
  756. {
  757. float temp = 96.0f; // Default
  758. if (!load_float("player/start_position_y", &temp))
  759. {
  760. FURI_LOG_E(TAG, "No data or parse error for start_position_y. Using default: 96.0f");
  761. temp = 96.0f;
  762. }
  763. player_context->start_position.y = temp;
  764. }
  765. // 15. dx (int8_t)
  766. {
  767. int8_t temp = 1; // Default
  768. if (!load_int8("player/dx", &temp))
  769. {
  770. FURI_LOG_E(TAG, "No data or parse error for dx. Using default: 1");
  771. temp = 1;
  772. }
  773. player_context->dx = temp;
  774. }
  775. // 16. dy (int8_t)
  776. {
  777. int8_t temp = 0; // Default
  778. if (!load_int8("player/dy", &temp))
  779. {
  780. FURI_LOG_E(TAG, "No data or parse error for dy. Using default: 0");
  781. temp = 0;
  782. }
  783. player_context->dy = temp;
  784. }
  785. return true;
  786. }
  787. // loads from STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json
  788. // then gets each key-value pair and saves it as it's own file so it can be loaded separately using
  789. // load_player_context
  790. bool set_player_context()
  791. {
  792. char file_path[256];
  793. snprintf(file_path, sizeof(file_path),
  794. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  795. FuriString *player_stats = flipper_http_load_from_file(file_path);
  796. if (!player_stats)
  797. {
  798. FURI_LOG_E(TAG, "Failed to load player stats from file: %s", file_path);
  799. return false;
  800. }
  801. // Get the key one-by-one and save it to a separate file
  802. // 1. Username (String)
  803. FuriString *username = get_json_value_furi("username", player_stats);
  804. if (username)
  805. {
  806. save_char("player/username", furi_string_get_cstr(username));
  807. furi_string_free(username);
  808. }
  809. // 2. Level (uint32_t)
  810. FuriString *level = get_json_value_furi("level", player_stats);
  811. if (level)
  812. {
  813. save_uint32("player/level", atoi(furi_string_get_cstr(level)));
  814. furi_string_free(level);
  815. }
  816. // 3. XP (uint32_t)
  817. FuriString *xp = get_json_value_furi("xp", player_stats);
  818. if (xp)
  819. {
  820. save_uint32("player/xp", atoi(furi_string_get_cstr(xp)));
  821. furi_string_free(xp);
  822. }
  823. // 4. Health (uint32_t)
  824. FuriString *health = get_json_value_furi("health", player_stats);
  825. if (health)
  826. {
  827. save_uint32("player/health", atoi(furi_string_get_cstr(health)));
  828. furi_string_free(health);
  829. }
  830. // 5. Strength (uint32_t)
  831. FuriString *strength = get_json_value_furi("strength", player_stats);
  832. if (strength)
  833. {
  834. save_uint32("player/strength", atoi(furi_string_get_cstr(strength)));
  835. furi_string_free(strength);
  836. }
  837. // 6. Max Health (uint32_t)
  838. FuriString *max_health = get_json_value_furi("max_health", player_stats);
  839. if (max_health)
  840. {
  841. save_uint32("player/max_health", atoi(furi_string_get_cstr(max_health)));
  842. furi_string_free(max_health);
  843. }
  844. // 7. Health Regen (uint32_t)
  845. FuriString *health_regen = get_json_value_furi("health_regen", player_stats);
  846. if (health_regen)
  847. {
  848. save_uint32("player/health_regen", atoi(furi_string_get_cstr(health_regen)));
  849. furi_string_free(health_regen);
  850. }
  851. // 8. Elapsed Health Regen (float)
  852. FuriString *elapsed_health_regen = get_json_value_furi("elapsed_health_regen", player_stats);
  853. if (elapsed_health_regen)
  854. {
  855. save_float("player/elapsed_health_regen", strtof(furi_string_get_cstr(elapsed_health_regen), NULL));
  856. furi_string_free(elapsed_health_regen);
  857. }
  858. // 9. Attack Timer (float)
  859. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  860. if (attack_timer)
  861. {
  862. save_float("player/attack_timer", strtof(furi_string_get_cstr(attack_timer), NULL));
  863. furi_string_free(attack_timer);
  864. }
  865. // 10. Elapsed Attack Timer (float)
  866. FuriString *elapsed_attack_timer = get_json_value_furi("elapsed_attack_timer", player_stats);
  867. if (elapsed_attack_timer)
  868. {
  869. save_float("player/elapsed_attack_timer", strtof(furi_string_get_cstr(elapsed_attack_timer), NULL));
  870. furi_string_free(elapsed_attack_timer);
  871. }
  872. // 11. Direction (enum PlayerDirection)
  873. FuriString *direction = get_json_value_furi("direction", player_stats);
  874. if (direction)
  875. {
  876. save_char("player/direction", furi_string_get_cstr(direction));
  877. furi_string_free(direction);
  878. }
  879. // 12. State (enum PlayerState)
  880. FuriString *state = get_json_value_furi("state", player_stats);
  881. if (state)
  882. {
  883. save_char("player/state", furi_string_get_cstr(state));
  884. furi_string_free(state);
  885. }
  886. // 13. Start Position X (float)
  887. FuriString *start_position_x = get_json_value_furi("start_position_x", player_stats);
  888. if (start_position_x)
  889. {
  890. save_float("player/start_position_x", strtof(furi_string_get_cstr(start_position_x), NULL));
  891. furi_string_free(start_position_x);
  892. }
  893. // 14. Start Position Y (float)
  894. FuriString *start_position_y = get_json_value_furi("start_position_y", player_stats);
  895. if (start_position_y)
  896. {
  897. save_float("player/start_position_y", strtof(furi_string_get_cstr(start_position_y), NULL));
  898. furi_string_free(start_position_y);
  899. }
  900. // 15. dx (int8_t)
  901. FuriString *dx = get_json_value_furi("dx", player_stats);
  902. if (dx)
  903. {
  904. save_int8("player/dx", atoi(furi_string_get_cstr(dx)));
  905. furi_string_free(dx);
  906. }
  907. // 16. dy (int8_t)
  908. FuriString *dy = get_json_value_furi("dy", player_stats);
  909. if (dy)
  910. {
  911. save_int8("player/dy", atoi(furi_string_get_cstr(dy)));
  912. furi_string_free(dy);
  913. }
  914. furi_string_free(player_stats);
  915. return true;
  916. }
  917. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  918. {
  919. furi_string_replace_str(string, needle, "", 0);
  920. }
  921. static FuriString *json_data(const FuriString *world_data, const char *key)
  922. {
  923. size_t json_data_pos = furi_string_search_str(world_data, key, 0);
  924. if (json_data_pos == FURI_STRING_FAILURE)
  925. {
  926. FURI_LOG_E("Game", "Failed to find json_data in world data");
  927. return NULL;
  928. }
  929. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  930. if (bracket_start == FURI_STRING_FAILURE)
  931. {
  932. FURI_LOG_E("Game", "Failed to find start of json_data array");
  933. return NULL;
  934. }
  935. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  936. if (bracket_end == FURI_STRING_FAILURE)
  937. {
  938. FURI_LOG_E("Game", "Failed to find end of json_data array");
  939. return NULL;
  940. }
  941. FuriString *json_data_str = furi_string_alloc();
  942. if (!json_data_str)
  943. {
  944. FURI_LOG_E("Game", "Failed to allocate json_data string");
  945. return NULL;
  946. }
  947. furi_string_cat_str(json_data_str, "{\"");
  948. furi_string_cat_str(json_data_str, key);
  949. furi_string_cat_str(json_data_str, "\":");
  950. {
  951. FuriString *temp_sub = furi_string_alloc();
  952. furi_string_set_strn(
  953. temp_sub,
  954. furi_string_get_cstr(world_data) + bracket_start,
  955. (bracket_end + 1) - bracket_start);
  956. furi_string_cat(json_data_str, temp_sub);
  957. furi_string_free(temp_sub);
  958. }
  959. furi_string_cat_str(json_data_str, "}");
  960. return json_data_str;
  961. }
  962. bool separate_world_data(char *id, FuriString *world_data)
  963. {
  964. if (!id || !world_data)
  965. {
  966. FURI_LOG_E("Game", "Invalid parameters");
  967. return false;
  968. }
  969. FuriString *file_json_data = json_data(world_data, "json_data");
  970. if (!file_json_data || furi_string_size(file_json_data) == 0)
  971. {
  972. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  973. if (file_json_data)
  974. {
  975. furi_string_free(file_json_data);
  976. }
  977. return false;
  978. }
  979. // Save file_json_data to disk
  980. char directory_path[256];
  981. snprintf(directory_path, sizeof(directory_path),
  982. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  983. Storage *storage = furi_record_open(RECORD_STORAGE);
  984. storage_common_mkdir(storage, directory_path);
  985. File *file = storage_file_alloc(storage);
  986. char file_path[256];
  987. snprintf(file_path, sizeof(file_path),
  988. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  989. id, id);
  990. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  991. {
  992. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  993. storage_file_free(file);
  994. furi_record_close(RECORD_STORAGE);
  995. furi_string_free(file_json_data);
  996. return false;
  997. }
  998. size_t data_size = furi_string_size(file_json_data);
  999. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  1000. {
  1001. FURI_LOG_E("Game", "Failed to write json_data");
  1002. }
  1003. storage_file_close(file);
  1004. furi_string_replace_at(file_json_data, 0, 1, "");
  1005. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  1006. // include the comma at the end of the json_data array
  1007. furi_string_cat_str(file_json_data, ",");
  1008. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  1009. furi_string_free(file_json_data);
  1010. // save npc_data to disk
  1011. FuriString *file_npc_data = json_data(world_data, "npc_data");
  1012. if (!file_npc_data)
  1013. {
  1014. FURI_LOG_E("Game", "Failed to get npc data");
  1015. return false;
  1016. }
  1017. snprintf(file_path, sizeof(file_path),
  1018. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
  1019. id, id);
  1020. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1021. {
  1022. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  1023. storage_file_free(file);
  1024. furi_record_close(RECORD_STORAGE);
  1025. furi_string_free(file_npc_data);
  1026. return false;
  1027. }
  1028. data_size = furi_string_size(file_npc_data);
  1029. if (storage_file_write(file, furi_string_get_cstr(file_npc_data), data_size) != data_size)
  1030. {
  1031. FURI_LOG_E("Game", "Failed to write npc_data");
  1032. }
  1033. storage_file_close(file);
  1034. furi_string_replace_at(file_npc_data, 0, 1, "");
  1035. furi_string_replace_at(file_npc_data, furi_string_size(file_npc_data) - 1, 1, "");
  1036. // include the comma at the end of the npc_data array
  1037. furi_string_cat_str(file_npc_data, ",");
  1038. furi_string_remove_str(world_data, furi_string_get_cstr(file_npc_data));
  1039. furi_string_free(file_npc_data);
  1040. // Save enemy_data to disk
  1041. FuriString *file_enemy_data = json_data(world_data, "enemy_data");
  1042. if (!file_enemy_data)
  1043. {
  1044. FURI_LOG_E("Game", "Failed to get enemy data");
  1045. return false;
  1046. }
  1047. snprintf(file_path, sizeof(file_path),
  1048. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  1049. id, id);
  1050. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1051. {
  1052. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  1053. storage_file_free(file);
  1054. furi_record_close(RECORD_STORAGE);
  1055. furi_string_free(file_enemy_data);
  1056. return false;
  1057. }
  1058. data_size = furi_string_size(file_enemy_data);
  1059. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  1060. {
  1061. FURI_LOG_E("Game", "Failed to write enemy_data");
  1062. }
  1063. furi_string_free(file_enemy_data);
  1064. // Clean up
  1065. storage_file_close(file);
  1066. storage_file_free(file);
  1067. furi_record_close(RECORD_STORAGE);
  1068. return true;
  1069. }