callback.c 72 KB

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