callback.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  1. #include <callback/callback.h>
  2. #include "engine/engine.h"
  3. #include "engine/game_engine.h"
  4. #include "engine/game_manager_i.h"
  5. #include "engine/level_i.h"
  6. #include "engine/entity_i.h"
  7. #include "game/storage.h"
  8. #include "alloc/alloc.h"
  9. // Below added by Derek Jamison
  10. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  11. #ifdef DEVELOPMENT
  12. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  13. #define DEV_CRASH() furi_crash()
  14. #else
  15. #define FURI_LOG_DEV(tag, format, ...)
  16. #define DEV_CRASH()
  17. #endif
  18. static void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
  19. {
  20. UNUSED(engine);
  21. GameManager *game_manager = context;
  22. game_manager_input_set(game_manager, input);
  23. game_manager_update(game_manager);
  24. game_manager_render(game_manager, canvas);
  25. }
  26. static int32_t game_app(void *p)
  27. {
  28. UNUSED(p);
  29. GameManager *game_manager = game_manager_alloc();
  30. if (!game_manager)
  31. {
  32. FURI_LOG_E("Game", "Failed to allocate game manager");
  33. return -1;
  34. }
  35. // Setup game engine settings...
  36. GameEngineSettings settings = game_engine_settings_init();
  37. settings.target_fps = atof_(fps_choices_str[fps_index]);
  38. settings.show_fps = game.show_fps;
  39. settings.always_backlight = strstr(yes_or_no_choices[screen_always_on_index], "Yes") != NULL;
  40. settings.frame_callback = frame_cb;
  41. settings.context = game_manager;
  42. GameEngine *engine = game_engine_alloc(settings);
  43. if (!engine)
  44. {
  45. FURI_LOG_E("Game", "Failed to allocate game engine");
  46. game_manager_free(game_manager);
  47. return -1;
  48. }
  49. game_manager_engine_set(game_manager, engine);
  50. // Allocate custom game context if needed
  51. void *game_context = NULL;
  52. if (game.context_size > 0)
  53. {
  54. game_context = malloc(game.context_size);
  55. game_manager_game_context_set(game_manager, game_context);
  56. }
  57. // Start the game
  58. game.start(game_manager, game_context);
  59. // 1) Run the engine
  60. game_engine_run(engine);
  61. // 2) Stop the game FIRST, so it can do any internal cleanup
  62. game.stop(game_context);
  63. // 3) Now free the engine
  64. game_engine_free(engine);
  65. // 4) Now free the manager
  66. game_manager_free(game_manager);
  67. // 5) Finally, free your custom context if it was allocated
  68. if (game_context)
  69. {
  70. free(game_context);
  71. }
  72. // 6) Check for leftover entities
  73. int32_t entities = entities_get_count();
  74. if (entities != 0)
  75. {
  76. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. static void error_draw(Canvas *canvas, DataLoaderModel *model)
  82. {
  83. if (canvas == NULL)
  84. {
  85. FURI_LOG_E(TAG, "error_draw - canvas is NULL");
  86. DEV_CRASH();
  87. return;
  88. }
  89. if (model->fhttp->last_response != NULL)
  90. {
  91. if (strstr(model->fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  92. {
  93. canvas_clear(canvas);
  94. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  95. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  96. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  97. }
  98. else if (strstr(model->fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  99. {
  100. canvas_clear(canvas);
  101. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  102. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  103. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  104. }
  105. else if (strstr(model->fhttp->last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  106. {
  107. canvas_clear(canvas);
  108. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  109. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  110. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  111. }
  112. else if (strstr(model->fhttp->last_response, "[PONG]") != NULL)
  113. {
  114. canvas_clear(canvas);
  115. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  116. }
  117. else
  118. {
  119. canvas_clear(canvas);
  120. FURI_LOG_E(TAG, "Received an error: %s", model->fhttp->last_response);
  121. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  122. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  123. }
  124. }
  125. else
  126. {
  127. canvas_clear(canvas);
  128. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  129. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  130. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  131. }
  132. }
  133. static bool alloc_message_view(void *context, MessageState state);
  134. static bool alloc_text_input_view(void *context, char *title);
  135. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  136. //
  137. static void callback_submenu_lobby_choices(void *context, uint32_t index);
  138. //
  139. static void wifi_settings_select(void *context, uint32_t index);
  140. static void updated_wifi_ssid(void *context);
  141. static void updated_wifi_pass(void *context);
  142. static void updated_username(void *context);
  143. static void updated_password(void *context);
  144. //
  145. static void fps_change(VariableItem *item);
  146. static void game_settings_select(void *context, uint32_t index);
  147. static void user_settings_select(void *context, uint32_t index);
  148. static void screen_on_change(VariableItem *item);
  149. static void sound_on_change(VariableItem *item);
  150. static void vibration_on_change(VariableItem *item);
  151. static void player_on_change(VariableItem *item);
  152. static void vgm_x_change(VariableItem *item);
  153. static void vgm_y_change(VariableItem *item);
  154. uint32_t callback_to_submenu(void *context)
  155. {
  156. UNUSED(context);
  157. return FlipWorldViewSubmenu;
  158. }
  159. static uint32_t callback_to_wifi_settings(void *context)
  160. {
  161. UNUSED(context);
  162. return FlipWorldViewVariableItemList;
  163. }
  164. static uint32_t callback_to_settings(void *context)
  165. {
  166. UNUSED(context);
  167. return FlipWorldViewSettings;
  168. }
  169. static void message_draw_callback(Canvas *canvas, void *model)
  170. {
  171. MessageModel *message_model = model;
  172. canvas_clear(canvas);
  173. if (message_model->message_state == MessageStateAbout)
  174. {
  175. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  176. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  177. canvas_draw_str(canvas, 0, 20, "Dev: JBlanked, codeallnight");
  178. canvas_draw_str(canvas, 0, 30, "GFX: the1anonlypr3");
  179. canvas_draw_str(canvas, 0, 40, "github.com/jblanked/FlipWorld");
  180. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  181. }
  182. else if (message_model->message_state == MessageStateLoading)
  183. {
  184. canvas_set_font(canvas, FontPrimary);
  185. if (game_mode_index != 1)
  186. {
  187. canvas_draw_str_aligned(canvas, 64, 0, AlignCenter, AlignTop, "Starting FlipWorld");
  188. canvas_set_font(canvas, FontSecondary);
  189. canvas_draw_str(canvas, 0, 50, "Please wait while your");
  190. canvas_draw_str(canvas, 0, 60, "game is started.");
  191. }
  192. else
  193. {
  194. canvas_draw_str_aligned(canvas, 64, 0, AlignCenter, AlignTop, "Loading Lobbies");
  195. canvas_set_font(canvas, FontSecondary);
  196. canvas_draw_str(canvas, 0, 60, "Please wait....");
  197. }
  198. }
  199. }
  200. // alloc
  201. static bool alloc_message_view(void *context, MessageState state)
  202. {
  203. FlipWorldApp *app = (FlipWorldApp *)context;
  204. if (!app)
  205. {
  206. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  207. return false;
  208. }
  209. if (!app->view_message)
  210. {
  211. if (!easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, message_draw_callback, NULL, (state == MessageStateLoading) ? NULL : callback_to_submenu, &app->view_dispatcher, app))
  212. {
  213. return false;
  214. }
  215. if (!app->view_message)
  216. {
  217. return false;
  218. }
  219. view_allocate_model(app->view_message, ViewModelTypeLockFree, sizeof(MessageModel));
  220. MessageModel *model = view_get_model(app->view_message);
  221. model->message_state = state;
  222. }
  223. return true;
  224. }
  225. static bool alloc_text_input_view(void *context, char *title)
  226. {
  227. FlipWorldApp *app = (FlipWorldApp *)context;
  228. if (!app)
  229. {
  230. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  231. return false;
  232. }
  233. if (!title)
  234. {
  235. FURI_LOG_E(TAG, "Title is NULL");
  236. return false;
  237. }
  238. app->text_input_buffer_size = 64;
  239. if (!app->text_input_buffer)
  240. {
  241. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  242. {
  243. return false;
  244. }
  245. }
  246. if (!app->text_input_temp_buffer)
  247. {
  248. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  249. {
  250. return false;
  251. }
  252. }
  253. if (!app->text_input)
  254. {
  255. if (!easy_flipper_set_uart_text_input(
  256. &app->text_input,
  257. FlipWorldViewTextInput,
  258. title,
  259. app->text_input_temp_buffer,
  260. app->text_input_buffer_size,
  261. is_str(title, "SSID") ? updated_wifi_ssid : is_str(title, "Password") ? updated_wifi_pass
  262. : is_str(title, "Username-Login") ? updated_username
  263. : updated_password,
  264. callback_to_wifi_settings,
  265. &app->view_dispatcher,
  266. app))
  267. {
  268. return false;
  269. }
  270. if (!app->text_input)
  271. {
  272. return false;
  273. }
  274. char ssid[64];
  275. char pass[64];
  276. char username[64];
  277. char password[64];
  278. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  279. {
  280. if (is_str(title, "SSID"))
  281. {
  282. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  283. }
  284. else if (is_str(title, "Password"))
  285. {
  286. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  287. }
  288. else if (is_str(title, "Username-Login"))
  289. {
  290. strncpy(app->text_input_temp_buffer, username, app->text_input_buffer_size);
  291. }
  292. else if (is_str(title, "Password-Login"))
  293. {
  294. strncpy(app->text_input_temp_buffer, password, app->text_input_buffer_size);
  295. }
  296. }
  297. }
  298. return true;
  299. }
  300. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  301. {
  302. FlipWorldApp *app = (FlipWorldApp *)context;
  303. if (!app)
  304. {
  305. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  306. return false;
  307. }
  308. char ssid[64];
  309. char pass[64];
  310. char username[64];
  311. char password[64];
  312. if (!app->variable_item_list)
  313. {
  314. switch (view_id)
  315. {
  316. case FlipWorldSubmenuIndexWiFiSettings:
  317. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_select, callback_to_settings, &app->view_dispatcher, app))
  318. {
  319. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  320. return false;
  321. }
  322. if (!app->variable_item_list)
  323. {
  324. FURI_LOG_E(TAG, "Variable item list is NULL");
  325. return false;
  326. }
  327. if (!app->variable_item_wifi_ssid)
  328. {
  329. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  330. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  331. }
  332. if (!app->variable_item_wifi_pass)
  333. {
  334. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  335. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  336. }
  337. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  338. {
  339. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  340. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  341. save_char("WiFi-SSID", ssid);
  342. save_char("WiFi-Password", pass);
  343. save_char("Flip-Social-Username", username);
  344. save_char("Flip-Social-Password", password);
  345. }
  346. break;
  347. case FlipWorldSubmenuIndexGameSettings:
  348. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_select, callback_to_settings, &app->view_dispatcher, app))
  349. {
  350. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  351. return false;
  352. }
  353. if (!app->variable_item_list)
  354. {
  355. FURI_LOG_E(TAG, "Variable item list is NULL");
  356. return false;
  357. }
  358. if (!app->variable_item_game_download_world)
  359. {
  360. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  361. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  362. }
  363. if (!app->variable_item_game_player_sprite)
  364. {
  365. app->variable_item_game_player_sprite = variable_item_list_add(app->variable_item_list, "Weapon", 4, player_on_change, NULL);
  366. variable_item_set_current_value_index(app->variable_item_game_player_sprite, 1);
  367. variable_item_set_current_value_text(app->variable_item_game_player_sprite, player_sprite_choices[1]);
  368. }
  369. if (!app->variable_item_game_fps)
  370. {
  371. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, fps_change, NULL);
  372. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  373. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[0]);
  374. }
  375. if (!app->variable_item_game_vgm_x)
  376. {
  377. app->variable_item_game_vgm_x = variable_item_list_add(app->variable_item_list, "VGM Horizontal", 12, vgm_x_change, NULL);
  378. variable_item_set_current_value_index(app->variable_item_game_vgm_x, 2);
  379. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[2]);
  380. }
  381. if (!app->variable_item_game_vgm_y)
  382. {
  383. app->variable_item_game_vgm_y = variable_item_list_add(app->variable_item_list, "VGM Vertical", 12, vgm_y_change, NULL);
  384. variable_item_set_current_value_index(app->variable_item_game_vgm_y, 2);
  385. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[2]);
  386. }
  387. if (!app->variable_item_game_screen_always_on)
  388. {
  389. app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, screen_on_change, NULL);
  390. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  391. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[1]);
  392. }
  393. if (!app->variable_item_game_sound_on)
  394. {
  395. app->variable_item_game_sound_on = variable_item_list_add(app->variable_item_list, "Sound On?", 2, sound_on_change, NULL);
  396. variable_item_set_current_value_index(app->variable_item_game_sound_on, 0);
  397. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[0]);
  398. }
  399. if (!app->variable_item_game_vibration_on)
  400. {
  401. app->variable_item_game_vibration_on = variable_item_list_add(app->variable_item_list, "Vibration On?", 2, vibration_on_change, NULL);
  402. variable_item_set_current_value_index(app->variable_item_game_vibration_on, 0);
  403. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[0]);
  404. }
  405. char _game_player_sprite[8];
  406. if (load_char("Game-Player-Sprite", _game_player_sprite, sizeof(_game_player_sprite)))
  407. {
  408. int index = is_str(_game_player_sprite, "naked") ? 0 : is_str(_game_player_sprite, "sword") ? 1
  409. : is_str(_game_player_sprite, "axe") ? 2
  410. : is_str(_game_player_sprite, "bow") ? 3
  411. : 0;
  412. variable_item_set_current_value_index(app->variable_item_game_player_sprite, index);
  413. variable_item_set_current_value_text(
  414. app->variable_item_game_player_sprite,
  415. is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  416. }
  417. char _game_fps[8];
  418. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  419. {
  420. int index = is_str(_game_fps, "30") ? 0 : is_str(_game_fps, "60") ? 1
  421. : is_str(_game_fps, "120") ? 2
  422. : is_str(_game_fps, "240") ? 3
  423. : 0;
  424. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[index]);
  425. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  426. }
  427. char _game_vgm_x[8];
  428. if (load_char("Game-VGM-X", _game_vgm_x, sizeof(_game_vgm_x)))
  429. {
  430. int vgm_x = atoi(_game_vgm_x);
  431. int index = vgm_x == -2 ? 0 : vgm_x == -1 ? 1
  432. : vgm_x == 0 ? 2
  433. : vgm_x == 1 ? 3
  434. : vgm_x == 2 ? 4
  435. : vgm_x == 3 ? 5
  436. : vgm_x == 4 ? 6
  437. : vgm_x == 5 ? 7
  438. : vgm_x == 6 ? 8
  439. : vgm_x == 7 ? 9
  440. : vgm_x == 8 ? 10
  441. : vgm_x == 9 ? 11
  442. : vgm_x == 10 ? 12
  443. : 2;
  444. variable_item_set_current_value_index(app->variable_item_game_vgm_x, index);
  445. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[index]);
  446. }
  447. char _game_vgm_y[8];
  448. if (load_char("Game-VGM-Y", _game_vgm_y, sizeof(_game_vgm_y)))
  449. {
  450. int vgm_y = atoi(_game_vgm_y);
  451. int index = vgm_y == -2 ? 0 : vgm_y == -1 ? 1
  452. : vgm_y == 0 ? 2
  453. : vgm_y == 1 ? 3
  454. : vgm_y == 2 ? 4
  455. : vgm_y == 3 ? 5
  456. : vgm_y == 4 ? 6
  457. : vgm_y == 5 ? 7
  458. : vgm_y == 6 ? 8
  459. : vgm_y == 7 ? 9
  460. : vgm_y == 8 ? 10
  461. : vgm_y == 9 ? 11
  462. : vgm_y == 10 ? 12
  463. : 2;
  464. variable_item_set_current_value_index(app->variable_item_game_vgm_y, index);
  465. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[index]);
  466. }
  467. char _game_screen_always_on[8];
  468. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  469. {
  470. int index = is_str(_game_screen_always_on, "No") ? 0 : is_str(_game_screen_always_on, "Yes") ? 1
  471. : 0;
  472. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[index]);
  473. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  474. }
  475. char _game_sound_on[8];
  476. if (load_char("Game-Sound-On", _game_sound_on, sizeof(_game_sound_on)))
  477. {
  478. int index = is_str(_game_sound_on, "No") ? 0 : is_str(_game_sound_on, "Yes") ? 1
  479. : 0;
  480. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[index]);
  481. variable_item_set_current_value_index(app->variable_item_game_sound_on, index);
  482. }
  483. char _game_vibration_on[8];
  484. if (load_char("Game-Vibration-On", _game_vibration_on, sizeof(_game_vibration_on)))
  485. {
  486. int index = is_str(_game_vibration_on, "No") ? 0 : is_str(_game_vibration_on, "Yes") ? 1
  487. : 0;
  488. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[index]);
  489. variable_item_set_current_value_index(app->variable_item_game_vibration_on, index);
  490. }
  491. break;
  492. case FlipWorldSubmenuIndexUserSettings:
  493. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_select, callback_to_settings, &app->view_dispatcher, app))
  494. {
  495. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  496. return false;
  497. }
  498. if (!app->variable_item_list)
  499. {
  500. FURI_LOG_E(TAG, "Variable item list is NULL");
  501. return false;
  502. }
  503. // if logged in, show profile info, otherwise show login/register
  504. if (is_logged_in() || is_logged_in_to_flip_social())
  505. {
  506. if (!app->variable_item_user_username)
  507. {
  508. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  509. variable_item_set_current_value_text(app->variable_item_user_username, "");
  510. }
  511. if (!app->variable_item_user_password)
  512. {
  513. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  514. variable_item_set_current_value_text(app->variable_item_user_password, "");
  515. }
  516. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  517. {
  518. variable_item_set_current_value_text(app->variable_item_user_username, username);
  519. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  520. }
  521. }
  522. else
  523. {
  524. if (!app->variable_item_user_username)
  525. {
  526. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  527. variable_item_set_current_value_text(app->variable_item_user_username, "");
  528. }
  529. if (!app->variable_item_user_password)
  530. {
  531. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  532. variable_item_set_current_value_text(app->variable_item_user_password, "");
  533. }
  534. }
  535. break;
  536. }
  537. }
  538. return true;
  539. }
  540. static bool alloc_submenu_settings(void *context)
  541. {
  542. FlipWorldApp *app = (FlipWorldApp *)context;
  543. if (!app)
  544. {
  545. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  546. return false;
  547. }
  548. if (!app->submenu_settings)
  549. {
  550. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  551. {
  552. return false;
  553. }
  554. if (!app->submenu_settings)
  555. {
  556. return false;
  557. }
  558. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  559. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  560. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  561. }
  562. return true;
  563. }
  564. static bool alloc_submenu_lobby(void *context)
  565. {
  566. FlipWorldApp *app = (FlipWorldApp *)context;
  567. if (!app)
  568. {
  569. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  570. return false;
  571. }
  572. if (!app->submenu_lobby)
  573. {
  574. if (!easy_flipper_set_submenu(&app->submenu_lobby, FlipWorldViewLobby, "Lobbies", callback_to_submenu, &app->view_dispatcher))
  575. {
  576. return false;
  577. }
  578. if (!app->submenu_lobby)
  579. {
  580. return false;
  581. }
  582. }
  583. return true;
  584. }
  585. static char *lobby_list[10];
  586. static void free_submenu_lobby(void *context)
  587. {
  588. FlipWorldApp *app = (FlipWorldApp *)context;
  589. if (!app)
  590. {
  591. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  592. return;
  593. }
  594. if (app->submenu_lobby)
  595. {
  596. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewLobby);
  597. submenu_free(app->submenu_lobby);
  598. app->submenu_lobby = NULL;
  599. }
  600. for (int i = 0; i < 10; i++)
  601. {
  602. if (lobby_list[i])
  603. {
  604. free(lobby_list[i]);
  605. lobby_list[i] = NULL;
  606. }
  607. }
  608. }
  609. // free
  610. static void free_message_view(void *context)
  611. {
  612. FlipWorldApp *app = (FlipWorldApp *)context;
  613. if (!app)
  614. {
  615. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  616. return;
  617. }
  618. if (app->view_message)
  619. {
  620. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMessage);
  621. view_free(app->view_message);
  622. app->view_message = NULL;
  623. }
  624. }
  625. static void free_text_input_view(void *context)
  626. {
  627. FlipWorldApp *app = (FlipWorldApp *)context;
  628. if (!app)
  629. {
  630. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  631. return;
  632. }
  633. if (app->text_input)
  634. {
  635. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  636. uart_text_input_free(app->text_input);
  637. app->text_input = NULL;
  638. }
  639. if (app->text_input_buffer)
  640. {
  641. free(app->text_input_buffer);
  642. app->text_input_buffer = NULL;
  643. }
  644. if (app->text_input_temp_buffer)
  645. {
  646. free(app->text_input_temp_buffer);
  647. app->text_input_temp_buffer = NULL;
  648. }
  649. }
  650. static void free_variable_item_list(void *context)
  651. {
  652. FlipWorldApp *app = (FlipWorldApp *)context;
  653. if (!app)
  654. {
  655. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  656. return;
  657. }
  658. if (app->variable_item_list)
  659. {
  660. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  661. variable_item_list_free(app->variable_item_list);
  662. app->variable_item_list = NULL;
  663. }
  664. if (app->variable_item_wifi_ssid)
  665. {
  666. free(app->variable_item_wifi_ssid);
  667. app->variable_item_wifi_ssid = NULL;
  668. }
  669. if (app->variable_item_wifi_pass)
  670. {
  671. free(app->variable_item_wifi_pass);
  672. app->variable_item_wifi_pass = NULL;
  673. }
  674. if (app->variable_item_game_fps)
  675. {
  676. free(app->variable_item_game_fps);
  677. app->variable_item_game_fps = NULL;
  678. }
  679. if (app->variable_item_game_screen_always_on)
  680. {
  681. free(app->variable_item_game_screen_always_on);
  682. app->variable_item_game_screen_always_on = NULL;
  683. }
  684. if (app->variable_item_game_download_world)
  685. {
  686. free(app->variable_item_game_download_world);
  687. app->variable_item_game_download_world = NULL;
  688. }
  689. if (app->variable_item_game_sound_on)
  690. {
  691. free(app->variable_item_game_sound_on);
  692. app->variable_item_game_sound_on = NULL;
  693. }
  694. if (app->variable_item_game_vibration_on)
  695. {
  696. free(app->variable_item_game_vibration_on);
  697. app->variable_item_game_vibration_on = NULL;
  698. }
  699. if (app->variable_item_game_player_sprite)
  700. {
  701. free(app->variable_item_game_player_sprite);
  702. app->variable_item_game_player_sprite = NULL;
  703. }
  704. if (app->variable_item_game_vgm_x)
  705. {
  706. free(app->variable_item_game_vgm_x);
  707. app->variable_item_game_vgm_x = NULL;
  708. }
  709. if (app->variable_item_game_vgm_y)
  710. {
  711. free(app->variable_item_game_vgm_y);
  712. app->variable_item_game_vgm_y = NULL;
  713. }
  714. if (app->variable_item_user_username)
  715. {
  716. free(app->variable_item_user_username);
  717. app->variable_item_user_username = NULL;
  718. }
  719. if (app->variable_item_user_password)
  720. {
  721. free(app->variable_item_user_password);
  722. app->variable_item_user_password = NULL;
  723. }
  724. }
  725. static void free_submenu_settings(void *context)
  726. {
  727. FlipWorldApp *app = (FlipWorldApp *)context;
  728. if (!app)
  729. {
  730. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  731. return;
  732. }
  733. if (app->submenu_settings)
  734. {
  735. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  736. submenu_free(app->submenu_settings);
  737. app->submenu_settings = NULL;
  738. }
  739. }
  740. static FuriThread *game_thread;
  741. static bool game_thread_running = false;
  742. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  743. {
  744. FlipWorldApp *app = (FlipWorldApp *)context;
  745. if (!app)
  746. {
  747. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  748. return;
  749. }
  750. if (should_free_variable_item_list)
  751. {
  752. free_variable_item_list(app);
  753. }
  754. free_message_view(app);
  755. free_text_input_view(app);
  756. // free game thread
  757. if (game_thread_running)
  758. {
  759. game_thread_running = false;
  760. if (game_thread)
  761. {
  762. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  763. furi_thread_join(game_thread);
  764. furi_thread_free(game_thread);
  765. game_thread = NULL;
  766. }
  767. }
  768. if (should_free_submenu_settings)
  769. {
  770. free_submenu_settings(app);
  771. free_submenu_lobby(app);
  772. }
  773. }
  774. static bool fetch_world_list(FlipperHTTP *fhttp)
  775. {
  776. if (!fhttp)
  777. {
  778. FURI_LOG_E(TAG, "fhttp is NULL");
  779. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  780. return false;
  781. }
  782. // ensure flip_world directory exists
  783. char directory_path[128];
  784. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  785. Storage *storage = furi_record_open(RECORD_STORAGE);
  786. storage_common_mkdir(storage, directory_path);
  787. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  788. storage_common_mkdir(storage, directory_path);
  789. furi_record_close(RECORD_STORAGE);
  790. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  791. fhttp->save_received_data = true;
  792. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/v5/list/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  793. }
  794. // we will load the palyer stats from the API and save them
  795. // in player_spawn game method, it will load the player stats that we saved
  796. static bool fetch_player_stats(FlipperHTTP *fhttp)
  797. {
  798. if (!fhttp)
  799. {
  800. FURI_LOG_E(TAG, "fhttp is NULL");
  801. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  802. return false;
  803. }
  804. char username[64];
  805. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  806. {
  807. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  808. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  809. return false;
  810. }
  811. char url[128];
  812. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/game-stats/%s/", username);
  813. // ensure the folders exist
  814. char directory_path[128];
  815. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  816. Storage *storage = furi_record_open(RECORD_STORAGE);
  817. storage_common_mkdir(storage, directory_path);
  818. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  819. storage_common_mkdir(storage, directory_path);
  820. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player");
  821. storage_common_mkdir(storage, directory_path);
  822. furi_record_close(RECORD_STORAGE);
  823. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  824. fhttp->save_received_data = true;
  825. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  826. }
  827. // static bool fetch_app_update(FlipperHTTP *fhttp)
  828. // {
  829. // if (!fhttp)
  830. // {
  831. // FURI_LOG_E(TAG, "fhttp is NULL");
  832. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  833. // return false;
  834. // }
  835. // return flipper_http_get_request_with_headers(fhttp, "https://www.jblanked.com/flipper/api/app/last-updated/flip_world/", "{\"Content-Type\":\"application/json\"}");
  836. // }
  837. // static bool parse_app_update(FlipperHTTP *fhttp)
  838. // {
  839. // if (!fhttp)
  840. // {
  841. // FURI_LOG_E(TAG, "fhttp is NULL");
  842. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  843. // return false;
  844. // }
  845. // if (fhttp->last_response == NULL || strlen(fhttp->last_response) == 0)
  846. // {
  847. // FURI_LOG_E(TAG, "fhttp->last_response is NULL or empty");
  848. // easy_flipper_dialog("Error", "fhttp->last_response is NULL or empty. Press BACK to return.");
  849. // return false;
  850. // }
  851. // bool last_update_available = false;
  852. // char last_updated_old[32];
  853. // // load the previous last_updated
  854. // if (!load_char("last_updated", last_updated_old, sizeof(last_updated_old)))
  855. // {
  856. // FURI_LOG_E(TAG, "Failed to load last_updated");
  857. // // it's okay, we'll just update it
  858. // }
  859. // // save the new last_updated
  860. // save_char("last_updated", fhttp->last_response);
  861. // // compare the two
  862. // if (strlen(last_updated_old) == 0 || !is_str(last_updated_old, fhttp->last_response))
  863. // {
  864. // last_update_available = true;
  865. // }
  866. // if (last_update_available)
  867. // {
  868. // easy_flipper_dialog("Update Available", "An update is available. Press OK to update.");
  869. // return true;
  870. // }
  871. // else
  872. // {
  873. // easy_flipper_dialog("No Update Available", "No update is available. Press OK to continue.");
  874. // return false;
  875. // }
  876. // }
  877. static bool start_game_thread(void *context)
  878. {
  879. FlipWorldApp *app = (FlipWorldApp *)context;
  880. if (!app)
  881. {
  882. FURI_LOG_E(TAG, "app is NULL");
  883. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  884. return false;
  885. }
  886. // free lobby if it exists
  887. free_submenu_lobby(app);
  888. // free game thread
  889. if (game_thread_running)
  890. {
  891. game_thread_running = false;
  892. if (game_thread)
  893. {
  894. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  895. furi_thread_join(game_thread);
  896. furi_thread_free(game_thread);
  897. }
  898. }
  899. // start game thread
  900. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  901. if (!thread)
  902. {
  903. FURI_LOG_E(TAG, "Failed to allocate game thread");
  904. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  905. return false;
  906. }
  907. furi_thread_start(thread);
  908. game_thread = thread;
  909. game_thread_running = true;
  910. return true;
  911. }
  912. // combine register, login, and world list fetch into one function to switch to the loader view
  913. static bool _fetch_game(DataLoaderModel *model)
  914. {
  915. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  916. if (!app)
  917. {
  918. FURI_LOG_E(TAG, "app is NULL");
  919. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  920. return false;
  921. }
  922. if (model->request_index == 0)
  923. {
  924. // login
  925. char username[64];
  926. char password[64];
  927. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  928. {
  929. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  930. view_dispatcher_switch_to_view(app->view_dispatcher,
  931. FlipWorldViewSubmenu); // just go back to the main menu for now
  932. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  933. return false;
  934. }
  935. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  936. {
  937. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  938. view_dispatcher_switch_to_view(app->view_dispatcher,
  939. FlipWorldViewSubmenu); // just go back to the main menu for now
  940. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  941. return false;
  942. }
  943. char payload[256];
  944. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  945. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  946. }
  947. else if (model->request_index == 1)
  948. {
  949. // check if login was successful
  950. char is_logged_in[8];
  951. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  952. {
  953. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  954. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  955. view_dispatcher_switch_to_view(app->view_dispatcher,
  956. FlipWorldViewSubmenu); // just go back to the main menu for now
  957. return false;
  958. }
  959. if (is_str(is_logged_in, "false") && is_str(model->title, "Registering..."))
  960. {
  961. // register
  962. char username[64];
  963. char password[64];
  964. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  965. {
  966. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  967. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  968. view_dispatcher_switch_to_view(app->view_dispatcher,
  969. FlipWorldViewSubmenu); // just go back to the main menu for now
  970. return false;
  971. }
  972. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  973. {
  974. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  975. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  976. view_dispatcher_switch_to_view(app->view_dispatcher,
  977. FlipWorldViewSubmenu); // just go back to the main menu for now
  978. return false;
  979. }
  980. char payload[172];
  981. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  982. model->title = "Registering...";
  983. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  984. }
  985. else
  986. {
  987. model->title = "Fetching World List..";
  988. return fetch_world_list(model->fhttp);
  989. }
  990. }
  991. else if (model->request_index == 2)
  992. {
  993. model->title = "Fetching World List..";
  994. return fetch_world_list(model->fhttp);
  995. }
  996. else if (model->request_index == 3)
  997. {
  998. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  999. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  1000. if (!world_list)
  1001. {
  1002. view_dispatcher_switch_to_view(app->view_dispatcher,
  1003. FlipWorldViewSubmenu); // just go back to the main menu for now
  1004. FURI_LOG_E(TAG, "Failed to load world list");
  1005. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  1006. return false;
  1007. }
  1008. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  1009. if (!first_world)
  1010. {
  1011. view_dispatcher_switch_to_view(app->view_dispatcher,
  1012. FlipWorldViewSubmenu); // just go back to the main menu for now
  1013. FURI_LOG_E(TAG, "Failed to get first world");
  1014. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  1015. furi_string_free(world_list);
  1016. return false;
  1017. }
  1018. if (world_exists(furi_string_get_cstr(first_world)))
  1019. {
  1020. furi_string_free(world_list);
  1021. furi_string_free(first_world);
  1022. if (!start_game_thread(app))
  1023. {
  1024. FURI_LOG_E(TAG, "Failed to start game thread");
  1025. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1026. view_dispatcher_switch_to_view(app->view_dispatcher,
  1027. FlipWorldViewSubmenu); // just go back to the main menu for now
  1028. return "Failed to start game thread";
  1029. }
  1030. return true;
  1031. }
  1032. 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));
  1033. model->fhttp->save_received_data = true;
  1034. char url[128];
  1035. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v5/get/world/%s/", furi_string_get_cstr(first_world));
  1036. furi_string_free(world_list);
  1037. furi_string_free(first_world);
  1038. return flipper_http_request(model->fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  1039. }
  1040. FURI_LOG_E(TAG, "Unknown request index");
  1041. return false;
  1042. }
  1043. static char *_parse_game(DataLoaderModel *model)
  1044. {
  1045. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  1046. if (model->request_index == 0)
  1047. {
  1048. if (!model->fhttp->last_response)
  1049. {
  1050. save_char("is_logged_in", "false");
  1051. // Go back to the main menu
  1052. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  1053. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1054. return "Response is empty...";
  1055. }
  1056. // Check for successful conditions
  1057. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  1058. {
  1059. save_char("is_logged_in", "true");
  1060. model->title = "Login successful!";
  1061. model->title = "Fetching World List..";
  1062. return "Login successful!";
  1063. }
  1064. // Check if user not found
  1065. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  1066. {
  1067. save_char("is_logged_in", "false");
  1068. model->title = "Registering...";
  1069. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  1070. }
  1071. // If not success, not found, check length conditions
  1072. size_t resp_len = strlen(model->fhttp->last_response);
  1073. if (resp_len == 0 || resp_len > 127)
  1074. {
  1075. // Empty or too long means failed login
  1076. save_char("is_logged_in", "false");
  1077. // Go back to the main menu
  1078. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1079. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1080. return "Failed to login...";
  1081. }
  1082. // Handle any other unknown response as a failure
  1083. save_char("is_logged_in", "false");
  1084. // Go back to the main menu
  1085. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1086. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1087. return "Failed to login...";
  1088. }
  1089. else if (model->request_index == 1)
  1090. {
  1091. if (is_str(model->title, "Registering..."))
  1092. {
  1093. // check registration response
  1094. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  1095. {
  1096. save_char("is_logged_in", "true");
  1097. char username[64];
  1098. char password[64];
  1099. // load the username and password, then save them
  1100. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  1101. {
  1102. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  1103. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  1104. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1105. return "Failed to load Flip-Social-Username";
  1106. }
  1107. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  1108. {
  1109. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  1110. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  1111. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1112. return "Failed to load Flip-Social-Password";
  1113. }
  1114. // load wifi ssid,pass then save
  1115. char ssid[64];
  1116. char pass[64];
  1117. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1118. {
  1119. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  1120. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  1121. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1122. return "Failed to load WiFi-SSID";
  1123. }
  1124. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  1125. {
  1126. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  1127. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  1128. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1129. return "Failed to load WiFi-Password";
  1130. }
  1131. save_settings(ssid, pass, username, password);
  1132. model->title = "Fetching World List..";
  1133. return "Account created!";
  1134. }
  1135. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  1136. {
  1137. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  1138. view_dispatcher_switch_to_view(app->view_dispatcher,
  1139. FlipWorldViewSubmenu); // just go back to the main menu for now
  1140. return "Please enter your credentials.";
  1141. }
  1142. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  1143. {
  1144. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  1145. view_dispatcher_switch_to_view(app->view_dispatcher,
  1146. FlipWorldViewSubmenu); // just go back to the main menu for now
  1147. return "Username already exists.";
  1148. }
  1149. else
  1150. {
  1151. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  1152. view_dispatcher_switch_to_view(app->view_dispatcher,
  1153. FlipWorldViewSubmenu); // just go back to the main menu for now
  1154. return "Registration failed...";
  1155. }
  1156. }
  1157. else
  1158. {
  1159. if (!start_game_thread(app))
  1160. {
  1161. FURI_LOG_E(TAG, "Failed to start game thread");
  1162. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1163. view_dispatcher_switch_to_view(app->view_dispatcher,
  1164. FlipWorldViewSubmenu); // just go back to the main menu for now
  1165. return "Failed to start game thread";
  1166. }
  1167. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1168. }
  1169. }
  1170. else if (model->request_index == 2)
  1171. {
  1172. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1173. }
  1174. else if (model->request_index == 3)
  1175. {
  1176. if (!start_game_thread(app))
  1177. {
  1178. FURI_LOG_E(TAG, "Failed to start game thread");
  1179. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1180. view_dispatcher_switch_to_view(app->view_dispatcher,
  1181. FlipWorldViewSubmenu); // just go back to the main menu for now
  1182. return "Failed to start game thread";
  1183. }
  1184. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1185. }
  1186. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  1187. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1188. return "Unknown error";
  1189. }
  1190. static void switch_to_view_get_game(FlipWorldApp *app)
  1191. {
  1192. generic_switch_to_view(app, "Starting Game..", _fetch_game, _parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  1193. }
  1194. static void run(FlipWorldApp *app)
  1195. {
  1196. if (!app)
  1197. {
  1198. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1199. return;
  1200. }
  1201. free_all_views(app, true, true);
  1202. if (!is_enough_heap(10000))
  1203. {
  1204. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  1205. return;
  1206. }
  1207. // check if logged in
  1208. if (is_logged_in() || is_logged_in_to_flip_social())
  1209. {
  1210. FlipperHTTP *fhttp = flipper_http_alloc();
  1211. if (!fhttp)
  1212. {
  1213. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1214. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1215. return;
  1216. }
  1217. bool fetch_world_list_i()
  1218. {
  1219. return fetch_world_list(fhttp);
  1220. }
  1221. bool parse_world_list_i()
  1222. {
  1223. return fhttp->state != ISSUE;
  1224. }
  1225. bool fetch_player_stats_i()
  1226. {
  1227. return fetch_player_stats(fhttp);
  1228. }
  1229. if (!alloc_message_view(app, MessageStateLoading))
  1230. {
  1231. FURI_LOG_E(TAG, "Failed to allocate message view");
  1232. return;
  1233. }
  1234. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1235. // Make the request
  1236. if (game_mode_index != 1) // not GAME_MODE_PVP
  1237. {
  1238. if (!flipper_http_process_response_async(fhttp, fetch_world_list_i, parse_world_list_i) || !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
  1239. {
  1240. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1241. flipper_http_free(fhttp);
  1242. }
  1243. else
  1244. {
  1245. flipper_http_free(fhttp);
  1246. }
  1247. if (!start_game_thread(app))
  1248. {
  1249. FURI_LOG_E(TAG, "Failed to start game thread");
  1250. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1251. return;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. // load pvp info (this returns the lobbies available)
  1257. bool fetch_pvp_lobbies()
  1258. {
  1259. // ensure flip_world directory exists
  1260. char directory_path[128];
  1261. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1262. Storage *storage = furi_record_open(RECORD_STORAGE);
  1263. storage_common_mkdir(storage, directory_path);
  1264. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp");
  1265. storage_common_mkdir(storage, directory_path);
  1266. furi_record_close(RECORD_STORAGE);
  1267. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/pvp_lobbies.json");
  1268. fhttp->save_received_data = true;
  1269. // 2 players max, 10 lobbies
  1270. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/pvp/lobbies/2/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  1271. }
  1272. bool parse_pvp_lobbies()
  1273. {
  1274. FURI_LOG_I(TAG, "allocating pvp lobbies");
  1275. free_submenu_lobby(app);
  1276. if (!alloc_submenu_lobby(app))
  1277. {
  1278. FURI_LOG_E(TAG, "Failed to allocate lobby submenu");
  1279. return false;
  1280. }
  1281. FURI_LOG_I(TAG, "Parsing pvp lobbies");
  1282. // add the lobbies to the submenu
  1283. FuriString *lobbies = flipper_http_load_from_file(fhttp->file_path);
  1284. if (!lobbies)
  1285. {
  1286. FURI_LOG_E(TAG, "Failed to load lobbies");
  1287. return false;
  1288. }
  1289. FURI_LOG_I(TAG, "Parsing lobbies 1");
  1290. // parse the lobbies
  1291. for (uint32_t i = 0; i < 10; i++)
  1292. {
  1293. FuriString *lobby = get_json_array_value_furi("lobbies", i, lobbies);
  1294. if (!lobby)
  1295. {
  1296. FURI_LOG_I(TAG, "No more lobbies");
  1297. break;
  1298. }
  1299. FuriString *lobby_id = get_json_value_furi("id", lobby);
  1300. if (!lobby_id)
  1301. {
  1302. FURI_LOG_E(TAG, "Failed to get lobby id");
  1303. furi_string_free(lobby);
  1304. return false;
  1305. }
  1306. // add the lobby to the submenu
  1307. submenu_add_item(app->submenu_lobby, furi_string_get_cstr(lobby_id), FlipWorldSubmenuIndexLobby + i, callback_submenu_lobby_choices, app);
  1308. // add the lobby to the list
  1309. if (!easy_flipper_set_buffer(&lobby_list[i], 64))
  1310. {
  1311. FURI_LOG_E(TAG, "Failed to allocate lobby list");
  1312. furi_string_free(lobby);
  1313. furi_string_free(lobby_id);
  1314. return false;
  1315. }
  1316. snprintf(lobby_list[i], 64, "%s", furi_string_get_cstr(lobby_id));
  1317. furi_string_free(lobby);
  1318. furi_string_free(lobby_id);
  1319. }
  1320. furi_string_free(lobbies);
  1321. return true;
  1322. }
  1323. // load pvp lobbies and player stats
  1324. if (!flipper_http_process_response_async(fhttp, fetch_pvp_lobbies, parse_pvp_lobbies) || !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
  1325. {
  1326. // unlike the pve/story, receiving data is necessary
  1327. // so send the user back to the main menu if it fails
  1328. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1329. easy_flipper_dialog("Error", "Failed to make request. Press BACK to return.");
  1330. view_dispatcher_switch_to_view(app->view_dispatcher,
  1331. FlipWorldViewSubmenu);
  1332. flipper_http_free(fhttp);
  1333. }
  1334. else
  1335. {
  1336. flipper_http_free(fhttp);
  1337. }
  1338. // switch to the lobby submenu
  1339. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewLobby);
  1340. }
  1341. }
  1342. else
  1343. {
  1344. switch_to_view_get_game(app);
  1345. }
  1346. }
  1347. void callback_submenu_choices(void *context, uint32_t index)
  1348. {
  1349. FlipWorldApp *app = (FlipWorldApp *)context;
  1350. if (!app)
  1351. {
  1352. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1353. return;
  1354. }
  1355. switch (index)
  1356. {
  1357. case FlipWorldSubmenuIndexGameSubmenu:
  1358. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewGameSubmenu);
  1359. break;
  1360. case FlipWorldSubmenuIndexStory:
  1361. game_mode_index = 2; // GAME_MODE_STORY
  1362. run(app);
  1363. break;
  1364. case FlipWorldSubmenuIndexPvP:
  1365. game_mode_index = 1; // GAME_MODE_PVP
  1366. run(app);
  1367. break;
  1368. case FlipWorldSubmenuIndexPvE:
  1369. game_mode_index = 0; // GAME_MODE_PVE
  1370. run(app);
  1371. break;
  1372. case FlipWorldSubmenuIndexMessage:
  1373. // About menu.
  1374. free_all_views(app, true, true);
  1375. if (!alloc_message_view(app, MessageStateAbout))
  1376. {
  1377. FURI_LOG_E(TAG, "Failed to allocate message view");
  1378. return;
  1379. }
  1380. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1381. break;
  1382. case FlipWorldSubmenuIndexSettings:
  1383. free_all_views(app, true, true);
  1384. if (!alloc_submenu_settings(app))
  1385. {
  1386. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1387. return;
  1388. }
  1389. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1390. break;
  1391. case FlipWorldSubmenuIndexWiFiSettings:
  1392. free_all_views(app, true, false);
  1393. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1394. {
  1395. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1396. return;
  1397. }
  1398. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1399. break;
  1400. case FlipWorldSubmenuIndexGameSettings:
  1401. free_all_views(app, true, false);
  1402. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1403. {
  1404. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1405. return;
  1406. }
  1407. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1408. break;
  1409. case FlipWorldSubmenuIndexUserSettings:
  1410. free_all_views(app, true, false);
  1411. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1412. {
  1413. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1414. return;
  1415. }
  1416. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. }
  1422. static void callback_submenu_lobby_choices(void *context, uint32_t index)
  1423. {
  1424. /* Handle other game lobbies
  1425. 1. when clicked on, send request to fetch the selected game lobby details
  1426. 2. start the websocket session
  1427. 3. start the game thread (the rest will be handled by game_start and player_update)
  1428. */
  1429. FlipWorldApp *app = (FlipWorldApp *)context;
  1430. if (!app)
  1431. {
  1432. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1433. return;
  1434. }
  1435. if (index >= FlipWorldSubmenuIndexLobby && index < FlipWorldSubmenuIndexLobby + 10)
  1436. {
  1437. uint32_t lobby_index = index - FlipWorldSubmenuIndexLobby;
  1438. // get the lobby id
  1439. FlipperHTTP *fhttp = flipper_http_alloc();
  1440. if (!fhttp)
  1441. {
  1442. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1443. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1444. return;
  1445. }
  1446. // send the request to fetch the lobby details
  1447. // start the websocket session
  1448. char websocket_url[128];
  1449. snprintf(websocket_url, sizeof(websocket_url), "wss://www.jblanked.com/flipper/api/world/pvp/lobby/%ld/", lobby_index);
  1450. if (!flipper_http_websocket_start(fhttp, websocket_url, 443, "{\"Content-Type\":\"application/json\"}"))
  1451. {
  1452. FURI_LOG_E(TAG, "Failed to start websocket session");
  1453. easy_flipper_dialog("Error", "Failed to start websocket session. Press BACK to return.");
  1454. flipper_http_free(fhttp);
  1455. return;
  1456. }
  1457. flipper_http_free(fhttp);
  1458. // start the game thread
  1459. if (!start_game_thread(app))
  1460. {
  1461. FURI_LOG_E(TAG, "Failed to start game thread");
  1462. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1463. return;
  1464. }
  1465. }
  1466. }
  1467. static void updated_wifi_ssid(void *context)
  1468. {
  1469. FlipWorldApp *app = (FlipWorldApp *)context;
  1470. if (!app)
  1471. {
  1472. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1473. return;
  1474. }
  1475. // store the entered text
  1476. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1477. // Ensure null-termination
  1478. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1479. // save the setting
  1480. save_char("WiFi-SSID", app->text_input_buffer);
  1481. // update the variable item text
  1482. if (app->variable_item_wifi_ssid)
  1483. {
  1484. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1485. // get value of password
  1486. char pass[64];
  1487. char username[64];
  1488. char password[64];
  1489. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1490. {
  1491. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1492. {
  1493. // save the settings
  1494. load_char("Flip-Social-Username", username, sizeof(username));
  1495. load_char("Flip-Social-Password", password, sizeof(password));
  1496. save_settings(app->text_input_buffer, pass, username, password);
  1497. // initialize the http
  1498. FlipperHTTP *fhttp = flipper_http_alloc();
  1499. if (fhttp)
  1500. {
  1501. // save the wifi if the device is connected
  1502. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1503. {
  1504. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1505. }
  1506. // free the resources
  1507. flipper_http_free(fhttp);
  1508. }
  1509. else
  1510. {
  1511. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1512. }
  1513. }
  1514. }
  1515. }
  1516. // switch to the settings view
  1517. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1518. }
  1519. static void updated_wifi_pass(void *context)
  1520. {
  1521. FlipWorldApp *app = (FlipWorldApp *)context;
  1522. if (!app)
  1523. {
  1524. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1525. return;
  1526. }
  1527. // store the entered text
  1528. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1529. // Ensure null-termination
  1530. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1531. // save the setting
  1532. save_char("WiFi-Password", app->text_input_buffer);
  1533. // update the variable item text
  1534. if (app->variable_item_wifi_pass)
  1535. {
  1536. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1537. }
  1538. // get value of ssid
  1539. char ssid[64];
  1540. char username[64];
  1541. char password[64];
  1542. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1543. {
  1544. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1545. {
  1546. // save the settings
  1547. load_char("Flip-Social-Username", username, sizeof(username));
  1548. load_char("Flip-Social-Password", password, sizeof(password));
  1549. save_settings(ssid, app->text_input_buffer, username, password);
  1550. // initialize the http
  1551. FlipperHTTP *fhttp = flipper_http_alloc();
  1552. if (fhttp)
  1553. {
  1554. // save the wifi if the device is connected
  1555. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1556. {
  1557. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1558. }
  1559. // free the resources
  1560. flipper_http_free(fhttp);
  1561. }
  1562. else
  1563. {
  1564. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1565. }
  1566. }
  1567. }
  1568. // switch to the settings view
  1569. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1570. }
  1571. static void updated_username(void *context)
  1572. {
  1573. FlipWorldApp *app = (FlipWorldApp *)context;
  1574. if (!app)
  1575. {
  1576. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1577. return;
  1578. }
  1579. // store the entered text
  1580. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1581. // Ensure null-termination
  1582. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1583. // save the setting
  1584. save_char("Flip-Social-Username", app->text_input_buffer);
  1585. // update the variable item text
  1586. if (app->variable_item_user_username)
  1587. {
  1588. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1589. }
  1590. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1591. }
  1592. static void updated_password(void *context)
  1593. {
  1594. FlipWorldApp *app = (FlipWorldApp *)context;
  1595. if (!app)
  1596. {
  1597. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1598. return;
  1599. }
  1600. // store the entered text
  1601. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1602. // Ensure null-termination
  1603. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1604. // save the setting
  1605. save_char("Flip-Social-Password", app->text_input_buffer);
  1606. // update the variable item text
  1607. if (app->variable_item_user_password)
  1608. {
  1609. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1610. }
  1611. // get value of username
  1612. char username[64];
  1613. char ssid[64];
  1614. char pass[64];
  1615. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1616. {
  1617. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1618. {
  1619. // save the settings
  1620. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1621. load_char("WiFi-Password", pass, sizeof(pass));
  1622. save_settings(ssid, pass, username, app->text_input_buffer);
  1623. }
  1624. }
  1625. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1626. }
  1627. static void wifi_settings_select(void *context, uint32_t index)
  1628. {
  1629. FlipWorldApp *app = (FlipWorldApp *)context;
  1630. if (!app)
  1631. {
  1632. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1633. return;
  1634. }
  1635. char ssid[64];
  1636. char pass[64];
  1637. char username[64];
  1638. char password[64];
  1639. switch (index)
  1640. {
  1641. case 0: // Input SSID
  1642. free_all_views(app, false, false);
  1643. if (!alloc_text_input_view(app, "SSID"))
  1644. {
  1645. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1646. return;
  1647. }
  1648. // load SSID
  1649. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1650. {
  1651. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1652. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1653. }
  1654. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1655. break;
  1656. case 1: // Input Password
  1657. free_all_views(app, false, false);
  1658. if (!alloc_text_input_view(app, "Password"))
  1659. {
  1660. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1661. return;
  1662. }
  1663. // load password
  1664. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1665. {
  1666. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1667. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1668. }
  1669. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1670. break;
  1671. default:
  1672. FURI_LOG_E(TAG, "Unknown configuration item index");
  1673. break;
  1674. }
  1675. }
  1676. static void fps_change(VariableItem *item)
  1677. {
  1678. uint8_t index = variable_item_get_current_value_index(item);
  1679. fps_index = index;
  1680. variable_item_set_current_value_text(item, fps_choices_str[index]);
  1681. variable_item_set_current_value_index(item, index);
  1682. save_char("Game-FPS", fps_choices_str[index]);
  1683. }
  1684. static void screen_on_change(VariableItem *item)
  1685. {
  1686. uint8_t index = variable_item_get_current_value_index(item);
  1687. screen_always_on_index = index;
  1688. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1689. variable_item_set_current_value_index(item, index);
  1690. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1691. }
  1692. static void sound_on_change(VariableItem *item)
  1693. {
  1694. uint8_t index = variable_item_get_current_value_index(item);
  1695. sound_on_index = index;
  1696. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1697. variable_item_set_current_value_index(item, index);
  1698. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1699. }
  1700. static void vibration_on_change(VariableItem *item)
  1701. {
  1702. uint8_t index = variable_item_get_current_value_index(item);
  1703. vibration_on_index = index;
  1704. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1705. variable_item_set_current_value_index(item, index);
  1706. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1707. }
  1708. static void player_on_change(VariableItem *item)
  1709. {
  1710. uint8_t index = variable_item_get_current_value_index(item);
  1711. player_sprite_index = index;
  1712. variable_item_set_current_value_text(item, is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  1713. variable_item_set_current_value_index(item, index);
  1714. save_char("Game-Player-Sprite", player_sprite_choices[index]);
  1715. }
  1716. static void vgm_x_change(VariableItem *item)
  1717. {
  1718. uint8_t index = variable_item_get_current_value_index(item);
  1719. vgm_x_index = index;
  1720. variable_item_set_current_value_text(item, vgm_levels[index]);
  1721. variable_item_set_current_value_index(item, index);
  1722. save_char("Game-VGM-X", vgm_levels[index]);
  1723. }
  1724. static void vgm_y_change(VariableItem *item)
  1725. {
  1726. uint8_t index = variable_item_get_current_value_index(item);
  1727. vgm_y_index = index;
  1728. variable_item_set_current_value_text(item, vgm_levels[index]);
  1729. variable_item_set_current_value_index(item, index);
  1730. save_char("Game-VGM-Y", vgm_levels[index]);
  1731. }
  1732. static bool _fetch_worlds(DataLoaderModel *model)
  1733. {
  1734. if (!model || !model->fhttp)
  1735. {
  1736. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1737. return false;
  1738. }
  1739. char directory_path[128];
  1740. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1741. Storage *storage = furi_record_open(RECORD_STORAGE);
  1742. storage_common_mkdir(storage, directory_path);
  1743. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1744. storage_common_mkdir(storage, directory_path);
  1745. furi_record_close(RECORD_STORAGE);
  1746. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1747. model->fhttp->save_received_data = true;
  1748. return flipper_http_request(model->fhttp, GET, "https://www.jblanked.com/flipper/api/world/v5/get/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  1749. }
  1750. static char *_parse_worlds(DataLoaderModel *model)
  1751. {
  1752. UNUSED(model);
  1753. return "World Pack Installed";
  1754. }
  1755. static void switch_to_view_get_worlds(FlipWorldApp *app)
  1756. {
  1757. generic_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1758. }
  1759. static void game_settings_select(void *context, uint32_t index)
  1760. {
  1761. FlipWorldApp *app = (FlipWorldApp *)context;
  1762. if (!app)
  1763. {
  1764. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1765. return;
  1766. }
  1767. switch (index)
  1768. {
  1769. case 0: // Download all world data as one huge json
  1770. switch_to_view_get_worlds(app);
  1771. case 1: // Player Sprite
  1772. break;
  1773. case 2: // Change FPS
  1774. break;
  1775. case 3: // VGM X
  1776. break;
  1777. case 4: // VGM Y
  1778. break;
  1779. case 5: // Screen Always On
  1780. break;
  1781. case 6: // Sound On
  1782. break;
  1783. case 7: // Vibration On
  1784. break;
  1785. }
  1786. }
  1787. static void user_settings_select(void *context, uint32_t index)
  1788. {
  1789. FlipWorldApp *app = (FlipWorldApp *)context;
  1790. if (!app)
  1791. {
  1792. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1793. return;
  1794. }
  1795. switch (index)
  1796. {
  1797. case 0: // Username
  1798. free_all_views(app, false, false);
  1799. if (!alloc_text_input_view(app, "Username-Login"))
  1800. {
  1801. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1802. return;
  1803. }
  1804. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1805. break;
  1806. case 1: // Password
  1807. free_all_views(app, false, false);
  1808. if (!alloc_text_input_view(app, "Password-Login"))
  1809. {
  1810. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1811. return;
  1812. }
  1813. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1814. break;
  1815. }
  1816. }
  1817. static void widget_set_text(char *message, Widget **widget)
  1818. {
  1819. if (widget == NULL)
  1820. {
  1821. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  1822. DEV_CRASH();
  1823. return;
  1824. }
  1825. if (message == NULL)
  1826. {
  1827. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  1828. DEV_CRASH();
  1829. return;
  1830. }
  1831. widget_reset(*widget);
  1832. uint32_t message_length = strlen(message); // Length of the message
  1833. uint32_t i = 0; // Index tracker
  1834. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1835. char *formatted_message; // Buffer to hold the final formatted message
  1836. // Allocate buffer with double the message length plus one for safety
  1837. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1838. {
  1839. return;
  1840. }
  1841. while (i < message_length)
  1842. {
  1843. uint32_t max_line_length = 31; // Maximum characters per line
  1844. uint32_t remaining_length = message_length - i; // Remaining characters
  1845. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1846. // Check for newline character within the current segment
  1847. uint32_t newline_pos = i;
  1848. bool found_newline = false;
  1849. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1850. {
  1851. if (message[newline_pos] == '\n')
  1852. {
  1853. found_newline = true;
  1854. break;
  1855. }
  1856. }
  1857. if (found_newline)
  1858. {
  1859. // If newline found, set line_length up to the newline
  1860. line_length = newline_pos - i;
  1861. }
  1862. // Temporary buffer to hold the current line
  1863. char line[32];
  1864. strncpy(line, message + i, line_length);
  1865. line[line_length] = '\0';
  1866. // If newline was found, skip it for the next iteration
  1867. if (found_newline)
  1868. {
  1869. i += line_length + 1; // +1 to skip the '\n' character
  1870. }
  1871. else
  1872. {
  1873. // Check if the line ends in the middle of a word and adjust accordingly
  1874. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1875. {
  1876. // Find the last space within the current line to avoid breaking a word
  1877. char *last_space = strrchr(line, ' ');
  1878. if (last_space != NULL)
  1879. {
  1880. // Adjust the line_length to avoid cutting the word
  1881. line_length = last_space - line;
  1882. line[line_length] = '\0'; // Null-terminate at the space
  1883. }
  1884. }
  1885. // Move the index forward by the determined line_length
  1886. i += line_length;
  1887. // Skip any spaces at the beginning of the next line
  1888. while (i < message_length && message[i] == ' ')
  1889. {
  1890. i++;
  1891. }
  1892. }
  1893. // Manually copy the fixed line into the formatted_message buffer
  1894. for (uint32_t j = 0; j < line_length; j++)
  1895. {
  1896. formatted_message[formatted_index++] = line[j];
  1897. }
  1898. // Add a newline character for line spacing
  1899. formatted_message[formatted_index++] = '\n';
  1900. }
  1901. // Null-terminate the formatted_message
  1902. formatted_message[formatted_index] = '\0';
  1903. // Add the formatted message to the widget
  1904. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1905. }
  1906. void loader_draw_callback(Canvas *canvas, void *model)
  1907. {
  1908. if (!canvas || !model)
  1909. {
  1910. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  1911. return;
  1912. }
  1913. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1914. HTTPState http_state = data_loader_model->fhttp->state;
  1915. DataState data_state = data_loader_model->data_state;
  1916. char *title = data_loader_model->title;
  1917. canvas_set_font(canvas, FontSecondary);
  1918. if (http_state == INACTIVE)
  1919. {
  1920. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1921. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1922. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1923. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1924. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1925. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1926. return;
  1927. }
  1928. if (data_state == DataStateError || data_state == DataStateParseError)
  1929. {
  1930. error_draw(canvas, data_loader_model);
  1931. return;
  1932. }
  1933. canvas_draw_str(canvas, 0, 7, title);
  1934. canvas_draw_str(canvas, 0, 17, "Loading...");
  1935. if (data_state == DataStateInitial)
  1936. {
  1937. return;
  1938. }
  1939. if (http_state == SENDING)
  1940. {
  1941. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1942. return;
  1943. }
  1944. if (http_state == RECEIVING || data_state == DataStateRequested)
  1945. {
  1946. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1947. return;
  1948. }
  1949. if (http_state == IDLE && data_state == DataStateReceived)
  1950. {
  1951. canvas_draw_str(canvas, 0, 27, "Processing...");
  1952. return;
  1953. }
  1954. if (http_state == IDLE && data_state == DataStateParsed)
  1955. {
  1956. canvas_draw_str(canvas, 0, 27, "Processed...");
  1957. return;
  1958. }
  1959. }
  1960. static void loader_process_callback(void *context)
  1961. {
  1962. if (context == NULL)
  1963. {
  1964. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  1965. DEV_CRASH();
  1966. return;
  1967. }
  1968. FlipWorldApp *app = (FlipWorldApp *)context;
  1969. View *view = app->view_loader;
  1970. DataState current_data_state;
  1971. DataLoaderModel *loader_model = NULL;
  1972. with_view_model(
  1973. view,
  1974. DataLoaderModel * model,
  1975. {
  1976. current_data_state = model->data_state;
  1977. loader_model = model;
  1978. },
  1979. false);
  1980. if (!loader_model || !loader_model->fhttp)
  1981. {
  1982. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1983. DEV_CRASH();
  1984. return;
  1985. }
  1986. if (current_data_state == DataStateInitial)
  1987. {
  1988. with_view_model(
  1989. view,
  1990. DataLoaderModel * model,
  1991. {
  1992. model->data_state = DataStateRequested;
  1993. DataLoaderFetch fetch = model->fetcher;
  1994. if (fetch == NULL)
  1995. {
  1996. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1997. model->data_state = DataStateError;
  1998. return;
  1999. }
  2000. // Clear any previous responses
  2001. strncpy(model->fhttp->last_response, "", 1);
  2002. bool request_status = fetch(model);
  2003. if (!request_status)
  2004. {
  2005. model->data_state = DataStateError;
  2006. }
  2007. },
  2008. true);
  2009. }
  2010. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  2011. {
  2012. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  2013. {
  2014. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  2015. {
  2016. FURI_LOG_DEV(TAG, "PONG received.");
  2017. }
  2018. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  2019. {
  2020. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2021. }
  2022. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  2023. {
  2024. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2025. }
  2026. else if (strlen(loader_model->fhttp->last_response))
  2027. {
  2028. // Still waiting on response
  2029. }
  2030. else
  2031. {
  2032. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  2033. }
  2034. }
  2035. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  2036. {
  2037. // continue waiting
  2038. }
  2039. else if (loader_model->fhttp->state == INACTIVE)
  2040. {
  2041. // inactive. try again
  2042. }
  2043. else if (loader_model->fhttp->state == ISSUE)
  2044. {
  2045. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  2046. }
  2047. else
  2048. {
  2049. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2050. DEV_CRASH();
  2051. }
  2052. }
  2053. else if (current_data_state == DataStateReceived)
  2054. {
  2055. with_view_model(
  2056. view,
  2057. DataLoaderModel * model,
  2058. {
  2059. char *data_text;
  2060. if (model->parser == NULL)
  2061. {
  2062. data_text = NULL;
  2063. FURI_LOG_DEV(TAG, "Parser is NULL");
  2064. DEV_CRASH();
  2065. }
  2066. else
  2067. {
  2068. data_text = model->parser(model);
  2069. }
  2070. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  2071. model->data_text = data_text;
  2072. if (data_text == NULL)
  2073. {
  2074. model->data_state = DataStateParseError;
  2075. }
  2076. else
  2077. {
  2078. model->data_state = DataStateParsed;
  2079. }
  2080. },
  2081. true);
  2082. }
  2083. else if (current_data_state == DataStateParsed)
  2084. {
  2085. with_view_model(
  2086. view,
  2087. DataLoaderModel * model,
  2088. {
  2089. if (++model->request_index < model->request_count)
  2090. {
  2091. model->data_state = DataStateInitial;
  2092. }
  2093. else
  2094. {
  2095. widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  2096. if (model->data_text != NULL)
  2097. {
  2098. free(model->data_text);
  2099. model->data_text = NULL;
  2100. }
  2101. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  2102. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  2103. }
  2104. },
  2105. true);
  2106. }
  2107. }
  2108. static void loader_timer_callback(void *context)
  2109. {
  2110. if (context == NULL)
  2111. {
  2112. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  2113. DEV_CRASH();
  2114. return;
  2115. }
  2116. FlipWorldApp *app = (FlipWorldApp *)context;
  2117. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  2118. }
  2119. static void loader_on_enter(void *context)
  2120. {
  2121. if (context == NULL)
  2122. {
  2123. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  2124. DEV_CRASH();
  2125. return;
  2126. }
  2127. FlipWorldApp *app = (FlipWorldApp *)context;
  2128. View *view = app->view_loader;
  2129. with_view_model(
  2130. view,
  2131. DataLoaderModel * model,
  2132. {
  2133. view_set_previous_callback(view, model->back_callback);
  2134. if (model->timer == NULL)
  2135. {
  2136. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  2137. }
  2138. furi_timer_start(model->timer, 250);
  2139. },
  2140. true);
  2141. }
  2142. static void loader_on_exit(void *context)
  2143. {
  2144. if (context == NULL)
  2145. {
  2146. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  2147. DEV_CRASH();
  2148. return;
  2149. }
  2150. FlipWorldApp *app = (FlipWorldApp *)context;
  2151. View *view = app->view_loader;
  2152. with_view_model(
  2153. view,
  2154. DataLoaderModel * model,
  2155. {
  2156. if (model->timer)
  2157. {
  2158. furi_timer_stop(model->timer);
  2159. }
  2160. },
  2161. false);
  2162. }
  2163. void loader_init(View *view)
  2164. {
  2165. if (view == NULL)
  2166. {
  2167. FURI_LOG_E(TAG, "loader_init - view is NULL");
  2168. DEV_CRASH();
  2169. return;
  2170. }
  2171. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  2172. view_set_enter_callback(view, loader_on_enter);
  2173. view_set_exit_callback(view, loader_on_exit);
  2174. }
  2175. void loader_free_model(View *view)
  2176. {
  2177. if (view == NULL)
  2178. {
  2179. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  2180. DEV_CRASH();
  2181. return;
  2182. }
  2183. with_view_model(
  2184. view,
  2185. DataLoaderModel * model,
  2186. {
  2187. if (model->timer)
  2188. {
  2189. furi_timer_free(model->timer);
  2190. model->timer = NULL;
  2191. }
  2192. if (model->parser_context)
  2193. {
  2194. // do not free the context here, it is the app context
  2195. // free(model->parser_context);
  2196. // model->parser_context = NULL;
  2197. }
  2198. if (model->fhttp)
  2199. {
  2200. flipper_http_free(model->fhttp);
  2201. model->fhttp = NULL;
  2202. }
  2203. },
  2204. false);
  2205. }
  2206. bool custom_event_callback(void *context, uint32_t index)
  2207. {
  2208. if (context == NULL)
  2209. {
  2210. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  2211. DEV_CRASH();
  2212. return false;
  2213. }
  2214. switch (index)
  2215. {
  2216. case FlipWorldCustomEventProcess:
  2217. loader_process_callback(context);
  2218. return true;
  2219. default:
  2220. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  2221. return false;
  2222. }
  2223. }
  2224. void generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  2225. {
  2226. if (app == NULL)
  2227. {
  2228. FURI_LOG_E(TAG, "generic_switch_to_view - app is NULL");
  2229. DEV_CRASH();
  2230. return;
  2231. }
  2232. View *view = app->view_loader;
  2233. if (view == NULL)
  2234. {
  2235. FURI_LOG_E(TAG, "generic_switch_to_view - view is NULL");
  2236. DEV_CRASH();
  2237. return;
  2238. }
  2239. with_view_model(
  2240. view,
  2241. DataLoaderModel * model,
  2242. {
  2243. model->title = title;
  2244. model->fetcher = fetcher;
  2245. model->parser = parser;
  2246. model->request_index = 0;
  2247. model->request_count = request_count;
  2248. model->back_callback = back;
  2249. model->data_state = DataStateInitial;
  2250. model->data_text = NULL;
  2251. //
  2252. model->parser_context = app;
  2253. if (!model->fhttp)
  2254. {
  2255. model->fhttp = flipper_http_alloc();
  2256. }
  2257. },
  2258. true);
  2259. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2260. }