game.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. #include <callback/game.h>
  2. //
  3. #include "engine/engine.h"
  4. #include "engine/game_engine.h"
  5. #include "engine/game_manager_i.h"
  6. #include "engine/level_i.h"
  7. #include "engine/entity_i.h"
  8. //
  9. #include "game/storage.h"
  10. //
  11. #include <callback/loader.h>
  12. #include <callback/free.h>
  13. #include <callback/alloc.h>
  14. #include <callback/callback.h>
  15. #include "alloc/alloc.h"
  16. #include <flip_storage/storage.h>
  17. bool user_hit_back = false;
  18. uint32_t lobby_index = -1;
  19. char *lobby_list[10];
  20. static uint8_t timer_iteration = 0; // timer iteration for the loading screen
  21. static uint8_t timer_refresh = 5; // duration for timer to refresh
  22. FuriThread *game_thread = NULL;
  23. FuriThread *waiting_thread = NULL;
  24. bool game_thread_running = false;
  25. bool waiting_thread_running = false;
  26. static void game_frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
  27. {
  28. UNUSED(engine);
  29. GameManager *game_manager = context;
  30. game_manager_input_set(game_manager, input);
  31. game_manager_update(game_manager);
  32. game_manager_render(game_manager, canvas);
  33. }
  34. static int32_t game_app(void *p)
  35. {
  36. UNUSED(p);
  37. GameManager *game_manager = game_manager_alloc();
  38. if (!game_manager)
  39. {
  40. FURI_LOG_E("Game", "Failed to allocate game manager");
  41. return -1;
  42. }
  43. // Setup game engine settings...
  44. GameEngineSettings settings = game_engine_settings_init();
  45. settings.target_fps = atof_(fps_choices_str[fps_index]);
  46. settings.show_fps = game.show_fps;
  47. settings.always_backlight = strstr(yes_or_no_choices[screen_always_on_index], "Yes") != NULL;
  48. settings.frame_callback = game_frame_cb;
  49. settings.context = game_manager;
  50. GameEngine *engine = game_engine_alloc(settings);
  51. if (!engine)
  52. {
  53. FURI_LOG_E("Game", "Failed to allocate game engine");
  54. game_manager_free(game_manager);
  55. return -1;
  56. }
  57. game_manager_engine_set(game_manager, engine);
  58. // Allocate custom game context if needed
  59. void *game_context = NULL;
  60. if (game.context_size > 0)
  61. {
  62. game_context = malloc(game.context_size);
  63. game_manager_game_context_set(game_manager, game_context);
  64. }
  65. // Start the game
  66. game.start(game_manager, game_context);
  67. // 1) Run the engine
  68. game_engine_run(engine);
  69. // 2) Stop the game FIRST, so it can do any internal cleanup
  70. game.stop(game_context);
  71. // 3) Now free the engine
  72. game_engine_free(engine);
  73. // 4) Now free the manager
  74. game_manager_free(game_manager);
  75. // 5) Finally, free your custom context if it was allocated
  76. if (game_context)
  77. {
  78. free(game_context);
  79. }
  80. // 6) Check for leftover entities
  81. int32_t entities = entities_get_count();
  82. if (entities != 0)
  83. {
  84. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  85. return -1;
  86. }
  87. return 0;
  88. }
  89. static int32_t game_waiting_app_callback(void *p)
  90. {
  91. FlipWorldApp *app = (FlipWorldApp *)p;
  92. furi_check(app);
  93. FlipperHTTP *fhttp = flipper_http_alloc();
  94. if (!fhttp)
  95. {
  96. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  97. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP");
  98. return -1;
  99. }
  100. user_hit_back = false;
  101. timer_iteration = 0;
  102. while (timer_iteration < 60 && !user_hit_back)
  103. {
  104. FURI_LOG_I(TAG, "Waiting for more players...");
  105. game_waiting_process(fhttp, app);
  106. FURI_LOG_I(TAG, "Waiting for more players... %d", timer_iteration);
  107. timer_iteration++;
  108. furi_delay_ms(1000 * timer_refresh);
  109. }
  110. // if we reach here, it means we timed out or the user hit back
  111. FURI_LOG_E(TAG, "No players joined within the timeout or user hit back");
  112. remove_player_from_lobby(fhttp);
  113. flipper_http_free(fhttp);
  114. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  115. return 0;
  116. }
  117. static bool game_start_waiting_thread(void *context)
  118. {
  119. FlipWorldApp *app = (FlipWorldApp *)context;
  120. furi_check(app);
  121. // free game thread
  122. if (waiting_thread_running)
  123. {
  124. waiting_thread_running = false;
  125. if (waiting_thread)
  126. {
  127. furi_thread_flags_set(furi_thread_get_id(waiting_thread), WorkerEvtStop);
  128. furi_thread_join(waiting_thread);
  129. furi_thread_free(waiting_thread);
  130. }
  131. }
  132. // start waiting thread
  133. FuriThread *thread = furi_thread_alloc_ex("waiting_thread", 2048, game_waiting_app_callback, app);
  134. if (!thread)
  135. {
  136. FURI_LOG_E(TAG, "Failed to allocate waiting thread");
  137. easy_flipper_dialog("Error", "Failed to allocate waiting thread. Restart your Flipper.");
  138. return false;
  139. }
  140. furi_thread_start(thread);
  141. waiting_thread = thread;
  142. waiting_thread_running = true;
  143. return true;
  144. }
  145. static bool game_fetch_world_list(FlipperHTTP *fhttp)
  146. {
  147. if (!fhttp)
  148. {
  149. FURI_LOG_E(TAG, "fhttp is NULL");
  150. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  151. return false;
  152. }
  153. // ensure flip_world directory exists
  154. char directory_path[128];
  155. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  156. Storage *storage = furi_record_open(RECORD_STORAGE);
  157. storage_common_mkdir(storage, directory_path);
  158. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  159. storage_common_mkdir(storage, directory_path);
  160. furi_record_close(RECORD_STORAGE);
  161. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  162. fhttp->save_received_data = true;
  163. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/v5/list/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  164. }
  165. // we will load the palyer stats from the API and save them
  166. // in player_spawn game method, it will load the player stats that we saved
  167. static bool game_fetch_player_stats(FlipperHTTP *fhttp)
  168. {
  169. if (!fhttp)
  170. {
  171. FURI_LOG_E(TAG, "fhttp is NULL");
  172. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  173. return false;
  174. }
  175. char username[64];
  176. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  177. {
  178. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  179. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  180. return false;
  181. }
  182. char url[128];
  183. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/game-stats/%s/", username);
  184. // ensure the folders exist
  185. char directory_path[128];
  186. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  187. Storage *storage = furi_record_open(RECORD_STORAGE);
  188. storage_common_mkdir(storage, directory_path);
  189. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  190. storage_common_mkdir(storage, directory_path);
  191. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player");
  192. storage_common_mkdir(storage, directory_path);
  193. furi_record_close(RECORD_STORAGE);
  194. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  195. fhttp->save_received_data = true;
  196. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  197. }
  198. // static bool fetch_app_update(FlipperHTTP *fhttp)
  199. // {
  200. // if (!fhttp)
  201. // {
  202. // FURI_LOG_E(TAG, "fhttp is NULL");
  203. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  204. // return false;
  205. // }
  206. // return flipper_http_get_request_with_headers(fhttp, "https://www.jblanked.com/flipper/api/app/last-updated/flip_world/", "{\"Content-Type\":\"application/json\"}");
  207. // }
  208. // static bool parse_app_update(FlipperHTTP *fhttp)
  209. // {
  210. // if (!fhttp)
  211. // {
  212. // FURI_LOG_E(TAG, "fhttp is NULL");
  213. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  214. // return false;
  215. // }
  216. // if (fhttp->last_response == NULL || strlen(fhttp->last_response) == 0)
  217. // {
  218. // FURI_LOG_E(TAG, "fhttp->last_response is NULL or empty");
  219. // easy_flipper_dialog("Error", "fhttp->last_response is NULL or empty. Press BACK to return.");
  220. // return false;
  221. // }
  222. // bool last_update_available = false;
  223. // char last_updated_old[32];
  224. // // load the previous last_updated
  225. // if (!load_char("last_updated", last_updated_old, sizeof(last_updated_old)))
  226. // {
  227. // FURI_LOG_E(TAG, "Failed to load last_updated");
  228. // // it's okay, we'll just update it
  229. // }
  230. // // save the new last_updated
  231. // save_char("last_updated", fhttp->last_response);
  232. // // compare the two
  233. // if (strlen(last_updated_old) == 0 || !is_str(last_updated_old, fhttp->last_response))
  234. // {
  235. // last_update_available = true;
  236. // }
  237. // if (last_update_available)
  238. // {
  239. // easy_flipper_dialog("Update Available", "An update is available. Press OK to update.");
  240. // return true;
  241. // }
  242. // else
  243. // {
  244. // easy_flipper_dialog("No Update Available", "No update is available. Press OK to continue.");
  245. // return false;
  246. // }
  247. // }
  248. static bool game_thread_start(void *context)
  249. {
  250. FlipWorldApp *app = (FlipWorldApp *)context;
  251. if (!app)
  252. {
  253. FURI_LOG_E(TAG, "app is NULL");
  254. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  255. return false;
  256. }
  257. // free everything but message_view
  258. free_variable_item_list(app);
  259. free_text_input_view(app);
  260. // free_submenu_other(app); // free lobby list or settings
  261. loader_view_free(app);
  262. free_game_submenu(app);
  263. // free game thread
  264. if (game_thread_running)
  265. {
  266. game_thread_running = false;
  267. if (game_thread)
  268. {
  269. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  270. furi_thread_join(game_thread);
  271. furi_thread_free(game_thread);
  272. }
  273. }
  274. // start game thread
  275. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  276. if (!thread)
  277. {
  278. FURI_LOG_E(TAG, "Failed to allocate game thread");
  279. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  280. return false;
  281. }
  282. furi_thread_start(thread);
  283. game_thread = thread;
  284. game_thread_running = true;
  285. return true;
  286. }
  287. // combine register, login, and world list fetch into one function to switch to the loader view
  288. static bool game_fetch(DataLoaderModel *model)
  289. {
  290. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  291. if (!app)
  292. {
  293. FURI_LOG_E(TAG, "app is NULL");
  294. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  295. return false;
  296. }
  297. if (model->request_index == 0)
  298. {
  299. // login
  300. char username[64];
  301. char password[64];
  302. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  303. {
  304. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  305. view_dispatcher_switch_to_view(app->view_dispatcher,
  306. FlipWorldViewSubmenu); // just go back to the main menu for now
  307. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  308. return false;
  309. }
  310. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  311. {
  312. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  313. view_dispatcher_switch_to_view(app->view_dispatcher,
  314. FlipWorldViewSubmenu); // just go back to the main menu for now
  315. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  316. return false;
  317. }
  318. char payload[256];
  319. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  320. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  321. }
  322. else if (model->request_index == 1)
  323. {
  324. // check if login was successful
  325. char is_logged_in[8];
  326. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  327. {
  328. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  329. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  330. view_dispatcher_switch_to_view(app->view_dispatcher,
  331. FlipWorldViewSubmenu); // just go back to the main menu for now
  332. return false;
  333. }
  334. if (is_str(is_logged_in, "false") && is_str(model->title, "Registering..."))
  335. {
  336. // register
  337. char username[64];
  338. char password[64];
  339. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  340. {
  341. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  342. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  343. view_dispatcher_switch_to_view(app->view_dispatcher,
  344. FlipWorldViewSubmenu); // just go back to the main menu for now
  345. return false;
  346. }
  347. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  348. {
  349. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  350. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  351. view_dispatcher_switch_to_view(app->view_dispatcher,
  352. FlipWorldViewSubmenu); // just go back to the main menu for now
  353. return false;
  354. }
  355. char payload[172];
  356. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  357. model->title = "Registering...";
  358. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  359. }
  360. else
  361. {
  362. model->title = "Fetching World List..";
  363. return game_fetch_world_list(model->fhttp);
  364. }
  365. }
  366. else if (model->request_index == 2)
  367. {
  368. model->title = "Fetching World List..";
  369. return game_fetch_world_list(model->fhttp);
  370. }
  371. else if (model->request_index == 3)
  372. {
  373. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  374. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  375. if (!world_list)
  376. {
  377. view_dispatcher_switch_to_view(app->view_dispatcher,
  378. FlipWorldViewSubmenu); // just go back to the main menu for now
  379. FURI_LOG_E(TAG, "Failed to load world list");
  380. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  381. return false;
  382. }
  383. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  384. if (!first_world)
  385. {
  386. view_dispatcher_switch_to_view(app->view_dispatcher,
  387. FlipWorldViewSubmenu); // just go back to the main menu for now
  388. FURI_LOG_E(TAG, "Failed to get first world");
  389. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  390. furi_string_free(world_list);
  391. return false;
  392. }
  393. if (world_exists(furi_string_get_cstr(first_world)))
  394. {
  395. furi_string_free(world_list);
  396. furi_string_free(first_world);
  397. if (!game_thread_start(app))
  398. {
  399. FURI_LOG_E(TAG, "Failed to start game thread");
  400. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  401. view_dispatcher_switch_to_view(app->view_dispatcher,
  402. FlipWorldViewSubmenu); // just go back to the main menu for now
  403. return "Failed to start game thread";
  404. }
  405. return true;
  406. }
  407. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  408. model->fhttp->save_received_data = true;
  409. char url[128];
  410. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v5/get/world/%s/", furi_string_get_cstr(first_world));
  411. furi_string_free(world_list);
  412. furi_string_free(first_world);
  413. return flipper_http_request(model->fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  414. }
  415. FURI_LOG_E(TAG, "Unknown request index");
  416. return false;
  417. }
  418. static char *game_parse(DataLoaderModel *model)
  419. {
  420. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  421. if (model->request_index == 0)
  422. {
  423. if (!model->fhttp->last_response)
  424. {
  425. save_char("is_logged_in", "false");
  426. // Go back to the main menu
  427. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  428. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  429. return "Response is empty...";
  430. }
  431. // Check for successful conditions
  432. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  433. {
  434. save_char("is_logged_in", "true");
  435. model->title = "Login successful!";
  436. model->title = "Fetching World List..";
  437. return "Login successful!";
  438. }
  439. // Check if user not found
  440. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  441. {
  442. save_char("is_logged_in", "false");
  443. model->title = "Registering...";
  444. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  445. }
  446. // If not success, not found, check length conditions
  447. size_t resp_len = strlen(model->fhttp->last_response);
  448. if (resp_len == 0 || resp_len > 127)
  449. {
  450. // Empty or too long means failed login
  451. save_char("is_logged_in", "false");
  452. // Go back to the main menu
  453. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  454. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  455. return "Failed to login...";
  456. }
  457. // Handle any other unknown response as a failure
  458. save_char("is_logged_in", "false");
  459. // Go back to the main menu
  460. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  461. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  462. return "Failed to login...";
  463. }
  464. else if (model->request_index == 1)
  465. {
  466. if (is_str(model->title, "Registering..."))
  467. {
  468. // check registration response
  469. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  470. {
  471. save_char("is_logged_in", "true");
  472. char username[64];
  473. char password[64];
  474. // load the username and password, then save them
  475. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  476. {
  477. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  478. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  479. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  480. return "Failed to load Flip-Social-Username";
  481. }
  482. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  483. {
  484. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  485. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  486. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  487. return "Failed to load Flip-Social-Password";
  488. }
  489. // load wifi ssid,pass then save
  490. char ssid[64];
  491. char pass[64];
  492. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  493. {
  494. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  495. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  496. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  497. return "Failed to load WiFi-SSID";
  498. }
  499. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  500. {
  501. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  502. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  503. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  504. return "Failed to load WiFi-Password";
  505. }
  506. save_settings(ssid, pass, username, password);
  507. model->title = "Fetching World List..";
  508. return "Account created!";
  509. }
  510. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  511. {
  512. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  513. view_dispatcher_switch_to_view(app->view_dispatcher,
  514. FlipWorldViewSubmenu); // just go back to the main menu for now
  515. return "Please enter your credentials.";
  516. }
  517. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  518. {
  519. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  520. view_dispatcher_switch_to_view(app->view_dispatcher,
  521. FlipWorldViewSubmenu); // just go back to the main menu for now
  522. return "Username already exists.";
  523. }
  524. else
  525. {
  526. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  527. view_dispatcher_switch_to_view(app->view_dispatcher,
  528. FlipWorldViewSubmenu); // just go back to the main menu for now
  529. return "Registration failed...";
  530. }
  531. }
  532. else
  533. {
  534. if (!game_thread_start(app))
  535. {
  536. FURI_LOG_E(TAG, "Failed to start game thread");
  537. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  538. view_dispatcher_switch_to_view(app->view_dispatcher,
  539. FlipWorldViewSubmenu); // just go back to the main menu for now
  540. return "Failed to start game thread";
  541. }
  542. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  543. }
  544. }
  545. else if (model->request_index == 2)
  546. {
  547. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  548. }
  549. else if (model->request_index == 3)
  550. {
  551. if (!game_thread_start(app))
  552. {
  553. FURI_LOG_E(TAG, "Failed to start game thread");
  554. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  555. view_dispatcher_switch_to_view(app->view_dispatcher,
  556. FlipWorldViewSubmenu); // just go back to the main menu for now
  557. return "Failed to start game thread";
  558. }
  559. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  560. }
  561. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  562. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  563. return "Unknown error";
  564. }
  565. static void game_switch_to_view(FlipWorldApp *app)
  566. {
  567. if (!loader_view_alloc(app))
  568. {
  569. FURI_LOG_E(TAG, "Failed to allocate view loader");
  570. return;
  571. }
  572. loader_switch_to_view(app, "Starting Game..", game_fetch, game_parse, 5, callback_to_submenu, FlipWorldViewLoader);
  573. }
  574. void game_run(FlipWorldApp *app)
  575. {
  576. if (!app)
  577. {
  578. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  579. return;
  580. }
  581. free_all_views(app, true, true, false);
  582. // only need to check if they have 30k free (game needs about 12k currently)
  583. if (!is_enough_heap(30000, false))
  584. {
  585. const size_t min_free = memmgr_get_free_heap();
  586. char message[64];
  587. snprintf(message, sizeof(message), "Not enough heap memory.\nThere are %zu bytes free.", min_free);
  588. easy_flipper_dialog("Error", message);
  589. return;
  590. }
  591. // check if logged in
  592. if (is_logged_in() || is_logged_in_to_flip_social())
  593. {
  594. FlipperHTTP *fhttp = flipper_http_alloc();
  595. if (!fhttp)
  596. {
  597. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  598. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  599. return;
  600. }
  601. bool game_fetch_world_list_i()
  602. {
  603. return game_fetch_world_list(fhttp);
  604. }
  605. bool parse_world_list_i()
  606. {
  607. return fhttp->state != ISSUE;
  608. }
  609. bool game_fetch_player_stats_i()
  610. {
  611. return game_fetch_player_stats(fhttp);
  612. }
  613. if (!alloc_message_view(app, MessageStateLoading))
  614. {
  615. FURI_LOG_E(TAG, "Failed to allocate message view");
  616. return;
  617. }
  618. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  619. // Make the request
  620. if (game_mode_index != 1) // not GAME_MODE_PVP
  621. {
  622. if (!flipper_http_process_response_async(fhttp, game_fetch_world_list_i, parse_world_list_i) || !flipper_http_process_response_async(fhttp, game_fetch_player_stats_i, set_player_context))
  623. {
  624. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  625. flipper_http_free(fhttp);
  626. }
  627. else
  628. {
  629. flipper_http_free(fhttp);
  630. }
  631. if (!game_thread_start(app))
  632. {
  633. FURI_LOG_E(TAG, "Failed to start game thread");
  634. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  635. return;
  636. }
  637. }
  638. else
  639. {
  640. // load pvp info (this returns the lobbies available)
  641. bool fetch_pvp_lobbies()
  642. {
  643. // ensure flip_world directory exists
  644. char directory_path[128];
  645. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  646. Storage *storage = furi_record_open(RECORD_STORAGE);
  647. storage_common_mkdir(storage, directory_path);
  648. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp");
  649. storage_common_mkdir(storage, directory_path);
  650. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies");
  651. storage_common_mkdir(storage, directory_path);
  652. furi_record_close(RECORD_STORAGE);
  653. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/pvp_lobbies.json");
  654. storage_simply_remove_recursive(storage, fhttp->file_path); // ensure the file is empty
  655. fhttp->save_received_data = true;
  656. // 2 players max, 10 lobbies
  657. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/pvp/lobbies/2/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  658. }
  659. bool parse_pvp_lobbies()
  660. {
  661. free_submenu_other(app);
  662. if (!alloc_submenu_other(app, FlipWorldViewLobby))
  663. {
  664. FURI_LOG_E(TAG, "Failed to allocate lobby submenu");
  665. return false;
  666. }
  667. // add the lobbies to the submenu
  668. FuriString *lobbies = flipper_http_load_from_file(fhttp->file_path);
  669. if (!lobbies)
  670. {
  671. FURI_LOG_E(TAG, "Failed to load lobbies");
  672. return false;
  673. }
  674. // parse the lobbies
  675. for (uint32_t i = 0; i < 10; i++)
  676. {
  677. FuriString *lobby = get_json_array_value_furi("lobbies", i, lobbies);
  678. if (!lobby)
  679. {
  680. FURI_LOG_I(TAG, "No more lobbies");
  681. break;
  682. }
  683. FuriString *lobby_id = get_json_value_furi("id", lobby);
  684. if (!lobby_id)
  685. {
  686. FURI_LOG_E(TAG, "Failed to get lobby id");
  687. furi_string_free(lobby);
  688. return false;
  689. }
  690. // add the lobby to the submenu
  691. submenu_add_item(app->submenu_other, furi_string_get_cstr(lobby_id), FlipWorldSubmenuIndexLobby + i, callback_submenu_lobby_choices, app);
  692. // add the lobby to the list
  693. if (!easy_flipper_set_buffer(&lobby_list[i], 64))
  694. {
  695. FURI_LOG_E(TAG, "Failed to allocate lobby list");
  696. furi_string_free(lobby);
  697. furi_string_free(lobby_id);
  698. return false;
  699. }
  700. snprintf(lobby_list[i], 64, "%s", furi_string_get_cstr(lobby_id));
  701. furi_string_free(lobby);
  702. furi_string_free(lobby_id);
  703. }
  704. furi_string_free(lobbies);
  705. return true;
  706. }
  707. // load pvp lobbies and player stats
  708. if (!flipper_http_process_response_async(fhttp, fetch_pvp_lobbies, parse_pvp_lobbies) || !flipper_http_process_response_async(fhttp, game_fetch_player_stats_i, set_player_context))
  709. {
  710. // unlike the pve/story, receiving data is necessary
  711. // so send the user back to the main menu if it fails
  712. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  713. easy_flipper_dialog("Error", "Failed to make request. Press BACK to return.");
  714. view_dispatcher_switch_to_view(app->view_dispatcher,
  715. FlipWorldViewSubmenu);
  716. flipper_http_free(fhttp);
  717. }
  718. else
  719. {
  720. flipper_http_free(fhttp);
  721. }
  722. // switch to the lobby submenu
  723. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  724. }
  725. }
  726. else
  727. {
  728. game_switch_to_view(app);
  729. }
  730. }
  731. bool game_fetch_lobby(FlipperHTTP *fhttp, char *lobby_name)
  732. {
  733. if (!fhttp)
  734. {
  735. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  736. return false;
  737. }
  738. if (!lobby_name || strlen(lobby_name) == 0)
  739. {
  740. FURI_LOG_E(TAG, "Lobby name is NULL or empty");
  741. return false;
  742. }
  743. char username[64];
  744. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  745. {
  746. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  747. return false;
  748. }
  749. // send the request to fetch the lobby details, with player_username
  750. char url[128];
  751. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/get/%s/%s/", lobby_name, username);
  752. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies/%s.json", lobby_name);
  753. fhttp->save_received_data = true;
  754. if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL))
  755. {
  756. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  757. return false;
  758. }
  759. fhttp->state = RECEIVING;
  760. while (fhttp->state != IDLE)
  761. {
  762. furi_delay_ms(100);
  763. }
  764. return true;
  765. }
  766. bool game_join_lobby(FlipperHTTP *fhttp, char *lobby_name)
  767. {
  768. if (!fhttp)
  769. {
  770. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  771. return false;
  772. }
  773. if (!lobby_name || strlen(lobby_name) == 0)
  774. {
  775. FURI_LOG_E(TAG, "Lobby name is NULL or empty");
  776. return false;
  777. }
  778. char username[64];
  779. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  780. {
  781. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  782. return false;
  783. }
  784. char url[128];
  785. char payload[128];
  786. snprintf(payload, sizeof(payload), "{\"username\":\"%s\", \"game_id\":\"%s\"}", username, lobby_name);
  787. save_char("pvp_lobby_name", lobby_name); // save the lobby name
  788. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/join/");
  789. if (!flipper_http_request(fhttp, POST, url, "{\"Content-Type\":\"application/json\"}", payload))
  790. {
  791. FURI_LOG_E(TAG, "Failed to join lobby");
  792. return false;
  793. }
  794. fhttp->state = RECEIVING;
  795. while (fhttp->state != IDLE)
  796. {
  797. furi_delay_ms(100);
  798. }
  799. return true;
  800. }
  801. static bool game_create_pvp_enemy(FuriString *lobby_details)
  802. {
  803. if (!lobby_details)
  804. {
  805. FURI_LOG_E(TAG, "Failed to load lobby details");
  806. return false;
  807. }
  808. char current_user[64];
  809. if (!load_char("Flip-Social-Username", current_user, sizeof(current_user)))
  810. {
  811. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  812. save_char("create_pvp_error", "Failed to load Flip-Social-Username");
  813. return false;
  814. }
  815. for (uint8_t i = 0; i < 2; i++)
  816. {
  817. // parse the lobby details
  818. FuriString *player_stats = get_json_array_value_furi("player_stats", i, lobby_details);
  819. if (!player_stats)
  820. {
  821. FURI_LOG_E(TAG, "Failed to get player stats");
  822. save_char("create_pvp_error", "Failed to get player stats array");
  823. return false;
  824. }
  825. // available keys from player_stats
  826. FuriString *username = get_json_value_furi("username", player_stats);
  827. if (!username)
  828. {
  829. FURI_LOG_E(TAG, "Failed to get username");
  830. save_char("create_pvp_error", "Failed to get username");
  831. furi_string_free(player_stats);
  832. return false;
  833. }
  834. // check if the username is the same as the current user
  835. if (is_str(furi_string_get_cstr(username), current_user))
  836. {
  837. furi_string_free(player_stats);
  838. furi_string_free(username);
  839. continue; // skip the current user
  840. }
  841. FuriString *strength = get_json_value_furi("strength", player_stats);
  842. FuriString *health = get_json_value_furi("health", player_stats);
  843. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  844. if (!strength || !health || !attack_timer)
  845. {
  846. FURI_LOG_E(TAG, "Failed to get player stats");
  847. save_char("create_pvp_error", "Failed to get player stats");
  848. furi_string_free(player_stats);
  849. furi_string_free(username);
  850. if (strength)
  851. furi_string_free(strength);
  852. if (health)
  853. furi_string_free(health);
  854. if (attack_timer)
  855. furi_string_free(attack_timer);
  856. return false;
  857. }
  858. // create enemy data
  859. FuriString *enemy_data = furi_string_alloc();
  860. furi_string_printf(
  861. enemy_data,
  862. "{\"enemy_data\":[{\"id\":\"sword\",\"is_user\":\"true\",\"username\":\"%s\","
  863. "\"index\":0,\"start_position\":{\"x\":350,\"y\":210},\"end_position\":{\"x\":350,\"y\":210},"
  864. "\"move_timer\":1,\"speed\":1,\"attack_timer\":%f,\"strength\":%f,\"health\":%f}]}",
  865. furi_string_get_cstr(username),
  866. (double)atof_furi(attack_timer),
  867. (double)atof_furi(strength),
  868. (double)atof_furi(health));
  869. char directory_path[128];
  870. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  871. Storage *storage = furi_record_open(RECORD_STORAGE);
  872. storage_common_mkdir(storage, directory_path);
  873. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  874. storage_common_mkdir(storage, directory_path);
  875. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/pvp_world");
  876. storage_common_mkdir(storage, directory_path);
  877. furi_record_close(RECORD_STORAGE);
  878. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/pvp_world/pvp_world_enemy_data.json");
  879. // remove the enemy_data file if it exists
  880. storage_simply_remove_recursive(storage, directory_path);
  881. File *file = storage_file_alloc(storage);
  882. if (!storage_file_open(file, directory_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  883. {
  884. FURI_LOG_E("Game", "Failed to open file for writing: %s", directory_path);
  885. save_char("create_pvp_error", "Failed to open file for writing");
  886. storage_file_free(file);
  887. furi_record_close(RECORD_STORAGE);
  888. furi_string_free(enemy_data);
  889. furi_string_free(player_stats);
  890. furi_string_free(username);
  891. furi_string_free(strength);
  892. furi_string_free(health);
  893. furi_string_free(attack_timer);
  894. return false;
  895. }
  896. size_t data_size = furi_string_size(enemy_data);
  897. if (storage_file_write(file, furi_string_get_cstr(enemy_data), data_size) != data_size)
  898. {
  899. FURI_LOG_E("Game", "Failed to write enemy_data");
  900. save_char("create_pvp_error", "Failed to write enemy_data");
  901. }
  902. storage_file_close(file);
  903. furi_string_free(enemy_data);
  904. furi_string_free(player_stats);
  905. furi_string_free(username);
  906. furi_string_free(strength);
  907. furi_string_free(health);
  908. furi_string_free(attack_timer);
  909. // player is found so break
  910. break;
  911. }
  912. return true;
  913. }
  914. size_t game_lobby_count(FlipperHTTP *fhttp, FuriString *lobby)
  915. {
  916. if (!fhttp)
  917. {
  918. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  919. return -1;
  920. }
  921. if (!lobby)
  922. {
  923. FURI_LOG_E(TAG, "Lobby details are NULL");
  924. return -1;
  925. }
  926. // check if the player is in the lobby
  927. FuriString *player_count = get_json_value_furi("player_count", lobby);
  928. if (!player_count)
  929. {
  930. FURI_LOG_E(TAG, "Failed to get player count");
  931. return -1;
  932. }
  933. const size_t count = atoi(furi_string_get_cstr(player_count));
  934. furi_string_free(player_count);
  935. return count;
  936. }
  937. bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby)
  938. {
  939. if (!fhttp)
  940. {
  941. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  942. return false;
  943. }
  944. if (!lobby)
  945. {
  946. FURI_LOG_E(TAG, "Lobby details are NULL");
  947. return false;
  948. }
  949. // check if the player is in the lobby
  950. FuriString *is_in_game = get_json_value_furi("is_in_game", lobby);
  951. if (!is_in_game)
  952. {
  953. FURI_LOG_E(TAG, "Failed to get is_in_game");
  954. furi_string_free(is_in_game);
  955. return false;
  956. }
  957. const bool in_game = is_str(furi_string_get_cstr(is_in_game), "true");
  958. furi_string_free(is_in_game);
  959. return in_game;
  960. }
  961. static bool game_start_ws(FlipperHTTP *fhttp, char *lobby_name)
  962. {
  963. if (!fhttp)
  964. {
  965. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  966. return false;
  967. }
  968. if (!lobby_name || strlen(lobby_name) == 0)
  969. {
  970. FURI_LOG_E(TAG, "Lobby name is NULL or empty");
  971. return false;
  972. }
  973. fhttp->state = IDLE; // ensure it's set to IDLE for the next request
  974. char websocket_url[128];
  975. snprintf(websocket_url, sizeof(websocket_url), "ws://www.jblanked.com/ws/game/%s/", lobby_name);
  976. if (!flipper_http_websocket_start(fhttp, websocket_url, 80, "{\"Content-Type\":\"application/json\"}"))
  977. {
  978. FURI_LOG_E(TAG, "Failed to start websocket");
  979. return false;
  980. }
  981. fhttp->state = RECEIVING;
  982. while (fhttp->state != IDLE)
  983. {
  984. furi_delay_ms(100);
  985. }
  986. return true;
  987. }
  988. // this will free both the fhttp and lobby
  989. void game_start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context)
  990. {
  991. FlipWorldApp *app = (FlipWorldApp *)context;
  992. furi_check(app, "FlipWorldApp is NULL");
  993. // only thing left to do is create the enemy data and start the websocket session
  994. if (!game_create_pvp_enemy(lobby))
  995. {
  996. FURI_LOG_E(TAG, "Failed to create pvp enemy context.");
  997. easy_flipper_dialog("Error", "Failed to create pvp enemy context. Press BACK to return.");
  998. flipper_http_free(fhttp);
  999. furi_string_free(lobby);
  1000. return;
  1001. }
  1002. furi_string_free(lobby);
  1003. // start the websocket session
  1004. if (!game_start_ws(fhttp, lobby_list[lobby_index]))
  1005. {
  1006. FURI_LOG_E(TAG, "Failed to start websocket session");
  1007. easy_flipper_dialog("Error", "Failed to start websocket session. Press BACK to return.");
  1008. flipper_http_free(fhttp);
  1009. return;
  1010. }
  1011. flipper_http_free(fhttp);
  1012. // start the game thread
  1013. if (!game_thread_start(app))
  1014. {
  1015. FURI_LOG_E(TAG, "Failed to start game thread");
  1016. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1017. return;
  1018. }
  1019. };
  1020. void game_waiting_process(FlipperHTTP *fhttp, void *context)
  1021. {
  1022. FlipWorldApp *app = (FlipWorldApp *)context;
  1023. if (!app)
  1024. {
  1025. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1026. return;
  1027. }
  1028. if (!fhttp)
  1029. {
  1030. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1031. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1032. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  1033. return;
  1034. }
  1035. // fetch the lobby details
  1036. if (!game_fetch_lobby(fhttp, lobby_list[lobby_index]))
  1037. {
  1038. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  1039. flipper_http_free(fhttp);
  1040. easy_flipper_dialog("Error", "Failed to fetch lobby details. Press BACK to return.");
  1041. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  1042. return;
  1043. }
  1044. // load the lobby details
  1045. FuriString *lobby = flipper_http_load_from_file(fhttp->file_path);
  1046. if (!lobby)
  1047. {
  1048. FURI_LOG_E(TAG, "Failed to load lobby details");
  1049. flipper_http_free(fhttp);
  1050. easy_flipper_dialog("Error", "Failed to load lobby details. Press BACK to return.");
  1051. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  1052. return;
  1053. }
  1054. // get the player count
  1055. const size_t count = game_lobby_count(fhttp, lobby);
  1056. if (count == 2)
  1057. {
  1058. // break out of this and start the game
  1059. game_start_pvp(fhttp, lobby, app); // this will free both the fhttp and lobby
  1060. return;
  1061. }
  1062. furi_string_free(lobby);
  1063. }
  1064. void game_waiting_lobby(void *context)
  1065. {
  1066. FlipWorldApp *app = (FlipWorldApp *)context;
  1067. furi_check(app, "waiting_lobby: FlipWorldApp is NULL");
  1068. if (!game_start_waiting_thread(app))
  1069. {
  1070. FURI_LOG_E(TAG, "Failed to start waiting thread");
  1071. easy_flipper_dialog("Error", "Failed to start waiting thread. Press BACK to return.");
  1072. return;
  1073. }
  1074. free_message_view(app);
  1075. if (!alloc_message_view(app, MessageStateWaitingLobby))
  1076. {
  1077. FURI_LOG_E(TAG, "Failed to allocate message view");
  1078. return;
  1079. }
  1080. // finally, switch to the waiting lobby view
  1081. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1082. };