LobbyService.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { AppKernel } from "@scripts/app/AppKernel";
  2. import { UIViewID } from "@scripts/define/UIDefine";
  3. import { LOCAL_SIGN } from "@scripts/constants/Localkey";
  4. import { IGameInfo } from "@scripts/store/pageSlice/data";
  5. import { localStorageGet } from "@scripts/utils/Helper";
  6. class LobbyService {
  7. async launchLobby() {
  8. const mainStore = AppKernel.stores.mainStore;
  9. const pageStore = AppKernel.stores.pageStore;
  10. const firstLaunch = mainStore.firstLaunch;
  11. if (firstLaunch) {
  12. // 刚启动未登录
  13. await Promise.all([
  14. mainStore.reqGetRoutes(),
  15. pageStore.reqPageModules(1),
  16. ])
  17. }
  18. const isLogin = mainStore.isLogin;
  19. if (!isLogin) {
  20. AppKernel.ui.close(UIViewID.HomeView);
  21. await AppKernel.ui.open(UIViewID.LoginView);
  22. return;
  23. }
  24. AppKernel.ui.close(UIViewID.LoginView);
  25. await AppKernel.ui.open(UIViewID.HomeView);
  26. const gameID = mainStore.gameID;
  27. if (gameID > 0) {
  28. this.onGameBack();
  29. } else {
  30. this.onAppLaunch();
  31. }
  32. }
  33. async onLoginSuccess() {
  34. const hallService = AppKernel.services.hallService;
  35. hallService.connect()
  36. AppKernel.ui.close(UIViewID.LoginView);
  37. await AppKernel.ui.open(UIViewID.HomeView);
  38. AppKernel.events.emit('login:success');
  39. }
  40. private async onGameBack() {
  41. const mainStore = AppKernel.stores.mainStore;
  42. const gameId = String(mainStore.gameID);
  43. mainStore.clearGame();
  44. AppKernel.events.emit('game:exited', { gameId, reason: 'returned-to-lobby' });
  45. }
  46. private async onAppLaunch() {
  47. }
  48. async openGame(gameID: number) {
  49. const hallService = AppKernel.services.hallService;
  50. const mainStore = AppKernel.stores.mainStore;
  51. mainStore.gameID = gameID
  52. hallService.sendState();
  53. const gameInfo = await mainStore.getGameCardInfo(gameID);
  54. if (!gameInfo) {
  55. console.error('进入游戏失败');
  56. return;
  57. }
  58. const link = this.parseGameUrl(gameInfo);
  59. if (!link) {
  60. console.error('进入游戏失败');
  61. return;
  62. }
  63. mainStore.gameLink = link;
  64. AppKernel.scenes.goToGame();
  65. }
  66. private parseGameUrl(gameInfo: IGameInfo) {
  67. const sign = localStorageGet(LOCAL_SIGN);
  68. let url = `https://testapi.usgamewin.com/game/igt/lunch?softwareid=2001609002`;// gameInfo.link;
  69. if (gameInfo.level > 0) url += "&level=" + gameInfo.level;
  70. if (!url) return;
  71. if (url.indexOf("?") === -1) url = url + "?sign=" + sign;
  72. else url = url + "&sign=" + sign;
  73. return url;
  74. }
  75. async exitGame() {
  76. const hallService = AppKernel.services.hallService;
  77. const mainStore = AppKernel.stores.mainStore;
  78. mainStore.clearGame();
  79. hallService.sendState();
  80. }
  81. }
  82. export const lobbyService = new LobbyService();