storage.c 31 KB

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