squidGame/tgx-games-client/assets/module_basic/scripts/LobbyScene.ts

141 lines
5.4 KiB
TypeScript

import { _decorator, Component, Director, director, sys } from 'cc';
import { tgxUIMgr, tgxSceneUtil, tgxAudioMgr, tgxUIWaiting, tgxUIAlert } from '../../core_tgx/tgx';
import { SceneDef } from '../../scripts/SceneDef';
import { UserMgr } from './UserMgr';
import { SubGameMgr } from './SubGameMgr';
import { EMusicDefine } from './MusicDefine';
import { UserLocalCache } from "db://assets/module_basic/scripts/UserLocalCache";
import { NetGameServer } from "db://assets/module_basic/scripts/NetGameServer";
import { NetworkReconnector } from "db://assets/module_basic/scripts/NetworkReconnector";
import { ModuleDef } from "db://assets/scripts/ModuleDef";
import { SceneUtil } from "db://assets/core_tgx/base/SceneUtils";
import { urlParse } from "db://assets/core_tgx/base/URLUtils";
import { NetUtil } from "db://assets/module_basic/scripts/NetUtil";
const { ccclass, property } = _decorator;
export const LAST_NAMES = ['勇敢的', '怂包的', '无辜的', '残暴的', '天真的', '胆小的', '幽默的', '好色的'];
export const GIVEN_NAMES = ['角斗士', '仲裁者', '看守者', '黑色教长', '格里芬', '好战者', '和平使者', '征服者', '百夫长'];
@ccclass('LobbyScene')
export class LobbyScene extends Component {
private _gameType: string = "";
async start() {
await this._initNetWork();
// if (!UserMgr.inst.userId) {
// tgxSceneUtil.loadScene(SceneDef.START);
// return;
// }
SubGameMgr.inst.exitSubGame();
tgxUIMgr.inst.closeAll();
// tgxUIMgr.inst.showUI(UIAnnouncement)
// tgxUIMgr.inst.showUI(UIChat);
}
private async _initNetWork() {
director.on(Director.EVENT_AFTER_SCENE_LAUNCH, () => {
NetworkReconnector.addToScene();
});
NetworkReconnector.addToScene();
tgxAudioMgr.inst.play(EMusicDefine.MUSIC_BGM_HALL, 1, ModuleDef.BASIC);
tgxUIWaiting.show('正在连接服务器');
NetGameServer.inst.startPing();
let serverList = await NetGameServer.inst.getGameServerList();
NetGameServer.inst.createConnection(serverList);
let ret = await NetGameServer.inst.ensureConnected();
if (!ret.isSucc) {
tgxUIAlert.show('连接服务器失败,请检查网络').onClick(() => {
SceneUtil.reloadScene();
});
return;
}
if (sys.isBrowser) {
let params = urlParse();
let account = params['a'];
let password = params['p'];
if (account && password) {
let ret = await UserMgr.inst.doLogin(account, password);
if (ret.isSucc) {
return;
}
}
}
NetUtil.addErrorFilter('INVALID_TOKEN', () => {
tgxUIAlert.show('TOKEN 过期,请重新登录').onClick(() => {
tgxSceneUtil.loadScene(SceneDef.LOBBY);
});
});
}
async onBtnSubGameClicked(event, gameType: string) {
tgxAudioMgr.inst.playCommonBtn(EMusicDefine.EFFECT_CLICK);
this._gameType = gameType;
await this._checkAccount();
}
private _enterLobbyScene(): void {
if (this._gameType == 'billiards' || this._gameType == 'gomoku' || this._gameType == 'tank' || this._gameType == 'kingtd' || this._gameType == 'arean') {
SubGameMgr.inst.enterSubLobby(this._gameType);
}
}
async doRegister(account: string, password: string) {
let ret = await NetGameServer.inst.callApi('login/Register', { account: account, password: password });
if (!ret.isSucc) {
if (ret.err.message == 'USER_HAS_BEEN_EXIST') {
tgxUIAlert.show('用户名已被使用!');
}
let account = this.generateAccount();
let pass = password || "123456";
await this.doRegister(account, pass);
return;
}
await this.doLogin(account, password);
UserLocalCache.inst.storeAccount(account);
UserLocalCache.inst.storePassword(password);
}
async doLogin(account: string, password: string) {
// tgxUIWaiting.show('正在登录');
let ret = await UserMgr.inst.doLogin(account, password);
if (!ret.isSucc) {
tgxUIWaiting.hide();
}
}
private async _checkAccount(): Promise<void> {
localStorage.clear();
let account = UserLocalCache.inst.account;
let password = UserLocalCache.inst.password;
if (account && password) {
await this.doLogin(account, password)
} else {
account = this.generateAccount();
password = "123456";
console.log("随机生成的账号 === ", account);
await this.doRegister(account, password);
}
}
update(deltaTime: number) {
}
public generateAccount(): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let account = '';
for (let i = 0; i < 16; i++) {
// 确保第一位是字母
if (i === 0) {
account += chars.charAt(Math.floor(Math.random() * chars.length));
} else {
account += chars.charAt(Math.floor(Math.random() * chars.length));
}
}
return account;
}
}