callback.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  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 game thread
  887. if (game_thread_running)
  888. {
  889. game_thread_running = false;
  890. if (game_thread)
  891. {
  892. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  893. furi_thread_join(game_thread);
  894. furi_thread_free(game_thread);
  895. }
  896. }
  897. // start game thread
  898. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  899. if (!thread)
  900. {
  901. FURI_LOG_E(TAG, "Failed to allocate game thread");
  902. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  903. return false;
  904. }
  905. furi_thread_start(thread);
  906. game_thread = thread;
  907. game_thread_running = true;
  908. return true;
  909. }
  910. // combine register, login, and world list fetch into one function to switch to the loader view
  911. static bool _fetch_game(DataLoaderModel *model)
  912. {
  913. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  914. if (!app)
  915. {
  916. FURI_LOG_E(TAG, "app is NULL");
  917. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  918. return false;
  919. }
  920. if (model->request_index == 0)
  921. {
  922. // login
  923. char username[64];
  924. char password[64];
  925. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  926. {
  927. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  928. view_dispatcher_switch_to_view(app->view_dispatcher,
  929. FlipWorldViewSubmenu); // just go back to the main menu for now
  930. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  931. return false;
  932. }
  933. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  934. {
  935. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  936. view_dispatcher_switch_to_view(app->view_dispatcher,
  937. FlipWorldViewSubmenu); // just go back to the main menu for now
  938. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  939. return false;
  940. }
  941. char payload[256];
  942. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  943. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  944. }
  945. else if (model->request_index == 1)
  946. {
  947. // check if login was successful
  948. char is_logged_in[8];
  949. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  950. {
  951. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  952. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  953. view_dispatcher_switch_to_view(app->view_dispatcher,
  954. FlipWorldViewSubmenu); // just go back to the main menu for now
  955. return false;
  956. }
  957. if (is_str(is_logged_in, "false") && is_str(model->title, "Registering..."))
  958. {
  959. // register
  960. char username[64];
  961. char password[64];
  962. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  963. {
  964. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  965. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  966. view_dispatcher_switch_to_view(app->view_dispatcher,
  967. FlipWorldViewSubmenu); // just go back to the main menu for now
  968. return false;
  969. }
  970. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  971. {
  972. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  973. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  974. view_dispatcher_switch_to_view(app->view_dispatcher,
  975. FlipWorldViewSubmenu); // just go back to the main menu for now
  976. return false;
  977. }
  978. char payload[172];
  979. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  980. model->title = "Registering...";
  981. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  982. }
  983. else
  984. {
  985. model->title = "Fetching World List..";
  986. return fetch_world_list(model->fhttp);
  987. }
  988. }
  989. else if (model->request_index == 2)
  990. {
  991. model->title = "Fetching World List..";
  992. return fetch_world_list(model->fhttp);
  993. }
  994. else if (model->request_index == 3)
  995. {
  996. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  997. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  998. if (!world_list)
  999. {
  1000. view_dispatcher_switch_to_view(app->view_dispatcher,
  1001. FlipWorldViewSubmenu); // just go back to the main menu for now
  1002. FURI_LOG_E(TAG, "Failed to load world list");
  1003. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  1004. return false;
  1005. }
  1006. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  1007. if (!first_world)
  1008. {
  1009. view_dispatcher_switch_to_view(app->view_dispatcher,
  1010. FlipWorldViewSubmenu); // just go back to the main menu for now
  1011. FURI_LOG_E(TAG, "Failed to get first world");
  1012. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  1013. furi_string_free(world_list);
  1014. return false;
  1015. }
  1016. if (world_exists(furi_string_get_cstr(first_world)))
  1017. {
  1018. furi_string_free(world_list);
  1019. furi_string_free(first_world);
  1020. if (!start_game_thread(app))
  1021. {
  1022. FURI_LOG_E(TAG, "Failed to start game thread");
  1023. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1024. view_dispatcher_switch_to_view(app->view_dispatcher,
  1025. FlipWorldViewSubmenu); // just go back to the main menu for now
  1026. return "Failed to start game thread";
  1027. }
  1028. return true;
  1029. }
  1030. 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));
  1031. model->fhttp->save_received_data = true;
  1032. char url[128];
  1033. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v5/get/world/%s/", furi_string_get_cstr(first_world));
  1034. furi_string_free(world_list);
  1035. furi_string_free(first_world);
  1036. return flipper_http_request(model->fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  1037. }
  1038. FURI_LOG_E(TAG, "Unknown request index");
  1039. return false;
  1040. }
  1041. static char *_parse_game(DataLoaderModel *model)
  1042. {
  1043. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  1044. if (model->request_index == 0)
  1045. {
  1046. if (!model->fhttp->last_response)
  1047. {
  1048. save_char("is_logged_in", "false");
  1049. // Go back to the main menu
  1050. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  1051. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1052. return "Response is empty...";
  1053. }
  1054. // Check for successful conditions
  1055. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  1056. {
  1057. save_char("is_logged_in", "true");
  1058. model->title = "Login successful!";
  1059. model->title = "Fetching World List..";
  1060. return "Login successful!";
  1061. }
  1062. // Check if user not found
  1063. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  1064. {
  1065. save_char("is_logged_in", "false");
  1066. model->title = "Registering...";
  1067. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  1068. }
  1069. // If not success, not found, check length conditions
  1070. size_t resp_len = strlen(model->fhttp->last_response);
  1071. if (resp_len == 0 || resp_len > 127)
  1072. {
  1073. // Empty or too long means failed login
  1074. save_char("is_logged_in", "false");
  1075. // Go back to the main menu
  1076. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1077. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1078. return "Failed to login...";
  1079. }
  1080. // Handle any other unknown response as a failure
  1081. save_char("is_logged_in", "false");
  1082. // Go back to the main menu
  1083. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1084. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1085. return "Failed to login...";
  1086. }
  1087. else if (model->request_index == 1)
  1088. {
  1089. if (is_str(model->title, "Registering..."))
  1090. {
  1091. // check registration response
  1092. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  1093. {
  1094. save_char("is_logged_in", "true");
  1095. char username[64];
  1096. char password[64];
  1097. // load the username and password, then save them
  1098. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  1099. {
  1100. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  1101. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  1102. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1103. return "Failed to load Flip-Social-Username";
  1104. }
  1105. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  1106. {
  1107. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  1108. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  1109. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1110. return "Failed to load Flip-Social-Password";
  1111. }
  1112. // load wifi ssid,pass then save
  1113. char ssid[64];
  1114. char pass[64];
  1115. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1116. {
  1117. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  1118. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  1119. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1120. return "Failed to load WiFi-SSID";
  1121. }
  1122. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  1123. {
  1124. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  1125. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  1126. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1127. return "Failed to load WiFi-Password";
  1128. }
  1129. save_settings(ssid, pass, username, password);
  1130. model->title = "Fetching World List..";
  1131. return "Account created!";
  1132. }
  1133. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  1134. {
  1135. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  1136. view_dispatcher_switch_to_view(app->view_dispatcher,
  1137. FlipWorldViewSubmenu); // just go back to the main menu for now
  1138. return "Please enter your credentials.";
  1139. }
  1140. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  1141. {
  1142. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  1143. view_dispatcher_switch_to_view(app->view_dispatcher,
  1144. FlipWorldViewSubmenu); // just go back to the main menu for now
  1145. return "Username already exists.";
  1146. }
  1147. else
  1148. {
  1149. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  1150. view_dispatcher_switch_to_view(app->view_dispatcher,
  1151. FlipWorldViewSubmenu); // just go back to the main menu for now
  1152. return "Registration failed...";
  1153. }
  1154. }
  1155. else
  1156. {
  1157. if (!start_game_thread(app))
  1158. {
  1159. FURI_LOG_E(TAG, "Failed to start game thread");
  1160. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1161. view_dispatcher_switch_to_view(app->view_dispatcher,
  1162. FlipWorldViewSubmenu); // just go back to the main menu for now
  1163. return "Failed to start game thread";
  1164. }
  1165. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1166. }
  1167. }
  1168. else if (model->request_index == 2)
  1169. {
  1170. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1171. }
  1172. else if (model->request_index == 3)
  1173. {
  1174. if (!start_game_thread(app))
  1175. {
  1176. FURI_LOG_E(TAG, "Failed to start game thread");
  1177. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1178. view_dispatcher_switch_to_view(app->view_dispatcher,
  1179. FlipWorldViewSubmenu); // just go back to the main menu for now
  1180. return "Failed to start game thread";
  1181. }
  1182. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1183. }
  1184. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  1185. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1186. return "Unknown error";
  1187. }
  1188. static void switch_to_view_get_game(FlipWorldApp *app)
  1189. {
  1190. generic_switch_to_view(app, "Starting Game..", _fetch_game, _parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  1191. }
  1192. static void run(FlipWorldApp *app)
  1193. {
  1194. if (!app)
  1195. {
  1196. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1197. return;
  1198. }
  1199. free_all_views(app, true, true);
  1200. if (!is_enough_heap(10000))
  1201. {
  1202. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  1203. return;
  1204. }
  1205. // check if logged in
  1206. if (is_logged_in() || is_logged_in_to_flip_social())
  1207. {
  1208. FlipperHTTP *fhttp = flipper_http_alloc();
  1209. if (!fhttp)
  1210. {
  1211. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1212. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1213. return;
  1214. }
  1215. bool fetch_world_list_i()
  1216. {
  1217. return fetch_world_list(fhttp);
  1218. }
  1219. bool parse_world_list_i()
  1220. {
  1221. return fhttp->state != ISSUE;
  1222. }
  1223. bool fetch_player_stats_i()
  1224. {
  1225. return fetch_player_stats(fhttp);
  1226. }
  1227. if (!alloc_message_view(app, MessageStateLoading))
  1228. {
  1229. FURI_LOG_E(TAG, "Failed to allocate message view");
  1230. return;
  1231. }
  1232. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1233. // Make the request
  1234. if (game_mode_index != 1) // not GAME_MODE_PVP
  1235. {
  1236. 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))
  1237. {
  1238. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1239. flipper_http_free(fhttp);
  1240. }
  1241. else
  1242. {
  1243. flipper_http_free(fhttp);
  1244. }
  1245. if (!start_game_thread(app))
  1246. {
  1247. FURI_LOG_E(TAG, "Failed to start game thread");
  1248. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1249. return;
  1250. }
  1251. }
  1252. else
  1253. {
  1254. // load pvp info (this returns the lobbies available)
  1255. bool fetch_pvp_lobbies()
  1256. {
  1257. // ensure flip_world directory exists
  1258. char directory_path[128];
  1259. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1260. Storage *storage = furi_record_open(RECORD_STORAGE);
  1261. storage_common_mkdir(storage, directory_path);
  1262. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp");
  1263. storage_common_mkdir(storage, directory_path);
  1264. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies");
  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. free_submenu_lobby(app);
  1275. if (!alloc_submenu_lobby(app))
  1276. {
  1277. FURI_LOG_E(TAG, "Failed to allocate lobby submenu");
  1278. return false;
  1279. }
  1280. // add the lobbies to the submenu
  1281. FuriString *lobbies = flipper_http_load_from_file(fhttp->file_path);
  1282. if (!lobbies)
  1283. {
  1284. FURI_LOG_E(TAG, "Failed to load lobbies");
  1285. return false;
  1286. }
  1287. // parse the lobbies
  1288. for (uint32_t i = 0; i < 10; i++)
  1289. {
  1290. FuriString *lobby = get_json_array_value_furi("lobbies", i, lobbies);
  1291. if (!lobby)
  1292. {
  1293. FURI_LOG_I(TAG, "No more lobbies");
  1294. break;
  1295. }
  1296. FuriString *lobby_id = get_json_value_furi("id", lobby);
  1297. if (!lobby_id)
  1298. {
  1299. FURI_LOG_E(TAG, "Failed to get lobby id");
  1300. furi_string_free(lobby);
  1301. return false;
  1302. }
  1303. // add the lobby to the submenu
  1304. submenu_add_item(app->submenu_lobby, furi_string_get_cstr(lobby_id), FlipWorldSubmenuIndexLobby + i, callback_submenu_lobby_choices, app);
  1305. // add the lobby to the list
  1306. if (!easy_flipper_set_buffer(&lobby_list[i], 64))
  1307. {
  1308. FURI_LOG_E(TAG, "Failed to allocate lobby list");
  1309. furi_string_free(lobby);
  1310. furi_string_free(lobby_id);
  1311. return false;
  1312. }
  1313. snprintf(lobby_list[i], 64, "%s", furi_string_get_cstr(lobby_id));
  1314. furi_string_free(lobby);
  1315. furi_string_free(lobby_id);
  1316. }
  1317. furi_string_free(lobbies);
  1318. return true;
  1319. }
  1320. // load pvp lobbies and player stats
  1321. 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))
  1322. {
  1323. // unlike the pve/story, receiving data is necessary
  1324. // so send the user back to the main menu if it fails
  1325. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1326. easy_flipper_dialog("Error", "Failed to make request. Press BACK to return.");
  1327. view_dispatcher_switch_to_view(app->view_dispatcher,
  1328. FlipWorldViewSubmenu);
  1329. flipper_http_free(fhttp);
  1330. }
  1331. else
  1332. {
  1333. flipper_http_free(fhttp);
  1334. }
  1335. // switch to the lobby submenu
  1336. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewLobby);
  1337. }
  1338. }
  1339. else
  1340. {
  1341. switch_to_view_get_game(app);
  1342. }
  1343. }
  1344. void callback_submenu_choices(void *context, uint32_t index)
  1345. {
  1346. FlipWorldApp *app = (FlipWorldApp *)context;
  1347. if (!app)
  1348. {
  1349. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1350. return;
  1351. }
  1352. switch (index)
  1353. {
  1354. case FlipWorldSubmenuIndexGameSubmenu:
  1355. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewGameSubmenu);
  1356. break;
  1357. case FlipWorldSubmenuIndexStory:
  1358. game_mode_index = 2; // GAME_MODE_STORY
  1359. run(app);
  1360. break;
  1361. case FlipWorldSubmenuIndexPvP:
  1362. game_mode_index = 1; // GAME_MODE_PVP
  1363. run(app);
  1364. break;
  1365. case FlipWorldSubmenuIndexPvE:
  1366. game_mode_index = 0; // GAME_MODE_PVE
  1367. run(app);
  1368. break;
  1369. case FlipWorldSubmenuIndexMessage:
  1370. // About menu.
  1371. free_all_views(app, true, true);
  1372. if (!alloc_message_view(app, MessageStateAbout))
  1373. {
  1374. FURI_LOG_E(TAG, "Failed to allocate message view");
  1375. return;
  1376. }
  1377. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1378. break;
  1379. case FlipWorldSubmenuIndexSettings:
  1380. free_all_views(app, true, true);
  1381. if (!alloc_submenu_settings(app))
  1382. {
  1383. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1384. return;
  1385. }
  1386. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1387. break;
  1388. case FlipWorldSubmenuIndexWiFiSettings:
  1389. free_all_views(app, true, false);
  1390. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1391. {
  1392. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1393. return;
  1394. }
  1395. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1396. break;
  1397. case FlipWorldSubmenuIndexGameSettings:
  1398. free_all_views(app, true, false);
  1399. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1400. {
  1401. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1402. return;
  1403. }
  1404. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1405. break;
  1406. case FlipWorldSubmenuIndexUserSettings:
  1407. free_all_views(app, true, false);
  1408. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1409. {
  1410. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1411. return;
  1412. }
  1413. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1414. break;
  1415. default:
  1416. break;
  1417. }
  1418. }
  1419. static void callback_submenu_lobby_choices(void *context, uint32_t index)
  1420. {
  1421. /* Handle other game lobbies
  1422. 1. when clicked on, send request to fetch the selected game lobby details
  1423. 2. start the websocket session
  1424. 3. start the game thread (the rest will be handled by game_start and player_update)
  1425. */
  1426. FlipWorldApp *app = (FlipWorldApp *)context;
  1427. if (!app)
  1428. {
  1429. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1430. return;
  1431. }
  1432. if (index >= FlipWorldSubmenuIndexLobby && index < FlipWorldSubmenuIndexLobby + 10)
  1433. {
  1434. uint32_t lobby_index = index - FlipWorldSubmenuIndexLobby;
  1435. FlipperHTTP *fhttp = flipper_http_alloc();
  1436. if (!fhttp)
  1437. {
  1438. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1439. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1440. return;
  1441. }
  1442. // send the request to fetch the lobby details
  1443. char url[128];
  1444. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/%s/", lobby_list[lobby_index]);
  1445. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies/%s.json", lobby_list[lobby_index]);
  1446. fhttp->save_received_data = true;
  1447. bool fetch_lobby()
  1448. {
  1449. if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL))
  1450. {
  1451. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  1452. flipper_http_free(fhttp);
  1453. return false;
  1454. }
  1455. return true;
  1456. }
  1457. bool parse_lobby()
  1458. {
  1459. // for now just return true and the game will handle the rest
  1460. // later we will parse the current player's attributes (which is not the current user)
  1461. // not sure how we're gonna handle two PlayerContexts O.o
  1462. return fhttp->state != ISSUE;
  1463. }
  1464. // well.. we need to wait for the response to be received
  1465. if (!flipper_http_process_response_async(fhttp, fetch_lobby, parse_lobby))
  1466. {
  1467. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  1468. easy_flipper_dialog("Error", "Failed to fetch lobby details. Press BACK to return.");
  1469. flipper_http_free(fhttp);
  1470. return;
  1471. }
  1472. // start the websocket session
  1473. bool start_ws()
  1474. {
  1475. char websocket_url[128];
  1476. snprintf(websocket_url, sizeof(websocket_url), "ws://www.jblanked.com/ws/game/%s/", lobby_list[lobby_index]);
  1477. if (!flipper_http_websocket_start(fhttp, websocket_url, 80, "{\"Content-Type\":\"application/json\"}"))
  1478. {
  1479. FURI_LOG_E(TAG, "Failed to start websocket session");
  1480. flipper_http_free(fhttp);
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. bool parse_ws()
  1486. {
  1487. // for now just return true and the game will handle the rest
  1488. return fhttp->state != ISSUE;
  1489. }
  1490. // start the websocket session
  1491. if (!flipper_http_process_response_async(fhttp, start_ws, parse_ws))
  1492. {
  1493. FURI_LOG_E(TAG, "Failed to start websocket session");
  1494. easy_flipper_dialog("Error", "Failed to start websocket session. Press BACK to return.");
  1495. flipper_http_free(fhttp);
  1496. return;
  1497. }
  1498. flipper_http_free(fhttp);
  1499. // start the game thread
  1500. if (!start_game_thread(app))
  1501. {
  1502. FURI_LOG_E(TAG, "Failed to start game thread");
  1503. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1504. return;
  1505. }
  1506. }
  1507. }
  1508. static void updated_wifi_ssid(void *context)
  1509. {
  1510. FlipWorldApp *app = (FlipWorldApp *)context;
  1511. if (!app)
  1512. {
  1513. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1514. return;
  1515. }
  1516. // store the entered text
  1517. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1518. // Ensure null-termination
  1519. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1520. // save the setting
  1521. save_char("WiFi-SSID", app->text_input_buffer);
  1522. // update the variable item text
  1523. if (app->variable_item_wifi_ssid)
  1524. {
  1525. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1526. // get value of password
  1527. char pass[64];
  1528. char username[64];
  1529. char password[64];
  1530. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1531. {
  1532. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1533. {
  1534. // save the settings
  1535. load_char("Flip-Social-Username", username, sizeof(username));
  1536. load_char("Flip-Social-Password", password, sizeof(password));
  1537. save_settings(app->text_input_buffer, pass, username, password);
  1538. // initialize the http
  1539. FlipperHTTP *fhttp = flipper_http_alloc();
  1540. if (fhttp)
  1541. {
  1542. // save the wifi if the device is connected
  1543. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1544. {
  1545. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1546. }
  1547. // free the resources
  1548. flipper_http_free(fhttp);
  1549. }
  1550. else
  1551. {
  1552. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1553. }
  1554. }
  1555. }
  1556. }
  1557. // switch to the settings view
  1558. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1559. }
  1560. static void updated_wifi_pass(void *context)
  1561. {
  1562. FlipWorldApp *app = (FlipWorldApp *)context;
  1563. if (!app)
  1564. {
  1565. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1566. return;
  1567. }
  1568. // store the entered text
  1569. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1570. // Ensure null-termination
  1571. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1572. // save the setting
  1573. save_char("WiFi-Password", app->text_input_buffer);
  1574. // update the variable item text
  1575. if (app->variable_item_wifi_pass)
  1576. {
  1577. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1578. }
  1579. // get value of ssid
  1580. char ssid[64];
  1581. char username[64];
  1582. char password[64];
  1583. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1584. {
  1585. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1586. {
  1587. // save the settings
  1588. load_char("Flip-Social-Username", username, sizeof(username));
  1589. load_char("Flip-Social-Password", password, sizeof(password));
  1590. save_settings(ssid, app->text_input_buffer, username, password);
  1591. // initialize the http
  1592. FlipperHTTP *fhttp = flipper_http_alloc();
  1593. if (fhttp)
  1594. {
  1595. // save the wifi if the device is connected
  1596. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1597. {
  1598. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1599. }
  1600. // free the resources
  1601. flipper_http_free(fhttp);
  1602. }
  1603. else
  1604. {
  1605. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1606. }
  1607. }
  1608. }
  1609. // switch to the settings view
  1610. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1611. }
  1612. static void updated_username(void *context)
  1613. {
  1614. FlipWorldApp *app = (FlipWorldApp *)context;
  1615. if (!app)
  1616. {
  1617. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1618. return;
  1619. }
  1620. // store the entered text
  1621. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1622. // Ensure null-termination
  1623. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1624. // save the setting
  1625. save_char("Flip-Social-Username", app->text_input_buffer);
  1626. // update the variable item text
  1627. if (app->variable_item_user_username)
  1628. {
  1629. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1630. }
  1631. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1632. }
  1633. static void updated_password(void *context)
  1634. {
  1635. FlipWorldApp *app = (FlipWorldApp *)context;
  1636. if (!app)
  1637. {
  1638. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1639. return;
  1640. }
  1641. // store the entered text
  1642. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1643. // Ensure null-termination
  1644. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1645. // save the setting
  1646. save_char("Flip-Social-Password", app->text_input_buffer);
  1647. // update the variable item text
  1648. if (app->variable_item_user_password)
  1649. {
  1650. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1651. }
  1652. // get value of username
  1653. char username[64];
  1654. char ssid[64];
  1655. char pass[64];
  1656. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1657. {
  1658. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1659. {
  1660. // save the settings
  1661. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1662. load_char("WiFi-Password", pass, sizeof(pass));
  1663. save_settings(ssid, pass, username, app->text_input_buffer);
  1664. }
  1665. }
  1666. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1667. }
  1668. static void wifi_settings_select(void *context, uint32_t index)
  1669. {
  1670. FlipWorldApp *app = (FlipWorldApp *)context;
  1671. if (!app)
  1672. {
  1673. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1674. return;
  1675. }
  1676. char ssid[64];
  1677. char pass[64];
  1678. char username[64];
  1679. char password[64];
  1680. switch (index)
  1681. {
  1682. case 0: // Input SSID
  1683. free_all_views(app, false, false);
  1684. if (!alloc_text_input_view(app, "SSID"))
  1685. {
  1686. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1687. return;
  1688. }
  1689. // load SSID
  1690. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1691. {
  1692. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1693. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1694. }
  1695. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1696. break;
  1697. case 1: // Input Password
  1698. free_all_views(app, false, false);
  1699. if (!alloc_text_input_view(app, "Password"))
  1700. {
  1701. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1702. return;
  1703. }
  1704. // load password
  1705. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1706. {
  1707. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1708. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1709. }
  1710. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1711. break;
  1712. default:
  1713. FURI_LOG_E(TAG, "Unknown configuration item index");
  1714. break;
  1715. }
  1716. }
  1717. static void fps_change(VariableItem *item)
  1718. {
  1719. uint8_t index = variable_item_get_current_value_index(item);
  1720. fps_index = index;
  1721. variable_item_set_current_value_text(item, fps_choices_str[index]);
  1722. variable_item_set_current_value_index(item, index);
  1723. save_char("Game-FPS", fps_choices_str[index]);
  1724. }
  1725. static void screen_on_change(VariableItem *item)
  1726. {
  1727. uint8_t index = variable_item_get_current_value_index(item);
  1728. screen_always_on_index = index;
  1729. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1730. variable_item_set_current_value_index(item, index);
  1731. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1732. }
  1733. static void sound_on_change(VariableItem *item)
  1734. {
  1735. uint8_t index = variable_item_get_current_value_index(item);
  1736. sound_on_index = index;
  1737. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1738. variable_item_set_current_value_index(item, index);
  1739. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1740. }
  1741. static void vibration_on_change(VariableItem *item)
  1742. {
  1743. uint8_t index = variable_item_get_current_value_index(item);
  1744. vibration_on_index = index;
  1745. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1746. variable_item_set_current_value_index(item, index);
  1747. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1748. }
  1749. static void player_on_change(VariableItem *item)
  1750. {
  1751. uint8_t index = variable_item_get_current_value_index(item);
  1752. player_sprite_index = index;
  1753. variable_item_set_current_value_text(item, is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  1754. variable_item_set_current_value_index(item, index);
  1755. save_char("Game-Player-Sprite", player_sprite_choices[index]);
  1756. }
  1757. static void vgm_x_change(VariableItem *item)
  1758. {
  1759. uint8_t index = variable_item_get_current_value_index(item);
  1760. vgm_x_index = index;
  1761. variable_item_set_current_value_text(item, vgm_levels[index]);
  1762. variable_item_set_current_value_index(item, index);
  1763. save_char("Game-VGM-X", vgm_levels[index]);
  1764. }
  1765. static void vgm_y_change(VariableItem *item)
  1766. {
  1767. uint8_t index = variable_item_get_current_value_index(item);
  1768. vgm_y_index = index;
  1769. variable_item_set_current_value_text(item, vgm_levels[index]);
  1770. variable_item_set_current_value_index(item, index);
  1771. save_char("Game-VGM-Y", vgm_levels[index]);
  1772. }
  1773. static bool _fetch_worlds(DataLoaderModel *model)
  1774. {
  1775. if (!model || !model->fhttp)
  1776. {
  1777. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1778. return false;
  1779. }
  1780. char directory_path[128];
  1781. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1782. Storage *storage = furi_record_open(RECORD_STORAGE);
  1783. storage_common_mkdir(storage, directory_path);
  1784. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1785. storage_common_mkdir(storage, directory_path);
  1786. furi_record_close(RECORD_STORAGE);
  1787. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1788. model->fhttp->save_received_data = true;
  1789. return flipper_http_request(model->fhttp, GET, "https://www.jblanked.com/flipper/api/world/v5/get/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  1790. }
  1791. static char *_parse_worlds(DataLoaderModel *model)
  1792. {
  1793. UNUSED(model);
  1794. return "World Pack Installed";
  1795. }
  1796. static void switch_to_view_get_worlds(FlipWorldApp *app)
  1797. {
  1798. generic_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1799. }
  1800. static void game_settings_select(void *context, uint32_t index)
  1801. {
  1802. FlipWorldApp *app = (FlipWorldApp *)context;
  1803. if (!app)
  1804. {
  1805. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1806. return;
  1807. }
  1808. switch (index)
  1809. {
  1810. case 0: // Download all world data as one huge json
  1811. switch_to_view_get_worlds(app);
  1812. case 1: // Player Sprite
  1813. break;
  1814. case 2: // Change FPS
  1815. break;
  1816. case 3: // VGM X
  1817. break;
  1818. case 4: // VGM Y
  1819. break;
  1820. case 5: // Screen Always On
  1821. break;
  1822. case 6: // Sound On
  1823. break;
  1824. case 7: // Vibration On
  1825. break;
  1826. }
  1827. }
  1828. static void user_settings_select(void *context, uint32_t index)
  1829. {
  1830. FlipWorldApp *app = (FlipWorldApp *)context;
  1831. if (!app)
  1832. {
  1833. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1834. return;
  1835. }
  1836. switch (index)
  1837. {
  1838. case 0: // Username
  1839. free_all_views(app, false, false);
  1840. if (!alloc_text_input_view(app, "Username-Login"))
  1841. {
  1842. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1843. return;
  1844. }
  1845. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1846. break;
  1847. case 1: // Password
  1848. free_all_views(app, false, false);
  1849. if (!alloc_text_input_view(app, "Password-Login"))
  1850. {
  1851. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1852. return;
  1853. }
  1854. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1855. break;
  1856. }
  1857. }
  1858. static void widget_set_text(char *message, Widget **widget)
  1859. {
  1860. if (widget == NULL)
  1861. {
  1862. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  1863. DEV_CRASH();
  1864. return;
  1865. }
  1866. if (message == NULL)
  1867. {
  1868. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  1869. DEV_CRASH();
  1870. return;
  1871. }
  1872. widget_reset(*widget);
  1873. uint32_t message_length = strlen(message); // Length of the message
  1874. uint32_t i = 0; // Index tracker
  1875. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1876. char *formatted_message; // Buffer to hold the final formatted message
  1877. // Allocate buffer with double the message length plus one for safety
  1878. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1879. {
  1880. return;
  1881. }
  1882. while (i < message_length)
  1883. {
  1884. uint32_t max_line_length = 31; // Maximum characters per line
  1885. uint32_t remaining_length = message_length - i; // Remaining characters
  1886. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1887. // Check for newline character within the current segment
  1888. uint32_t newline_pos = i;
  1889. bool found_newline = false;
  1890. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1891. {
  1892. if (message[newline_pos] == '\n')
  1893. {
  1894. found_newline = true;
  1895. break;
  1896. }
  1897. }
  1898. if (found_newline)
  1899. {
  1900. // If newline found, set line_length up to the newline
  1901. line_length = newline_pos - i;
  1902. }
  1903. // Temporary buffer to hold the current line
  1904. char line[32];
  1905. strncpy(line, message + i, line_length);
  1906. line[line_length] = '\0';
  1907. // If newline was found, skip it for the next iteration
  1908. if (found_newline)
  1909. {
  1910. i += line_length + 1; // +1 to skip the '\n' character
  1911. }
  1912. else
  1913. {
  1914. // Check if the line ends in the middle of a word and adjust accordingly
  1915. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1916. {
  1917. // Find the last space within the current line to avoid breaking a word
  1918. char *last_space = strrchr(line, ' ');
  1919. if (last_space != NULL)
  1920. {
  1921. // Adjust the line_length to avoid cutting the word
  1922. line_length = last_space - line;
  1923. line[line_length] = '\0'; // Null-terminate at the space
  1924. }
  1925. }
  1926. // Move the index forward by the determined line_length
  1927. i += line_length;
  1928. // Skip any spaces at the beginning of the next line
  1929. while (i < message_length && message[i] == ' ')
  1930. {
  1931. i++;
  1932. }
  1933. }
  1934. // Manually copy the fixed line into the formatted_message buffer
  1935. for (uint32_t j = 0; j < line_length; j++)
  1936. {
  1937. formatted_message[formatted_index++] = line[j];
  1938. }
  1939. // Add a newline character for line spacing
  1940. formatted_message[formatted_index++] = '\n';
  1941. }
  1942. // Null-terminate the formatted_message
  1943. formatted_message[formatted_index] = '\0';
  1944. // Add the formatted message to the widget
  1945. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1946. }
  1947. void loader_draw_callback(Canvas *canvas, void *model)
  1948. {
  1949. if (!canvas || !model)
  1950. {
  1951. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  1952. return;
  1953. }
  1954. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1955. HTTPState http_state = data_loader_model->fhttp->state;
  1956. DataState data_state = data_loader_model->data_state;
  1957. char *title = data_loader_model->title;
  1958. canvas_set_font(canvas, FontSecondary);
  1959. if (http_state == INACTIVE)
  1960. {
  1961. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1962. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1963. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1964. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1965. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1966. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1967. return;
  1968. }
  1969. if (data_state == DataStateError || data_state == DataStateParseError)
  1970. {
  1971. error_draw(canvas, data_loader_model);
  1972. return;
  1973. }
  1974. canvas_draw_str(canvas, 0, 7, title);
  1975. canvas_draw_str(canvas, 0, 17, "Loading...");
  1976. if (data_state == DataStateInitial)
  1977. {
  1978. return;
  1979. }
  1980. if (http_state == SENDING)
  1981. {
  1982. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1983. return;
  1984. }
  1985. if (http_state == RECEIVING || data_state == DataStateRequested)
  1986. {
  1987. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1988. return;
  1989. }
  1990. if (http_state == IDLE && data_state == DataStateReceived)
  1991. {
  1992. canvas_draw_str(canvas, 0, 27, "Processing...");
  1993. return;
  1994. }
  1995. if (http_state == IDLE && data_state == DataStateParsed)
  1996. {
  1997. canvas_draw_str(canvas, 0, 27, "Processed...");
  1998. return;
  1999. }
  2000. }
  2001. static void loader_process_callback(void *context)
  2002. {
  2003. if (context == NULL)
  2004. {
  2005. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  2006. DEV_CRASH();
  2007. return;
  2008. }
  2009. FlipWorldApp *app = (FlipWorldApp *)context;
  2010. View *view = app->view_loader;
  2011. DataState current_data_state;
  2012. DataLoaderModel *loader_model = NULL;
  2013. with_view_model(
  2014. view,
  2015. DataLoaderModel * model,
  2016. {
  2017. current_data_state = model->data_state;
  2018. loader_model = model;
  2019. },
  2020. false);
  2021. if (!loader_model || !loader_model->fhttp)
  2022. {
  2023. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  2024. DEV_CRASH();
  2025. return;
  2026. }
  2027. if (current_data_state == DataStateInitial)
  2028. {
  2029. with_view_model(
  2030. view,
  2031. DataLoaderModel * model,
  2032. {
  2033. model->data_state = DataStateRequested;
  2034. DataLoaderFetch fetch = model->fetcher;
  2035. if (fetch == NULL)
  2036. {
  2037. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  2038. model->data_state = DataStateError;
  2039. return;
  2040. }
  2041. // Clear any previous responses
  2042. strncpy(model->fhttp->last_response, "", 1);
  2043. bool request_status = fetch(model);
  2044. if (!request_status)
  2045. {
  2046. model->data_state = DataStateError;
  2047. }
  2048. },
  2049. true);
  2050. }
  2051. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  2052. {
  2053. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  2054. {
  2055. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  2056. {
  2057. FURI_LOG_DEV(TAG, "PONG received.");
  2058. }
  2059. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  2060. {
  2061. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2062. }
  2063. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  2064. {
  2065. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2066. }
  2067. else if (strlen(loader_model->fhttp->last_response))
  2068. {
  2069. // Still waiting on response
  2070. }
  2071. else
  2072. {
  2073. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  2074. }
  2075. }
  2076. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  2077. {
  2078. // continue waiting
  2079. }
  2080. else if (loader_model->fhttp->state == INACTIVE)
  2081. {
  2082. // inactive. try again
  2083. }
  2084. else if (loader_model->fhttp->state == ISSUE)
  2085. {
  2086. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  2087. }
  2088. else
  2089. {
  2090. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2091. DEV_CRASH();
  2092. }
  2093. }
  2094. else if (current_data_state == DataStateReceived)
  2095. {
  2096. with_view_model(
  2097. view,
  2098. DataLoaderModel * model,
  2099. {
  2100. char *data_text;
  2101. if (model->parser == NULL)
  2102. {
  2103. data_text = NULL;
  2104. FURI_LOG_DEV(TAG, "Parser is NULL");
  2105. DEV_CRASH();
  2106. }
  2107. else
  2108. {
  2109. data_text = model->parser(model);
  2110. }
  2111. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  2112. model->data_text = data_text;
  2113. if (data_text == NULL)
  2114. {
  2115. model->data_state = DataStateParseError;
  2116. }
  2117. else
  2118. {
  2119. model->data_state = DataStateParsed;
  2120. }
  2121. },
  2122. true);
  2123. }
  2124. else if (current_data_state == DataStateParsed)
  2125. {
  2126. with_view_model(
  2127. view,
  2128. DataLoaderModel * model,
  2129. {
  2130. if (++model->request_index < model->request_count)
  2131. {
  2132. model->data_state = DataStateInitial;
  2133. }
  2134. else
  2135. {
  2136. widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  2137. if (model->data_text != NULL)
  2138. {
  2139. free(model->data_text);
  2140. model->data_text = NULL;
  2141. }
  2142. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  2143. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  2144. }
  2145. },
  2146. true);
  2147. }
  2148. }
  2149. static void loader_timer_callback(void *context)
  2150. {
  2151. if (context == NULL)
  2152. {
  2153. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  2154. DEV_CRASH();
  2155. return;
  2156. }
  2157. FlipWorldApp *app = (FlipWorldApp *)context;
  2158. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  2159. }
  2160. static void loader_on_enter(void *context)
  2161. {
  2162. if (context == NULL)
  2163. {
  2164. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  2165. DEV_CRASH();
  2166. return;
  2167. }
  2168. FlipWorldApp *app = (FlipWorldApp *)context;
  2169. View *view = app->view_loader;
  2170. with_view_model(
  2171. view,
  2172. DataLoaderModel * model,
  2173. {
  2174. view_set_previous_callback(view, model->back_callback);
  2175. if (model->timer == NULL)
  2176. {
  2177. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  2178. }
  2179. furi_timer_start(model->timer, 250);
  2180. },
  2181. true);
  2182. }
  2183. static void loader_on_exit(void *context)
  2184. {
  2185. if (context == NULL)
  2186. {
  2187. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  2188. DEV_CRASH();
  2189. return;
  2190. }
  2191. FlipWorldApp *app = (FlipWorldApp *)context;
  2192. View *view = app->view_loader;
  2193. with_view_model(
  2194. view,
  2195. DataLoaderModel * model,
  2196. {
  2197. if (model->timer)
  2198. {
  2199. furi_timer_stop(model->timer);
  2200. }
  2201. },
  2202. false);
  2203. }
  2204. void loader_init(View *view)
  2205. {
  2206. if (view == NULL)
  2207. {
  2208. FURI_LOG_E(TAG, "loader_init - view is NULL");
  2209. DEV_CRASH();
  2210. return;
  2211. }
  2212. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  2213. view_set_enter_callback(view, loader_on_enter);
  2214. view_set_exit_callback(view, loader_on_exit);
  2215. }
  2216. void loader_free_model(View *view)
  2217. {
  2218. if (view == NULL)
  2219. {
  2220. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  2221. DEV_CRASH();
  2222. return;
  2223. }
  2224. with_view_model(
  2225. view,
  2226. DataLoaderModel * model,
  2227. {
  2228. if (model->timer)
  2229. {
  2230. furi_timer_free(model->timer);
  2231. model->timer = NULL;
  2232. }
  2233. if (model->parser_context)
  2234. {
  2235. // do not free the context here, it is the app context
  2236. // free(model->parser_context);
  2237. // model->parser_context = NULL;
  2238. }
  2239. if (model->fhttp)
  2240. {
  2241. flipper_http_free(model->fhttp);
  2242. model->fhttp = NULL;
  2243. }
  2244. },
  2245. false);
  2246. }
  2247. bool custom_event_callback(void *context, uint32_t index)
  2248. {
  2249. if (context == NULL)
  2250. {
  2251. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  2252. DEV_CRASH();
  2253. return false;
  2254. }
  2255. switch (index)
  2256. {
  2257. case FlipWorldCustomEventProcess:
  2258. loader_process_callback(context);
  2259. return true;
  2260. default:
  2261. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  2262. return false;
  2263. }
  2264. }
  2265. void generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  2266. {
  2267. if (app == NULL)
  2268. {
  2269. FURI_LOG_E(TAG, "generic_switch_to_view - app is NULL");
  2270. DEV_CRASH();
  2271. return;
  2272. }
  2273. View *view = app->view_loader;
  2274. if (view == NULL)
  2275. {
  2276. FURI_LOG_E(TAG, "generic_switch_to_view - view is NULL");
  2277. DEV_CRASH();
  2278. return;
  2279. }
  2280. with_view_model(
  2281. view,
  2282. DataLoaderModel * model,
  2283. {
  2284. model->title = title;
  2285. model->fetcher = fetcher;
  2286. model->parser = parser;
  2287. model->request_index = 0;
  2288. model->request_count = request_count;
  2289. model->back_callback = back;
  2290. model->data_state = DataStateInitial;
  2291. model->data_text = NULL;
  2292. //
  2293. model->parser_context = app;
  2294. if (!model->fhttp)
  2295. {
  2296. model->fhttp = flipper_http_alloc();
  2297. }
  2298. },
  2299. true);
  2300. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2301. }