callback.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  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 :
  421. vgm_x == -1 ? 1 :
  422. vgm_x == 0 ? 2 :
  423. vgm_x == 1 ? 3 :
  424. vgm_x == 2 ? 4 :
  425. vgm_x == 3 ? 5 :
  426. vgm_x == 4 ? 6 :
  427. vgm_x == 5 ? 7 :
  428. vgm_x == 6 ? 8 :
  429. vgm_x == 7 ? 9 :
  430. vgm_x == 8 ? 10 :
  431. vgm_x == 9 ? 11 :
  432. vgm_x == 10 ? 12 :
  433. 2;
  434. variable_item_set_current_value_index(app->variable_item_game_vgm_x, index);
  435. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[index]);
  436. }
  437. char _game_vgm_y[8];
  438. if (load_char("Game-VGM-Y", _game_vgm_y, sizeof(_game_vgm_y)))
  439. {
  440. int vgm_y = atoi(_game_vgm_y);
  441. int index = vgm_y == -2 ? 0 :
  442. vgm_y == -1 ? 1 :
  443. vgm_y == 0 ? 2 :
  444. vgm_y == 1 ? 3 :
  445. vgm_y == 2 ? 4 :
  446. vgm_y == 3 ? 5 :
  447. vgm_y == 4 ? 6 :
  448. vgm_y == 5 ? 7 :
  449. vgm_y == 6 ? 8 :
  450. vgm_y == 7 ? 9 :
  451. vgm_y == 8 ? 10 :
  452. vgm_y == 9 ? 11 :
  453. vgm_y == 10 ? 12 :
  454. 2;
  455. variable_item_set_current_value_index(app->variable_item_game_vgm_y, index);
  456. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[index]);
  457. }
  458. char _game_screen_always_on[8];
  459. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  460. {
  461. int index = is_str(_game_screen_always_on, "No") ? 0 : is_str(_game_screen_always_on, "Yes") ? 1 : 0;
  462. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[index]);
  463. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  464. }
  465. char _game_sound_on[8];
  466. if (load_char("Game-Sound-On", _game_sound_on, sizeof(_game_sound_on)))
  467. {
  468. int index = is_str(_game_sound_on, "No") ? 0 : is_str(_game_sound_on, "Yes") ? 1 : 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 : 0;
  476. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[index]);
  477. variable_item_set_current_value_index(app->variable_item_game_vibration_on, index);
  478. }
  479. break;
  480. case FlipWorldSubmenuIndexUserSettings:
  481. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_select, callback_to_settings, &app->view_dispatcher, app))
  482. {
  483. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  484. return false;
  485. }
  486. if (!app->variable_item_list)
  487. {
  488. FURI_LOG_E(TAG, "Variable item list is NULL");
  489. return false;
  490. }
  491. // if logged in, show profile info, otherwise show login/register
  492. if (is_logged_in() || is_logged_in_to_flip_social())
  493. {
  494. if (!app->variable_item_user_username)
  495. {
  496. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  497. variable_item_set_current_value_text(app->variable_item_user_username, "");
  498. }
  499. if (!app->variable_item_user_password)
  500. {
  501. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  502. variable_item_set_current_value_text(app->variable_item_user_password, "");
  503. }
  504. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  505. {
  506. variable_item_set_current_value_text(app->variable_item_user_username, username);
  507. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  508. }
  509. }
  510. else
  511. {
  512. if (!app->variable_item_user_username)
  513. {
  514. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  515. variable_item_set_current_value_text(app->variable_item_user_username, "");
  516. }
  517. if (!app->variable_item_user_password)
  518. {
  519. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  520. variable_item_set_current_value_text(app->variable_item_user_password, "");
  521. }
  522. }
  523. break;
  524. }
  525. }
  526. return true;
  527. }
  528. static bool alloc_submenu_settings(void *context)
  529. {
  530. FlipWorldApp *app = (FlipWorldApp *)context;
  531. if (!app)
  532. {
  533. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  534. return false;
  535. }
  536. if (!app->submenu_settings)
  537. {
  538. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  539. {
  540. return NULL;
  541. }
  542. if (!app->submenu_settings)
  543. {
  544. return false;
  545. }
  546. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  547. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  548. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  549. }
  550. return true;
  551. }
  552. // free
  553. static void free_message_view(void *context)
  554. {
  555. FlipWorldApp *app = (FlipWorldApp *)context;
  556. if (!app)
  557. {
  558. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  559. return;
  560. }
  561. if (app->view_message)
  562. {
  563. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMessage);
  564. view_free(app->view_message);
  565. app->view_message = NULL;
  566. }
  567. }
  568. static void free_text_input_view(void *context)
  569. {
  570. FlipWorldApp *app = (FlipWorldApp *)context;
  571. if (!app)
  572. {
  573. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  574. return;
  575. }
  576. if (app->text_input)
  577. {
  578. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  579. text_input_free(app->text_input);
  580. app->text_input = NULL;
  581. }
  582. if (app->text_input_buffer)
  583. {
  584. free(app->text_input_buffer);
  585. app->text_input_buffer = NULL;
  586. }
  587. if (app->text_input_temp_buffer)
  588. {
  589. free(app->text_input_temp_buffer);
  590. app->text_input_temp_buffer = NULL;
  591. }
  592. }
  593. static void free_variable_item_list(void *context)
  594. {
  595. FlipWorldApp *app = (FlipWorldApp *)context;
  596. if (!app)
  597. {
  598. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  599. return;
  600. }
  601. if (app->variable_item_list)
  602. {
  603. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  604. variable_item_list_free(app->variable_item_list);
  605. app->variable_item_list = NULL;
  606. }
  607. if (app->variable_item_wifi_ssid)
  608. {
  609. free(app->variable_item_wifi_ssid);
  610. app->variable_item_wifi_ssid = NULL;
  611. }
  612. if (app->variable_item_wifi_pass)
  613. {
  614. free(app->variable_item_wifi_pass);
  615. app->variable_item_wifi_pass = NULL;
  616. }
  617. if (app->variable_item_game_fps)
  618. {
  619. free(app->variable_item_game_fps);
  620. app->variable_item_game_fps = NULL;
  621. }
  622. if (app->variable_item_game_screen_always_on)
  623. {
  624. free(app->variable_item_game_screen_always_on);
  625. app->variable_item_game_screen_always_on = NULL;
  626. }
  627. if (app->variable_item_game_download_world)
  628. {
  629. free(app->variable_item_game_download_world);
  630. app->variable_item_game_download_world = NULL;
  631. }
  632. if (app->variable_item_game_sound_on)
  633. {
  634. free(app->variable_item_game_sound_on);
  635. app->variable_item_game_sound_on = NULL;
  636. }
  637. if (app->variable_item_game_vibration_on)
  638. {
  639. free(app->variable_item_game_vibration_on);
  640. app->variable_item_game_vibration_on = NULL;
  641. }
  642. if (app->variable_item_game_player_sprite)
  643. {
  644. free(app->variable_item_game_player_sprite);
  645. app->variable_item_game_player_sprite = NULL;
  646. }
  647. if (app->variable_item_game_vgm_x)
  648. {
  649. free(app->variable_item_game_vgm_x);
  650. app->variable_item_game_vgm_x = NULL;
  651. }
  652. if (app->variable_item_game_vgm_y)
  653. {
  654. free(app->variable_item_game_vgm_y);
  655. app->variable_item_game_vgm_y = NULL;
  656. }
  657. if (app->variable_item_user_username)
  658. {
  659. free(app->variable_item_user_username);
  660. app->variable_item_user_username = NULL;
  661. }
  662. if (app->variable_item_user_password)
  663. {
  664. free(app->variable_item_user_password);
  665. app->variable_item_user_password = NULL;
  666. }
  667. }
  668. static void free_submenu_settings(void *context)
  669. {
  670. FlipWorldApp *app = (FlipWorldApp *)context;
  671. if (!app)
  672. {
  673. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  674. return;
  675. }
  676. if (app->submenu_settings)
  677. {
  678. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  679. submenu_free(app->submenu_settings);
  680. app->submenu_settings = NULL;
  681. }
  682. }
  683. static FuriThread *game_thread;
  684. static bool game_thread_running = false;
  685. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  686. {
  687. FlipWorldApp *app = (FlipWorldApp *)context;
  688. if (!app)
  689. {
  690. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  691. return;
  692. }
  693. if (should_free_variable_item_list)
  694. {
  695. free_variable_item_list(app);
  696. }
  697. free_message_view(app);
  698. free_text_input_view(app);
  699. // free game thread
  700. if (game_thread_running)
  701. {
  702. game_thread_running = false;
  703. if (game_thread)
  704. {
  705. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  706. furi_thread_join(game_thread);
  707. furi_thread_free(game_thread);
  708. game_thread = NULL;
  709. }
  710. }
  711. if (should_free_submenu_settings) free_submenu_settings(app);
  712. }
  713. static bool fetch_world_list(FlipperHTTP *fhttp)
  714. {
  715. if (!fhttp)
  716. {
  717. FURI_LOG_E(TAG, "fhttp is NULL");
  718. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  719. return false;
  720. }
  721. // ensure flip_world directory exists
  722. char directory_path[128];
  723. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  724. Storage *storage = furi_record_open(RECORD_STORAGE);
  725. storage_common_mkdir(storage, directory_path);
  726. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  727. storage_common_mkdir(storage, directory_path);
  728. furi_record_close(RECORD_STORAGE);
  729. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  730. fhttp->save_received_data = true;
  731. return flipper_http_get_request_with_headers(fhttp, "https://www.flipsocial.net/api/world/v5/list/10/", "{\"Content-Type\":\"application/json\"}");
  732. }
  733. // we will load the palyer stats from the API and save them
  734. // in player_spawn game method, it will load the player stats that we saved
  735. static bool fetch_player_stats(FlipperHTTP *fhttp)
  736. {
  737. if (!fhttp)
  738. {
  739. FURI_LOG_E(TAG, "fhttp is NULL");
  740. easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  741. return false;
  742. }
  743. char username[64];
  744. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  745. {
  746. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  747. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  748. return false;
  749. }
  750. char url[128];
  751. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/game-stats/%s/", username);
  752. // ensure the folders exist
  753. char directory_path[128];
  754. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  755. Storage *storage = furi_record_open(RECORD_STORAGE);
  756. storage_common_mkdir(storage, directory_path);
  757. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  758. storage_common_mkdir(storage, directory_path);
  759. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player");
  760. storage_common_mkdir(storage, directory_path);
  761. furi_record_close(RECORD_STORAGE);
  762. snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
  763. fhttp->save_received_data = true;
  764. return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
  765. }
  766. // static bool fetch_app_update(FlipperHTTP *fhttp)
  767. // {
  768. // if (!fhttp)
  769. // {
  770. // FURI_LOG_E(TAG, "fhttp is NULL");
  771. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  772. // return false;
  773. // }
  774. // return flipper_http_get_request_with_headers(fhttp, "https://www.flipsocial.net/api/app/last-updated/flip_world/", "{\"Content-Type\":\"application/json\"}");
  775. // }
  776. // static bool parse_app_update(FlipperHTTP *fhttp)
  777. // {
  778. // if (!fhttp)
  779. // {
  780. // FURI_LOG_E(TAG, "fhttp is NULL");
  781. // easy_flipper_dialog("Error", "fhttp is NULL. Press BACK to return.");
  782. // return false;
  783. // }
  784. // if (fhttp->last_response == NULL || strlen(fhttp->last_response) == 0)
  785. // {
  786. // FURI_LOG_E(TAG, "fhttp->last_response is NULL or empty");
  787. // easy_flipper_dialog("Error", "fhttp->last_response is NULL or empty. Press BACK to return.");
  788. // return false;
  789. // }
  790. // bool last_update_available = false;
  791. // char last_updated_old[32];
  792. // // load the previous last_updated
  793. // if (!load_char("last_updated", last_updated_old, sizeof(last_updated_old)))
  794. // {
  795. // FURI_LOG_E(TAG, "Failed to load last_updated");
  796. // // it's okay, we'll just update it
  797. // }
  798. // // save the new last_updated
  799. // save_char("last_updated", fhttp->last_response);
  800. // // compare the two
  801. // if (strlen(last_updated_old) == 0 || !is_str(last_updated_old, fhttp->last_response))
  802. // {
  803. // last_update_available = true;
  804. // }
  805. // if (last_update_available)
  806. // {
  807. // easy_flipper_dialog("Update Available", "An update is available. Press OK to update.");
  808. // return true;
  809. // }
  810. // else
  811. // {
  812. // easy_flipper_dialog("No Update Available", "No update is available. Press OK to continue.");
  813. // return false;
  814. // }
  815. // }
  816. static bool start_game_thread(void *context)
  817. {
  818. FlipWorldApp *app = (FlipWorldApp *)context;
  819. if (!app)
  820. {
  821. FURI_LOG_E(TAG, "app is NULL");
  822. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  823. return false;
  824. }
  825. // free game thread
  826. if (game_thread_running)
  827. {
  828. game_thread_running = false;
  829. if (game_thread)
  830. {
  831. furi_thread_flags_set(furi_thread_get_id(game_thread), WorkerEvtStop);
  832. furi_thread_join(game_thread);
  833. furi_thread_free(game_thread);
  834. }
  835. }
  836. // start game thread
  837. FuriThread *thread = furi_thread_alloc_ex("game", 2048, game_app, app);
  838. if (!thread)
  839. {
  840. FURI_LOG_E(TAG, "Failed to allocate game thread");
  841. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  842. return false;
  843. }
  844. furi_thread_start(thread);
  845. game_thread = thread;
  846. game_thread_running = true;
  847. return true;
  848. }
  849. // combine register, login, and world list fetch into one function to switch to the loader view
  850. static bool _fetch_game(DataLoaderModel *model)
  851. {
  852. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  853. if (!app)
  854. {
  855. FURI_LOG_E(TAG, "app is NULL");
  856. easy_flipper_dialog("Error", "app is NULL. Press BACK to return.");
  857. return false;
  858. }
  859. if (model->request_index == 0)
  860. {
  861. // login
  862. char username[64];
  863. char password[64];
  864. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  865. {
  866. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  867. view_dispatcher_switch_to_view(app->view_dispatcher,
  868. FlipWorldViewSubmenu); // just go back to the main menu for now
  869. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  870. return false;
  871. }
  872. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  873. {
  874. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  875. view_dispatcher_switch_to_view(app->view_dispatcher,
  876. FlipWorldViewSubmenu); // just go back to the main menu for now
  877. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  878. return false;
  879. }
  880. char payload[256];
  881. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  882. return flipper_http_post_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  883. }
  884. else if (model->request_index == 1)
  885. {
  886. // check if login was successful
  887. char is_logged_in[8];
  888. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  889. {
  890. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  891. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  892. view_dispatcher_switch_to_view(app->view_dispatcher,
  893. FlipWorldViewSubmenu); // just go back to the main menu for now
  894. return false;
  895. }
  896. if (is_str(is_logged_in, "false") && is_str(model->title, "Registering..."))
  897. {
  898. // register
  899. char username[64];
  900. char password[64];
  901. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  902. {
  903. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  904. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  905. view_dispatcher_switch_to_view(app->view_dispatcher,
  906. FlipWorldViewSubmenu); // just go back to the main menu for now
  907. return false;
  908. }
  909. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  910. {
  911. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  912. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  913. view_dispatcher_switch_to_view(app->view_dispatcher,
  914. FlipWorldViewSubmenu); // just go back to the main menu for now
  915. return false;
  916. }
  917. char payload[172];
  918. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  919. model->title = "Registering...";
  920. return flipper_http_post_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  921. }
  922. else
  923. {
  924. model->title = "Fetching World List..";
  925. return fetch_world_list(model->fhttp);
  926. }
  927. }
  928. else if (model->request_index == 2)
  929. {
  930. model->title = "Fetching World List..";
  931. return fetch_world_list(model->fhttp);
  932. }
  933. else if (model->request_index == 3)
  934. {
  935. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  936. FuriString *world_list = flipper_http_load_from_file(model->fhttp->file_path);
  937. if (!world_list)
  938. {
  939. view_dispatcher_switch_to_view(app->view_dispatcher,
  940. FlipWorldViewSubmenu); // just go back to the main menu for now
  941. FURI_LOG_E(TAG, "Failed to load world list");
  942. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  943. return false;
  944. }
  945. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  946. if (!first_world)
  947. {
  948. view_dispatcher_switch_to_view(app->view_dispatcher,
  949. FlipWorldViewSubmenu); // just go back to the main menu for now
  950. FURI_LOG_E(TAG, "Failed to get first world");
  951. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  952. furi_string_free(world_list);
  953. return false;
  954. }
  955. if (world_exists(furi_string_get_cstr(first_world)))
  956. {
  957. furi_string_free(world_list);
  958. furi_string_free(first_world);
  959. if (!start_game_thread(app))
  960. {
  961. FURI_LOG_E(TAG, "Failed to start game thread");
  962. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  963. view_dispatcher_switch_to_view(app->view_dispatcher,
  964. FlipWorldViewSubmenu); // just go back to the main menu for now
  965. return "Failed to start game thread";
  966. }
  967. return true;
  968. }
  969. 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));
  970. model->fhttp->save_received_data = true;
  971. char url[128];
  972. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v5/get/world/%s/", furi_string_get_cstr(first_world));
  973. furi_string_free(world_list);
  974. furi_string_free(first_world);
  975. return flipper_http_get_request_with_headers(model->fhttp, url, "{\"Content-Type\":\"application/json\"}");
  976. }
  977. FURI_LOG_E(TAG, "Unknown request index");
  978. return false;
  979. }
  980. static char *_parse_game(DataLoaderModel *model)
  981. {
  982. FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
  983. if (model->request_index == 0)
  984. {
  985. if (!model->fhttp->last_response)
  986. {
  987. save_char("is_logged_in", "false");
  988. // Go back to the main menu
  989. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  990. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  991. return "Response is empty...";
  992. }
  993. // Check for successful conditions
  994. if (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User found") != NULL)
  995. {
  996. save_char("is_logged_in", "true");
  997. model->title = "Login successful!";
  998. model->title = "Fetching World List..";
  999. return "Login successful!";
  1000. }
  1001. // Check if user not found
  1002. if (strstr(model->fhttp->last_response, "User not found") != NULL)
  1003. {
  1004. save_char("is_logged_in", "false");
  1005. model->title = "Registering...";
  1006. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  1007. }
  1008. // If not success, not found, check length conditions
  1009. size_t resp_len = strlen(model->fhttp->last_response);
  1010. if (resp_len == 0 || resp_len > 127)
  1011. {
  1012. // Empty or too long means failed login
  1013. save_char("is_logged_in", "false");
  1014. // Go back to the main menu
  1015. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1016. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1017. return "Failed to login...";
  1018. }
  1019. // Handle any other unknown response as a failure
  1020. save_char("is_logged_in", "false");
  1021. // Go back to the main menu
  1022. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  1023. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1024. return "Failed to login...";
  1025. }
  1026. else if (model->request_index == 1)
  1027. {
  1028. if (is_str(model->title, "Registering..."))
  1029. {
  1030. // check registration response
  1031. if (model->fhttp->last_response != NULL && (strstr(model->fhttp->last_response, "[SUCCESS]") != NULL || strstr(model->fhttp->last_response, "User created") != NULL))
  1032. {
  1033. save_char("is_logged_in", "true");
  1034. char username[64];
  1035. char password[64];
  1036. // load the username and password, then save them
  1037. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  1038. {
  1039. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  1040. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  1041. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1042. return "Failed to load Flip-Social-Username";
  1043. }
  1044. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  1045. {
  1046. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  1047. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  1048. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1049. return "Failed to load Flip-Social-Password";
  1050. }
  1051. // load wifi ssid,pass then save
  1052. char ssid[64];
  1053. char pass[64];
  1054. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1055. {
  1056. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  1057. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  1058. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1059. return "Failed to load WiFi-SSID";
  1060. }
  1061. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  1062. {
  1063. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  1064. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  1065. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);
  1066. return "Failed to load WiFi-Password";
  1067. }
  1068. save_settings(ssid, pass, username, password);
  1069. model->title = "Fetching World List..";
  1070. return "Account created!";
  1071. }
  1072. else if (strstr(model->fhttp->last_response, "Username or password not provided") != NULL)
  1073. {
  1074. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  1075. view_dispatcher_switch_to_view(app->view_dispatcher,
  1076. FlipWorldViewSubmenu); // just go back to the main menu for now
  1077. return "Please enter your credentials.";
  1078. }
  1079. else if (strstr(model->fhttp->last_response, "User already exists") != NULL || strstr(model->fhttp->last_response, "Multiple users found") != NULL)
  1080. {
  1081. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  1082. view_dispatcher_switch_to_view(app->view_dispatcher,
  1083. FlipWorldViewSubmenu); // just go back to the main menu for now
  1084. return "Username already exists.";
  1085. }
  1086. else
  1087. {
  1088. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  1089. view_dispatcher_switch_to_view(app->view_dispatcher,
  1090. FlipWorldViewSubmenu); // just go back to the main menu for now
  1091. return "Registration failed...";
  1092. }
  1093. }
  1094. else
  1095. {
  1096. if (!start_game_thread(app))
  1097. {
  1098. FURI_LOG_E(TAG, "Failed to start game thread");
  1099. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1100. view_dispatcher_switch_to_view(app->view_dispatcher,
  1101. FlipWorldViewSubmenu); // just go back to the main menu for now
  1102. return "Failed to start game thread";
  1103. }
  1104. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1105. }
  1106. }
  1107. else if (model->request_index == 2)
  1108. {
  1109. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1110. }
  1111. else if (model->request_index == 3)
  1112. {
  1113. if (!start_game_thread(app))
  1114. {
  1115. FURI_LOG_E(TAG, "Failed to start game thread");
  1116. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1117. view_dispatcher_switch_to_view(app->view_dispatcher,
  1118. FlipWorldViewSubmenu); // just go back to the main menu for now
  1119. return "Failed to start game thread";
  1120. }
  1121. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1122. }
  1123. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  1124. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1125. return "Unknown error";
  1126. }
  1127. static void switch_to_view_get_game(FlipWorldApp *app)
  1128. {
  1129. generic_switch_to_view(app, "Starting Game..", _fetch_game, _parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  1130. }
  1131. void callback_submenu_choices(void *context, uint32_t index)
  1132. {
  1133. FlipWorldApp *app = (FlipWorldApp *)context;
  1134. if (!app)
  1135. {
  1136. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1137. return;
  1138. }
  1139. switch (index)
  1140. {
  1141. case FlipWorldSubmenuIndexRun:
  1142. free_all_views(app, true, true);
  1143. if (!is_enough_heap(60000))
  1144. {
  1145. easy_flipper_dialog("Error", "Not enough heap memory.\nPlease restart your Flipper.");
  1146. return;
  1147. }
  1148. // check if logged in
  1149. if (is_logged_in() || is_logged_in_to_flip_social())
  1150. {
  1151. FlipperHTTP *fhttp = flipper_http_alloc();
  1152. if (!fhttp)
  1153. {
  1154. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  1155. easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
  1156. return;
  1157. }
  1158. bool fetch_world_list_i()
  1159. {
  1160. return fetch_world_list(fhttp);
  1161. }
  1162. bool parse_world_list_i()
  1163. {
  1164. return fhttp->state != ISSUE;
  1165. }
  1166. bool fetch_player_stats_i()
  1167. {
  1168. return fetch_player_stats(fhttp);
  1169. }
  1170. if (!alloc_message_view(app, MessageStateLoading))
  1171. {
  1172. FURI_LOG_E(TAG, "Failed to allocate message view");
  1173. return;
  1174. }
  1175. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1176. // Make the request
  1177. if (!flipper_http_process_response_async(fhttp, fetch_world_list_i, parse_world_list_i) || !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
  1178. {
  1179. FURI_LOG_E(HTTP_TAG, "Failed to make request");
  1180. flipper_http_free(fhttp);
  1181. }
  1182. else
  1183. {
  1184. flipper_http_free(fhttp);
  1185. }
  1186. if (!alloc_submenu_settings(app))
  1187. {
  1188. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1189. return;
  1190. }
  1191. if (!start_game_thread(app))
  1192. {
  1193. FURI_LOG_E(TAG, "Failed to start game thread");
  1194. easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
  1195. return;
  1196. }
  1197. }
  1198. else
  1199. {
  1200. switch_to_view_get_game(app);
  1201. }
  1202. break;
  1203. case FlipWorldSubmenuIndexMessage:
  1204. // About menu.
  1205. free_all_views(app, true, true);
  1206. if (!alloc_message_view(app, MessageStateAbout))
  1207. {
  1208. FURI_LOG_E(TAG, "Failed to allocate message view");
  1209. return;
  1210. }
  1211. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
  1212. break;
  1213. case FlipWorldSubmenuIndexSettings:
  1214. free_all_views(app, true, true);
  1215. if (!alloc_submenu_settings(app))
  1216. {
  1217. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1218. return;
  1219. }
  1220. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1221. break;
  1222. case FlipWorldSubmenuIndexWiFiSettings:
  1223. free_all_views(app, true, false);
  1224. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1225. {
  1226. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1227. return;
  1228. }
  1229. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1230. break;
  1231. case FlipWorldSubmenuIndexGameSettings:
  1232. free_all_views(app, true, false);
  1233. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1234. {
  1235. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1236. return;
  1237. }
  1238. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1239. break;
  1240. case FlipWorldSubmenuIndexUserSettings:
  1241. free_all_views(app, true, false);
  1242. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1243. {
  1244. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1245. return;
  1246. }
  1247. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1248. break;
  1249. default:
  1250. break;
  1251. }
  1252. }
  1253. static void updated_wifi_ssid(void *context)
  1254. {
  1255. FlipWorldApp *app = (FlipWorldApp *)context;
  1256. if (!app)
  1257. {
  1258. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1259. return;
  1260. }
  1261. // store the entered text
  1262. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1263. // Ensure null-termination
  1264. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1265. // save the setting
  1266. save_char("WiFi-SSID", app->text_input_buffer);
  1267. // update the variable item text
  1268. if (app->variable_item_wifi_ssid)
  1269. {
  1270. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1271. // get value of password
  1272. char pass[64];
  1273. char username[64];
  1274. char password[64];
  1275. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1276. {
  1277. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1278. {
  1279. // save the settings
  1280. load_char("Flip-Social-Username", username, sizeof(username));
  1281. load_char("Flip-Social-Password", password, sizeof(password));
  1282. save_settings(app->text_input_buffer, pass, username, password);
  1283. // initialize the http
  1284. FlipperHTTP *fhttp = flipper_http_alloc();
  1285. if (fhttp)
  1286. {
  1287. // save the wifi if the device is connected
  1288. if (!flipper_http_save_wifi(fhttp, app->text_input_buffer, pass))
  1289. {
  1290. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1291. }
  1292. // free the resources
  1293. flipper_http_free(fhttp);
  1294. }
  1295. else
  1296. {
  1297. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1298. }
  1299. }
  1300. }
  1301. }
  1302. // switch to the settings view
  1303. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1304. }
  1305. static void updated_wifi_pass(void *context)
  1306. {
  1307. FlipWorldApp *app = (FlipWorldApp *)context;
  1308. if (!app)
  1309. {
  1310. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1311. return;
  1312. }
  1313. // store the entered text
  1314. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1315. // Ensure null-termination
  1316. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1317. // save the setting
  1318. save_char("WiFi-Password", app->text_input_buffer);
  1319. // update the variable item text
  1320. if (app->variable_item_wifi_pass)
  1321. {
  1322. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1323. }
  1324. // get value of ssid
  1325. char ssid[64];
  1326. char username[64];
  1327. char password[64];
  1328. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1329. {
  1330. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1331. {
  1332. // save the settings
  1333. load_char("Flip-Social-Username", username, sizeof(username));
  1334. load_char("Flip-Social-Password", password, sizeof(password));
  1335. save_settings(ssid, app->text_input_buffer, username, password);
  1336. // initialize the http
  1337. FlipperHTTP *fhttp = flipper_http_alloc();
  1338. if (fhttp)
  1339. {
  1340. // save the wifi if the device is connected
  1341. if (!flipper_http_save_wifi(fhttp, ssid, app->text_input_buffer))
  1342. {
  1343. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1344. }
  1345. // free the resources
  1346. flipper_http_free(fhttp);
  1347. }
  1348. else
  1349. {
  1350. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1351. }
  1352. }
  1353. }
  1354. // switch to the settings view
  1355. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1356. }
  1357. static void updated_username(void *context)
  1358. {
  1359. FlipWorldApp *app = (FlipWorldApp *)context;
  1360. if (!app)
  1361. {
  1362. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1363. return;
  1364. }
  1365. // store the entered text
  1366. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1367. // Ensure null-termination
  1368. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1369. // save the setting
  1370. save_char("Flip-Social-Username", app->text_input_buffer);
  1371. // update the variable item text
  1372. if (app->variable_item_user_username)
  1373. {
  1374. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1375. }
  1376. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1377. }
  1378. static void updated_password(void *context)
  1379. {
  1380. FlipWorldApp *app = (FlipWorldApp *)context;
  1381. if (!app)
  1382. {
  1383. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1384. return;
  1385. }
  1386. // store the entered text
  1387. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1388. // Ensure null-termination
  1389. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1390. // save the setting
  1391. save_char("Flip-Social-Password", app->text_input_buffer);
  1392. // update the variable item text
  1393. if (app->variable_item_user_password)
  1394. {
  1395. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1396. }
  1397. // get value of username
  1398. char username[64];
  1399. char ssid[64];
  1400. char pass[64];
  1401. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1402. {
  1403. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1404. {
  1405. // save the settings
  1406. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1407. load_char("WiFi-Password", pass, sizeof(pass));
  1408. save_settings(ssid, pass, username, app->text_input_buffer);
  1409. }
  1410. }
  1411. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1412. }
  1413. static void wifi_settings_select(void *context, uint32_t index)
  1414. {
  1415. FlipWorldApp *app = (FlipWorldApp *)context;
  1416. if (!app)
  1417. {
  1418. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1419. return;
  1420. }
  1421. char ssid[64];
  1422. char pass[64];
  1423. char username[64];
  1424. char password[64];
  1425. switch (index)
  1426. {
  1427. case 0: // Input SSID
  1428. free_all_views(app, false, false);
  1429. if (!alloc_text_input_view(app, "SSID"))
  1430. {
  1431. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1432. return;
  1433. }
  1434. // load SSID
  1435. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1436. {
  1437. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1438. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1439. }
  1440. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1441. break;
  1442. case 1: // Input Password
  1443. free_all_views(app, false, false);
  1444. if (!alloc_text_input_view(app, "Password"))
  1445. {
  1446. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1447. return;
  1448. }
  1449. // load password
  1450. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1451. {
  1452. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1453. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1454. }
  1455. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1456. break;
  1457. default:
  1458. FURI_LOG_E(TAG, "Unknown configuration item index");
  1459. break;
  1460. }
  1461. }
  1462. static void fps_change(VariableItem *item)
  1463. {
  1464. uint8_t index = variable_item_get_current_value_index(item);
  1465. fps_index = index;
  1466. variable_item_set_current_value_text(item, fps_choices_str[index]);
  1467. variable_item_set_current_value_index(item, index);
  1468. save_char("Game-FPS", fps_choices_str[index]);
  1469. }
  1470. static void screen_on_change(VariableItem *item)
  1471. {
  1472. uint8_t index = variable_item_get_current_value_index(item);
  1473. screen_always_on_index = index;
  1474. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1475. variable_item_set_current_value_index(item, index);
  1476. save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
  1477. }
  1478. static void sound_on_change(VariableItem *item)
  1479. {
  1480. uint8_t index = variable_item_get_current_value_index(item);
  1481. sound_on_index = index;
  1482. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1483. variable_item_set_current_value_index(item, index);
  1484. save_char("Game-Sound-On", yes_or_no_choices[index]);
  1485. }
  1486. static void vibration_on_change(VariableItem *item)
  1487. {
  1488. uint8_t index = variable_item_get_current_value_index(item);
  1489. vibration_on_index = index;
  1490. variable_item_set_current_value_text(item, yes_or_no_choices[index]);
  1491. variable_item_set_current_value_index(item, index);
  1492. save_char("Game-Vibration-On", yes_or_no_choices[index]);
  1493. }
  1494. static void player_on_change(VariableItem *item)
  1495. {
  1496. uint8_t index = variable_item_get_current_value_index(item);
  1497. player_sprite_index = index;
  1498. variable_item_set_current_value_text(item, is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  1499. variable_item_set_current_value_index(item, index);
  1500. save_char("Game-Player-Sprite", player_sprite_choices[index]);
  1501. }
  1502. static void vgm_x_change(VariableItem *item)
  1503. {
  1504. uint8_t index = variable_item_get_current_value_index(item);
  1505. vgm_x_index = index;
  1506. variable_item_set_current_value_text(item, vgm_levels[index]);
  1507. variable_item_set_current_value_index(item, index);
  1508. save_char("Game-VGM-X", vgm_levels[index]);
  1509. }
  1510. static void vgm_y_change(VariableItem *item)
  1511. {
  1512. uint8_t index = variable_item_get_current_value_index(item);
  1513. vgm_y_index = index;
  1514. variable_item_set_current_value_text(item, vgm_levels[index]);
  1515. variable_item_set_current_value_index(item, index);
  1516. save_char("Game-VGM-Y", vgm_levels[index]);
  1517. }
  1518. static bool _fetch_worlds(DataLoaderModel *model)
  1519. {
  1520. if (!model || !model->fhttp)
  1521. {
  1522. FURI_LOG_E(TAG, "model or fhttp is NULL");
  1523. return false;
  1524. }
  1525. char directory_path[128];
  1526. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  1527. Storage *storage = furi_record_open(RECORD_STORAGE);
  1528. storage_common_mkdir(storage, directory_path);
  1529. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1530. storage_common_mkdir(storage, directory_path);
  1531. furi_record_close(RECORD_STORAGE);
  1532. snprintf(model->fhttp->file_path, sizeof(model->fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1533. model->fhttp->save_received_data = true;
  1534. return flipper_http_get_request_with_headers(model->fhttp, "https://www.flipsocial.net/api/world/v5/get/10/", "{\"Content-Type\":\"application/json\"}");
  1535. }
  1536. static char *_parse_worlds(DataLoaderModel *model)
  1537. {
  1538. UNUSED(model);
  1539. return "World Pack Installed";
  1540. }
  1541. static void switch_to_view_get_worlds(FlipWorldApp *app)
  1542. {
  1543. generic_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1544. }
  1545. static void game_settings_select(void *context, uint32_t index)
  1546. {
  1547. FlipWorldApp *app = (FlipWorldApp *)context;
  1548. if (!app)
  1549. {
  1550. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1551. return;
  1552. }
  1553. switch (index)
  1554. {
  1555. case 0: // Download all world data as one huge json
  1556. switch_to_view_get_worlds(app);
  1557. case 1: // Player Sprite
  1558. break;
  1559. case 2: // Change FPS
  1560. break;
  1561. case 3: // VGM X
  1562. break;
  1563. case 4: // VGM Y
  1564. break;
  1565. case 5: // Screen Always On
  1566. break;
  1567. case 6: // Sound On
  1568. break;
  1569. case 7: // Vibration On
  1570. break;
  1571. }
  1572. }
  1573. static void user_settings_select(void *context, uint32_t index)
  1574. {
  1575. FlipWorldApp *app = (FlipWorldApp *)context;
  1576. if (!app)
  1577. {
  1578. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1579. return;
  1580. }
  1581. switch (index)
  1582. {
  1583. case 0: // Username
  1584. free_all_views(app, false, false);
  1585. if (!alloc_text_input_view(app, "Username-Login"))
  1586. {
  1587. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1588. return;
  1589. }
  1590. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1591. break;
  1592. case 1: // Password
  1593. free_all_views(app, false, false);
  1594. if (!alloc_text_input_view(app, "Password-Login"))
  1595. {
  1596. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1597. return;
  1598. }
  1599. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1600. break;
  1601. }
  1602. }
  1603. static void widget_set_text(char *message, Widget **widget)
  1604. {
  1605. if (widget == NULL)
  1606. {
  1607. FURI_LOG_E(TAG, "set_widget_text - widget is NULL");
  1608. DEV_CRASH();
  1609. return;
  1610. }
  1611. if (message == NULL)
  1612. {
  1613. FURI_LOG_E(TAG, "set_widget_text - message is NULL");
  1614. DEV_CRASH();
  1615. return;
  1616. }
  1617. widget_reset(*widget);
  1618. uint32_t message_length = strlen(message); // Length of the message
  1619. uint32_t i = 0; // Index tracker
  1620. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1621. char *formatted_message; // Buffer to hold the final formatted message
  1622. // Allocate buffer with double the message length plus one for safety
  1623. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1624. {
  1625. return;
  1626. }
  1627. while (i < message_length)
  1628. {
  1629. uint32_t max_line_length = 31; // Maximum characters per line
  1630. uint32_t remaining_length = message_length - i; // Remaining characters
  1631. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1632. // Check for newline character within the current segment
  1633. uint32_t newline_pos = i;
  1634. bool found_newline = false;
  1635. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1636. {
  1637. if (message[newline_pos] == '\n')
  1638. {
  1639. found_newline = true;
  1640. break;
  1641. }
  1642. }
  1643. if (found_newline)
  1644. {
  1645. // If newline found, set line_length up to the newline
  1646. line_length = newline_pos - i;
  1647. }
  1648. // Temporary buffer to hold the current line
  1649. char line[32];
  1650. strncpy(line, message + i, line_length);
  1651. line[line_length] = '\0';
  1652. // If newline was found, skip it for the next iteration
  1653. if (found_newline)
  1654. {
  1655. i += line_length + 1; // +1 to skip the '\n' character
  1656. }
  1657. else
  1658. {
  1659. // Check if the line ends in the middle of a word and adjust accordingly
  1660. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1661. {
  1662. // Find the last space within the current line to avoid breaking a word
  1663. char *last_space = strrchr(line, ' ');
  1664. if (last_space != NULL)
  1665. {
  1666. // Adjust the line_length to avoid cutting the word
  1667. line_length = last_space - line;
  1668. line[line_length] = '\0'; // Null-terminate at the space
  1669. }
  1670. }
  1671. // Move the index forward by the determined line_length
  1672. i += line_length;
  1673. // Skip any spaces at the beginning of the next line
  1674. while (i < message_length && message[i] == ' ')
  1675. {
  1676. i++;
  1677. }
  1678. }
  1679. // Manually copy the fixed line into the formatted_message buffer
  1680. for (uint32_t j = 0; j < line_length; j++)
  1681. {
  1682. formatted_message[formatted_index++] = line[j];
  1683. }
  1684. // Add a newline character for line spacing
  1685. formatted_message[formatted_index++] = '\n';
  1686. }
  1687. // Null-terminate the formatted_message
  1688. formatted_message[formatted_index] = '\0';
  1689. // Add the formatted message to the widget
  1690. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1691. }
  1692. void loader_draw_callback(Canvas *canvas, void *model)
  1693. {
  1694. if (!canvas || !model)
  1695. {
  1696. FURI_LOG_E(TAG, "loader_draw_callback - canvas or model is NULL");
  1697. return;
  1698. }
  1699. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1700. SerialState http_state = data_loader_model->fhttp->state;
  1701. DataState data_state = data_loader_model->data_state;
  1702. char *title = data_loader_model->title;
  1703. canvas_set_font(canvas, FontSecondary);
  1704. if (http_state == INACTIVE)
  1705. {
  1706. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1707. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1708. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1709. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1710. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1711. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1712. return;
  1713. }
  1714. if (data_state == DataStateError || data_state == DataStateParseError)
  1715. {
  1716. error_draw(canvas, data_loader_model);
  1717. return;
  1718. }
  1719. canvas_draw_str(canvas, 0, 7, title);
  1720. canvas_draw_str(canvas, 0, 17, "Loading...");
  1721. if (data_state == DataStateInitial)
  1722. {
  1723. return;
  1724. }
  1725. if (http_state == SENDING)
  1726. {
  1727. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1728. return;
  1729. }
  1730. if (http_state == RECEIVING || data_state == DataStateRequested)
  1731. {
  1732. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1733. return;
  1734. }
  1735. if (http_state == IDLE && data_state == DataStateReceived)
  1736. {
  1737. canvas_draw_str(canvas, 0, 27, "Processing...");
  1738. return;
  1739. }
  1740. if (http_state == IDLE && data_state == DataStateParsed)
  1741. {
  1742. canvas_draw_str(canvas, 0, 27, "Processed...");
  1743. return;
  1744. }
  1745. }
  1746. static void loader_process_callback(void *context)
  1747. {
  1748. if (context == NULL)
  1749. {
  1750. FURI_LOG_E(TAG, "loader_process_callback - context is NULL");
  1751. DEV_CRASH();
  1752. return;
  1753. }
  1754. FlipWorldApp *app = (FlipWorldApp *)context;
  1755. View *view = app->view_loader;
  1756. DataState current_data_state;
  1757. DataLoaderModel *loader_model = NULL;
  1758. with_view_model(
  1759. view,
  1760. DataLoaderModel * model,
  1761. {
  1762. current_data_state = model->data_state;
  1763. loader_model = model;
  1764. },
  1765. false);
  1766. if (!loader_model || !loader_model->fhttp)
  1767. {
  1768. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1769. DEV_CRASH();
  1770. return;
  1771. }
  1772. if (current_data_state == DataStateInitial)
  1773. {
  1774. with_view_model(
  1775. view,
  1776. DataLoaderModel * model,
  1777. {
  1778. model->data_state = DataStateRequested;
  1779. DataLoaderFetch fetch = model->fetcher;
  1780. if (fetch == NULL)
  1781. {
  1782. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1783. model->data_state = DataStateError;
  1784. return;
  1785. }
  1786. // Clear any previous responses
  1787. strncpy(model->fhttp->last_response, "", 1);
  1788. bool request_status = fetch(model);
  1789. if (!request_status)
  1790. {
  1791. model->data_state = DataStateError;
  1792. }
  1793. },
  1794. true);
  1795. }
  1796. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1797. {
  1798. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1799. {
  1800. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1801. {
  1802. FURI_LOG_DEV(TAG, "PONG received.");
  1803. }
  1804. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9))
  1805. {
  1806. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1807. }
  1808. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9))
  1809. {
  1810. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1811. }
  1812. else if (strlen(loader_model->fhttp->last_response))
  1813. {
  1814. // Still waiting on response
  1815. }
  1816. else
  1817. {
  1818. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1819. }
  1820. }
  1821. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1822. {
  1823. // continue waiting
  1824. }
  1825. else if (loader_model->fhttp->state == INACTIVE)
  1826. {
  1827. // inactive. try again
  1828. }
  1829. else if (loader_model->fhttp->state == ISSUE)
  1830. {
  1831. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1832. }
  1833. else
  1834. {
  1835. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1836. DEV_CRASH();
  1837. }
  1838. }
  1839. else if (current_data_state == DataStateReceived)
  1840. {
  1841. with_view_model(
  1842. view,
  1843. DataLoaderModel * model,
  1844. {
  1845. char *data_text;
  1846. if (model->parser == NULL)
  1847. {
  1848. data_text = NULL;
  1849. FURI_LOG_DEV(TAG, "Parser is NULL");
  1850. DEV_CRASH();
  1851. }
  1852. else
  1853. {
  1854. data_text = model->parser(model);
  1855. }
  1856. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1857. model->data_text = data_text;
  1858. if (data_text == NULL)
  1859. {
  1860. model->data_state = DataStateParseError;
  1861. }
  1862. else
  1863. {
  1864. model->data_state = DataStateParsed;
  1865. }
  1866. },
  1867. true);
  1868. }
  1869. else if (current_data_state == DataStateParsed)
  1870. {
  1871. with_view_model(
  1872. view,
  1873. DataLoaderModel * model,
  1874. {
  1875. if (++model->request_index < model->request_count)
  1876. {
  1877. model->data_state = DataStateInitial;
  1878. }
  1879. else
  1880. {
  1881. widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1882. if (model->data_text != NULL)
  1883. {
  1884. free(model->data_text);
  1885. model->data_text = NULL;
  1886. }
  1887. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1888. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1889. }
  1890. },
  1891. true);
  1892. }
  1893. }
  1894. static void loader_timer_callback(void *context)
  1895. {
  1896. if (context == NULL)
  1897. {
  1898. FURI_LOG_E(TAG, "loader_timer_callback - context is NULL");
  1899. DEV_CRASH();
  1900. return;
  1901. }
  1902. FlipWorldApp *app = (FlipWorldApp *)context;
  1903. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1904. }
  1905. static void loader_on_enter(void *context)
  1906. {
  1907. if (context == NULL)
  1908. {
  1909. FURI_LOG_E(TAG, "loader_on_enter - context is NULL");
  1910. DEV_CRASH();
  1911. return;
  1912. }
  1913. FlipWorldApp *app = (FlipWorldApp *)context;
  1914. View *view = app->view_loader;
  1915. with_view_model(
  1916. view,
  1917. DataLoaderModel * model,
  1918. {
  1919. view_set_previous_callback(view, model->back_callback);
  1920. if (model->timer == NULL)
  1921. {
  1922. model->timer = furi_timer_alloc(loader_timer_callback, FuriTimerTypePeriodic, app);
  1923. }
  1924. furi_timer_start(model->timer, 250);
  1925. },
  1926. true);
  1927. }
  1928. static void loader_on_exit(void *context)
  1929. {
  1930. if (context == NULL)
  1931. {
  1932. FURI_LOG_E(TAG, "loader_on_exit - context is NULL");
  1933. DEV_CRASH();
  1934. return;
  1935. }
  1936. FlipWorldApp *app = (FlipWorldApp *)context;
  1937. View *view = app->view_loader;
  1938. with_view_model(
  1939. view,
  1940. DataLoaderModel * model,
  1941. {
  1942. if (model->timer)
  1943. {
  1944. furi_timer_stop(model->timer);
  1945. }
  1946. },
  1947. false);
  1948. }
  1949. void loader_init(View *view)
  1950. {
  1951. if (view == NULL)
  1952. {
  1953. FURI_LOG_E(TAG, "loader_init - view is NULL");
  1954. DEV_CRASH();
  1955. return;
  1956. }
  1957. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1958. view_set_enter_callback(view, loader_on_enter);
  1959. view_set_exit_callback(view, loader_on_exit);
  1960. }
  1961. void loader_free_model(View *view)
  1962. {
  1963. if (view == NULL)
  1964. {
  1965. FURI_LOG_E(TAG, "loader_free_model - view is NULL");
  1966. DEV_CRASH();
  1967. return;
  1968. }
  1969. with_view_model(
  1970. view,
  1971. DataLoaderModel * model,
  1972. {
  1973. if (model->timer)
  1974. {
  1975. furi_timer_free(model->timer);
  1976. model->timer = NULL;
  1977. }
  1978. if (model->parser_context)
  1979. {
  1980. // do not free the context here, it is the app context
  1981. // free(model->parser_context);
  1982. // model->parser_context = NULL;
  1983. }
  1984. if (model->fhttp)
  1985. {
  1986. flipper_http_free(model->fhttp);
  1987. model->fhttp = NULL;
  1988. }
  1989. },
  1990. false);
  1991. }
  1992. bool custom_event_callback(void *context, uint32_t index)
  1993. {
  1994. if (context == NULL)
  1995. {
  1996. FURI_LOG_E(TAG, "custom_event_callback - context is NULL");
  1997. DEV_CRASH();
  1998. return false;
  1999. }
  2000. switch (index)
  2001. {
  2002. case FlipWorldCustomEventProcess:
  2003. loader_process_callback(context);
  2004. return true;
  2005. default:
  2006. FURI_LOG_DEV(TAG, "custom_event_callback. Unknown index: %ld", index);
  2007. return false;
  2008. }
  2009. }
  2010. void generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  2011. {
  2012. if (app == NULL)
  2013. {
  2014. FURI_LOG_E(TAG, "generic_switch_to_view - app is NULL");
  2015. DEV_CRASH();
  2016. return;
  2017. }
  2018. View *view = app->view_loader;
  2019. if (view == NULL)
  2020. {
  2021. FURI_LOG_E(TAG, "generic_switch_to_view - view is NULL");
  2022. DEV_CRASH();
  2023. return;
  2024. }
  2025. with_view_model(
  2026. view,
  2027. DataLoaderModel * model,
  2028. {
  2029. model->title = title;
  2030. model->fetcher = fetcher;
  2031. model->parser = parser;
  2032. model->request_index = 0;
  2033. model->request_count = request_count;
  2034. model->back_callback = back;
  2035. model->data_state = DataStateInitial;
  2036. model->data_text = NULL;
  2037. //
  2038. model->parser_context = app;
  2039. if (!model->fhttp)
  2040. {
  2041. model->fhttp = flipper_http_alloc();
  2042. }
  2043. },
  2044. true);
  2045. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  2046. }