2025-03-06 22:46:48 +08:00
|
|
|
import { _decorator, Component, Director, director, sys } from 'cc';
|
|
|
|
import { tgxUIMgr, tgxSceneUtil, tgxAudioMgr, tgxUIWaiting, tgxUIAlert } from '../../core_tgx/tgx';
|
2025-02-17 21:36:37 +08:00
|
|
|
import { SceneDef } from '../../scripts/SceneDef';
|
|
|
|
import { UserMgr } from './UserMgr';
|
|
|
|
import { SubGameMgr } from './SubGameMgr';
|
|
|
|
import { EMusicDefine } from './MusicDefine';
|
2025-03-06 22:46:48 +08:00
|
|
|
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";
|
2025-02-17 21:36:37 +08:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
export const LAST_NAMES = ['勇敢的', '怂包的', '无辜的', '残暴的', '天真的', '胆小的', '幽默的', '好色的'];
|
|
|
|
export const GIVEN_NAMES = ['角斗士', '仲裁者', '看守者', '黑色教长', '格里芬', '好战者', '和平使者', '征服者', '百夫长'];
|
|
|
|
@ccclass('LobbyScene')
|
|
|
|
export class LobbyScene extends Component {
|
2025-03-06 22:46:48 +08:00
|
|
|
private _gameType: string = "";
|
2025-02-17 21:36:37 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2025-03-06 22:46:48 +08:00
|
|
|
private _enterLobbyScene(): void {
|
|
|
|
if (this._gameType == 'billiards' || this._gameType == 'gomoku' || this._gameType == 'tank' || this._gameType == 'kingtd' || this._gameType == 'arean') {
|
2025-02-17 21:36:37 +08:00
|
|
|
SubGameMgr.inst.enterSubLobby(this._gameType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-06 22:46:48 +08:00
|
|
|
async doRegister(account: string, password: string) {
|
|
|
|
let ret = await NetGameServer.inst.callApi('login/Register', { account: account, password: password });
|
2025-02-17 21:36:37 +08:00
|
|
|
if (!ret.isSucc) {
|
2025-03-06 22:46:48 +08:00
|
|
|
if (ret.err.message == 'USER_HAS_BEEN_EXIST') {
|
2025-02-17 21:36:37 +08:00
|
|
|
tgxUIAlert.show('用户名已被使用!');
|
|
|
|
}
|
2025-03-06 22:46:48 +08:00
|
|
|
let account = this.generateAccount();
|
2025-02-18 09:45:06 +08:00
|
|
|
let pass = password || "123456";
|
2025-03-06 22:46:48 +08:00
|
|
|
await this.doRegister(account, pass);
|
2025-02-17 21:36:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2025-03-06 22:46:48 +08:00
|
|
|
await this.doLogin(account, password);
|
2025-02-17 21:36:37 +08:00
|
|
|
|
|
|
|
UserLocalCache.inst.storeAccount(account);
|
|
|
|
UserLocalCache.inst.storePassword(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
async doLogin(account: string, password: string) {
|
|
|
|
// tgxUIWaiting.show('正在登录');
|
|
|
|
let ret = await UserMgr.inst.doLogin(account, password);
|
2025-03-06 22:46:48 +08:00
|
|
|
if (!ret.isSucc) {
|
2025-02-17 21:36:37 +08:00
|
|
|
tgxUIWaiting.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _checkAccount(): Promise<void> {
|
2025-03-07 13:33:59 +08:00
|
|
|
localStorage.clear();
|
2025-02-17 21:36:37 +08:00
|
|
|
let account = UserLocalCache.inst.account;
|
|
|
|
let password = UserLocalCache.inst.password;
|
|
|
|
if (account && password) {
|
2025-03-06 22:46:48 +08:00
|
|
|
await this.doLogin(account, password)
|
2025-02-17 21:36:37 +08:00
|
|
|
} else {
|
2025-03-06 22:46:48 +08:00
|
|
|
account = this.generateAccount();
|
2025-02-18 09:45:06 +08:00
|
|
|
password = "123456";
|
2025-02-17 21:36:37 +08:00
|
|
|
console.log("随机生成的账号 === ", account);
|
2025-03-06 22:46:48 +08:00
|
|
|
await this.doRegister(account, password);
|
2025-02-17 21:36:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update(deltaTime: number) {
|
|
|
|
|
|
|
|
}
|
2025-03-06 22:46:48 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2025-02-07 10:49:34 +08:00
|
|
|
}
|