AppLobby.ts 7.0 KB

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