callback.c 74 KB

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