AppLobby.ts 6.7 KB

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