callback.c 83 KB

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