storage.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. #include <game/storage.h>
  2. 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. // Helper function to load an integer
  440. static bool load_number(const char *path_name, int *value)
  441. {
  442. if (!path_name || !value)
  443. {
  444. FURI_LOG_E(TAG, "Invalid arguments to load_number");
  445. return false;
  446. }
  447. char buffer[64];
  448. if (!load_char(path_name, buffer, sizeof(buffer)))
  449. {
  450. FURI_LOG_E(TAG, "Failed to load number from path: %s", path_name);
  451. return false;
  452. }
  453. *value = atoi(buffer);
  454. return true;
  455. }
  456. // Helper function to load a float
  457. static bool load_float(const char *path_name, float *value)
  458. {
  459. if (!path_name || !value)
  460. {
  461. FURI_LOG_E(TAG, "Invalid arguments to load_float");
  462. return false;
  463. }
  464. char buffer[64];
  465. if (!load_char(path_name, buffer, sizeof(buffer)))
  466. {
  467. FURI_LOG_E(TAG, "Failed to load float from path: %s", path_name);
  468. return false;
  469. }
  470. // check if the string is a valid float
  471. char *endptr;
  472. *value = strtof(buffer, &endptr);
  473. if (endptr == buffer)
  474. {
  475. FURI_LOG_E(TAG, "Failed to parse float from path: %s", path_name);
  476. return false;
  477. }
  478. return true;
  479. }
  480. // Helper function to load an int8_t
  481. static bool load_int8(const char *path_name, int8_t *value)
  482. {
  483. if (!path_name || !value)
  484. {
  485. FURI_LOG_E(TAG, "Invalid arguments to load_int8");
  486. return false;
  487. }
  488. char buffer[64];
  489. if (!load_char(path_name, buffer, sizeof(buffer)))
  490. {
  491. FURI_LOG_E(TAG, "Failed to load int8 from path: %s", path_name);
  492. return false;
  493. }
  494. long temp = strtol(buffer, NULL, 10);
  495. if (temp < INT8_MIN || temp > INT8_MAX)
  496. {
  497. FURI_LOG_E(TAG, "Value out of range for int8: %ld", temp);
  498. return false;
  499. }
  500. // check if the string is a valid int8
  501. char *endptr;
  502. *value = (int8_t)strtol(buffer, &endptr, 10);
  503. if (endptr == buffer)
  504. {
  505. FURI_LOG_E(TAG, "Failed to parse int8 from path: %s", path_name);
  506. return false;
  507. }
  508. return true;
  509. }
  510. // Helper function to load a uint32_t
  511. bool load_uint32(const char *path_name, uint32_t *value)
  512. {
  513. if (!path_name || !value)
  514. {
  515. FURI_LOG_E(TAG, "Invalid arguments to load_uint32");
  516. return false;
  517. }
  518. char buffer[64];
  519. if (!load_char(path_name, buffer, sizeof(buffer)))
  520. {
  521. FURI_LOG_E(TAG, "Failed to load uint32 from path: %s", path_name);
  522. return false;
  523. }
  524. // check if the string is a valid uint32
  525. char *endptr;
  526. *value = strtoul(buffer, &endptr, 10);
  527. if (endptr == buffer)
  528. {
  529. FURI_LOG_E(TAG, "Failed to parse uint32 from path: %s", path_name);
  530. return false;
  531. }
  532. return true;
  533. }
  534. bool load_player_context(PlayerContext *player_context)
  535. {
  536. if (!player_context)
  537. {
  538. FURI_LOG_E(TAG, "Invalid player context");
  539. return false;
  540. }
  541. // 1. Username (String)
  542. if (!load_char("player/username", player_context->username, sizeof(player_context->username)))
  543. {
  544. FURI_LOG_E(TAG, "No data or parse error for username. Using default: 'Unknown'");
  545. memset(player_context->username, 0, sizeof(player_context->username));
  546. strncpy(player_context->username, "Unknown", sizeof(player_context->username) - 1);
  547. }
  548. // 2. Level (uint32_t)
  549. {
  550. uint32_t temp = 1; // Default
  551. if (!load_char("player/level", (char *)&temp, sizeof(temp)))
  552. {
  553. FURI_LOG_E(TAG, "No data or parse error for level. Using default: 1");
  554. }
  555. else
  556. {
  557. // char buffer[64];
  558. if (load_uint32("player/level", &temp))
  559. {
  560. player_context->level = temp;
  561. }
  562. else
  563. {
  564. FURI_LOG_E(TAG, "Failed to parse level. Using default: 1");
  565. player_context->level = 1;
  566. }
  567. }
  568. player_context->level = temp;
  569. }
  570. // 3. XP (uint32_t)
  571. {
  572. uint32_t temp = 0; // Default
  573. if (!load_uint32("player/xp", &temp))
  574. {
  575. FURI_LOG_E(TAG, "No data or parse error for xp. Using default: 0");
  576. temp = 0;
  577. }
  578. player_context->xp = temp;
  579. }
  580. // 4. Health (uint32_t)
  581. {
  582. uint32_t temp = 100; // Default
  583. if (!load_uint32("player/health", &temp))
  584. {
  585. FURI_LOG_E(TAG, "No data or parse error for health. Using default: 100");
  586. temp = 100;
  587. }
  588. player_context->health = temp;
  589. }
  590. // 5. Strength (uint32_t)
  591. {
  592. uint32_t temp = 10; // Default
  593. if (!load_uint32("player/strength", &temp))
  594. {
  595. FURI_LOG_E(TAG, "No data or parse error for strength. Using default: 10");
  596. temp = 10;
  597. }
  598. player_context->strength = temp;
  599. }
  600. // 6. Max Health (uint32_t)
  601. {
  602. uint32_t temp = 100; // Default
  603. if (!load_uint32("player/max_health", &temp))
  604. {
  605. FURI_LOG_E(TAG, "No data or parse error for max_health. Using default: 100");
  606. temp = 100;
  607. }
  608. player_context->max_health = temp;
  609. }
  610. // 7. Health Regen (uint32_t)
  611. {
  612. uint32_t temp = 1; // Default
  613. if (!load_uint32("player/health_regen", &temp))
  614. {
  615. FURI_LOG_E(TAG, "No data or parse error for health_regen. Using default: 1");
  616. temp = 1;
  617. }
  618. player_context->health_regen = temp;
  619. }
  620. // 8. Elapsed Health Regen (float)
  621. {
  622. float temp = 0.0f; // Default
  623. if (!load_float("player/elapsed_health_regen", &temp))
  624. {
  625. FURI_LOG_E(TAG, "No data or parse error for elapsed_health_regen. Using default: 0.0f");
  626. temp = 0.0f;
  627. }
  628. player_context->elapsed_health_regen = temp;
  629. }
  630. // 9. Attack Timer (float)
  631. {
  632. float temp = 0.1f; // Default
  633. if (!load_float("player/attack_timer", &temp))
  634. {
  635. FURI_LOG_E(TAG, "No data or parse error for attack_timer. Using default: 0.1f");
  636. temp = 0.1f;
  637. }
  638. player_context->attack_timer = temp;
  639. }
  640. // 10. Elapsed Attack Timer (float)
  641. {
  642. float temp = 0.0f; // Default
  643. if (!load_float("player/elapsed_attack_timer", &temp))
  644. {
  645. FURI_LOG_E(TAG, "No data or parse error for elapsed_attack_timer. Using default: 0.0f");
  646. temp = 0.0f;
  647. }
  648. player_context->elapsed_attack_timer = temp;
  649. }
  650. // 11. Direction (enum PlayerDirection)
  651. {
  652. int direction_int = 3; // Default to ENTITY_RIGHT
  653. if (!load_number("player/direction", &direction_int))
  654. {
  655. FURI_LOG_E(TAG, "No data or parse error for direction. Defaulting to ENTITY_RIGHT");
  656. direction_int = 3;
  657. }
  658. switch (direction_int)
  659. {
  660. case 0:
  661. player_context->direction = ENTITY_UP;
  662. break;
  663. case 1:
  664. player_context->direction = ENTITY_DOWN;
  665. break;
  666. case 2:
  667. player_context->direction = ENTITY_LEFT;
  668. break;
  669. case 3:
  670. player_context->direction = ENTITY_RIGHT;
  671. break;
  672. default:
  673. FURI_LOG_E(TAG, "Invalid direction value: %d. Defaulting to ENTITY_RIGHT", direction_int);
  674. player_context->direction = ENTITY_RIGHT;
  675. break;
  676. }
  677. }
  678. // 12. State (enum PlayerState)
  679. {
  680. int state_int = 0; // Default to ENTITY_IDLE
  681. if (!load_number("player/state", &state_int))
  682. {
  683. FURI_LOG_E(TAG, "No data or parse error for state. Defaulting to ENTITY_IDLE");
  684. state_int = 0;
  685. }
  686. switch (state_int)
  687. {
  688. case 0:
  689. player_context->state = ENTITY_IDLE;
  690. break;
  691. case 1:
  692. player_context->state = ENTITY_MOVING;
  693. break;
  694. case 2:
  695. player_context->state = ENTITY_ATTACKING;
  696. break;
  697. case 3:
  698. player_context->state = ENTITY_ATTACKED;
  699. break;
  700. case 4:
  701. player_context->state = ENTITY_DEAD;
  702. break;
  703. default:
  704. FURI_LOG_E(TAG, "Invalid state value: %d. Defaulting to ENTITY_IDLE", state_int);
  705. player_context->state = ENTITY_IDLE;
  706. break;
  707. }
  708. }
  709. // 13. Start Position X (float)
  710. {
  711. float temp = 192.0f; // Default
  712. if (!load_float("player/start_position_x", &temp))
  713. {
  714. FURI_LOG_E(TAG, "No data or parse error for start_position_x. Using default: 192.0f");
  715. temp = 192.0f;
  716. }
  717. player_context->start_position.x = temp;
  718. }
  719. // 14. Start Position Y (float)
  720. {
  721. float temp = 96.0f; // Default
  722. if (!load_float("player/start_position_y", &temp))
  723. {
  724. FURI_LOG_E(TAG, "No data or parse error for start_position_y. Using default: 96.0f");
  725. temp = 96.0f;
  726. }
  727. player_context->start_position.y = temp;
  728. }
  729. // 15. dx (int8_t)
  730. {
  731. int8_t temp = 1; // Default
  732. if (!load_int8("player/dx", &temp))
  733. {
  734. FURI_LOG_E(TAG, "No data or parse error for dx. Using default: 1");
  735. temp = 1;
  736. }
  737. player_context->dx = temp;
  738. }
  739. // 16. dy (int8_t)
  740. {
  741. int8_t temp = 0; // Default
  742. if (!load_int8("player/dy", &temp))
  743. {
  744. FURI_LOG_E(TAG, "No data or parse error for dy. Using default: 0");
  745. temp = 0;
  746. }
  747. player_context->dy = temp;
  748. }
  749. return true;
  750. }
  751. // loads from STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json
  752. // then gets each key-value pair and saves it as it's own file so it can be loaded separately using
  753. // load_player_context
  754. bool set_player_context()
  755. {
  756. char file_path[256];
  757. snprintf(file_path, sizeof(file_path),
  758. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  759. FuriString *player_stats = flipper_http_load_from_file(file_path);
  760. if (!player_stats)
  761. {
  762. FURI_LOG_E(TAG, "Failed to load player stats from file: %s", file_path);
  763. return false;
  764. }
  765. // Get the key one-by-one and save it to a separate file
  766. // 1. Username (String)
  767. FuriString *username = get_json_value_furi("username", player_stats);
  768. if (username)
  769. {
  770. save_char("player/username", furi_string_get_cstr(username));
  771. furi_string_free(username);
  772. }
  773. // 2. Level (uint32_t)
  774. FuriString *level = get_json_value_furi("level", player_stats);
  775. if (level)
  776. {
  777. save_uint32("player/level", atoi(furi_string_get_cstr(level)));
  778. furi_string_free(level);
  779. }
  780. // 3. XP (uint32_t)
  781. FuriString *xp = get_json_value_furi("xp", player_stats);
  782. if (xp)
  783. {
  784. save_uint32("player/xp", atoi(furi_string_get_cstr(xp)));
  785. furi_string_free(xp);
  786. }
  787. // 4. Health (uint32_t)
  788. FuriString *health = get_json_value_furi("health", player_stats);
  789. if (health)
  790. {
  791. save_uint32("player/health", atoi(furi_string_get_cstr(health)));
  792. furi_string_free(health);
  793. }
  794. // 5. Strength (uint32_t)
  795. FuriString *strength = get_json_value_furi("strength", player_stats);
  796. if (strength)
  797. {
  798. save_uint32("player/strength", atoi(furi_string_get_cstr(strength)));
  799. furi_string_free(strength);
  800. }
  801. // 6. Max Health (uint32_t)
  802. FuriString *max_health = get_json_value_furi("max_health", player_stats);
  803. if (max_health)
  804. {
  805. save_uint32("player/max_health", atoi(furi_string_get_cstr(max_health)));
  806. furi_string_free(max_health);
  807. }
  808. // 7. Health Regen (uint32_t)
  809. FuriString *health_regen = get_json_value_furi("health_regen", player_stats);
  810. if (health_regen)
  811. {
  812. save_uint32("player/health_regen", atoi(furi_string_get_cstr(health_regen)));
  813. furi_string_free(health_regen);
  814. }
  815. // 8. Elapsed Health Regen (float)
  816. FuriString *elapsed_health_regen = get_json_value_furi("elapsed_health_regen", player_stats);
  817. if (elapsed_health_regen)
  818. {
  819. save_float("player/elapsed_health_regen", strtof(furi_string_get_cstr(elapsed_health_regen), NULL));
  820. furi_string_free(elapsed_health_regen);
  821. }
  822. // 9. Attack Timer (float)
  823. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  824. if (attack_timer)
  825. {
  826. save_float("player/attack_timer", strtof(furi_string_get_cstr(attack_timer), NULL));
  827. furi_string_free(attack_timer);
  828. }
  829. // 10. Elapsed Attack Timer (float)
  830. FuriString *elapsed_attack_timer = get_json_value_furi("elapsed_attack_timer", player_stats);
  831. if (elapsed_attack_timer)
  832. {
  833. save_float("player/elapsed_attack_timer", strtof(furi_string_get_cstr(elapsed_attack_timer), NULL));
  834. furi_string_free(elapsed_attack_timer);
  835. }
  836. // 11. Direction (enum PlayerDirection)
  837. FuriString *direction = get_json_value_furi("direction", player_stats);
  838. if (direction)
  839. {
  840. save_char("player/direction", furi_string_get_cstr(direction));
  841. furi_string_free(direction);
  842. }
  843. // 12. State (enum PlayerState)
  844. FuriString *state = get_json_value_furi("state", player_stats);
  845. if (state)
  846. {
  847. save_char("player/state", furi_string_get_cstr(state));
  848. furi_string_free(state);
  849. }
  850. // 13. Start Position X (float)
  851. FuriString *start_position_x = get_json_value_furi("start_position_x", player_stats);
  852. if (start_position_x)
  853. {
  854. save_float("player/start_position_x", strtof(furi_string_get_cstr(start_position_x), NULL));
  855. furi_string_free(start_position_x);
  856. }
  857. // 14. Start Position Y (float)
  858. FuriString *start_position_y = get_json_value_furi("start_position_y", player_stats);
  859. if (start_position_y)
  860. {
  861. save_float("player/start_position_y", strtof(furi_string_get_cstr(start_position_y), NULL));
  862. furi_string_free(start_position_y);
  863. }
  864. // 15. dx (int8_t)
  865. FuriString *dx = get_json_value_furi("dx", player_stats);
  866. if (dx)
  867. {
  868. save_int8("player/dx", atoi(furi_string_get_cstr(dx)));
  869. furi_string_free(dx);
  870. }
  871. // 16. dy (int8_t)
  872. FuriString *dy = get_json_value_furi("dy", player_stats);
  873. if (dy)
  874. {
  875. save_int8("player/dy", atoi(furi_string_get_cstr(dy)));
  876. furi_string_free(dy);
  877. }
  878. furi_string_free(player_stats);
  879. return true;
  880. }
  881. static inline void furi_string_remove_str(FuriString *string, const char *needle)
  882. {
  883. furi_string_replace_str(string, needle, "", 0);
  884. }
  885. static FuriString *json_data(const FuriString *world_data, const char *key)
  886. {
  887. size_t json_data_pos = furi_string_search_str(world_data, key, 0);
  888. if (json_data_pos == FURI_STRING_FAILURE)
  889. {
  890. FURI_LOG_E("Game", "Failed to find json_data in world data");
  891. return NULL;
  892. }
  893. size_t bracket_start = furi_string_search_char(world_data, '[', json_data_pos);
  894. if (bracket_start == FURI_STRING_FAILURE)
  895. {
  896. FURI_LOG_E("Game", "Failed to find start of json_data array");
  897. return NULL;
  898. }
  899. size_t bracket_end = furi_string_search_char(world_data, ']', bracket_start);
  900. if (bracket_end == FURI_STRING_FAILURE)
  901. {
  902. FURI_LOG_E("Game", "Failed to find end of json_data array");
  903. return NULL;
  904. }
  905. FuriString *json_data_str = furi_string_alloc();
  906. if (!json_data_str)
  907. {
  908. FURI_LOG_E("Game", "Failed to allocate json_data string");
  909. return NULL;
  910. }
  911. furi_string_cat_str(json_data_str, "{\"");
  912. furi_string_cat_str(json_data_str, key);
  913. furi_string_cat_str(json_data_str, "\":");
  914. {
  915. FuriString *temp_sub = furi_string_alloc();
  916. furi_string_set_strn(
  917. temp_sub,
  918. furi_string_get_cstr(world_data) + bracket_start,
  919. (bracket_end + 1) - bracket_start);
  920. furi_string_cat(json_data_str, temp_sub);
  921. furi_string_free(temp_sub);
  922. }
  923. furi_string_cat_str(json_data_str, "}");
  924. return json_data_str;
  925. }
  926. bool separate_world_data(char *id, FuriString *world_data)
  927. {
  928. if (!id || !world_data)
  929. {
  930. FURI_LOG_E("Game", "Invalid parameters");
  931. return false;
  932. }
  933. FuriString *file_json_data = json_data(world_data, "json_data");
  934. if (!file_json_data || furi_string_size(file_json_data) == 0)
  935. {
  936. FURI_LOG_E("Game", "Failed to get json data in separate_world_data");
  937. if (file_json_data)
  938. {
  939. furi_string_free(file_json_data);
  940. }
  941. return false;
  942. }
  943. // Save file_json_data to disk
  944. char directory_path[256];
  945. snprintf(directory_path, sizeof(directory_path),
  946. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  947. Storage *storage = furi_record_open(RECORD_STORAGE);
  948. storage_common_mkdir(storage, directory_path);
  949. File *file = storage_file_alloc(storage);
  950. char file_path[256];
  951. snprintf(file_path, sizeof(file_path),
  952. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json",
  953. id, id);
  954. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  955. {
  956. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  957. storage_file_free(file);
  958. furi_record_close(RECORD_STORAGE);
  959. furi_string_free(file_json_data);
  960. return false;
  961. }
  962. size_t data_size = furi_string_size(file_json_data);
  963. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  964. {
  965. FURI_LOG_E("Game", "Failed to write json_data");
  966. }
  967. storage_file_close(file);
  968. furi_string_replace_at(file_json_data, 0, 1, "");
  969. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  970. // include the comma at the end of the json_data array
  971. furi_string_cat_str(file_json_data, ",");
  972. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  973. furi_string_free(file_json_data);
  974. // save npc_data to disk
  975. FuriString *file_npc_data = json_data(world_data, "npc_data");
  976. if (!file_npc_data)
  977. {
  978. FURI_LOG_I("Game", "NPC data is not present");
  979. // not every map has npc_data, so we can skip this
  980. }
  981. else
  982. {
  983. snprintf(file_path, sizeof(file_path),
  984. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
  985. id, id);
  986. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  987. {
  988. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  989. storage_file_free(file);
  990. furi_record_close(RECORD_STORAGE);
  991. furi_string_free(file_npc_data);
  992. return false;
  993. }
  994. data_size = furi_string_size(file_npc_data);
  995. if (storage_file_write(file, furi_string_get_cstr(file_npc_data), data_size) != data_size)
  996. {
  997. FURI_LOG_E("Game", "Failed to write npc_data");
  998. }
  999. storage_file_close(file);
  1000. furi_string_replace_at(file_npc_data, 0, 1, "");
  1001. furi_string_replace_at(file_npc_data, furi_string_size(file_npc_data) - 1, 1, "");
  1002. // include the comma at the end of the npc_data array
  1003. furi_string_cat_str(file_npc_data, ",");
  1004. furi_string_remove_str(world_data, furi_string_get_cstr(file_npc_data));
  1005. furi_string_free(file_npc_data);
  1006. }
  1007. // Save enemy_data to disk
  1008. FuriString *file_enemy_data = json_data(world_data, "enemy_data");
  1009. if (!file_enemy_data)
  1010. {
  1011. FURI_LOG_E("Game", "Failed to get enemy data");
  1012. return false;
  1013. }
  1014. snprintf(file_path, sizeof(file_path),
  1015. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  1016. id, id);
  1017. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1018. {
  1019. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  1020. storage_file_free(file);
  1021. furi_record_close(RECORD_STORAGE);
  1022. furi_string_free(file_enemy_data);
  1023. return false;
  1024. }
  1025. data_size = furi_string_size(file_enemy_data);
  1026. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  1027. {
  1028. FURI_LOG_E("Game", "Failed to write enemy_data");
  1029. }
  1030. furi_string_free(file_enemy_data);
  1031. // Clean up
  1032. storage_file_close(file);
  1033. storage_file_free(file);
  1034. furi_record_close(RECORD_STORAGE);
  1035. return true;
  1036. }