import { AppKernel } from "@scripts/app/AppKernel"; import { UIViewID } from "@scripts/define/UIDefine"; import { LOCAL_SIGN } from "@scripts/constants/Localkey"; import { IGameInfo } from "@scripts/store/pageSlice/data"; import { localStorageGet } from "@scripts/utils/Helper"; class LobbyService { async launchLobby() { const mainStore = AppKernel.stores.mainStore; const pageStore = AppKernel.stores.pageStore; const firstLaunch = mainStore.firstLaunch; if (firstLaunch) { // 刚启动未登录 await Promise.all([ mainStore.reqGetRoutes(), pageStore.reqPageModules(1), ]) } const isLogin = mainStore.isLogin; if (!isLogin) { AppKernel.ui.close(UIViewID.HomeView); await AppKernel.ui.open(UIViewID.LoginView); return; } AppKernel.ui.close(UIViewID.LoginView); await AppKernel.ui.open(UIViewID.HomeView); const gameID = mainStore.gameID; if (gameID > 0) { this.onGameBack(); } else { this.onAppLaunch(); } } async onLoginSuccess() { const hallService = AppKernel.services.hallService; hallService.connect() AppKernel.ui.close(UIViewID.LoginView); await AppKernel.ui.open(UIViewID.HomeView); AppKernel.events.emit('login:success'); } private async onGameBack() { const mainStore = AppKernel.stores.mainStore; const gameId = String(mainStore.gameID); mainStore.clearGame(); AppKernel.events.emit('game:exited', { gameId, reason: 'returned-to-lobby' }); } private async onAppLaunch() { } async openGame(gameID: number) { const hallService = AppKernel.services.hallService; const mainStore = AppKernel.stores.mainStore; mainStore.gameID = gameID hallService.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; } async exitGame() { const hallService = AppKernel.services.hallService; const mainStore = AppKernel.stores.mainStore; mainStore.clearGame(); hallService.sendState(); } } export const lobbyService = new LobbyService();