callback.c 81 KB

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