AppLobby.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. ])
  39. await AppKernel.ui.closeAll();
  40. AppKernel.ui.open(UIViewID.HomeView);
  41. AppKernel.events.emit('login:success');
  42. this.onAppLaunch();
  43. }
  44. private async onGameBack() {
  45. await AppKernel.ui.open(UIViewID.HomeView);
  46. AppKernel.ui.closeAll();
  47. const gameStore = AppKernel.stores.gameStore;
  48. const gameId = String(gameStore.gameID);
  49. gameStore.clearGame();
  50. AppKernel.events.emit('game:exited', { gameId, reason: 'returned-to-lobby' });
  51. }
  52. private async onAppLaunch() {
  53. }
  54. async openGame(gameID: number) {
  55. const hallClient = AppKernel.services.hallClient;
  56. const gameStore = AppKernel.stores.gameStore;
  57. gameStore.gameID = gameID
  58. hallClient.sendState();
  59. // const gameInfo = await mainStore.getGameCardInfo(gameID);
  60. // if (!gameInfo) {
  61. // console.error('进入游戏失败');
  62. // return;
  63. // }
  64. // const link = this.parseGameUrl(gameInfo);
  65. // if (!link) {
  66. // console.error('进入游戏失败');
  67. // return;
  68. // }
  69. // mainStore.gameLink = link;
  70. // AppKernel.scenes.goToGame();
  71. }
  72. private parseGameUrl(gameInfo: IGameInfo) {
  73. // const sign = localStorageGet(LOCAL_SIGN);
  74. // let url = `https://testapi.usgamewin.com/game/igt/lunch?softwareid=2001609002`;// gameInfo.link;
  75. // if (gameInfo.level > 0) url += "&level=" + gameInfo.level;
  76. // if (!url) return;
  77. // if (url.indexOf("?") === -1) url = url + "?sign=" + sign;
  78. // else url = url + "&sign=" + sign;
  79. // return url;
  80. return "";
  81. }
  82. async exitGame() {
  83. const hallClient = AppKernel.services.hallClient;
  84. const gameStore = AppKernel.stores.gameStore;
  85. gameStore.clearGame();
  86. hallClient.sendState();
  87. }
  88. async onPaymentFinished() {
  89. AppKernel.events.emit('hall:pay-finished');
  90. const mainStore = AppKernel.stores.mainStore;
  91. const payStore = AppKernel.stores.payStore;
  92. payStore.reqWithdrawBaseInfo();
  93. mainStore.reqUserInfo();
  94. }
  95. async logOut() {
  96. AppKernel.ui.loading(true);
  97. const mainStore = AppKernel.stores.mainStore;
  98. if (!await mainStore.reqLogout()) {
  99. console.error('log out failed!!!');
  100. AppKernel.ui.loading(false);
  101. return;
  102. }
  103. localStorageRemove(LOCAL_SIGN);
  104. AppKernel.fb?.logOut();
  105. await AppKernel.stores.onUserLogout();
  106. await AppKernel.ui.open(UIViewID.LoginView);
  107. AppKernel.ui.loading(false);
  108. }
  109. }
  110. export const appLobby = new AppLobby();