callback.c 72 KB

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