game.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. furi_check(app, "FlipWorldApp is NULL");
  577. free_all_views(app, true, true, false);
  578. // only need to check if they have 30k free (game needs about 12k currently)
  579. if (!is_enough_heap(30000, false))
  580. {
  581. const size_t min_free = memmgr_get_free_heap();
  582. char message[64];
  583. snprintf(message, sizeof(message), "Not enough heap memory.\nThere are %zu bytes free.", min_free);
  584. easy_flipper_dialog("Error", message);
  585. return;
  586. }
  587. // check if logged in
  588. if (is_logged_in() || is_logged_in_to_flip_social())
  589. {
  590. FlipperHTTP *fhttp = flipper_http_alloc();
  591. if (!fhttp)
  592. {
  593. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  594. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  595. return;
  596. }
  597. bool game_fetch_world_list_i()
  598. {
  599. return game_fetch_world_list(fhttp);
  600. }
  601. bool parse_world_list_i()
  602. {
  603. return fhttp->state != ISSUE;
  604. }
  605. bool game_fetch_player_stats_i()
  606. {
  607. return game_fetch_player_stats(fhttp);
  608. }
  609. if (!alloc_message_view(app, MessageStateLoading))
  610. {
  611. FURI_LOG_E(TAG, "Failed to allocate message view");
  612. return;
  613. }
  614. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  615. // Make the request
  616. if (game_mode_index != 1) // not GAME_MODE_PVP
  617. {
  618. 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))
  619. {
  620. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  621. flipper_http_free(fhttp);
  622. }
  623. else
  624. {
  625. flipper_http_free(fhttp);
  626. }
  627. if (!game_thread_start(app))
  628. {
  629. FURI_LOG_E(TAG, "Failed to start game thread");
  630. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  631. return;
  632. }
  633. }
  634. else
  635. {
  636. // load pvp info (this returns the lobbies available)
  637. bool fetch_pvp_lobbies()
  638. {
  639. // ensure flip_world directory exists
  640. char directory_path[128];
  641. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  642. Storage *storage = furi_record_open(RECORD_STORAGE);
  643. storage_common_mkdir(storage, directory_path);
  644. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp");
  645. storage_common_mkdir(storage, directory_path);
  646. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies");
  647. storage_common_mkdir(storage, directory_path);
  648. furi_record_close(RECORD_STORAGE);
  649. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/pvp_lobbies.json");
  650. storage_simply_remove_recursive(storage, fhttp->file_path); // ensure the file is empty
  651. fhttp->save_received_data = true;
  652. // 2 players max, 10 lobbies
  653. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/pvp/lobbies/2/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  654. }
  655. bool parse_pvp_lobbies()
  656. {
  657. free_submenu_other(app);
  658. if (!alloc_submenu_other(app, FlipWorldViewLobby))
  659. {
  660. FURI_LOG_E(TAG, "Failed to allocate lobby submenu");
  661. return false;
  662. }
  663. // add the lobbies to the submenu
  664. FuriString *lobbies = flipper_http_load_from_file(fhttp->file_path);
  665. if (!lobbies)
  666. {
  667. FURI_LOG_E(TAG, "Failed to load lobbies");
  668. return false;
  669. }
  670. // parse the lobbies
  671. for (uint32_t i = 0; i < 10; i++)
  672. {
  673. FuriString *lobby = get_json_array_value_furi("lobbies", i, lobbies);
  674. if (!lobby)
  675. {
  676. FURI_LOG_I(TAG, "No more lobbies");
  677. break;
  678. }
  679. FuriString *lobby_id = get_json_value_furi("id", lobby);
  680. if (!lobby_id)
  681. {
  682. FURI_LOG_E(TAG, "Failed to get lobby id");
  683. furi_string_free(lobby);
  684. return false;
  685. }
  686. // add the lobby to the submenu
  687. submenu_add_item(app->submenu_other, furi_string_get_cstr(lobby_id), FlipWorldSubmenuIndexLobby + i, callback_submenu_lobby_choices, app);
  688. // add the lobby to the list
  689. if (!easy_flipper_set_buffer(&lobby_list[i], 64))
  690. {
  691. FURI_LOG_E(TAG, "Failed to allocate lobby list");
  692. furi_string_free(lobby);
  693. furi_string_free(lobby_id);
  694. return false;
  695. }
  696. snprintf(lobby_list[i], 64, "%s", furi_string_get_cstr(lobby_id));
  697. furi_string_free(lobby);
  698. furi_string_free(lobby_id);
  699. }
  700. furi_string_free(lobbies);
  701. return true;
  702. }
  703. // load pvp lobbies and player stats
  704. 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))
  705. {
  706. // unlike the pve/story, receiving data is necessary
  707. // so send the user back to the main menu if it fails
  708. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  709. easy_flipper_dialog("Error", "Failed to make request. Press BACK to return.");
  710. view_dispatcher_switch_to_view(app->view_dispatcher,
  711. FlipWorldViewSubmenu);
  712. flipper_http_free(fhttp);
  713. }
  714. else
  715. {
  716. flipper_http_free(fhttp);
  717. }
  718. // switch to the lobby submenu
  719. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  720. }
  721. }
  722. else
  723. {
  724. game_switch_to_view(app);
  725. }
  726. }
  727. bool game_fetch_lobby(FlipperHTTP *fhttp, char *lobby_name)
  728. {
  729. if (!fhttp)
  730. {
  731. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  732. return false;
  733. }
  734. if (!lobby_name || strlen(lobby_name) == 0)
  735. {
  736. FURI_LOG_E(TAG, "Lobby name is NULL or empty");
  737. return false;
  738. }
  739. char username[64];
  740. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  741. {
  742. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  743. return false;
  744. }
  745. // send the request to fetch the lobby details, with player_username
  746. char url[128];
  747. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/get/%s/%s/", lobby_name, username);
  748. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies/%s.json", lobby_name);
  749. fhttp->save_received_data = true;
  750. if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL))
  751. {
  752. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  753. return false;
  754. }
  755. fhttp->state = RECEIVING;
  756. while (fhttp->state != IDLE)
  757. {
  758. furi_delay_ms(100);
  759. }
  760. return true;
  761. }
  762. bool game_join_lobby(FlipperHTTP *fhttp, char *lobby_name)
  763. {
  764. if (!fhttp)
  765. {
  766. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  767. return false;
  768. }
  769. if (!lobby_name || strlen(lobby_name) == 0)
  770. {
  771. FURI_LOG_E(TAG, "Lobby name is NULL or empty");
  772. return false;
  773. }
  774. char username[64];
  775. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  776. {
  777. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  778. return false;
  779. }
  780. char url[128];
  781. char payload[128];
  782. snprintf(payload, sizeof(payload), "{\"username\":\"%s\", \"game_id\":\"%s\"}", username, lobby_name);
  783. save_char("pvp_lobby_name", lobby_name); // save the lobby name
  784. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/join/");
  785. if (!flipper_http_request(fhttp, POST, url, "{\"Content-Type\":\"application/json\"}", payload))
  786. {
  787. FURI_LOG_E(TAG, "Failed to join lobby");
  788. return false;
  789. }
  790. fhttp->state = RECEIVING;
  791. while (fhttp->state != IDLE)
  792. {
  793. furi_delay_ms(100);
  794. }
  795. return true;
  796. }
  797. static bool game_create_pvp_enemy(FuriString *lobby_details)
  798. {
  799. if (!lobby_details)
  800. {
  801. FURI_LOG_E(TAG, "Failed to load lobby details");
  802. return false;
  803. }
  804. char current_user[64];
  805. if (!load_char("Flip-Social-Username", current_user, sizeof(current_user)))
  806. {
  807. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  808. save_char("create_pvp_error", "Failed to load Flip-Social-Username");
  809. return false;
  810. }
  811. for (uint8_t i = 0; i < 2; i++)
  812. {
  813. // parse the lobby details
  814. FuriString *player_stats = get_json_array_value_furi("player_stats", i, lobby_details);
  815. if (!player_stats)
  816. {
  817. FURI_LOG_E(TAG, "Failed to get player stats");
  818. save_char("create_pvp_error", "Failed to get player stats array");
  819. return false;
  820. }
  821. // available keys from player_stats
  822. FuriString *username = get_json_value_furi("username", player_stats);
  823. if (!username)
  824. {
  825. FURI_LOG_E(TAG, "Failed to get username");
  826. save_char("create_pvp_error", "Failed to get username");
  827. furi_string_free(player_stats);
  828. return false;
  829. }
  830. // check if the username is the same as the current user
  831. if (is_str(furi_string_get_cstr(username), current_user))
  832. {
  833. furi_string_free(player_stats);
  834. furi_string_free(username);
  835. continue; // skip the current user
  836. }
  837. FuriString *strength = get_json_value_furi("strength", player_stats);
  838. FuriString *health = get_json_value_furi("health", player_stats);
  839. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  840. if (!strength || !health || !attack_timer)
  841. {
  842. FURI_LOG_E(TAG, "Failed to get player stats");
  843. save_char("create_pvp_error", "Failed to get player stats");
  844. furi_string_free(player_stats);
  845. furi_string_free(username);
  846. if (strength)
  847. furi_string_free(strength);
  848. if (health)
  849. furi_string_free(health);
  850. if (attack_timer)
  851. furi_string_free(attack_timer);
  852. return false;
  853. }
  854. // create enemy data
  855. FuriString *enemy_data = furi_string_alloc();
  856. furi_string_printf(
  857. enemy_data,
  858. "{\"enemy_data\":[{\"id\":\"sword\",\"is_user\":\"true\",\"username\":\"%s\","
  859. "\"index\":0,\"start_position\":{\"x\":350,\"y\":210},\"end_position\":{\"x\":350,\"y\":210},"
  860. "\"move_timer\":1,\"speed\":1,\"attack_timer\":%f,\"strength\":%f,\"health\":%f}]}",
  861. furi_string_get_cstr(username),
  862. (double)atof_furi(attack_timer),
  863. (double)atof_furi(strength),
  864. (double)atof_furi(health));
  865. char directory_path[128];
  866. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  867. Storage *storage = furi_record_open(RECORD_STORAGE);
  868. storage_common_mkdir(storage, directory_path);
  869. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  870. storage_common_mkdir(storage, directory_path);
  871. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/pvp_world");
  872. storage_common_mkdir(storage, directory_path);
  873. furi_record_close(RECORD_STORAGE);
  874. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/pvp_world/pvp_world_enemy_data.json");
  875. // remove the enemy_data file if it exists
  876. storage_simply_remove_recursive(storage, directory_path);
  877. File *file = storage_file_alloc(storage);
  878. if (!storage_file_open(file, directory_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  879. {
  880. FURI_LOG_E("Game", "Failed to open file for writing: %s", directory_path);
  881. save_char("create_pvp_error", "Failed to open file for writing");
  882. storage_file_free(file);
  883. furi_record_close(RECORD_STORAGE);
  884. furi_string_free(enemy_data);
  885. furi_string_free(player_stats);
  886. furi_string_free(username);
  887. furi_string_free(strength);
  888. furi_string_free(health);
  889. furi_string_free(attack_timer);
  890. return false;
  891. }
  892. size_t data_size = furi_string_size(enemy_data);
  893. if (storage_file_write(file, furi_string_get_cstr(enemy_data), data_size) != data_size)
  894. {
  895. FURI_LOG_E("Game", "Failed to write enemy_data");
  896. save_char("create_pvp_error", "Failed to write enemy_data");
  897. }
  898. storage_file_close(file);
  899. furi_string_free(enemy_data);
  900. furi_string_free(player_stats);
  901. furi_string_free(username);
  902. furi_string_free(strength);
  903. furi_string_free(health);
  904. furi_string_free(attack_timer);
  905. // player is found so break
  906. break;
  907. }
  908. return true;
  909. }
  910. size_t game_lobby_count(FlipperHTTP *fhttp, FuriString *lobby)
  911. {
  912. if (!fhttp)
  913. {
  914. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  915. return -1;
  916. }
  917. if (!lobby)
  918. {
  919. FURI_LOG_E(TAG, "Lobby details are NULL");
  920. return -1;
  921. }
  922. // check if the player is in the lobby
  923. FuriString *player_count = get_json_value_furi("player_count", lobby);
  924. if (!player_count)
  925. {
  926. FURI_LOG_E(TAG, "Failed to get player count");
  927. return -1;
  928. }
  929. const size_t count = atoi(furi_string_get_cstr(player_count));
  930. furi_string_free(player_count);
  931. return count;
  932. }
  933. bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby)
  934. {
  935. if (!fhttp)
  936. {
  937. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  938. return false;
  939. }
  940. if (!lobby)
  941. {
  942. FURI_LOG_E(TAG, "Lobby details are NULL");
  943. return false;
  944. }
  945. // check if the player is in the lobby
  946. FuriString *is_in_game = get_json_value_furi("is_in_game", lobby);
  947. if (!is_in_game)
  948. {
  949. FURI_LOG_E(TAG, "Failed to get is_in_game");
  950. furi_string_free(is_in_game);
  951. return false;
  952. }
  953. const bool in_game = is_str(furi_string_get_cstr(is_in_game), "true");
  954. furi_string_free(is_in_game);
  955. return in_game;
  956. }
  957. static bool game_start_ws(FlipperHTTP *fhttp, char *lobby_name)
  958. {
  959. if (!fhttp)
  960. {
  961. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  962. return false;
  963. }
  964. if (!lobby_name || strlen(lobby_name) == 0)
  965. {
  966. FURI_LOG_E(TAG, "Lobby name is NULL or empty");
  967. return false;
  968. }
  969. fhttp->state = IDLE; // ensure it's set to IDLE for the next request
  970. char websocket_url[128];
  971. snprintf(websocket_url, sizeof(websocket_url), "ws://www.jblanked.com/ws/game/%s/", lobby_name);
  972. if (!flipper_http_websocket_start(fhttp, websocket_url, 80, "{\"Content-Type\":\"application/json\"}"))
  973. {
  974. FURI_LOG_E(TAG, "Failed to start websocket");
  975. return false;
  976. }
  977. fhttp->state = RECEIVING;
  978. while (fhttp->state != IDLE)
  979. {
  980. furi_delay_ms(100);
  981. }
  982. return true;
  983. }
  984. // this will free both the fhttp and lobby
  985. void game_start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context)
  986. {
  987. FlipWorldApp *app = (FlipWorldApp *)context;
  988. furi_check(app, "FlipWorldApp is NULL");
  989. // only thing left to do is create the enemy data and start the websocket session
  990. if (!game_create_pvp_enemy(lobby))
  991. {
  992. FURI_LOG_E(TAG, "Failed to create pvp enemy context.");
  993. easy_flipper_dialog("Error", "Failed to create pvp enemy context. Press BACK to return.");
  994. flipper_http_free(fhttp);
  995. furi_string_free(lobby);
  996. return;
  997. }
  998. furi_string_free(lobby);
  999. // start the websocket session
  1000. if (!game_start_ws(fhttp, lobby_list[lobby_index]))
  1001. {
  1002. FURI_LOG_E(TAG, "Failed to start websocket session");
  1003. easy_flipper_dialog("Error", "Failed to start websocket session. Press BACK to return.");
  1004. flipper_http_free(fhttp);
  1005. return;
  1006. }
  1007. flipper_http_free(fhttp);
  1008. // start the game thread
  1009. if (!game_thread_start(app))
  1010. {
  1011. FURI_LOG_E(TAG, "Failed to start game thread");
  1012. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1013. return;
  1014. }
  1015. };
  1016. void game_waiting_process(FlipperHTTP *fhttp, void *context)
  1017. {
  1018. FlipWorldApp *app = (FlipWorldApp *)context;
  1019. furi_check(app, "FlipWorldApp is NULL");
  1020. if (!fhttp)
  1021. {
  1022. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1023. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1024. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  1025. return;
  1026. }
  1027. // fetch the lobby details
  1028. if (!game_fetch_lobby(fhttp, lobby_list[lobby_index]))
  1029. {
  1030. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  1031. flipper_http_free(fhttp);
  1032. easy_flipper_dialog("Error", "Failed to fetch lobby details. Press BACK to return.");
  1033. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  1034. return;
  1035. }
  1036. // load the lobby details
  1037. FuriString *lobby = flipper_http_load_from_file(fhttp->file_path);
  1038. if (!lobby)
  1039. {
  1040. FURI_LOG_E(TAG, "Failed to load lobby details");
  1041. flipper_http_free(fhttp);
  1042. easy_flipper_dialog("Error", "Failed to load lobby details. Press BACK to return.");
  1043. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
  1044. return;
  1045. }
  1046. // get the player count
  1047. const size_t count = game_lobby_count(fhttp, lobby);
  1048. if (count == 2)
  1049. {
  1050. // break out of this and start the game
  1051. game_start_pvp(fhttp, lobby, app); // this will free both the fhttp and lobby
  1052. return;
  1053. }
  1054. furi_string_free(lobby);
  1055. }
  1056. void game_waiting_lobby(void *context)
  1057. {
  1058. FlipWorldApp *app = (FlipWorldApp *)context;
  1059. furi_check(app, "waiting_lobby: FlipWorldApp is NULL");
  1060. if (!game_start_waiting_thread(app))
  1061. {
  1062. FURI_LOG_E(TAG, "Failed to start waiting thread");
  1063. easy_flipper_dialog("Error", "Failed to start waiting thread. Press BACK to return.");
  1064. return;
  1065. }
  1066. free_message_view(app);
  1067. if (!alloc_message_view(app, MessageStateWaitingLobby))
  1068. {
  1069. FURI_LOG_E(TAG, "Failed to allocate message view");
  1070. return;
  1071. }
  1072. // finally, switch to the waiting lobby view
  1073. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1074. };