import { AppKernel } from "@scripts/app/AppKernel"; import { UIViewID } from "@scripts/define/UIDefine"; import { LOCAL_SIGN } from "@scripts/constants/Localkey"; import { localStorageGet, localStorageRemove } from "@scripts/utils/Helper"; import { CURRENCY_TYPE } from "@scripts/define/Define"; import { IGameInfo } from "@scripts/store/gameStore/data"; let firstLaunch = true; class AppLobby { private _tempOnConnected; async launchLobby() { const mainStore = AppKernel.stores.mainStore; if (firstLaunch) { firstLaunch = false; AppKernel.sound.playBgm(); // 自动登录 await mainStore.reqGetRoutes() } const isLogin = mainStore.isLogin; if (!isLogin) { AppKernel.ui.close(UIViewID.HomeView); await AppKernel.ui.open(UIViewID.LoginView); return; } const gameStore = AppKernel.stores.gameStore; const gameID = gameStore.gameID; if (gameID > 0) { await this.onGameBack(); } else { await this.onLoginSuccess(); } } async onLoginSuccess() { const gameStore = AppKernel.stores.gameStore; const payStore = AppKernel.stores.payStore; const hallClient = AppKernel.services.hallClient; hallClient.connect(); await Promise.all([ gameStore.reqAllGames(), payStore.reqRechargeConfig(), payStore.reqFirstPackVisible(), ]) await AppKernel.ui.closeAll(); AppKernel.ui.open(UIViewID.HomeView); AppKernel.events.emit('login:success'); this.onAppLaunch(); } private async onGameBack() { await AppKernel.ui.open(UIViewID.HomeView); AppKernel.ui.closeAll(); const gameStore = AppKernel.stores.gameStore; const gameId = String(gameStore.gameID); gameStore.clearGame(); AppKernel.events.emit('game:exited', { gameId, reason: 'returned-to-lobby' }); } private async onAppLaunch() { } async openGame(game: IGameInfo) { AppKernel.ui.loading(true); const mainStore = AppKernel.stores.mainStore; const gameStore = AppKernel.stores.gameStore; const url = await gameStore.reqLaunchGame(game.gameCode, mainStore.currencyKind); AppKernel.ui.loading(false); console.log('game url: ', url); if (!!url) { const currency = mainStore.currencyKind; gameStore.onEnterGame(game, currency); AppKernel.services.hallClient.sendState(); } // const gameInfo = await mainStore.getGameCardInfo(gameID); // if (!gameInfo) { // console.error('进入游戏失败'); // return; // } // const link = this.parseGameUrl(gameInfo); // if (!link) { // console.error('进入游戏失败'); // return; // } // mainStore.gameLink = link; // AppKernel.scenes.goToGame(); } private parseGameUrl(gameInfo: IGameInfo) { // const sign = localStorageGet(LOCAL_SIGN); // let url = `https://testapi.usgamewin.com/game/igt/lunch?softwareid=2001609002`;// gameInfo.link; // if (gameInfo.level > 0) url += "&level=" + gameInfo.level; // if (!url) return; // if (url.indexOf("?") === -1) url = url + "?sign=" + sign; // else url = url + "&sign=" + sign; // return url; return ""; } async exitGame() { const hallClient = AppKernel.services.hallClient; const gameStore = AppKernel.stores.gameStore; gameStore.clearGame(); hallClient.sendState(); } async onPaymentFinished() { AppKernel.events.emit('hall:pay-finished'); const mainStore = AppKernel.stores.mainStore; AppKernel.services.paymentServices.onPaySuccess(); AppKernel.ui.toast("充值成功!!"); const updater = mainStore.reqUserInfo(); await Promise.all([ updater, ]) AppKernel.events.emit('user:update') } async logOut() { AppKernel.ui.loading(true); const mainStore = AppKernel.stores.mainStore; if (!await mainStore.reqLogout()) { console.error('log out failed!!!'); AppKernel.ui.loading(false); return; } await this.openLogin(); } async openLogin() { if (!AppKernel.ui.isLoading) AppKernel.ui.loading(true); localStorageRemove(LOCAL_SIGN); AppKernel.fb?.logOut(); AppKernel.services.hallClient.disconnect(); await AppKernel.stores.onUserLogout(); await AppKernel.ui.open(UIViewID.LoginView); AppKernel.ui.loading(false); } onHallClientConnected() { } hallClientConnected() { console.log('[HallClient] connected!!!'); const onConnected = this._tempOnConnected; onConnected && onConnected(); AppKernel.stores.mainStore.setServerKick(false); AppKernel.events.emit('hall:connected'); } hallClientTimeout() { console.log('[HallClient] timeout!!!'); AppKernel.events.emit('hall:timeout'); AppKernel.ui.dialog({ message: AppKernel.i18n.t("home.disconnected"), lblConfirm: AppKernel.i18n.t("home.reconnect"), callBack: (res) => { if (res) { this.hallClientReconnect(); } return true; } }) } handleKickout() { console.log('[HallClient] kuckout!!!'); delete this._tempOnConnected; const mainStore = AppKernel.stores.mainStore; mainStore.setServerKick(true); AppKernel.events.emit('hall:kicked'); AppKernel.ui.dialog({ message: AppKernel.i18n.t("home.offline"), lblConfirm: AppKernel.i18n.t("home.reconnect"), callBack: (res) => { if (res) { this.hallClientReconnect(); } return true; } }) } private hallClientReconnect() { this._tempOnConnected = () => { delete this._tempOnConnected; AppKernel.ui.loading(false); } AppKernel.ui.loading(true); const hallClient = AppKernel.services.hallClient; hallClient.connect(); } } export const appLobby = new AppLobby();