| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- 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 AppKernel.ui.open(UIViewID.LoginView);
- const loginView = AppKernel.ui.get(UIViewID.LoginView) as any;
- loginView?._googleWebBtn?.setActive(false);
- await AppKernel.ui.open(UIViewID.AutoLogin);
- // 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);
- await Promise.all([
- AppKernel.stores.mainStore.reqUserInfo(),
- ])
- 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;
- let url = await gameStore.reqLaunchGame(game.gameCode, mainStore.currencyKind);
- AppKernel.ui.loading(false);
- console.log('game url: ', url);
- if (!!url) {
- AppKernel.scenes.goToGame();
- const currency = mainStore.currencyKind;
- gameStore.onEnterGame(game, currency, url);
- 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();
|