callback.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  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.flipsocial.net/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.flipsocial.net/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.flipsocial.net/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.flipsocial.net/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.flipsocial.net/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.flipsocial.net/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. void callback_submenu_choices(void *context, uint32_t index)
  1134. {
  1135. FlipWorldApp *app = (FlipWorldApp *)context;
  1136. if (!app)
  1137. {
  1138. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1139. return;
  1140. }
  1141. switch (index)
  1142. {
  1143. case FlipWorldSubmenuIndexGameSubmenu:
  1144. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewGameSubmenu);
  1145. break;
  1146. case FlipWorldSubmenuIndexStory:
  1147. easy_flipper_dialog("Unavailable", "\nStory mode is not ready yet.\nPress BACK to return.");
  1148. break;
  1149. case FlipWorldSubmenuIndexPvP:
  1150. easy_flipper_dialog("Unavailable", "\nPvP mode is not ready yet.\nPress BACK to return.");
  1151. break;
  1152. case FlipWorldSubmenuIndexPvE:
  1153. free_all_views(app, true, true);
  1154. if (!is_enough_heap(60000))
  1155. {
  1156. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  1157. return;
  1158. }
  1159. // check if logged in
  1160. if (is_logged_in() || is_logged_in_to_flip_social())
  1161. {
  1162. FlipperHTTP *fhttp = flipper_http_alloc();
  1163. if (!fhttp)
  1164. {
  1165. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1166. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1167. return;
  1168. }
  1169. bool fetch_world_list_i()
  1170. {
  1171. return fetch_world_list(fhttp);
  1172. }
  1173. bool parse_world_list_i()
  1174. {
  1175. return fhttp->state != ISSUE;
  1176. }
  1177. bool fetch_player_stats_i()
  1178. {
  1179. return fetch_player_stats(fhttp);
  1180. }
  1181. if (!alloc_message_view(app, MessageStateLoading))
  1182. {
  1183. FURI_LOG_E(TAG, "Failed to allocate message view");
  1184. return;
  1185. }
  1186. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1187. // Make the request
  1188. 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))
  1189. {
  1190. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1191. flipper_http_free(fhttp);
  1192. }
  1193. else
  1194. {
  1195. flipper_http_free(fhttp);
  1196. }
  1197. if (!alloc_submenu_settings(app))
  1198. {
  1199. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1200. return;
  1201. }
  1202. if (!start_game_thread(app))
  1203. {
  1204. FURI_LOG_E(TAG, "Failed to start game thread");
  1205. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1206. return;
  1207. }
  1208. }
  1209. else
  1210. {
  1211. switch_to_view_get_game(app);
  1212. }
  1213. break;
  1214. case FlipWorldSubmenuIndexMessage:
  1215. // About menu.
  1216. free_all_views(app, true, true);
  1217. if (!alloc_message_view(app, MessageStateAbout))
  1218. {
  1219. FURI_LOG_E(TAG, "Failed to allocate message view");
  1220. return;
  1221. }
  1222. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1223. break;
  1224. case FlipWorldSubmenuIndexSettings:
  1225. free_all_views(app, true, true);
  1226. if (!alloc_submenu_settings(app))
  1227. {
  1228. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1229. return;
  1230. }
  1231. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1232. break;
  1233. case FlipWorldSubmenuIndexWiFiSettings:
  1234. free_all_views(app, true, false);
  1235. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1236. {
  1237. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1238. return;
  1239. }
  1240. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1241. break;
  1242. case FlipWorldSubmenuIndexGameSettings:
  1243. free_all_views(app, true, false);
  1244. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1245. {
  1246. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1247. return;
  1248. }
  1249. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1250. break;
  1251. case FlipWorldSubmenuIndexUserSettings:
  1252. free_all_views(app, true, false);
  1253. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1254. {
  1255. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1256. return;
  1257. }
  1258. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1259. break;
  1260. default:
  1261. break;
  1262. }
  1263. }
  1264. static void updated_wifi_ssid(void *context)
  1265. {
  1266. FlipWorldApp *app = (FlipWorldApp *)context;
  1267. if (!app)
  1268. {
  1269. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1270. return;
  1271. }
  1272. // store the entered text
  1273. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1274. // Ensure null-termination
  1275. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1276. // save the setting
  1277. save_char("WiFi-SSID", app->text_input_buffer);
  1278. // update the variable item text
  1279. if (app->variable_item_wifi_ssid)
  1280. {
  1281. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1282. // get value of password
  1283. char pass[64];
  1284. char username[64];
  1285. char password[64];
  1286. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1287. {
  1288. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1289. {
  1290. // save the settings
  1291. load_char("Flip-Social-Username", username, sizeof(username));
  1292. load_char("Flip-Social-Password", password, sizeof(password));
  1293. save_settings(app->text_input_buffer, pass, username, password);
  1294. // initialize the http
  1295. FlipperHTTP *fhttp = flipper_http_alloc();
  1296. if (fhttp)
  1297. {
  1298. // save the wifi if the device is connected
  1299. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1300. {
  1301. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1302. }
  1303. // free the resources
  1304. flipper_http_free(fhttp);
  1305. }
  1306. else
  1307. {
  1308. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1309. }
  1310. }
  1311. }
  1312. }
  1313. // switch to the settings view
  1314. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1315. }
  1316. static void updated_wifi_pass(void *context)
  1317. {
  1318. FlipWorldApp *app = (FlipWorldApp *)context;
  1319. if (!app)
  1320. {
  1321. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1322. return;
  1323. }
  1324. // store the entered text
  1325. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1326. // Ensure null-termination
  1327. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1328. // save the setting
  1329. save_char("WiFi-Password", app->text_input_buffer);
  1330. // update the variable item text
  1331. if (app->variable_item_wifi_pass)
  1332. {
  1333. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1334. }
  1335. // get value of ssid
  1336. char ssid[64];
  1337. char username[64];
  1338. char password[64];
  1339. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1340. {
  1341. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1342. {
  1343. // save the settings
  1344. load_char("Flip-Social-Username", username, sizeof(username));
  1345. load_char("Flip-Social-Password", password, sizeof(password));
  1346. save_settings(ssid, app->text_input_buffer, username, password);
  1347. // initialize the http
  1348. FlipperHTTP *fhttp = flipper_http_alloc();
  1349. if (fhttp)
  1350. {
  1351. // save the wifi if the device is connected
  1352. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1353. {
  1354. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1355. }
  1356. // free the resources
  1357. flipper_http_free(fhttp);
  1358. }
  1359. else
  1360. {
  1361. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1362. }
  1363. }
  1364. }
  1365. // switch to the settings view
  1366. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1367. }
  1368. static void updated_username(void *context)
  1369. {
  1370. FlipWorldApp *app = (FlipWorldApp *)context;
  1371. if (!app)
  1372. {
  1373. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1374. return;
  1375. }
  1376. // store the entered text
  1377. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1378. // Ensure null-termination
  1379. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1380. // save the setting
  1381. save_char("Flip-Social-Username", app->text_input_buffer);
  1382. // update the variable item text
  1383. if (app->variable_item_user_username)
  1384. {
  1385. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1386. }
  1387. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1388. }
  1389. static void updated_password(void *context)
  1390. {
  1391. FlipWorldApp *app = (FlipWorldApp *)context;
  1392. if (!app)
  1393. {
  1394. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1395. return;
  1396. }
  1397. // store the entered text
  1398. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1399. // Ensure null-termination
  1400. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1401. // save the setting
  1402. save_char("Flip-Social-Password", app->text_input_buffer);
  1403. // update the variable item text
  1404. if (app->variable_item_user_password)
  1405. {
  1406. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1407. }
  1408. // get value of username
  1409. char username[64];
  1410. char ssid[64];
  1411. char pass[64];
  1412. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1413. {
  1414. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1415. {
  1416. // save the settings
  1417. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1418. load_char("WiFi-Password", pass, sizeof(pass));
  1419. save_settings(ssid, pass, username, app->text_input_buffer);
  1420. }
  1421. }
  1422. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1423. }
  1424. static void wifi_settings_select(void *context, uint32_t index)
  1425. {
  1426. FlipWorldApp *app = (FlipWorldApp *)context;
  1427. if (!app)
  1428. {
  1429. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1430. return;
  1431. }
  1432. char ssid[64];
  1433. char pass[64];
  1434. char username[64];
  1435. char password[64];
  1436. switch (index)
  1437. {
  1438. case 0: // Input SSID
  1439. free_all_views(app, false, false);
  1440. if (!alloc_text_input_view(app, "SSID"))
  1441. {
  1442. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1443. return;
  1444. }
  1445. // load SSID
  1446. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1447. {
  1448. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1449. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1450. }
  1451. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1452. break;
  1453. case 1: // Input Password
  1454. free_all_views(app, false, false);
  1455. if (!alloc_text_input_view(app, "Password"))
  1456. {
  1457. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1458. return;
  1459. }
  1460. // load password
  1461. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1462. {
  1463. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1464. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1465. }
  1466. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1467. break;
  1468. default:
  1469. FURI_LOG_E(TAG, "Unknown configuration item index");
  1470. break;
  1471. }
  1472. }
  1473. static void fps_change(VariableItem *item)
  1474. {
  1475. uint8_t index = variable_item_get_current_value_index(item);
  1476. fps_index = index;
  1477. variable_item_set_current_value_text(item, fps_choices_str[index]);
  1478. variable_item_set_current_value_index(item, index);
  1479. save_char("Game-FPS", fps_choices_str[index]);
  1480. }
  1481. static void screen_on_change(VariableItem *item)
  1482. {
  1483. uint8_t index = variable_item_get_current_value_index(item);
  1484. screen_always_on_index = index;
  1485. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1486. variable_item_set_current_value_index(item, index);
  1487. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1488. }
  1489. static void sound_on_change(VariableItem *item)
  1490. {
  1491. uint8_t index = variable_item_get_current_value_index(item);
  1492. sound_on_index = index;
  1493. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1494. variable_item_set_current_value_index(item, index);
  1495. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1496. }
  1497. static void vibration_on_change(VariableItem *item)
  1498. {
  1499. uint8_t index = variable_item_get_current_value_index(item);
  1500. vibration_on_index = index;
  1501. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1502. variable_item_set_current_value_index(item, index);
  1503. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1504. }
  1505. static void player_on_change(VariableItem *item)
  1506. {
  1507. uint8_t index = variable_item_get_current_value_index(item);
  1508. player_sprite_index = index;
  1509. variable_item_set_current_value_text(item, is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  1510. variable_item_set_current_value_index(item, index);
  1511. save_char("Game-Player-Sprite", player_sprite_choices[index]);
  1512. }
  1513. static void vgm_x_change(VariableItem *item)
  1514. {
  1515. uint8_t index = variable_item_get_current_value_index(item);
  1516. vgm_x_index = index;
  1517. variable_item_set_current_value_text(item, vgm_levels[index]);
  1518. variable_item_set_current_value_index(item, index);
  1519. save_char("Game-VGM-X", vgm_levels[index]);
  1520. }
  1521. static void vgm_y_change(VariableItem *item)
  1522. {
  1523. uint8_t index = variable_item_get_current_value_index(item);
  1524. vgm_y_index = index;
  1525. variable_item_set_current_value_text(item, vgm_levels[index]);
  1526. variable_item_set_current_value_index(item, index);
  1527. save_char("Game-VGM-Y", vgm_levels[index]);
  1528. }
  1529. static bool _fetch_worlds(DataLoaderModel *model)
  1530. {
  1531. if (!model || !model->fhttp)
  1532. {
  1533. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1534. return false;
  1535. }
  1536. char directory_path[128];
  1537. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1538. Storage *storage = furi_record_open(RECORD_STORAGE);
  1539. storage_common_mkdir(storage, directory_path);
  1540. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1541. storage_common_mkdir(storage, directory_path);
  1542. furi_record_close(RECORD_STORAGE);
  1543. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1544. model->fhttp->save_received_data = true;
  1545. return flipper_http_request(model->fhttp, GET, "https://www.flipsocial.net/api/world/v5/get/10/", "{\"Content-Type\":\"application/json\"}", NULL);
  1546. }
  1547. static char *_parse_worlds(DataLoaderModel *model)
  1548. {
  1549. UNUSED(model);
  1550. return "World Pack Installed";
  1551. }
  1552. static void switch_to_view_get_worlds(FlipWorldApp *app)
  1553. {
  1554. generic_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1555. }
  1556. static void game_settings_select(void *context, uint32_t index)
  1557. {
  1558. FlipWorldApp *app = (FlipWorldApp *)context;
  1559. if (!app)
  1560. {
  1561. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1562. return;
  1563. }
  1564. switch (index)
  1565. {
  1566. case 0: // Download all world data as one huge json
  1567. switch_to_view_get_worlds(app);
  1568. case 1: // Player Sprite
  1569. break;
  1570. case 2: // Change FPS
  1571. break;
  1572. case 3: // VGM X
  1573. break;
  1574. case 4: // VGM Y
  1575. break;
  1576. case 5: // Screen Always On
  1577. break;
  1578. case 6: // Sound On
  1579. break;
  1580. case 7: // Vibration On
  1581. break;
  1582. }
  1583. }
  1584. static void user_settings_select(void *context, uint32_t index)
  1585. {
  1586. FlipWorldApp *app = (FlipWorldApp *)context;
  1587. if (!app)
  1588. {
  1589. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1590. return;
  1591. }
  1592. switch (index)
  1593. {
  1594. case 0: // Username
  1595. free_all_views(app, false, false);
  1596. if (!alloc_text_input_view(app, "Username-Login"))
  1597. {
  1598. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1599. return;
  1600. }
  1601. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1602. break;
  1603. case 1: // Password
  1604. free_all_views(app, false, false);
  1605. if (!alloc_text_input_view(app, "Password-Login"))
  1606. {
  1607. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1608. return;
  1609. }
  1610. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1611. break;
  1612. }
  1613. }
  1614. static void widget_set_text(char *message, Widget **widget)
  1615. {
  1616. if (widget == NULL)
  1617. {
  1618. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  1619. DEV_CRASH();
  1620. return;
  1621. }
  1622. if (message == NULL)
  1623. {
  1624. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  1625. DEV_CRASH();
  1626. return;
  1627. }
  1628. widget_reset(*widget);
  1629. uint32_t message_length = strlen(message); // Length of the message
  1630. uint32_t i = 0; // Index tracker
  1631. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1632. char *formatted_message; // Buffer to hold the final formatted message
  1633. // Allocate buffer with double the message length plus one for safety
  1634. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1635. {
  1636. return;
  1637. }
  1638. while (i < message_length)
  1639. {
  1640. uint32_t max_line_length = 31; // Maximum characters per line
  1641. uint32_t remaining_length = message_length - i; // Remaining characters
  1642. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1643. // Check for newline character within the current segment
  1644. uint32_t newline_pos = i;
  1645. bool found_newline = false;
  1646. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1647. {
  1648. if (message[newline_pos] == '\n')
  1649. {
  1650. found_newline = true;
  1651. break;
  1652. }
  1653. }
  1654. if (found_newline)
  1655. {
  1656. // If newline found, set line_length up to the newline
  1657. line_length = newline_pos - i;
  1658. }
  1659. // Temporary buffer to hold the current line
  1660. char line[32];
  1661. strncpy(line, message + i, line_length);
  1662. line[line_length] = '\0';
  1663. // If newline was found, skip it for the next iteration
  1664. if (found_newline)
  1665. {
  1666. i += line_length + 1; // +1 to skip the '\n' character
  1667. }
  1668. else
  1669. {
  1670. // Check if the line ends in the middle of a word and adjust accordingly
  1671. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1672. {
  1673. // Find the last space within the current line to avoid breaking a word
  1674. char *last_space = strrchr(line, ' ');
  1675. if (last_space != NULL)
  1676. {
  1677. // Adjust the line_length to avoid cutting the word
  1678. line_length = last_space - line;
  1679. line[line_length] = '\0'; // Null-terminate at the space
  1680. }
  1681. }
  1682. // Move the index forward by the determined line_length
  1683. i += line_length;
  1684. // Skip any spaces at the beginning of the next line
  1685. while (i < message_length && message[i] == ' ')
  1686. {
  1687. i++;
  1688. }
  1689. }
  1690. // Manually copy the fixed line into the formatted_message buffer
  1691. for (uint32_t j = 0; j < line_length; j++)
  1692. {
  1693. formatted_message[formatted_index++] = line[j];
  1694. }
  1695. // Add a newline character for line spacing
  1696. formatted_message[formatted_index++] = '\n';
  1697. }
  1698. // Null-terminate the formatted_message
  1699. formatted_message[formatted_index] = '\0';
  1700. // Add the formatted message to the widget
  1701. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1702. }
  1703. void loader_draw_callback(Canvas *canvas, void *model)
  1704. {
  1705. if (!canvas || !model)
  1706. {
  1707. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  1708. return;
  1709. }
  1710. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1711. HTTPState http_state = data_loader_model->fhttp->state;
  1712. DataState data_state = data_loader_model->data_state;
  1713. char *title = data_loader_model->title;
  1714. canvas_set_font(canvas, FontSecondary);
  1715. if (http_state == INACTIVE)
  1716. {
  1717. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1718. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1719. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1720. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1721. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1722. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1723. return;
  1724. }
  1725. if (data_state == DataStateError || data_state == DataStateParseError)
  1726. {
  1727. error_draw(canvas, data_loader_model);
  1728. return;
  1729. }
  1730. canvas_draw_str(canvas, 0, 7, title);
  1731. canvas_draw_str(canvas, 0, 17, "Loading...");
  1732. if (data_state == DataStateInitial)
  1733. {
  1734. return;
  1735. }
  1736. if (http_state == SENDING)
  1737. {
  1738. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1739. return;
  1740. }
  1741. if (http_state == RECEIVING || data_state == DataStateRequested)
  1742. {
  1743. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1744. return;
  1745. }
  1746. if (http_state == IDLE && data_state == DataStateReceived)
  1747. {
  1748. canvas_draw_str(canvas, 0, 27, "Processing...");
  1749. return;
  1750. }
  1751. if (http_state == IDLE && data_state == DataStateParsed)
  1752. {
  1753. canvas_draw_str(canvas, 0, 27, "Processed...");
  1754. return;
  1755. }
  1756. }
  1757. static void loader_process_callback(void *context)
  1758. {
  1759. if (context == NULL)
  1760. {
  1761. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  1762. DEV_CRASH();
  1763. return;
  1764. }
  1765. FlipWorldApp *app = (FlipWorldApp *)context;
  1766. View *view = app->view_loader;
  1767. DataState current_data_state;
  1768. DataLoaderModel *loader_model = NULL;
  1769. with_view_model(
  1770. view,
  1771. DataLoaderModel * model,
  1772. {
  1773. current_data_state = model->data_state;
  1774. loader_model = model;
  1775. },
  1776. false);
  1777. if (!loader_model || !loader_model->fhttp)
  1778. {
  1779. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1780. DEV_CRASH();
  1781. return;
  1782. }
  1783. if (current_data_state == DataStateInitial)
  1784. {
  1785. with_view_model(
  1786. view,
  1787. DataLoaderModel * model,
  1788. {
  1789. model->data_state = DataStateRequested;
  1790. DataLoaderFetch fetch = model->fetcher;
  1791. if (fetch == NULL)
  1792. {
  1793. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1794. model->data_state = DataStateError;
  1795. return;
  1796. }
  1797. // Clear any previous responses
  1798. strncpy(model->fhttp->last_response, "", 1);
  1799. bool request_status = fetch(model);
  1800. if (!request_status)
  1801. {
  1802. model->data_state = DataStateError;
  1803. }
  1804. },
  1805. true);
  1806. }
  1807. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1808. {
  1809. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1810. {
  1811. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1812. {
  1813. FURI_LOG_DEV(TAG, "PONG received.");
  1814. }
  1815. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  1816. {
  1817. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1818. }
  1819. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  1820. {
  1821. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1822. }
  1823. else if (strlen(loader_model->fhttp->last_response))
  1824. {
  1825. // Still waiting on response
  1826. }
  1827. else
  1828. {
  1829. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1830. }
  1831. }
  1832. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1833. {
  1834. // continue waiting
  1835. }
  1836. else if (loader_model->fhttp->state == INACTIVE)
  1837. {
  1838. // inactive. try again
  1839. }
  1840. else if (loader_model->fhttp->state == ISSUE)
  1841. {
  1842. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1843. }
  1844. else
  1845. {
  1846. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1847. DEV_CRASH();
  1848. }
  1849. }
  1850. else if (current_data_state == DataStateReceived)
  1851. {
  1852. with_view_model(
  1853. view,
  1854. DataLoaderModel * model,
  1855. {
  1856. char *data_text;
  1857. if (model->parser == NULL)
  1858. {
  1859. data_text = NULL;
  1860. FURI_LOG_DEV(TAG, "Parser is NULL");
  1861. DEV_CRASH();
  1862. }
  1863. else
  1864. {
  1865. data_text = model->parser(model);
  1866. }
  1867. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1868. model->data_text = data_text;
  1869. if (data_text == NULL)
  1870. {
  1871. model->data_state = DataStateParseError;
  1872. }
  1873. else
  1874. {
  1875. model->data_state = DataStateParsed;
  1876. }
  1877. },
  1878. true);
  1879. }
  1880. else if (current_data_state == DataStateParsed)
  1881. {
  1882. with_view_model(
  1883. view,
  1884. DataLoaderModel * model,
  1885. {
  1886. if (++model->request_index < model->request_count)
  1887. {
  1888. model->data_state = DataStateInitial;
  1889. }
  1890. else
  1891. {
  1892. widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1893. if (model->data_text != NULL)
  1894. {
  1895. free(model->data_text);
  1896. model->data_text = NULL;
  1897. }
  1898. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1899. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1900. }
  1901. },
  1902. true);
  1903. }
  1904. }
  1905. static void loader_timer_callback(void *context)
  1906. {
  1907. if (context == NULL)
  1908. {
  1909. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  1910. DEV_CRASH();
  1911. return;
  1912. }
  1913. FlipWorldApp *app = (FlipWorldApp *)context;
  1914. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1915. }
  1916. static void loader_on_enter(void *context)
  1917. {
  1918. if (context == NULL)
  1919. {
  1920. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  1921. DEV_CRASH();
  1922. return;
  1923. }
  1924. FlipWorldApp *app = (FlipWorldApp *)context;
  1925. View *view = app->view_loader;
  1926. with_view_model(
  1927. view,
  1928. DataLoaderModel * model,
  1929. {
  1930. view_set_previous_callback(view, model->back_callback);
  1931. if (model->timer == NULL)
  1932. {
  1933. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  1934. }
  1935. furi_timer_start(model->timer, 250);
  1936. },
  1937. true);
  1938. }
  1939. static void loader_on_exit(void *context)
  1940. {
  1941. if (context == NULL)
  1942. {
  1943. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  1944. DEV_CRASH();
  1945. return;
  1946. }
  1947. FlipWorldApp *app = (FlipWorldApp *)context;
  1948. View *view = app->view_loader;
  1949. with_view_model(
  1950. view,
  1951. DataLoaderModel * model,
  1952. {
  1953. if (model->timer)
  1954. {
  1955. furi_timer_stop(model->timer);
  1956. }
  1957. },
  1958. false);
  1959. }
  1960. void loader_init(View *view)
  1961. {
  1962. if (view == NULL)
  1963. {
  1964. FURI_LOG_E(TAG, "loader_init - view is NULL");
  1965. DEV_CRASH();
  1966. return;
  1967. }
  1968. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1969. view_set_enter_callback(view, loader_on_enter);
  1970. view_set_exit_callback(view, loader_on_exit);
  1971. }
  1972. void loader_free_model(View *view)
  1973. {
  1974. if (view == NULL)
  1975. {
  1976. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  1977. DEV_CRASH();
  1978. return;
  1979. }
  1980. with_view_model(
  1981. view,
  1982. DataLoaderModel * model,
  1983. {
  1984. if (model->timer)
  1985. {
  1986. furi_timer_free(model->timer);
  1987. model->timer = NULL;
  1988. }
  1989. if (model->parser_context)
  1990. {
  1991. // do not free the context here, it is the app context
  1992. // free(model->parser_context);
  1993. // model->parser_context = NULL;
  1994. }
  1995. if (model->fhttp)
  1996. {
  1997. flipper_http_free(model->fhttp);
  1998. model->fhttp = NULL;
  1999. }
  2000. },
  2001. false);
  2002. }
  2003. bool custom_event_callback(void *context, uint32_t index)
  2004. {
  2005. if (context == NULL)
  2006. {
  2007. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  2008. DEV_CRASH();
  2009. return false;
  2010. }
  2011. switch (index)
  2012. {
  2013. case FlipWorldCustomEventProcess:
  2014. loader_process_callback(context);
  2015. return true;
  2016. default:
  2017. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  2018. return false;
  2019. }
  2020. }
  2021. void generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  2022. {
  2023. if (app == NULL)
  2024. {
  2025. FURI_LOG_E(TAG, "generic_switch_to_view - app is NULL");
  2026. DEV_CRASH();
  2027. return;
  2028. }
  2029. View *view = app->view_loader;
  2030. if (view == NULL)
  2031. {
  2032. FURI_LOG_E(TAG, "generic_switch_to_view - view is NULL");
  2033. DEV_CRASH();
  2034. return;
  2035. }
  2036. with_view_model(
  2037. view,
  2038. DataLoaderModel * model,
  2039. {
  2040. model->title = title;
  2041. model->fetcher = fetcher;
  2042. model->parser = parser;
  2043. model->request_index = 0;
  2044. model->request_count = request_count;
  2045. model->back_callback = back;
  2046. model->data_state = DataStateInitial;
  2047. model->data_text = NULL;
  2048. //
  2049. model->parser_context = app;
  2050. if (!model->fhttp)
  2051. {
  2052. model->fhttp = flipper_http_alloc();
  2053. }
  2054. },
  2055. true);
  2056. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2057. }