import { Sprite, Label, resources, SpriteFrame, assetManager, Prefab, instantiate, v3 } from "cc"; import { tgxUIWaiting, tgxUIAlert, tgxSceneUtil, tgxAudioMgr } from "../../core_tgx/tgx"; import {UserLocalCache} from "db://assets/module_basic/scripts/UserLocalCache"; import {NetGameServer} from "db://assets/module_basic/scripts/NetGameServer"; import {SceneDef} from "db://assets/scripts/SceneDef"; import {GameTypes} from "db://assets/module_basic/scripts/SubGameDef"; import {SubGameMgr} from "db://assets/module_basic/scripts/SubGameMgr"; import { ModuleContext } from "../../core_tgx/base/ModuleContext"; import {UserInfo} from "db://assets/module_basic/shared/types/UserInfo"; import {GameServerAuthParams} from "db://assets/module_basic/shared/types/GameServerAuthParams"; import {EAreanBuyType} from "../../../../tgx-games-server/src/shared/protocols/public/arean/AreanTypeDef"; export class UserMgr { private static _inst: UserMgr; public static get inst(): UserMgr { if (!this._inst) { this._inst = new UserMgr(); } return this._inst; } constructor() { tgxAudioMgr.inst.musicVolume = UserLocalCache.inst.musicVolume; tgxAudioMgr.inst.soundVolume = UserLocalCache.inst.soundVolume; } private _userInfo: UserInfo = { uid: '', name: '', visualId: 0, gender: 0, introduction: '', } private _roomId: string; private _userInfoMap: { [key: string]: UserInfo } = {}; public get userId(): string { return this._userInfo.uid; } public get uid(): string { return this._userInfo.uid; } public get name(): string { return this._userInfo.name; } public get visualId(): number { return this._userInfo.visualId; } public get roomId(): string { return this._roomId; } public get coin():number{ return this._userInfo.coin || 0; } private _reTrayGameParmas: GameServerAuthParams = null; public get reTrayGameParmas(): GameServerAuthParams { return this._reTrayGameParmas; } public set reTrayGameParmas(v: GameServerAuthParams) { this._reTrayGameParmas = v; } public set roomId(v: string) { this._roomId = v; } public isInRoom(): boolean { return !!this._roomId; } private _isInGame: boolean = false; public set isInGame(value: boolean) { this._isInGame = value; } public get isInGame(): boolean { return this._isInGame; } // 获取玩家信息 public get userInfo(): UserInfo { return this._userInfo; } // 更新英雄熟练度 public updateHeroProficiency(heroId: number, proficiency: number) : void{ let heroData = this._userInfo.hero.find((hero)=> hero.heroId == heroId); if(heroData){ heroData.proficiency = proficiency; } } async doLogin(account: string, password: string) { let ret = await NetGameServer.inst.callApi('login/Login', { account: account, password: password }); if (ret.isSucc) { console.log('login succ',ret); this._userInfo = ret.res.userInfo; // TODO 测试接口 // this._userInfo.danGrading = 18; this._userInfoMap[this._userInfo.uid] = this._userInfo; let ret3 = await this.doAuth(ret.res.gameServerInfo); if (!ret3.isSucc) { tgxUIAlert.show('认证失败'); return ret3; } } else { if (ret.err.message == 'USER_NOT_EXISTS' || ret.err.message == 'PASSWORD_WRONG') { tgxUIAlert.show('用户名或者密码错误!'); } else { tgxUIAlert.show(ret.err.message); } } return ret; } async doAuth(params: GameServerAuthParams) { if (params) { NetGameServer.inst.authParams = params; if (params.serverUrl != NetGameServer.inst.serverUrl) { NetGameServer.inst.createConnection([params.serverUrl]); let ret2 = await NetGameServer.inst.ensureConnected(); if (!ret2.isSucc) { tgxUIAlert.show(ret2.err.message); return ret2; } } let ret = await NetGameServer.inst.callApi('login/AuthClient', { uid: this._userInfo.uid, token: params.token, time: params.time, }); if (!ret.isSucc) { tgxUIAlert.show(ret.err.message); return ret; } } //没有名字,表示还未创建角色,则进入角色创建流程 if (!UserMgr.inst.name) { tgxUIWaiting.show('角色准备中'); await tgxSceneUtil.loadScene(SceneDef.CREATE_ROLE); } //如果角色在房间中,则进入房间 else if (params.roomId) { if(params.gameType==GameTypes.Arean){ // let ret = await NetGameServer.inst.conn.callApi("ExitRoom", {}); // if (ret.isSucc) { //进入大厅 UserMgr._inst.roomId = params.roomId; UserMgr._inst.reTrayGameParmas = params; await tgxSceneUtil.loadScene(SceneDef.LOBBY); // } }else{ let ret2 = await UserMgr.inst.doTryEnterRoom(params.roomId); if (!ret2.isSucc) { //进入大厅 await tgxSceneUtil.loadScene(SceneDef.LOBBY); } } } else { //进入大厅 await tgxSceneUtil.loadScene(SceneDef.LOBBY); } return { isSucc: true }; } async doCreateRole(name: string, visualId: number) { let ret = await NetGameServer.inst.callApi('login/CreateRole', { name: name, visualId: visualId }); if (ret.isSucc) { this._userInfo.name = ret.res.name; this._userInfo.visualId = ret.res.visualId; } return ret; } async doTryEnterRoom(id: string, password?: string) { tgxUIWaiting.show('进入世界'); let ret = await NetGameServer.inst.callApi('lobby/TryEnterRoom', { id: id, password: password }, { timeout: 10000 }); tgxUIWaiting.hide(); if (ret.isSucc) { let params = ret.res; tgxUIWaiting.show('进入世界'); if(!UserMgr._inst.isInGame){ return await SubGameMgr.inst.enterSubGame(params,this.uid); }else{ let ret = await SubGameMgr.gameMgr.enterRoom(params,this.uid); if (ret.isSucc) { console.log('进入房间成功'); } } } else { tgxUIAlert.show(ret.err.message); } return ret; } /** 称号 */ public async setTitle(uid: string,lab : Label): Promise { let info = await this.rpc_GetUserInfo(uid); // lab.string = "临时称号" } /** 段位图标 */ public async setDanIcon(userId: string, sprIcon: Sprite, bundleName?: string): Promise { } async setUserIconAndName(userId: string, sprIcon: Sprite, lblName?: Label, bundleName?: string) { bundleName ||= ModuleContext.getDefaultModule(); if (!sprIcon && !lblName) { return; } let info = await this.rpc_GetUserInfo(userId); console.log("房间的玩家信息 ===",info); if (!info) { return; } if (lblName && lblName.isValid) { lblName.string = info.name || 'User_' + userId; } // if (sprIcon && sprIcon.isValid) { // let localHeroData = areanMgr.cfgMgr.HeroDatas.getAllDataMap(); // let curHeroData = localHeroData.get(info.heroId); // if(curHeroData){ // let skin = curHeroData.skinRes[0]; // let skinId = areanMgr.cfgMgr.SkinData.getData(skin).icon // resLoader.load(ModuleDef.Arean,`res/Prefebs/heroSpine/${skinId}`,(err: Error, prefab: Prefab) => { // if(err){ // console.error(err); // return; // }else { // // let spineNode = instantiate(prefab); // // sprIcon.node.destroyAllChildren(); // // sprIcon.node.addChild(spineNode); // // spineNode.scale = v3(0.8,0.8,1); // // spineNode.position = v3(0,-60,0); // } // }) // }else { // // console.error(`${info.heroId}英雄数据不存在`); // } // // let bundle = resources; // // if (bundleName) { // // bundle = assetManager.getBundle(bundleName); // // } // // bundle.load('icons/icon' + info.visualId+"/spriteFrame", (err, sp: SpriteFrame) => { // // if (!sprIcon.isValid) { // // return; // // } // // // let tex = new Texture2D(); // // // tex.image = data; // // // let spriteFrame = new SpriteFrame(); // // // spriteFrame.texture = tex; // // sprIcon.spriteFrame = sp; // // }); // // // // // let sp : SpriteFrame = resLoader.getSpriteFrame('icons/icon' + info.visualId,SpriteFrame,ModuleDef.BASIC); // // // if(sp) sprIcon.spriteFrame = sp; // } } public async setUserIconAndName1(userId: string, sprIcon: Sprite,headFrame : Sprite, lblName?: Label, bundleName?: string): Promise { bundleName ||= ModuleContext.getDefaultModule(); if (!sprIcon && !lblName) { return; } let info = await this.rpc_GetUserInfo(userId); console.log("房间的玩家信息 ===", info); if (!info) { return; } if (lblName && lblName.isValid) { lblName.string = info.name || 'User_' + userId; } let bundle = resources; if (bundleName) { bundle = assetManager.getBundle(bundleName); } if (sprIcon && sprIcon.isValid) { bundle.load('icons/touxiang_0'+ this.formatLastTwoDigits(info.visualId) + "/spriteFrame", (err, sp: SpriteFrame) => { if (!sprIcon.isValid) { return; } // let tex = new Texture2D(); // tex.image = data; // let spriteFrame = new SpriteFrame(); // spriteFrame.texture = tex; sprIcon.spriteFrame = sp; }); } if (headFrame && headFrame.isValid) { bundle.load('icons/touxiangFrame_0'+ this.formatLastTwoDigits(info.frameIcon) + "/spriteFrame", (err, sp: SpriteFrame) => { if (!headFrame.isValid) { return; } headFrame.spriteFrame = sp; }); } // let sp : SpriteFrame = resLoader.getSpriteFrame('icons/icon' + info.visualId,SpriteFrame,ModuleDef.BASIC); // if(sp) sprIcon.spriteFrame = sp; } formatLastTwoDigits(num: number): string { const numStr = num.toString(); const lastTwoDigits = numStr.slice(-2); if (lastTwoDigits === "00") { return "0"; } else if (lastTwoDigits.startsWith("0")) { return lastTwoDigits.slice(1); } // 否则,返回最后两位数字 return lastTwoDigits; } async rpc_GetUserInfo(userId: string) { let info = this._userInfoMap[userId]; let testFun = (_info : UserInfo)=>{ // //TODO 测试数据 // _info.skin = [10101,10301]; // _info.hero = [1001,1003]; } if (info) { testFun(info); return info as UserInfo; } if(!userId){ return null; } let ret = await NetGameServer.inst.callApi('lobby/GetUserInfo', { uid: userId }); if (!ret.isSucc || !ret.res.infos.length) { return null; } info = ret.res.infos[0]; this._userInfoMap[info.uid] = info; testFun(info); return info; } async rpc_GetUserInfos(userIds: Array) { let uncachedIds = []; for (let i = 0; i < userIds.length; ++i) { let userId = userIds[i]; if (!this._userInfoMap[userId]) { uncachedIds.push(userId); } } if (uncachedIds.length == 0) { return this._userInfoMap; } let ret = await NetGameServer.inst.callApi('lobby/GetUserInfo', { uids: uncachedIds }); if (!ret.isSucc || !ret.res.infos.length) { return null; } for (let i = 0; i < ret.res.infos.length; ++i) { let info = ret.res.infos[i]; this._userInfoMap[info.uid] = info; } return this._userInfoMap; } async rpc_QuickPlay(type: string, immediate?: boolean) { console.log("rpc_QuickPlay----快速游戏", type, immediate); let ret = await NetGameServer.inst.callApi("lobby/StartMatch", { type: type, immediate: immediate,gameModel:UserLocalCache.inst.fightModel }); return ret; } async rpc_QuickPlayCancel() { let ret = await NetGameServer.inst.callApi("lobby/CancelMatch", {}); ret = await NetGameServer.inst.callApi("ExitRoom", { }); return ret; } async rpc_GetRoomList(type: string, curPage: number, pageItemNum: number) { let ret = await NetGameServer.inst.callApi('lobby/GetRoomList', { type: type, curPage, pageItemNum }); return ret; } async rpc_CreateRoom(type: string, name: string, password: string) { let mode = UserLocalCache.inst.fightModel; let ret = await NetGameServer.inst.callApi("lobby/CreateRoom", { roomName: name, gameType: type, password: password ,gameModel:mode}); return ret; } async rpc_ModifyUserInfo(gender: number | undefined, introduction: string | undefined) { let ret = await NetGameServer.inst.callApi("lobby/ModifyUserInfo", { gender: gender, introduction: introduction }); if (ret.isSucc) { if (ret.res.gender != undefined) { this._userInfo.gender = ret.res.gender; } if (ret.res.introduction != undefined) { this._userInfo.introduction = ret.res.introduction; } } return ret; } async rpc_GetAnnouncement(type: string) { let ret = await NetGameServer.inst.callApi("lobby/GetAnnouncement", { type: type }); return ret; } async rpc_GetNotice() { let ret = await NetGameServer.inst.callApi("lobby/GetNotice", {}); return ret; } // 英雄列表 async rpc_getHeroList(uid : string){ let list = await NetGameServer.inst.callApi("lobby/hero/GetHeroList", {uid: uid}); let info : UserInfo = this._userInfoMap[uid]; if(info){ info.hero = list.res.heroList } return list.res.heroList; } // 添加英雄 async rpc_addHero(uid : string,heroId : number,buyType : EAreanBuyType){ return await NetGameServer.inst.callApi("lobby/hero/AddHero", {heroId: heroId,buyType : buyType}); } // // 段位信息 async rpc_getDanInfo(uid : string){ let list = await NetGameServer.inst.callApi("lobby/user/DanInfo", {uid: uid}); let info : UserInfo = this._userInfoMap[uid]; console.log("info === ",list.res,info); if(info){ info.danInfo = list.res.danInfo } return list.res.danInfo; } // 道具信息 async rpc_getItemInfo(uid : string){ let list = await NetGameServer.inst.callApi("item/GetItemInfo", {uid: uid}); let info : UserInfo = this._userInfoMap[uid]; console.log("info === ",list.res,info); if(info){ info.itemList = list.res.itemList } return info.itemList; } // 选择英雄皮肤 async rpc_updateHeroSkin(heroId : number,skinId : number){ await NetGameServer.inst.callApi("lobby/hero/UpdateHeroSkin", {heroId: heroId,skinId : skinId}); } // 更新英雄熟练度 async rpc_updateHeroProficiency(heroId : number,value : number){ return NetGameServer.inst.callApi("lobby/hero/UpdateHeroProficiency", {heroId: heroId,proficiency : value}); } /** 使用道具 */ async rpc_use_item(itemId : number) { return await NetGameServer.inst.conn.callApi("arean/reward/AranaUseItem", {itemId : itemId}); } /** 使用道具 */ async rpc_update_head_frame(headId : number,frameId : number) { return await NetGameServer.inst.conn.callApi("lobby/user/UpdateVisual", {visualId : headId,frameId : frameId}); } }