callback.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  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 char *lobby_list[10];
  586. static void free_submenu_lobby(void *context)
  587. {
  588. FlipWorldApp *app = (FlipWorldApp *)context;
  589. if (!app)
  590. {
  591. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  592. return;
  593. }
  594. if (app->submenu_lobby)
  595. {
  596. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewLobby);
  597. submenu_free(app->submenu_lobby);
  598. app->submenu_lobby = NULL;
  599. }
  600. for (int i = 0; i < 10; i++)
  601. {
  602. if (lobby_list[i])
  603. {
  604. free(lobby_list[i]);
  605. lobby_list[i] = NULL;
  606. }
  607. }
  608. }
  609. // free
  610. static void free_message_view(void *context)
  611. {
  612. FlipWorldApp *app = (FlipWorldApp *)context;
  613. if (!app)
  614. {
  615. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  616. return;
  617. }
  618. if (app->view_message)
  619. {
  620. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMessage);
  621. view_free(app->view_message);
  622. app->view_message = NULL;
  623. }
  624. }
  625. static void free_text_input_view(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->text_input)
  634. {
  635. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  636. uart_text_input_free(app->text_input);
  637. app->text_input = NULL;
  638. }
  639. if (app->text_input_buffer)
  640. {
  641. free(app->text_input_buffer);
  642. app->text_input_buffer = NULL;
  643. }
  644. if (app->text_input_temp_buffer)
  645. {
  646. free(app->text_input_temp_buffer);
  647. app->text_input_temp_buffer = NULL;
  648. }
  649. }
  650. static void free_variable_item_list(void *context)
  651. {
  652. FlipWorldApp *app = (FlipWorldApp *)context;
  653. if (!app)
  654. {
  655. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  656. return;
  657. }
  658. if (app->variable_item_list)
  659. {
  660. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  661. variable_item_list_free(app->variable_item_list);
  662. app->variable_item_list = NULL;
  663. }
  664. if (app->variable_item_wifi_ssid)
  665. {
  666. free(app->variable_item_wifi_ssid);
  667. app->variable_item_wifi_ssid = NULL;
  668. }
  669. if (app->variable_item_wifi_pass)
  670. {
  671. free(app->variable_item_wifi_pass);
  672. app->variable_item_wifi_pass = NULL;
  673. }
  674. if (app->variable_item_game_fps)
  675. {
  676. free(app->variable_item_game_fps);
  677. app->variable_item_game_fps = NULL;
  678. }
  679. if (app->variable_item_game_screen_always_on)
  680. {
  681. free(app->variable_item_game_screen_always_on);
  682. app->variable_item_game_screen_always_on = NULL;
  683. }
  684. if (app->variable_item_game_download_world)
  685. {
  686. free(app->variable_item_game_download_world);
  687. app->variable_item_game_download_world = NULL;
  688. }
  689. if (app->variable_item_game_sound_on)
  690. {
  691. free(app->variable_item_game_sound_on);
  692. app->variable_item_game_sound_on = NULL;
  693. }
  694. if (app->variable_item_game_vibration_on)
  695. {
  696. free(app->variable_item_game_vibration_on);
  697. app->variable_item_game_vibration_on = NULL;
  698. }
  699. if (app->variable_item_game_player_sprite)
  700. {
  701. free(app->variable_item_game_player_sprite);
  702. app->variable_item_game_player_sprite = NULL;
  703. }
  704. if (app->variable_item_game_vgm_x)
  705. {
  706. free(app->variable_item_game_vgm_x);
  707. app->variable_item_game_vgm_x = NULL;
  708. }
  709. if (app->variable_item_game_vgm_y)
  710. {
  711. free(app->variable_item_game_vgm_y);
  712. app->variable_item_game_vgm_y = NULL;
  713. }
  714. if (app->variable_item_user_username)
  715. {
  716. free(app->variable_item_user_username);
  717. app->variable_item_user_username = NULL;
  718. }
  719. if (app->variable_item_user_password)
  720. {
  721. free(app->variable_item_user_password);
  722. app->variable_item_user_password = NULL;
  723. }
  724. }
  725. static void free_submenu_settings(void *context)
  726. {
  727. FlipWorldApp *app = (FlipWorldApp *)context;
  728. if (!app)
  729. {
  730. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  731. return;
  732. }
  733. if (app->submenu_settings)
  734. {
  735. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  736. submenu_free(app->submenu_settings);
  737. app->submenu_settings = NULL;
  738. }
  739. }
  740. static FuriThread *game_thread;
  741. static bool game_thread_running = false;
  742. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  743. {
  744. FlipWorldApp *app = (FlipWorldApp *)context;
  745. if (!app)
  746. {
  747. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  748. return;
  749. }
  750. if (should_free_variable_item_list)
  751. {
  752. free_variable_item_list(app);
  753. }
  754. free_message_view(app);
  755. free_text_input_view(app);
  756. // free game thread
  757. if (game_thread_running)
  758. {
  759. game_thread_running = false;
  760. if (game_thread)
  761. {
  762. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  763. furi_thread_join(game_thread);
  764. furi_thread_free(game_thread);
  765. game_thread = NULL;
  766. }
  767. }
  768. if (should_free_submenu_settings)
  769. {
  770. free_submenu_settings(app);
  771. free_submenu_lobby(app);
  772. }
  773. // free Derek's loader
  774. free_view_loader(app);
  775. }
  776. static bool fetch_world_list(FlipperHTTP *fhttp)
  777. {
  778. if (!fhttp)
  779. {
  780. FURI_LOG_E(TAG, "fhttp is NULL");
  781. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  782. return false;
  783. }
  784. // ensure flip_world directory exists
  785. char directory_path[128];
  786. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  787. Storage *storage = furi_record_open(RECORD_STORAGE);
  788. storage_common_mkdir(storage, directory_path);
  789. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  790. storage_common_mkdir(storage, directory_path);
  791. furi_record_close(RECORD_STORAGE);
  792. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  793. fhttp->save_received_data = true;
  794. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/v5/list/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  795. }
  796. // we will load the palyer stats from the API and save them
  797. // in player_spawn game method, it will load the player stats that we saved
  798. static bool fetch_player_stats(FlipperHTTP *fhttp)
  799. {
  800. if (!fhttp)
  801. {
  802. FURI_LOG_E(TAG, "fhttp is NULL");
  803. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  804. return false;
  805. }
  806. char username[64];
  807. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  808. {
  809. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  810. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  811. return false;
  812. }
  813. char url[128];
  814. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/game-stats/%s/", username);
  815. // ensure the folders exist
  816. char directory_path[128];
  817. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  818. Storage *storage = furi_record_open(RECORD_STORAGE);
  819. storage_common_mkdir(storage, directory_path);
  820. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  821. storage_common_mkdir(storage, directory_path);
  822. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player");
  823. storage_common_mkdir(storage, directory_path);
  824. furi_record_close(RECORD_STORAGE);
  825. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  826. fhttp->save_received_data = true;
  827. return flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  828. }
  829. // static bool fetch_app_update(FlipperHTTP *fhttp)
  830. // {
  831. // if (!fhttp)
  832. // {
  833. // FURI_LOG_E(TAG, "fhttp is NULL");
  834. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  835. // return false;
  836. // }
  837. // return flipper_http_get_request_with_headers(fhttp, "https://www.jblanked.com/flipper/api/app/last-updated/flip_world/", "{\"Content-Type\":\"application/json\"}");
  838. // }
  839. // static bool parse_app_update(FlipperHTTP *fhttp)
  840. // {
  841. // if (!fhttp)
  842. // {
  843. // FURI_LOG_E(TAG, "fhttp is NULL");
  844. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  845. // return false;
  846. // }
  847. // if (fhttp->last_response == NULL || strlen(fhttp->last_response) == 0)
  848. // {
  849. // FURI_LOG_E(TAG, "fhttp->last_response is NULL or empty");
  850. // easy_flipper_dialog("Error", "fhttp->last_response is NULL or empty. Press BACK to return.");
  851. // return false;
  852. // }
  853. // bool last_update_available = false;
  854. // char last_updated_old[32];
  855. // // load the previous last_updated
  856. // if (!load_char("last_updated", last_updated_old, sizeof(last_updated_old)))
  857. // {
  858. // FURI_LOG_E(TAG, "Failed to load last_updated");
  859. // // it's okay, we'll just update it
  860. // }
  861. // // save the new last_updated
  862. // save_char("last_updated", fhttp->last_response);
  863. // // compare the two
  864. // if (strlen(last_updated_old) == 0 || !is_str(last_updated_old, fhttp->last_response))
  865. // {
  866. // last_update_available = true;
  867. // }
  868. // if (last_update_available)
  869. // {
  870. // easy_flipper_dialog("Update Available", "An update is available. Press OK to update.");
  871. // return true;
  872. // }
  873. // else
  874. // {
  875. // easy_flipper_dialog("No Update Available", "No update is available. Press OK to continue.");
  876. // return false;
  877. // }
  878. // }
  879. static bool start_game_thread(void *context)
  880. {
  881. FlipWorldApp *app = (FlipWorldApp *)context;
  882. if (!app)
  883. {
  884. FURI_LOG_E(TAG, "app is NULL");
  885. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  886. return false;
  887. }
  888. // free everything but message_view
  889. free_variable_item_list(app);
  890. free_text_input_view(app);
  891. free_submenu_settings(app);
  892. free_submenu_lobby(app);
  893. free_view_loader(app);
  894. // free game thread
  895. if (game_thread_running)
  896. {
  897. game_thread_running = false;
  898. if (game_thread)
  899. {
  900. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  901. furi_thread_join(game_thread);
  902. furi_thread_free(game_thread);
  903. }
  904. }
  905. // start game thread
  906. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  907. if (!thread)
  908. {
  909. FURI_LOG_E(TAG, "Failed to allocate game thread");
  910. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  911. return false;
  912. }
  913. furi_thread_start(thread);
  914. game_thread = thread;
  915. game_thread_running = true;
  916. return true;
  917. }
  918. // combine register, login, and world list fetch into one function to switch to the loader view
  919. static bool _fetch_game(DataLoaderModel *model)
  920. {
  921. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  922. if (!app)
  923. {
  924. FURI_LOG_E(TAG, "app is NULL");
  925. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  926. return false;
  927. }
  928. if (model->request_index == 0)
  929. {
  930. // login
  931. char username[64];
  932. char password[64];
  933. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  934. {
  935. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  936. view_dispatcher_switch_to_view(app->view_dispatcher,
  937. FlipWorldViewSubmenu); // just go back to the main menu for now
  938. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  939. return false;
  940. }
  941. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  942. {
  943. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  944. view_dispatcher_switch_to_view(app->view_dispatcher,
  945. FlipWorldViewSubmenu); // just go back to the main menu for now
  946. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  947. return false;
  948. }
  949. char payload[256];
  950. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  951. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  952. }
  953. else if (model->request_index == 1)
  954. {
  955. // check if login was successful
  956. char is_logged_in[8];
  957. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  958. {
  959. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  960. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  961. view_dispatcher_switch_to_view(app->view_dispatcher,
  962. FlipWorldViewSubmenu); // just go back to the main menu for now
  963. return false;
  964. }
  965. if (is_str(is_logged_in, "false") && is_str(model->title, "Registering..."))
  966. {
  967. // register
  968. char username[64];
  969. char password[64];
  970. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  971. {
  972. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  973. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  974. view_dispatcher_switch_to_view(app->view_dispatcher,
  975. FlipWorldViewSubmenu); // just go back to the main menu for now
  976. return false;
  977. }
  978. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  979. {
  980. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  981. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  982. view_dispatcher_switch_to_view(app->view_dispatcher,
  983. FlipWorldViewSubmenu); // just go back to the main menu for now
  984. return false;
  985. }
  986. char payload[172];
  987. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  988. model->title = "Registering...";
  989. return flipper_http_request(model->fhttp, POST, "https://www.jblanked.com/flipper/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  990. }
  991. else
  992. {
  993. model->title = "Fetching World List..";
  994. return fetch_world_list(model->fhttp);
  995. }
  996. }
  997. else if (model->request_index == 2)
  998. {
  999. model->title = "Fetching World List..";
  1000. return fetch_world_list(model->fhttp);
  1001. }
  1002. else if (model->request_index == 3)
  1003. {
  1004. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  1005. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  1006. if (!world_list)
  1007. {
  1008. view_dispatcher_switch_to_view(app->view_dispatcher,
  1009. FlipWorldViewSubmenu); // just go back to the main menu for now
  1010. FURI_LOG_E(TAG, "Failed to load world list");
  1011. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  1012. return false;
  1013. }
  1014. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  1015. if (!first_world)
  1016. {
  1017. view_dispatcher_switch_to_view(app->view_dispatcher,
  1018. FlipWorldViewSubmenu); // just go back to the main menu for now
  1019. FURI_LOG_E(TAG, "Failed to get first world");
  1020. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  1021. furi_string_free(world_list);
  1022. return false;
  1023. }
  1024. if (world_exists(furi_string_get_cstr(first_world)))
  1025. {
  1026. furi_string_free(world_list);
  1027. furi_string_free(first_world);
  1028. if (!start_game_thread(app))
  1029. {
  1030. FURI_LOG_E(TAG, "Failed to start game thread");
  1031. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1032. view_dispatcher_switch_to_view(app->view_dispatcher,
  1033. FlipWorldViewSubmenu); // just go back to the main menu for now
  1034. return "Failed to start game thread";
  1035. }
  1036. return true;
  1037. }
  1038. 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));
  1039. model->fhttp->save_received_data = true;
  1040. char url[128];
  1041. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v5/get/world/%s/", furi_string_get_cstr(first_world));
  1042. furi_string_free(world_list);
  1043. furi_string_free(first_world);
  1044. return flipper_http_request(model->fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL);
  1045. }
  1046. FURI_LOG_E(TAG, "Unknown request index");
  1047. return false;
  1048. }
  1049. static char *_parse_game(DataLoaderModel *model)
  1050. {
  1051. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  1052. if (model->request_index == 0)
  1053. {
  1054. if (!model->fhttp->last_response)
  1055. {
  1056. save_char("is_logged_in", "false");
  1057. // Go back to the main menu
  1058. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  1059. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1060. return "Response is empty...";
  1061. }
  1062. // Check for successful conditions
  1063. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  1064. {
  1065. save_char("is_logged_in", "true");
  1066. model->title = "Login successful!";
  1067. model->title = "Fetching World List..";
  1068. return "Login successful!";
  1069. }
  1070. // Check if user not found
  1071. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  1072. {
  1073. save_char("is_logged_in", "false");
  1074. model->title = "Registering...";
  1075. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  1076. }
  1077. // If not success, not found, check length conditions
  1078. size_t resp_len = strlen(model->fhttp->last_response);
  1079. if (resp_len == 0 || resp_len > 127)
  1080. {
  1081. // Empty or too long means failed login
  1082. save_char("is_logged_in", "false");
  1083. // Go back to the main menu
  1084. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1085. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1086. return "Failed to login...";
  1087. }
  1088. // Handle any other unknown response as a failure
  1089. save_char("is_logged_in", "false");
  1090. // Go back to the main menu
  1091. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1092. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1093. return "Failed to login...";
  1094. }
  1095. else if (model->request_index == 1)
  1096. {
  1097. if (is_str(model->title, "Registering..."))
  1098. {
  1099. // check registration response
  1100. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  1101. {
  1102. save_char("is_logged_in", "true");
  1103. char username[64];
  1104. char password[64];
  1105. // load the username and password, then save them
  1106. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  1107. {
  1108. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  1109. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  1110. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1111. return "Failed to load Flip-Social-Username";
  1112. }
  1113. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  1114. {
  1115. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  1116. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  1117. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1118. return "Failed to load Flip-Social-Password";
  1119. }
  1120. // load wifi ssid,pass then save
  1121. char ssid[64];
  1122. char pass[64];
  1123. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1124. {
  1125. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  1126. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  1127. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1128. return "Failed to load WiFi-SSID";
  1129. }
  1130. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  1131. {
  1132. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  1133. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  1134. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1135. return "Failed to load WiFi-Password";
  1136. }
  1137. save_settings(ssid, pass, username, password);
  1138. model->title = "Fetching World List..";
  1139. return "Account created!";
  1140. }
  1141. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  1142. {
  1143. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  1144. view_dispatcher_switch_to_view(app->view_dispatcher,
  1145. FlipWorldViewSubmenu); // just go back to the main menu for now
  1146. return "Please enter your credentials.";
  1147. }
  1148. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  1149. {
  1150. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  1151. view_dispatcher_switch_to_view(app->view_dispatcher,
  1152. FlipWorldViewSubmenu); // just go back to the main menu for now
  1153. return "Username already exists.";
  1154. }
  1155. else
  1156. {
  1157. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  1158. view_dispatcher_switch_to_view(app->view_dispatcher,
  1159. FlipWorldViewSubmenu); // just go back to the main menu for now
  1160. return "Registration failed...";
  1161. }
  1162. }
  1163. else
  1164. {
  1165. if (!start_game_thread(app))
  1166. {
  1167. FURI_LOG_E(TAG, "Failed to start game thread");
  1168. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1169. view_dispatcher_switch_to_view(app->view_dispatcher,
  1170. FlipWorldViewSubmenu); // just go back to the main menu for now
  1171. return "Failed to start game thread";
  1172. }
  1173. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1174. }
  1175. }
  1176. else if (model->request_index == 2)
  1177. {
  1178. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1179. }
  1180. else if (model->request_index == 3)
  1181. {
  1182. if (!start_game_thread(app))
  1183. {
  1184. FURI_LOG_E(TAG, "Failed to start game thread");
  1185. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1186. view_dispatcher_switch_to_view(app->view_dispatcher,
  1187. FlipWorldViewSubmenu); // just go back to the main menu for now
  1188. return "Failed to start game thread";
  1189. }
  1190. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1191. }
  1192. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  1193. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1194. return "Unknown error";
  1195. }
  1196. static void switch_to_view_get_game(FlipWorldApp *app)
  1197. {
  1198. if (!alloc_view_loader(app))
  1199. {
  1200. FURI_LOG_E(TAG, "Failed to allocate view loader");
  1201. return;
  1202. }
  1203. generic_switch_to_view(app, "Starting Game..", _fetch_game, _parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  1204. }
  1205. static void run(FlipWorldApp *app)
  1206. {
  1207. if (!app)
  1208. {
  1209. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1210. return;
  1211. }
  1212. free_all_views(app, true, true);
  1213. if (!is_enough_heap(60000))
  1214. {
  1215. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  1216. return;
  1217. }
  1218. // check if logged in
  1219. if (is_logged_in() || is_logged_in_to_flip_social())
  1220. {
  1221. FlipperHTTP *fhttp = flipper_http_alloc();
  1222. if (!fhttp)
  1223. {
  1224. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1225. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1226. return;
  1227. }
  1228. bool fetch_world_list_i()
  1229. {
  1230. return fetch_world_list(fhttp);
  1231. }
  1232. bool parse_world_list_i()
  1233. {
  1234. return fhttp->state != ISSUE;
  1235. }
  1236. bool fetch_player_stats_i()
  1237. {
  1238. return fetch_player_stats(fhttp);
  1239. }
  1240. if (!alloc_message_view(app, MessageStateLoading))
  1241. {
  1242. FURI_LOG_E(TAG, "Failed to allocate message view");
  1243. return;
  1244. }
  1245. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1246. // Make the request
  1247. if (game_mode_index != 1) // not GAME_MODE_PVP
  1248. {
  1249. 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))
  1250. {
  1251. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1252. flipper_http_free(fhttp);
  1253. }
  1254. else
  1255. {
  1256. flipper_http_free(fhttp);
  1257. }
  1258. if (!start_game_thread(app))
  1259. {
  1260. FURI_LOG_E(TAG, "Failed to start game thread");
  1261. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1262. return;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. // load pvp info (this returns the lobbies available)
  1268. bool fetch_pvp_lobbies()
  1269. {
  1270. // ensure flip_world directory exists
  1271. char directory_path[128];
  1272. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1273. Storage *storage = furi_record_open(RECORD_STORAGE);
  1274. storage_common_mkdir(storage, directory_path);
  1275. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp");
  1276. storage_common_mkdir(storage, directory_path);
  1277. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies");
  1278. storage_common_mkdir(storage, directory_path);
  1279. furi_record_close(RECORD_STORAGE);
  1280. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/pvp_lobbies.json");
  1281. fhttp->save_received_data = true;
  1282. // 2 players max, 10 lobbies
  1283. return flipper_http_request(fhttp, GET, "https://www.jblanked.com/flipper/api/world/pvp/lobbies/2/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  1284. }
  1285. bool parse_pvp_lobbies()
  1286. {
  1287. free_submenu_lobby(app);
  1288. if (!alloc_submenu_lobby(app))
  1289. {
  1290. FURI_LOG_E(TAG, "Failed to allocate lobby submenu");
  1291. return false;
  1292. }
  1293. // add the lobbies to the submenu
  1294. FuriString *lobbies = flipper_http_load_from_file(fhttp->file_path);
  1295. if (!lobbies)
  1296. {
  1297. FURI_LOG_E(TAG, "Failed to load lobbies");
  1298. return false;
  1299. }
  1300. // parse the lobbies
  1301. for (uint32_t i = 0; i < 10; i++)
  1302. {
  1303. FuriString *lobby = get_json_array_value_furi("lobbies", i, lobbies);
  1304. if (!lobby)
  1305. {
  1306. FURI_LOG_I(TAG, "No more lobbies");
  1307. break;
  1308. }
  1309. FuriString *lobby_id = get_json_value_furi("id", lobby);
  1310. if (!lobby_id)
  1311. {
  1312. FURI_LOG_E(TAG, "Failed to get lobby id");
  1313. furi_string_free(lobby);
  1314. return false;
  1315. }
  1316. // add the lobby to the submenu
  1317. submenu_add_item(app->submenu_lobby, furi_string_get_cstr(lobby_id), FlipWorldSubmenuIndexLobby + i, callback_submenu_lobby_choices, app);
  1318. // add the lobby to the list
  1319. if (!easy_flipper_set_buffer(&lobby_list[i], 64))
  1320. {
  1321. FURI_LOG_E(TAG, "Failed to allocate lobby list");
  1322. furi_string_free(lobby);
  1323. furi_string_free(lobby_id);
  1324. return false;
  1325. }
  1326. snprintf(lobby_list[i], 64, "%s", furi_string_get_cstr(lobby_id));
  1327. furi_string_free(lobby);
  1328. furi_string_free(lobby_id);
  1329. }
  1330. furi_string_free(lobbies);
  1331. return true;
  1332. }
  1333. // load pvp lobbies and player stats
  1334. 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))
  1335. {
  1336. // unlike the pve/story, receiving data is necessary
  1337. // so send the user back to the main menu if it fails
  1338. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1339. easy_flipper_dialog("Error", "Failed to make request. Press BACK to return.");
  1340. view_dispatcher_switch_to_view(app->view_dispatcher,
  1341. FlipWorldViewSubmenu);
  1342. flipper_http_free(fhttp);
  1343. }
  1344. else
  1345. {
  1346. flipper_http_free(fhttp);
  1347. }
  1348. // switch to the lobby submenu
  1349. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewLobby);
  1350. }
  1351. }
  1352. else
  1353. {
  1354. switch_to_view_get_game(app);
  1355. }
  1356. }
  1357. void callback_submenu_choices(void *context, uint32_t index)
  1358. {
  1359. FlipWorldApp *app = (FlipWorldApp *)context;
  1360. if (!app)
  1361. {
  1362. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1363. return;
  1364. }
  1365. switch (index)
  1366. {
  1367. case FlipWorldSubmenuIndexGameSubmenu:
  1368. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewGameSubmenu);
  1369. break;
  1370. case FlipWorldSubmenuIndexStory:
  1371. game_mode_index = 2; // GAME_MODE_STORY
  1372. run(app);
  1373. break;
  1374. case FlipWorldSubmenuIndexPvP:
  1375. game_mode_index = 1; // GAME_MODE_PVP
  1376. run(app);
  1377. break;
  1378. case FlipWorldSubmenuIndexPvE:
  1379. game_mode_index = 0; // GAME_MODE_PVE
  1380. run(app);
  1381. break;
  1382. case FlipWorldSubmenuIndexMessage:
  1383. // About menu.
  1384. free_all_views(app, true, true);
  1385. if (!alloc_message_view(app, MessageStateAbout))
  1386. {
  1387. FURI_LOG_E(TAG, "Failed to allocate message view");
  1388. return;
  1389. }
  1390. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1391. break;
  1392. case FlipWorldSubmenuIndexSettings:
  1393. free_all_views(app, true, true);
  1394. if (!alloc_submenu_settings(app))
  1395. {
  1396. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1397. return;
  1398. }
  1399. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1400. break;
  1401. case FlipWorldSubmenuIndexWiFiSettings:
  1402. free_all_views(app, true, false);
  1403. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1404. {
  1405. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1406. return;
  1407. }
  1408. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1409. break;
  1410. case FlipWorldSubmenuIndexGameSettings:
  1411. free_all_views(app, true, false);
  1412. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1413. {
  1414. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1415. return;
  1416. }
  1417. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1418. break;
  1419. case FlipWorldSubmenuIndexUserSettings:
  1420. free_all_views(app, true, false);
  1421. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1422. {
  1423. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1424. return;
  1425. }
  1426. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1427. break;
  1428. default:
  1429. break;
  1430. }
  1431. }
  1432. static void callback_submenu_lobby_choices(void *context, uint32_t index)
  1433. {
  1434. /* Handle other game lobbies
  1435. 1. when clicked on, send request to fetch the selected game lobby details
  1436. 2. start the websocket session
  1437. 3. start the game thread (the rest will be handled by game_start and player_update)
  1438. */
  1439. FlipWorldApp *app = (FlipWorldApp *)context;
  1440. if (!app)
  1441. {
  1442. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1443. return;
  1444. }
  1445. if (index >= FlipWorldSubmenuIndexLobby && index < FlipWorldSubmenuIndexLobby + 10)
  1446. {
  1447. uint32_t lobby_index = index - FlipWorldSubmenuIndexLobby;
  1448. FlipperHTTP *fhttp = flipper_http_alloc();
  1449. if (!fhttp)
  1450. {
  1451. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1452. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1453. return;
  1454. }
  1455. // send the request to fetch the lobby details
  1456. char url[128];
  1457. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/%s/", lobby_list[lobby_index]);
  1458. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/pvp/lobbies/%s.json", lobby_list[lobby_index]);
  1459. fhttp->save_received_data = true;
  1460. bool fetch_lobby()
  1461. {
  1462. if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\":\"application/json\"}", NULL))
  1463. {
  1464. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  1465. flipper_http_free(fhttp);
  1466. return false;
  1467. }
  1468. return true;
  1469. }
  1470. bool parse_lobby()
  1471. {
  1472. /*
  1473. expected output from fhttp->file_path
  1474. {"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":{}}
  1475. expected out to save
  1476. {"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"}]}
  1477. */
  1478. // 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
  1479. // saved at STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s/%s_json_data.json
  1480. // ensure flip_world directory exists
  1481. char directory_path[128];
  1482. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1483. Storage *storage = furi_record_open(RECORD_STORAGE);
  1484. storage_common_mkdir(storage, directory_path);
  1485. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1486. storage_common_mkdir(storage, directory_path);
  1487. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/pvp_world");
  1488. storage_common_mkdir(storage, directory_path);
  1489. furi_record_close(RECORD_STORAGE);
  1490. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/pvp_world/pvp_world_enemy_data.json");
  1491. FuriString *lobby = flipper_http_load_from_file(fhttp->file_path);
  1492. if (!lobby)
  1493. {
  1494. FURI_LOG_E(TAG, "Failed to load lobby details");
  1495. flipper_http_free(fhttp);
  1496. return false;
  1497. }
  1498. // parse the lobby details
  1499. FuriString *player_stats = get_json_array_value_furi("player_stats", 0, lobby);
  1500. if (!player_stats)
  1501. {
  1502. FURI_LOG_E(TAG, "Failed to get player stats");
  1503. furi_string_free(lobby);
  1504. flipper_http_free(fhttp);
  1505. return false;
  1506. }
  1507. // available keys from player_stats
  1508. FuriString *username = get_json_value_furi("username", player_stats);
  1509. FuriString *strength = get_json_value_furi("strength", player_stats);
  1510. FuriString *health = get_json_value_furi("health", player_stats);
  1511. FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
  1512. if (!username || !strength || !health || !attack_timer)
  1513. {
  1514. FURI_LOG_E(TAG, "Failed to get player stats");
  1515. furi_string_free(player_stats);
  1516. furi_string_free(lobby);
  1517. flipper_http_free(fhttp);
  1518. return false;
  1519. }
  1520. // create enemy data
  1521. FuriString *enemy_data = furi_string_alloc();
  1522. 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}]}",
  1523. furi_string_get_cstr(attack_timer), furi_string_get_cstr(strength), furi_string_get_cstr(health));
  1524. File *file = storage_file_alloc(storage);
  1525. if (!storage_file_open(file, directory_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1526. {
  1527. FURI_LOG_E("Game", "Failed to open file for writing: %s", directory_path);
  1528. storage_file_free(file);
  1529. furi_record_close(RECORD_STORAGE);
  1530. furi_string_free(enemy_data);
  1531. furi_string_free(player_stats);
  1532. furi_string_free(lobby);
  1533. furi_string_free(username);
  1534. furi_string_free(strength);
  1535. furi_string_free(health);
  1536. furi_string_free(attack_timer);
  1537. return false;
  1538. }
  1539. size_t data_size = furi_string_size(enemy_data);
  1540. if (storage_file_write(file, furi_string_get_cstr(enemy_data), data_size) != data_size)
  1541. {
  1542. FURI_LOG_E("Game", "Failed to write enemy_data");
  1543. }
  1544. storage_file_close(file);
  1545. furi_string_free(enemy_data);
  1546. furi_string_free(player_stats);
  1547. furi_string_free(lobby);
  1548. furi_string_free(username);
  1549. furi_string_free(strength);
  1550. furi_string_free(health);
  1551. furi_string_free(attack_timer);
  1552. return true;
  1553. }
  1554. // well.. we need to wait for the response to be received
  1555. if (!flipper_http_process_response_async(fhttp, fetch_lobby, parse_lobby))
  1556. {
  1557. FURI_LOG_E(TAG, "Failed to fetch lobby details");
  1558. easy_flipper_dialog("Error", "Failed to fetch lobby details. Press BACK to return.");
  1559. flipper_http_free(fhttp);
  1560. return;
  1561. }
  1562. // start the websocket session
  1563. bool start_ws()
  1564. {
  1565. char websocket_url[128];
  1566. snprintf(websocket_url, sizeof(websocket_url), "ws://www.jblanked.com/ws/game/%s/", lobby_list[lobby_index]);
  1567. if (!flipper_http_websocket_start(fhttp, websocket_url, 80, "{\"Content-Type\":\"application/json\"}"))
  1568. {
  1569. FURI_LOG_E(TAG, "Failed to start websocket session");
  1570. return false;
  1571. }
  1572. return true;
  1573. }
  1574. bool parse_ws()
  1575. {
  1576. // for now just return true and the game will handle the rest
  1577. return fhttp->state != ISSUE;
  1578. }
  1579. // start the websocket session
  1580. if (!flipper_http_process_response_async(fhttp, start_ws, parse_ws))
  1581. {
  1582. FURI_LOG_E(TAG, "Failed to start websocket session");
  1583. easy_flipper_dialog("Error", "Failed to start websocket session. Press BACK to return.");
  1584. flipper_http_free(fhttp);
  1585. return;
  1586. }
  1587. flipper_http_free(fhttp);
  1588. // start the game thread
  1589. if (!start_game_thread(app))
  1590. {
  1591. FURI_LOG_E(TAG, "Failed to start game thread");
  1592. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1593. return;
  1594. }
  1595. }
  1596. }
  1597. static void updated_wifi_ssid(void *context)
  1598. {
  1599. FlipWorldApp *app = (FlipWorldApp *)context;
  1600. if (!app)
  1601. {
  1602. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1603. return;
  1604. }
  1605. // store the entered text
  1606. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1607. // Ensure null-termination
  1608. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1609. // save the setting
  1610. save_char("WiFi-SSID", app->text_input_buffer);
  1611. // update the variable item text
  1612. if (app->variable_item_wifi_ssid)
  1613. {
  1614. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1615. // get value of password
  1616. char pass[64];
  1617. char username[64];
  1618. char password[64];
  1619. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1620. {
  1621. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1622. {
  1623. // save the settings
  1624. load_char("Flip-Social-Username", username, sizeof(username));
  1625. load_char("Flip-Social-Password", password, sizeof(password));
  1626. save_settings(app->text_input_buffer, pass, username, password);
  1627. // initialize the http
  1628. FlipperHTTP *fhttp = flipper_http_alloc();
  1629. if (fhttp)
  1630. {
  1631. // save the wifi if the device is connected
  1632. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1633. {
  1634. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1635. }
  1636. // free the resources
  1637. flipper_http_free(fhttp);
  1638. }
  1639. else
  1640. {
  1641. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1642. }
  1643. }
  1644. }
  1645. }
  1646. // switch to the settings view
  1647. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1648. }
  1649. static void updated_wifi_pass(void *context)
  1650. {
  1651. FlipWorldApp *app = (FlipWorldApp *)context;
  1652. if (!app)
  1653. {
  1654. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1655. return;
  1656. }
  1657. // store the entered text
  1658. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1659. // Ensure null-termination
  1660. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1661. // save the setting
  1662. save_char("WiFi-Password", app->text_input_buffer);
  1663. // update the variable item text
  1664. if (app->variable_item_wifi_pass)
  1665. {
  1666. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1667. }
  1668. // get value of ssid
  1669. char ssid[64];
  1670. char username[64];
  1671. char password[64];
  1672. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1673. {
  1674. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1675. {
  1676. // save the settings
  1677. load_char("Flip-Social-Username", username, sizeof(username));
  1678. load_char("Flip-Social-Password", password, sizeof(password));
  1679. save_settings(ssid, app->text_input_buffer, username, password);
  1680. // initialize the http
  1681. FlipperHTTP *fhttp = flipper_http_alloc();
  1682. if (fhttp)
  1683. {
  1684. // save the wifi if the device is connected
  1685. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1686. {
  1687. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1688. }
  1689. // free the resources
  1690. flipper_http_free(fhttp);
  1691. }
  1692. else
  1693. {
  1694. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1695. }
  1696. }
  1697. }
  1698. // switch to the settings view
  1699. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1700. }
  1701. static void updated_username(void *context)
  1702. {
  1703. FlipWorldApp *app = (FlipWorldApp *)context;
  1704. if (!app)
  1705. {
  1706. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1707. return;
  1708. }
  1709. // store the entered text
  1710. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1711. // Ensure null-termination
  1712. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1713. // save the setting
  1714. save_char("Flip-Social-Username", app->text_input_buffer);
  1715. // update the variable item text
  1716. if (app->variable_item_user_username)
  1717. {
  1718. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1719. }
  1720. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1721. }
  1722. static void updated_password(void *context)
  1723. {
  1724. FlipWorldApp *app = (FlipWorldApp *)context;
  1725. if (!app)
  1726. {
  1727. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1728. return;
  1729. }
  1730. // store the entered text
  1731. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1732. // Ensure null-termination
  1733. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1734. // save the setting
  1735. save_char("Flip-Social-Password", app->text_input_buffer);
  1736. // update the variable item text
  1737. if (app->variable_item_user_password)
  1738. {
  1739. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1740. }
  1741. // get value of username
  1742. char username[64];
  1743. char ssid[64];
  1744. char pass[64];
  1745. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1746. {
  1747. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1748. {
  1749. // save the settings
  1750. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1751. load_char("WiFi-Password", pass, sizeof(pass));
  1752. save_settings(ssid, pass, username, app->text_input_buffer);
  1753. }
  1754. }
  1755. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1756. }
  1757. static void wifi_settings_select(void *context, uint32_t index)
  1758. {
  1759. FlipWorldApp *app = (FlipWorldApp *)context;
  1760. if (!app)
  1761. {
  1762. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1763. return;
  1764. }
  1765. char ssid[64];
  1766. char pass[64];
  1767. char username[64];
  1768. char password[64];
  1769. switch (index)
  1770. {
  1771. case 0: // Input SSID
  1772. free_all_views(app, false, false);
  1773. if (!alloc_text_input_view(app, "SSID"))
  1774. {
  1775. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1776. return;
  1777. }
  1778. // load SSID
  1779. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1780. {
  1781. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1782. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1783. }
  1784. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1785. break;
  1786. case 1: // Input Password
  1787. free_all_views(app, false, false);
  1788. if (!alloc_text_input_view(app, "Password"))
  1789. {
  1790. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1791. return;
  1792. }
  1793. // load password
  1794. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1795. {
  1796. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1797. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1798. }
  1799. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1800. break;
  1801. default:
  1802. FURI_LOG_E(TAG, "Unknown configuration item index");
  1803. break;
  1804. }
  1805. }
  1806. static void fps_change(VariableItem *item)
  1807. {
  1808. uint8_t index = variable_item_get_current_value_index(item);
  1809. fps_index = index;
  1810. variable_item_set_current_value_text(item, fps_choices_str[index]);
  1811. variable_item_set_current_value_index(item, index);
  1812. save_char("Game-FPS", fps_choices_str[index]);
  1813. }
  1814. static void screen_on_change(VariableItem *item)
  1815. {
  1816. uint8_t index = variable_item_get_current_value_index(item);
  1817. screen_always_on_index = index;
  1818. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1819. variable_item_set_current_value_index(item, index);
  1820. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1821. }
  1822. static void sound_on_change(VariableItem *item)
  1823. {
  1824. uint8_t index = variable_item_get_current_value_index(item);
  1825. sound_on_index = index;
  1826. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1827. variable_item_set_current_value_index(item, index);
  1828. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1829. }
  1830. static void vibration_on_change(VariableItem *item)
  1831. {
  1832. uint8_t index = variable_item_get_current_value_index(item);
  1833. vibration_on_index = index;
  1834. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1835. variable_item_set_current_value_index(item, index);
  1836. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1837. }
  1838. static void player_on_change(VariableItem *item)
  1839. {
  1840. uint8_t index = variable_item_get_current_value_index(item);
  1841. player_sprite_index = index;
  1842. variable_item_set_current_value_text(item, is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  1843. variable_item_set_current_value_index(item, index);
  1844. save_char("Game-Player-Sprite", player_sprite_choices[index]);
  1845. }
  1846. static void vgm_x_change(VariableItem *item)
  1847. {
  1848. uint8_t index = variable_item_get_current_value_index(item);
  1849. vgm_x_index = index;
  1850. variable_item_set_current_value_text(item, vgm_levels[index]);
  1851. variable_item_set_current_value_index(item, index);
  1852. save_char("Game-VGM-X", vgm_levels[index]);
  1853. }
  1854. static void vgm_y_change(VariableItem *item)
  1855. {
  1856. uint8_t index = variable_item_get_current_value_index(item);
  1857. vgm_y_index = index;
  1858. variable_item_set_current_value_text(item, vgm_levels[index]);
  1859. variable_item_set_current_value_index(item, index);
  1860. save_char("Game-VGM-Y", vgm_levels[index]);
  1861. }
  1862. static bool _fetch_worlds(DataLoaderModel *model)
  1863. {
  1864. if (!model || !model->fhttp)
  1865. {
  1866. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1867. return false;
  1868. }
  1869. char directory_path[128];
  1870. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1871. Storage *storage = furi_record_open(RECORD_STORAGE);
  1872. storage_common_mkdir(storage, directory_path);
  1873. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1874. storage_common_mkdir(storage, directory_path);
  1875. furi_record_close(RECORD_STORAGE);
  1876. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1877. model->fhttp->save_received_data = true;
  1878. return flipper_http_request(model->fhttp, GET, "https://www.jblanked.com/flipper/api/world/v5/get/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  1879. }
  1880. static char *_parse_worlds(DataLoaderModel *model)
  1881. {
  1882. UNUSED(model);
  1883. return "World Pack Installed";
  1884. }
  1885. static void switch_to_view_get_worlds(FlipWorldApp *app)
  1886. {
  1887. if (!alloc_view_loader(app))
  1888. {
  1889. FURI_LOG_E(TAG, "Failed to allocate view loader");
  1890. return;
  1891. }
  1892. generic_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1893. }
  1894. static void game_settings_select(void *context, uint32_t index)
  1895. {
  1896. FlipWorldApp *app = (FlipWorldApp *)context;
  1897. if (!app)
  1898. {
  1899. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1900. return;
  1901. }
  1902. switch (index)
  1903. {
  1904. case 0: // Download all world data as one huge json
  1905. switch_to_view_get_worlds(app);
  1906. case 1: // Player Sprite
  1907. break;
  1908. case 2: // Change FPS
  1909. break;
  1910. case 3: // VGM X
  1911. break;
  1912. case 4: // VGM Y
  1913. break;
  1914. case 5: // Screen Always On
  1915. break;
  1916. case 6: // Sound On
  1917. break;
  1918. case 7: // Vibration On
  1919. break;
  1920. }
  1921. }
  1922. static void user_settings_select(void *context, uint32_t index)
  1923. {
  1924. FlipWorldApp *app = (FlipWorldApp *)context;
  1925. if (!app)
  1926. {
  1927. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1928. return;
  1929. }
  1930. switch (index)
  1931. {
  1932. case 0: // Username
  1933. free_all_views(app, false, false);
  1934. if (!alloc_text_input_view(app, "Username-Login"))
  1935. {
  1936. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1937. return;
  1938. }
  1939. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1940. break;
  1941. case 1: // Password
  1942. free_all_views(app, false, false);
  1943. if (!alloc_text_input_view(app, "Password-Login"))
  1944. {
  1945. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1946. return;
  1947. }
  1948. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1949. break;
  1950. }
  1951. }
  1952. static void widget_set_text(char *message, Widget **widget)
  1953. {
  1954. if (widget == NULL)
  1955. {
  1956. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  1957. DEV_CRASH();
  1958. return;
  1959. }
  1960. if (message == NULL)
  1961. {
  1962. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  1963. DEV_CRASH();
  1964. return;
  1965. }
  1966. widget_reset(*widget);
  1967. uint32_t message_length = strlen(message); // Length of the message
  1968. uint32_t i = 0; // Index tracker
  1969. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1970. char *formatted_message; // Buffer to hold the final formatted message
  1971. // Allocate buffer with double the message length plus one for safety
  1972. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1973. {
  1974. return;
  1975. }
  1976. while (i < message_length)
  1977. {
  1978. uint32_t max_line_length = 31; // Maximum characters per line
  1979. uint32_t remaining_length = message_length - i; // Remaining characters
  1980. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1981. // Check for newline character within the current segment
  1982. uint32_t newline_pos = i;
  1983. bool found_newline = false;
  1984. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1985. {
  1986. if (message[newline_pos] == '\n')
  1987. {
  1988. found_newline = true;
  1989. break;
  1990. }
  1991. }
  1992. if (found_newline)
  1993. {
  1994. // If newline found, set line_length up to the newline
  1995. line_length = newline_pos - i;
  1996. }
  1997. // Temporary buffer to hold the current line
  1998. char line[32];
  1999. strncpy(line, message + i, line_length);
  2000. line[line_length] = '\0';
  2001. // If newline was found, skip it for the next iteration
  2002. if (found_newline)
  2003. {
  2004. i += line_length + 1; // +1 to skip the '\n' character
  2005. }
  2006. else
  2007. {
  2008. // Check if the line ends in the middle of a word and adjust accordingly
  2009. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  2010. {
  2011. // Find the last space within the current line to avoid breaking a word
  2012. char *last_space = strrchr(line, ' ');
  2013. if (last_space != NULL)
  2014. {
  2015. // Adjust the line_length to avoid cutting the word
  2016. line_length = last_space - line;
  2017. line[line_length] = '\0'; // Null-terminate at the space
  2018. }
  2019. }
  2020. // Move the index forward by the determined line_length
  2021. i += line_length;
  2022. // Skip any spaces at the beginning of the next line
  2023. while (i < message_length && message[i] == ' ')
  2024. {
  2025. i++;
  2026. }
  2027. }
  2028. // Manually copy the fixed line into the formatted_message buffer
  2029. for (uint32_t j = 0; j < line_length; j++)
  2030. {
  2031. formatted_message[formatted_index++] = line[j];
  2032. }
  2033. // Add a newline character for line spacing
  2034. formatted_message[formatted_index++] = '\n';
  2035. }
  2036. // Null-terminate the formatted_message
  2037. formatted_message[formatted_index] = '\0';
  2038. // Add the formatted message to the widget
  2039. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  2040. }
  2041. void loader_draw_callback(Canvas *canvas, void *model)
  2042. {
  2043. if (!canvas || !model)
  2044. {
  2045. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  2046. return;
  2047. }
  2048. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  2049. HTTPState http_state = data_loader_model->fhttp->state;
  2050. DataState data_state = data_loader_model->data_state;
  2051. char *title = data_loader_model->title;
  2052. canvas_set_font(canvas, FontSecondary);
  2053. if (http_state == INACTIVE)
  2054. {
  2055. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  2056. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  2057. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  2058. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  2059. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  2060. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  2061. return;
  2062. }
  2063. if (data_state == DataStateError || data_state == DataStateParseError)
  2064. {
  2065. error_draw(canvas, data_loader_model);
  2066. return;
  2067. }
  2068. canvas_draw_str(canvas, 0, 7, title);
  2069. canvas_draw_str(canvas, 0, 17, "Loading...");
  2070. if (data_state == DataStateInitial)
  2071. {
  2072. return;
  2073. }
  2074. if (http_state == SENDING)
  2075. {
  2076. canvas_draw_str(canvas, 0, 27, "Fetching...");
  2077. return;
  2078. }
  2079. if (http_state == RECEIVING || data_state == DataStateRequested)
  2080. {
  2081. canvas_draw_str(canvas, 0, 27, "Receiving...");
  2082. return;
  2083. }
  2084. if (http_state == IDLE && data_state == DataStateReceived)
  2085. {
  2086. canvas_draw_str(canvas, 0, 27, "Processing...");
  2087. return;
  2088. }
  2089. if (http_state == IDLE && data_state == DataStateParsed)
  2090. {
  2091. canvas_draw_str(canvas, 0, 27, "Processed...");
  2092. return;
  2093. }
  2094. }
  2095. static void loader_process_callback(void *context)
  2096. {
  2097. if (context == NULL)
  2098. {
  2099. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  2100. DEV_CRASH();
  2101. return;
  2102. }
  2103. FlipWorldApp *app = (FlipWorldApp *)context;
  2104. View *view = app->view_loader;
  2105. DataState current_data_state;
  2106. DataLoaderModel *loader_model = NULL;
  2107. with_view_model(
  2108. view,
  2109. DataLoaderModel * model,
  2110. {
  2111. current_data_state = model->data_state;
  2112. loader_model = model;
  2113. },
  2114. false);
  2115. if (!loader_model || !loader_model->fhttp)
  2116. {
  2117. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  2118. DEV_CRASH();
  2119. return;
  2120. }
  2121. if (current_data_state == DataStateInitial)
  2122. {
  2123. with_view_model(
  2124. view,
  2125. DataLoaderModel * model,
  2126. {
  2127. model->data_state = DataStateRequested;
  2128. DataLoaderFetch fetch = model->fetcher;
  2129. if (fetch == NULL)
  2130. {
  2131. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  2132. model->data_state = DataStateError;
  2133. return;
  2134. }
  2135. // Clear any previous responses
  2136. strncpy(model->fhttp->last_response, "", 1);
  2137. bool request_status = fetch(model);
  2138. if (!request_status)
  2139. {
  2140. model->data_state = DataStateError;
  2141. }
  2142. },
  2143. true);
  2144. }
  2145. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  2146. {
  2147. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  2148. {
  2149. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  2150. {
  2151. FURI_LOG_DEV(TAG, "PONG received.");
  2152. }
  2153. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  2154. {
  2155. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2156. }
  2157. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  2158. {
  2159. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2160. }
  2161. else if (strlen(loader_model->fhttp->last_response))
  2162. {
  2163. // Still waiting on response
  2164. }
  2165. else
  2166. {
  2167. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  2168. }
  2169. }
  2170. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  2171. {
  2172. // continue waiting
  2173. }
  2174. else if (loader_model->fhttp->state == INACTIVE)
  2175. {
  2176. // inactive. try again
  2177. }
  2178. else if (loader_model->fhttp->state == ISSUE)
  2179. {
  2180. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  2181. }
  2182. else
  2183. {
  2184. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  2185. DEV_CRASH();
  2186. }
  2187. }
  2188. else if (current_data_state == DataStateReceived)
  2189. {
  2190. with_view_model(
  2191. view,
  2192. DataLoaderModel * model,
  2193. {
  2194. char *data_text;
  2195. if (model->parser == NULL)
  2196. {
  2197. data_text = NULL;
  2198. FURI_LOG_DEV(TAG, "Parser is NULL");
  2199. DEV_CRASH();
  2200. }
  2201. else
  2202. {
  2203. data_text = model->parser(model);
  2204. }
  2205. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  2206. model->data_text = data_text;
  2207. if (data_text == NULL)
  2208. {
  2209. model->data_state = DataStateParseError;
  2210. }
  2211. else
  2212. {
  2213. model->data_state = DataStateParsed;
  2214. }
  2215. },
  2216. true);
  2217. }
  2218. else if (current_data_state == DataStateParsed)
  2219. {
  2220. with_view_model(
  2221. view,
  2222. DataLoaderModel * model,
  2223. {
  2224. if (++model->request_index < model->request_count)
  2225. {
  2226. model->data_state = DataStateInitial;
  2227. }
  2228. else
  2229. {
  2230. widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  2231. if (model->data_text != NULL)
  2232. {
  2233. free(model->data_text);
  2234. model->data_text = NULL;
  2235. }
  2236. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  2237. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  2238. }
  2239. },
  2240. true);
  2241. }
  2242. }
  2243. static void loader_timer_callback(void *context)
  2244. {
  2245. if (context == NULL)
  2246. {
  2247. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  2248. DEV_CRASH();
  2249. return;
  2250. }
  2251. FlipWorldApp *app = (FlipWorldApp *)context;
  2252. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  2253. }
  2254. static void loader_on_enter(void *context)
  2255. {
  2256. if (context == NULL)
  2257. {
  2258. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  2259. DEV_CRASH();
  2260. return;
  2261. }
  2262. FlipWorldApp *app = (FlipWorldApp *)context;
  2263. View *view = app->view_loader;
  2264. with_view_model(
  2265. view,
  2266. DataLoaderModel * model,
  2267. {
  2268. view_set_previous_callback(view, model->back_callback);
  2269. if (model->timer == NULL)
  2270. {
  2271. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  2272. }
  2273. furi_timer_start(model->timer, 250);
  2274. },
  2275. true);
  2276. }
  2277. static void loader_on_exit(void *context)
  2278. {
  2279. if (context == NULL)
  2280. {
  2281. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  2282. DEV_CRASH();
  2283. return;
  2284. }
  2285. FlipWorldApp *app = (FlipWorldApp *)context;
  2286. View *view = app->view_loader;
  2287. with_view_model(
  2288. view,
  2289. DataLoaderModel * model,
  2290. {
  2291. if (model->timer)
  2292. {
  2293. furi_timer_stop(model->timer);
  2294. }
  2295. },
  2296. false);
  2297. }
  2298. void loader_init(View *view)
  2299. {
  2300. if (view == NULL)
  2301. {
  2302. FURI_LOG_E(TAG, "loader_init - view is NULL");
  2303. DEV_CRASH();
  2304. return;
  2305. }
  2306. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  2307. view_set_enter_callback(view, loader_on_enter);
  2308. view_set_exit_callback(view, loader_on_exit);
  2309. }
  2310. void loader_free_model(View *view)
  2311. {
  2312. if (view == NULL)
  2313. {
  2314. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  2315. DEV_CRASH();
  2316. return;
  2317. }
  2318. with_view_model(
  2319. view,
  2320. DataLoaderModel * model,
  2321. {
  2322. if (model->timer)
  2323. {
  2324. furi_timer_free(model->timer);
  2325. model->timer = NULL;
  2326. }
  2327. if (model->parser_context)
  2328. {
  2329. // do not free the context here, it is the app context
  2330. // free(model->parser_context);
  2331. // model->parser_context = NULL;
  2332. }
  2333. if (model->fhttp)
  2334. {
  2335. flipper_http_free(model->fhttp);
  2336. model->fhttp = NULL;
  2337. }
  2338. },
  2339. false);
  2340. }
  2341. bool custom_event_callback(void *context, uint32_t index)
  2342. {
  2343. if (context == NULL)
  2344. {
  2345. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  2346. DEV_CRASH();
  2347. return false;
  2348. }
  2349. switch (index)
  2350. {
  2351. case FlipWorldCustomEventProcess:
  2352. loader_process_callback(context);
  2353. return true;
  2354. default:
  2355. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  2356. return false;
  2357. }
  2358. }
  2359. void generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  2360. {
  2361. if (app == NULL)
  2362. {
  2363. FURI_LOG_E(TAG, "generic_switch_to_view - app is NULL");
  2364. DEV_CRASH();
  2365. return;
  2366. }
  2367. View *view = app->view_loader;
  2368. if (view == NULL)
  2369. {
  2370. FURI_LOG_E(TAG, "generic_switch_to_view - view is NULL");
  2371. DEV_CRASH();
  2372. return;
  2373. }
  2374. with_view_model(
  2375. view,
  2376. DataLoaderModel * model,
  2377. {
  2378. model->title = title;
  2379. model->fetcher = fetcher;
  2380. model->parser = parser;
  2381. model->request_index = 0;
  2382. model->request_count = request_count;
  2383. model->back_callback = back;
  2384. model->data_state = DataStateInitial;
  2385. model->data_text = NULL;
  2386. //
  2387. model->parser_context = app;
  2388. if (!model->fhttp)
  2389. {
  2390. model->fhttp = flipper_http_alloc();
  2391. }
  2392. },
  2393. true);
  2394. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2395. }