AppLobby.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. let firstLaunch = true;
  8. class AppLobby {
  9. private _tempOnConnected;
  10. async launchLobby() {
  11. const mainStore = AppKernel.stores.mainStore;
  12. if (firstLaunch) {
  13. firstLaunch = false;
  14. AppKernel.sound.playBgm();
  15. // 自动登录
  16. await mainStore.reqGetRoutes()
  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. const gameStore = AppKernel.stores.gameStore;
  25. const gameID = gameStore.gameID;
  26. if (gameID > 0) {
  27. await this.onGameBack();
  28. } else {
  29. await this.onLoginSuccess();
  30. }
  31. }
  32. async onLoginSuccess() {
  33. const gameStore = AppKernel.stores.gameStore;
  34. const payStore = AppKernel.stores.payStore;
  35. const hallClient = AppKernel.services.hallClient;
  36. hallClient.connect();
  37. await Promise.all([
  38. gameStore.reqAllGames(),
  39. payStore.reqRechargeConfig(),
  40. payStore.reqFirstPackVisible(),
  41. ])
  42. await AppKernel.ui.closeAll();
  43. AppKernel.ui.open(UIViewID.HomeView);
  44. AppKernel.events.emit('login:success');
  45. this.onAppLaunch();
  46. }
  47. private async onGameBack() {
  48. await AppKernel.ui.open(UIViewID.HomeView);
  49. AppKernel.ui.closeAll();
  50. const gameStore = AppKernel.stores.gameStore;
  51. const gameId = String(gameStore.gameID);
  52. gameStore.clearGame();
  53. AppKernel.events.emit('game:exited', { gameId, reason: 'returned-to-lobby' });
  54. }
  55. private async onAppLaunch() {
  56. }
  57. async openGame(game: IGameInfo) {
  58. AppKernel.ui.loading(true);
  59. const mainStore = AppKernel.stores.mainStore;
  60. const gameStore = AppKernel.stores.gameStore;
  61. const url = await gameStore.reqLaunchGame(game.gameCode, mainStore.currencyKind);
  62. AppKernel.ui.loading(false);
  63. console.log('game url: ', url);
  64. if (!!url) {
  65. const currency = mainStore.currencyKind;
  66. gameStore.onEnterGame(game, currency);
  67. AppKernel.services.hallClient.sendState();
  68. }
  69. // const gameInfo = await mainStore.getGameCardInfo(gameID);
  70. // if (!gameInfo) {
  71. // console.error('进入游戏失败');
  72. // return;
  73. // }
  74. // const link = this.parseGameUrl(gameInfo);
  75. // if (!link) {
  76. // console.error('进入游戏失败');
  77. // return;
  78. // }
  79. // mainStore.gameLink = link;
  80. // AppKernel.scenes.goToGame();
  81. }
  82. private parseGameUrl(gameInfo: IGameInfo) {
  83. // const sign = localStorageGet(LOCAL_SIGN);
  84. // let url = `https://testapi.usgamewin.com/game/igt/lunch?softwareid=2001609002`;// gameInfo.link;
  85. // if (gameInfo.level > 0) url += "&level=" + gameInfo.level;
  86. // if (!url) return;
  87. // if (url.indexOf("?") === -1) url = url + "?sign=" + sign;
  88. // else url = url + "&sign=" + sign;
  89. // return url;
  90. return "";
  91. }
  92. async exitGame() {
  93. const hallClient = AppKernel.services.hallClient;
  94. const gameStore = AppKernel.stores.gameStore;
  95. gameStore.clearGame();
  96. hallClient.sendState();
  97. }
  98. async onPaymentFinished() {
  99. AppKernel.events.emit('hall:pay-finished');
  100. const mainStore = AppKernel.stores.mainStore;
  101. AppKernel.services.paymentServices.onPaySuccess();
  102. AppKernel.ui.toast("充值成功!!");
  103. const updater = mainStore.reqUserInfo();
  104. await Promise.all([
  105. updater,
  106. ])
  107. AppKernel.events.emit('user:update')
  108. }
  109. async logOut() {
  110. AppKernel.ui.loading(true);
  111. const mainStore = AppKernel.stores.mainStore;
  112. if (!await mainStore.reqLogout()) {
  113. console.error('log out failed!!!');
  114. AppKernel.ui.loading(false);
  115. return;
  116. }
  117. await this.openLogin();
  118. }
  119. async openLogin() {
  120. if (!AppKernel.ui.isLoading) AppKernel.ui.loading(true);
  121. localStorageRemove(LOCAL_SIGN);
  122. AppKernel.fb?.logOut();
  123. AppKernel.services.hallClient.disconnect();
  124. await AppKernel.stores.onUserLogout();
  125. await AppKernel.ui.open(UIViewID.LoginView);
  126. AppKernel.ui.loading(false);
  127. }
  128. onHallClientConnected() {
  129. }
  130. hallClientConnected() {
  131. console.log('[HallClient] connected!!!');
  132. const onConnected = this._tempOnConnected;
  133. onConnected && onConnected();
  134. AppKernel.stores.mainStore.setServerKick(false);
  135. AppKernel.events.emit('hall:connected');
  136. }
  137. hallClientTimeout() {
  138. console.log('[HallClient] timeout!!!');
  139. AppKernel.events.emit('hall:timeout');
  140. AppKernel.ui.dialog({
  141. message: AppKernel.i18n.t("home.disconnected"),
  142. lblConfirm: AppKernel.i18n.t("home.reconnect"),
  143. callBack: (res) => {
  144. if (res) {
  145. this.hallClientReconnect();
  146. }
  147. return true;
  148. }
  149. })
  150. }
  151. handleKickout() {
  152. console.log('[HallClient] kuckout!!!');
  153. delete this._tempOnConnected;
  154. const mainStore = AppKernel.stores.mainStore;
  155. mainStore.setServerKick(true);
  156. AppKernel.events.emit('hall:kicked');
  157. AppKernel.ui.dialog({
  158. message: AppKernel.i18n.t("home.offline"),
  159. lblConfirm: AppKernel.i18n.t("home.reconnect"),
  160. callBack: (res) => {
  161. if (res) {
  162. this.hallClientReconnect();
  163. }
  164. return true;
  165. }
  166. })
  167. }
  168. private hallClientReconnect() {
  169. this._tempOnConnected = () => {
  170. delete this._tempOnConnected;
  171. AppKernel.ui.loading(false);
  172. }
  173. AppKernel.ui.loading(true);
  174. const hallClient = AppKernel.services.hallClient;
  175. hallClient.connect();
  176. }
  177. }
  178. export const appLobby = new AppLobby();