callback.c 79 KB

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