callback.c 80 KB

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