AppLobby.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { AppKernel } from "@scripts/app/AppKernel";
  2. import { UIViewID } from "@scripts/define/UIDefine";
  3. import { LOCAL_SIGN } from "@scripts/constants/Localkey";
  4. import { localStorageGet, localStorageRemove } from "@scripts/utils/Helper";
  5. import { CURRENCY_TYPE } from "@scripts/define/Define";
  6. import { IGameInfo } from "@scripts/store/gameStore/data";
  7. class AppLobby {
  8. async launchLobby() {
  9. const mainStore = AppKernel.stores.mainStore;
  10. const firstLaunch = mainStore.firstLaunch;
  11. if (firstLaunch) {
  12. AppKernel.sound.playBgm();
  13. // 自动登录
  14. await mainStore.reqGetRoutes()
  15. }
  16. const isLogin = mainStore.isLogin;
  17. if (!isLogin) {
  18. AppKernel.ui.close(UIViewID.HomeView);
  19. await AppKernel.ui.open(UIViewID.LoginView);
  20. return;
  21. }
  22. const gameStore = AppKernel.stores.gameStore;
  23. const gameID = gameStore.gameID;
  24. if (gameID > 0) {
  25. await this.onGameBack();
  26. } else {
  27. await this.onLoginSuccess();
  28. }
  29. }
  30. async onLoginSuccess() {
  31. const gameStore = AppKernel.stores.gameStore;
  32. const payStore = AppKernel.stores.payStore;
  33. const hallClient = AppKernel.services.hallClient;
  34. hallClient.connect();
  35. await Promise.all([
  36. gameStore.reqAllGames(),
  37. payStore.reqRechargeConfig(),
  38. payStore.reqFirstPackVisible(),
  39. ])
  40. await AppKernel.ui.closeAll();
  41. AppKernel.ui.open(UIViewID.HomeView);
  42. AppKernel.events.emit('login:success');
  43. this.onAppLaunch();
  44. }
  45. private async onGameBack() {
  46. await AppKernel.ui.open(UIViewID.HomeView);
  47. AppKernel.ui.closeAll();
  48. const gameStore = AppKernel.stores.gameStore;
  49. const gameId = String(gameStore.gameID);
  50. gameStore.clearGame();
  51. AppKernel.events.emit('game:exited', { gameId, reason: 'returned-to-lobby' });
  52. }
  53. private async onAppLaunch() {
  54. }
  55. async openGame(game: IGameInfo) {
  56. AppKernel.ui.loading(true);
  57. const hallClient = AppKernel.services.hallClient;
  58. const mainStore = AppKernel.stores.mainStore;
  59. const gameStore = AppKernel.stores.gameStore;
  60. gameStore.gameID = game.gameID;
  61. hallClient.sendState();
  62. const url = await gameStore.reqLaunchGame(game.gameCode, mainStore.currencyKind);
  63. AppKernel.ui.loading(false);
  64. console.log('game url: ', url);
  65. // const gameInfo = await mainStore.getGameCardInfo(gameID);
  66. // if (!gameInfo) {
  67. // console.error('进入游戏失败');
  68. // return;
  69. // }
  70. // const link = this.parseGameUrl(gameInfo);
  71. // if (!link) {
  72. // console.error('进入游戏失败');
  73. // return;
  74. // }
  75. // mainStore.gameLink = link;
  76. // AppKernel.scenes.goToGame();
  77. }
  78. private parseGameUrl(gameInfo: IGameInfo) {
  79. // const sign = localStorageGet(LOCAL_SIGN);
  80. // let url = `https://testapi.usgamewin.com/game/igt/lunch?softwareid=2001609002`;// gameInfo.link;
  81. // if (gameInfo.level > 0) url += "&level=" + gameInfo.level;
  82. // if (!url) return;
  83. // if (url.indexOf("?") === -1) url = url + "?sign=" + sign;
  84. // else url = url + "&sign=" + sign;
  85. // return url;
  86. return "";
  87. }
  88. async exitGame() {
  89. const hallClient = AppKernel.services.hallClient;
  90. const gameStore = AppKernel.stores.gameStore;
  91. gameStore.clearGame();
  92. hallClient.sendState();
  93. }
  94. async onPaymentFinished() {
  95. AppKernel.events.emit('hall:pay-finished');
  96. const mainStore = AppKernel.stores.mainStore;
  97. const payStore = AppKernel.stores.payStore;
  98. payStore.reqWithdrawBaseInfo();
  99. mainStore.reqUserInfo();
  100. }
  101. async logOut() {
  102. AppKernel.ui.loading(true);
  103. const mainStore = AppKernel.stores.mainStore;
  104. if (!await mainStore.reqLogout()) {
  105. console.error('log out failed!!!');
  106. AppKernel.ui.loading(false);
  107. return;
  108. }
  109. localStorageRemove(LOCAL_SIGN);
  110. AppKernel.fb?.logOut();
  111. await AppKernel.stores.onUserLogout();
  112. await AppKernel.ui.open(UIViewID.LoginView);
  113. AppKernel.ui.loading(false);
  114. }
  115. }
  116. export const appLobby = new AppLobby();