AppLobby.ts 7.0 KB

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