storage.c 32 KB

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