AppLobby.ts 6.5 KB

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