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

306 lines
9.3 KiB
TypeScript

import { tgxSceneUtil, tgxUIAlert, tgxUIWaiting, } from "../../core_tgx/tgx";
import { director } from "cc";
import {SceneDef} from "db://assets/scripts/SceneDef";
import {UserMgr} from "db://assets/module_basic/scripts/UserMgr";
import {SubGameMgr} from "db://assets/module_basic/scripts/SubGameMgr";
import {NetGameServer} from "db://assets/module_basic/scripts/NetGameServer";
import {IRoomData} from "db://assets/module_basic/shared/types/RoomData";
import {GameServerAuthParams} from "db://assets/module_basic/shared/types/GameServerAuthParams";
import {ServiceType} from "db://assets/module_basic/shared/protocols/serviceProto_public";
import {MsgRoomDismissedPush} from "db://assets/module_basic/shared/protocols/public/MsgRoomDismissedPush";
import {MsgRoomDataSyncPush} from "db://assets/module_basic/shared/protocols/public/MsgRoomDataSyncPush";
import {MsgRoomDataChangedPush} from "db://assets/module_basic/shared/protocols/public/MsgRoomDataChangedPush";
import {MsgUserComesToRoomPush} from "db://assets/module_basic/shared/protocols/public/MsgUserComesToRoomPush";
import {MsgUserDataChangedPush} from "db://assets/module_basic/shared/protocols/public/MsgUserDataChangedPush";
import {MsgUserLeavesFromRoomPush} from "db://assets/module_basic/shared/protocols/public/MsgUserLeavesFromRoomPush";
// export class GameMode {
// /**
// * @en solo mode
// * @zh 对战模式
// */
// public static SOLO: string = 'solo';
// /**
// * @en ai mode
// * @zh 单机模式
// */
// public static AI: string = 'ai';
// /**
// * @en online mode
// * @zh 在线模式
// */
// public static ONLINE: string = 'online';
// /**
// * @en level mode
// * @zh 关卡模式
// *
// */
// public static LEVEL: string = 'level';
// }
export class RoomEvent {
/**
* @en PN is used for notifying data changed, then the client can get the new data from the master server.
* @zh 用于通知客户端哪些数据变动,方便从 Master 获取最新数据
*
*/
public static PN: string = 'RoomEvent.PN';
public static NEW_USER_COMES: string = 'RoomEvent.NEW_USER_IN_TABLE';
public static WATCHER_LIST_CHANGED: string = 'RoomEvent.WATCHER_LIST_CHANGED';
public static USER_LEAVES: string = 'RoomEvent.USER_LEAVES';
public static USER_DATA_CHANGED: string = 'RoomEvent.USER_DATA_CHANGED';
/**
*转让房主
*/
public static User_Make_Room: string = 'RoomEvent.User_Make_Room';
/**
* 金币改变
*
*/
public static ChangeCoin = 'RoomEvent.ChangeCoin';
// 段位变化
public static DanUpdate = 'AreanEvent.DanUpdate';
}
export class GameMgr {
/**
* @en record the message name and callback function, so that we can remove it when exit sub game
* @zh 记录监听的消息名和回调函数,方便退出子游戏的时候移除监听
*/
protected _msgHandlers: { msgName: any, func: Function, thisArg: any }[] = [];
protected _data: IRoomData
public get data(): IRoomData {
return this._data;
}
protected init() {
}
public reset() {
this._data = null;
}
async backToLobby() {
let ret = await this.rpc_LeaveRoom();
if (ret.isSucc) {
tgxUIWaiting.show('正在返回大厅');
tgxSceneUtil.loadScene(SubGameMgr.current.entry);
}
else {
tgxUIAlert.show(ret.err.message);
}
}
backToLogin() {
tgxUIAlert.show('网络链接断开,请重新登录').onClick(() => {
this.reset();
tgxSceneUtil.loadScene(SceneDef.LOGIN);
});
}
public get worldId(): string {
if (!this._data) {
return '';
}
return this._data.id;
}
public get isPlayer(): boolean {
if (!this._data) {
return false;
}
let user = this.getUser(UserMgr.inst.userId);
return user ? !!user.playerId : false;
}
public get isPlaying(): boolean {
if (!this._data) {
return false;
}
return this._data.isPlaying;
}
public get isWatcher(): boolean {
if (!this._data) {
return false;
}
let user = this.getUser(UserMgr.inst.userId);
return user ? !user.playerId : false;
}
public get selfUser() {
return this.getUser(UserMgr.inst.userId);
}
public getUser(uid: string) {
for (let i = 0; i < this._data.userList.length; ++i) {
let u = this._data.userList[i];
if (u.uid == uid) {
return u;
}
}
}
async enterRoom(params: GameServerAuthParams,uid: string) {
SubGameMgr.gameMgr = this;
this.reset();
await NetGameServer.inst.connectToRoomServer(params);
let ret = await NetGameServer.inst.ensureConnected();
if (!ret.isSucc) {
return ret;
}
let ret2 = await NetGameServer.inst.joinRoomServer(uid);
if (ret2.isSucc) {
await this.beforeLoadScene();
}
return ret2;
}
protected async beforeLoadScene() {
}
protected listenMsg<T extends keyof ServiceType['msg']>(msgName: T | RegExp, func: Function, thisArg?: any) {
this._msgHandlers.push({ msgName, func, thisArg });
NetGameServer.inst.listenMsg(msgName, func, thisArg);
}
protected unlistenAll() {
this._msgHandlers.forEach(v => {
console.log("取消事件监听 === ",v.msgName,v.func,v.thisArg);
NetGameServer.inst.unlistenMsg(v.msgName, v.func, v.thisArg);
});
this._msgHandlers = [];
}
initNetMsgHandlers() {
this.listenMsg('RoomDismissedPush', this.onNet_RoomDismissedPush, this);
this.listenMsg('RoomDataSyncPush', this.onNet_RoomDataSyncPush, this);
this.listenMsg('RoomDataChangedPush', this.onNet_RoomDataChangedPush, this);
this.listenMsg('UserComesToRoomPush', this.onNet_UserComesToRoomPush, this);
this.listenMsg('UserDataChangedPush', this.onNet_UserDataChangedPush, this);
this.listenMsg('UserLeavesFromRoomPush', this.onNet_UserLeavesFromRoomPush, this);
}
async rpc_LeaveRoom() {
let ret = await NetGameServer.inst.conn.callApi("ExitRoom", {});
if (ret.isSucc) {
// this.unlistenAll();
UserMgr.inst.roomId= null;
}
return ret;
}
async rpc_JoinGame() {
let ret = await NetGameServer.inst.conn.callApi("JoinGame", {});
return ret;
}
async rpc_Ready() {
let ret = await NetGameServer.inst.conn.callApi("Ready", {});
return ret;
}
//==========================
onNet_RoomDismissedPush(msg: MsgRoomDismissedPush) {
}
onNet_RoomDataSyncPush(msg: MsgRoomDataSyncPush) {
this._data = msg.data;
}
onNet_RoomDataChangedPush(msg: MsgRoomDataChangedPush) {
if (!this._data) {
return;
}
if (msg.isPlaying !== undefined) {
this._data.isPlaying = msg.isPlaying;
}
if (msg.numPlayer !== undefined) {
this._data.playerNum = msg.numPlayer;
}
/*
if (msg.watcherList !== undefined) {
this._gameData.watcherList = msg.watcherList;
director.getScene().emit(RoomEvent.WATCHER_LIST_CHANGED);
}
*/
}
onNet_UserComesToRoomPush(msg: MsgUserComesToRoomPush) {
if (!this._data) {
return;
}
let user = this.getUser(msg.uid);
if (!user) {
this._data.userList.push(msg);
}
director.getScene().emit(RoomEvent.NEW_USER_COMES, msg);
}
onNet_UserDataChangedPush(msg: MsgUserDataChangedPush) {
if (!this._data) {
return;
}
let userId = msg.uid;
let u = this.getUser(userId);
if (!u) {
return;
}
if (msg.ready !== undefined) {
u.ready = msg.ready;
}
if (msg.playerId !== undefined) {
u.playerId = msg.playerId;
}
if (msg.isOnline !== undefined) {
u.isOnline = msg.isOnline;
}
director.getScene().emit(RoomEvent.USER_DATA_CHANGED, msg);
}
onNet_UserLeavesFromRoomPush(msg: MsgUserLeavesFromRoomPush) {
if (!this._data) {
return;
}
let ismakeRoom:boolean = false;
let isPlayer = false;
for (let i = 0; i < this._data.userList.length; ++i) {
let p = this._data.userList[i];
if (p.uid == msg.uid) {
this._data.userList.splice(i, 1);
isPlayer = !!p.playerId;
if(p.seatIndex ==0){//转让房主
ismakeRoom = true;
}
break;
}
}
this._data.userList = msg.userList;
if(ismakeRoom){
director.getScene().emit(RoomEvent.User_Make_Room);
}
director.getScene().emit(RoomEvent.USER_LEAVES, { uid: msg.uid, isPlayer: isPlayer });
}
}