callback.c 97 KB

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