storage.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. return false;
  974. }
  975. // Save file_json_data to disk
  976. char directory_path[256];
  977. snprintf(directory_path, sizeof(directory_path),
  978. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s", id);
  979. Storage *storage = furi_record_open(RECORD_STORAGE);
  980. storage_common_mkdir(storage, directory_path);
  981. File *file = storage_file_alloc(storage);
  982. char file_path[256];
  983. snprintf(file_path, sizeof(file_path),
  984. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_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_json_data);
  992. return false;
  993. }
  994. size_t data_size = furi_string_size(file_json_data);
  995. if (storage_file_write(file, furi_string_get_cstr(file_json_data), data_size) != data_size)
  996. {
  997. FURI_LOG_E("Game", "Failed to write json_data");
  998. }
  999. storage_file_close(file);
  1000. furi_string_replace_at(file_json_data, 0, 1, "");
  1001. furi_string_replace_at(file_json_data, furi_string_size(file_json_data) - 1, 1, "");
  1002. // include the comma at the end of the json_data array
  1003. furi_string_cat_str(file_json_data, ",");
  1004. furi_string_remove_str(world_data, furi_string_get_cstr(file_json_data));
  1005. furi_string_free(file_json_data);
  1006. // save npc_data to disk
  1007. FuriString *file_npc_data = json_data(world_data, "npc_data");
  1008. if (!file_npc_data)
  1009. {
  1010. FURI_LOG_E("Game", "Failed to get npc data");
  1011. return false;
  1012. }
  1013. snprintf(file_path, sizeof(file_path),
  1014. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_npc_data.json",
  1015. id, id);
  1016. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1017. {
  1018. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  1019. storage_file_free(file);
  1020. furi_record_close(RECORD_STORAGE);
  1021. furi_string_free(file_npc_data);
  1022. return false;
  1023. }
  1024. data_size = furi_string_size(file_npc_data);
  1025. if (storage_file_write(file, furi_string_get_cstr(file_npc_data), data_size) != data_size)
  1026. {
  1027. FURI_LOG_E("Game", "Failed to write npc_data");
  1028. }
  1029. storage_file_close(file);
  1030. furi_string_replace_at(file_npc_data, 0, 1, "");
  1031. furi_string_replace_at(file_npc_data, furi_string_size(file_npc_data) - 1, 1, "");
  1032. // include the comma at the end of the npc_data array
  1033. furi_string_cat_str(file_npc_data, ",");
  1034. furi_string_remove_str(world_data, furi_string_get_cstr(file_npc_data));
  1035. furi_string_free(file_npc_data);
  1036. // Save enemy_data to disk
  1037. FuriString *file_enemy_data = json_data(world_data, "enemy_data");
  1038. if (!file_enemy_data)
  1039. {
  1040. FURI_LOG_E("Game", "Failed to get enemy data");
  1041. return false;
  1042. }
  1043. snprintf(file_path, sizeof(file_path),
  1044. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_enemy_data.json",
  1045. id, id);
  1046. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1047. {
  1048. FURI_LOG_E("Game", "Failed to open file for writing: %s", file_path);
  1049. storage_file_free(file);
  1050. furi_record_close(RECORD_STORAGE);
  1051. furi_string_free(file_enemy_data);
  1052. return false;
  1053. }
  1054. data_size = furi_string_size(file_enemy_data);
  1055. if (storage_file_write(file, furi_string_get_cstr(file_enemy_data), data_size) != data_size)
  1056. {
  1057. FURI_LOG_E("Game", "Failed to write enemy_data");
  1058. }
  1059. furi_string_free(file_enemy_data);
  1060. // Clean up
  1061. storage_file_close(file);
  1062. storage_file_free(file);
  1063. furi_record_close(RECORD_STORAGE);
  1064. return true;
  1065. }