refactor(common): 重构游戏数据同步与单例模块代码
- 移除 GameDataSyncManager 类及相关依赖,简化数据同步管理逻辑 - 在 SingletonModuleComp 中集成数据管理功能,使用本地数组替代字典结构存储英雄数据 - 优化本地与云端数据同步方法,适配云函数接口改动 - 修改英雄判断逻辑,支持基于数组的查询方式 - 修正金币数据的增减接口,增加异步云调用与本地更新的统一处理 - 删除冗余注释及无用代码,提升代码可读性和维护性 - 调整数据结构定义和类型声明,保障类型安全与代码健壮性
This commit is contained in:
@@ -3,14 +3,10 @@ import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ec
|
||||
import { Initialize } from "../initialize/Initialize";
|
||||
import { GameMap } from "../map/GameMap";
|
||||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||||
import { GameData, WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { gameDataSyncManager } from "./GameDataSyncManager";
|
||||
import { WxCloudApi, UserGameData } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { Test } from "./Test";
|
||||
import { GameEvent } from "./config/GameEvent";
|
||||
|
||||
|
||||
// import { Role } from "../role/Role";
|
||||
// import { data } from "../data/data";
|
||||
/** 游戏模块 */
|
||||
@ecs.register('SingletonModule')
|
||||
export class SingletonModuleComp extends ecs.Comp {
|
||||
@@ -18,8 +14,6 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
initialize: Initialize = null!;
|
||||
/** 游戏地图 */
|
||||
map: GameMap = null!;
|
||||
/** 游戏数据同步管理器 */
|
||||
private gameDataSyncManager = gameDataSyncManager;
|
||||
mission:any={
|
||||
status:0, //0:未开始 1:进行中 2:胜利 3:失败
|
||||
play:false,
|
||||
@@ -32,24 +26,17 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
data:any={
|
||||
score:0,
|
||||
mission:1,
|
||||
gold:100, //升级主要资源
|
||||
diamond:100, //商店购买 及 双倍奖励资源
|
||||
meat:0,
|
||||
exp:0,
|
||||
task:0,
|
||||
}
|
||||
|
||||
fight_hero: number = 5001; // 单个出战英雄
|
||||
heros:any = {
|
||||
5001:{uuid:5001,lv:1},
|
||||
5005:{uuid:5005,lv:1},
|
||||
};
|
||||
monsters:any = [];
|
||||
sk_info:any = []
|
||||
monsters_dead:any = []
|
||||
heros_dead:any = []
|
||||
enhancements:any=[]
|
||||
items: any = {}; // 物品数据
|
||||
vmdata: any = {
|
||||
game_over:false,
|
||||
game_pause:false,
|
||||
@@ -61,7 +48,9 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
fight_time:0,//战斗时间
|
||||
level:1,//关卡等级
|
||||
max_mission:4,//最大关卡
|
||||
coin:0,
|
||||
},
|
||||
gold: 100, // 金币数据(MVVM绑定字段)
|
||||
};
|
||||
vmAdd() {
|
||||
VM.add(this.vmdata, "data");
|
||||
@@ -74,9 +63,9 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
|
||||
// ==================== 数据管理方法 ====================
|
||||
|
||||
/**
|
||||
* 判断是否为微信客户端
|
||||
*/
|
||||
/**
|
||||
* 判断是否为微信客户端
|
||||
*/
|
||||
private isWxClient(): boolean {
|
||||
// 检查是否存在微信API
|
||||
return typeof wx !== 'undefined' && typeof (wx as any).getSystemInfoSync === 'function';
|
||||
@@ -84,26 +73,63 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
|
||||
finishGuide(index:number){
|
||||
smc.guides[index]=1
|
||||
this.syncGuide()
|
||||
}
|
||||
|
||||
syncGuide(){
|
||||
//存储到远程服务器 后续再添加
|
||||
}
|
||||
|
||||
//调试用
|
||||
syncDataFromLocal(){
|
||||
if(this.isWxClient()) return
|
||||
const loginResult = new Test().load_data_from_local()
|
||||
this.gameDataSyncManager.overrideLocalDataWithRemote(loginResult, "本地调试");
|
||||
this.overrideLocalDataWithRemote(loginResult, "本地调试");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用远程数据覆盖本地数据(统一方法)
|
||||
* @param remoteData 远程数据(云端或本地调试)
|
||||
* @param dataSource 数据源描述
|
||||
*/
|
||||
async overrideLocalDataWithRemote(remoteData: UserGameData, dataSource: string) {
|
||||
try {
|
||||
// 直接覆盖基础游戏数据
|
||||
if (remoteData.data) {
|
||||
// 保留原有的data字段数据
|
||||
Object.assign(this.data, remoteData.data);
|
||||
// 同步gold到vmdata
|
||||
if (remoteData.data.gold !== undefined) {
|
||||
this.vmdata.gold = remoteData.data.gold;
|
||||
}
|
||||
}
|
||||
|
||||
// 直接覆盖出战英雄配置
|
||||
if (remoteData.fight_heros) {
|
||||
this.fight_hero = remoteData.fight_heros[0] || this.fight_hero;
|
||||
}
|
||||
|
||||
// 直接覆盖英雄数据
|
||||
if (remoteData.heros) {
|
||||
this.heros = { ...remoteData.heros };
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[SMC]: ${dataSource}数据覆盖失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
addHero(hero_uuid:number,autoSave:boolean=true){
|
||||
if(this.isWxClient()){
|
||||
if(this.gameDataSyncManager.addHero(hero_uuid)){
|
||||
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
|
||||
return true
|
||||
}
|
||||
return false
|
||||
// 适配原有接口,保持与云函数的兼容性
|
||||
const result = WxCloudApi.addHero(hero_uuid);
|
||||
result.then((res) => {
|
||||
if(res.result.code === 200) {
|
||||
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}).catch((error) => {
|
||||
console.error(`[SMC]: 添加英雄异常:`, error);
|
||||
this.error()
|
||||
return false
|
||||
});
|
||||
}
|
||||
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
|
||||
return true
|
||||
@@ -113,7 +139,15 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
setFightHero(heroId: number, autoSave: boolean = true) {
|
||||
this.fight_hero = heroId;
|
||||
if (this.isWxClient()) {
|
||||
this.gameDataSyncManager.updateFightHeros({ 0: heroId }); // 适配原有接口
|
||||
// 适配原有接口,保持与云函数的兼容性
|
||||
WxCloudApi.updateFightHeros({ 0: heroId }).then((result) => {
|
||||
if (result.result.code !== 200) {
|
||||
console.warn(`[SMC]: 出战英雄配置更新失败: ${result.result.msg}`);
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(`[SMC]: 更新出战英雄配置异常:`, error);
|
||||
this.error()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,35 +156,64 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
return this.fight_hero;
|
||||
}
|
||||
|
||||
getHasHeroUUID(){
|
||||
let heros=this.heros
|
||||
let heros_uuid=[]
|
||||
for(let key in heros){
|
||||
heros_uuid.push(heros[key].uuid)
|
||||
}
|
||||
return heros_uuid
|
||||
}
|
||||
|
||||
error(){
|
||||
oops.gui.toast("数据处理异常,请重试或重新登录")
|
||||
}
|
||||
|
||||
addGold(gold:number,autoSave:boolean=true){
|
||||
if(this.isWxClient()){
|
||||
if(this.gameDataSyncManager.addGameProperty("gold",gold)){
|
||||
this.data.gold+=gold
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}
|
||||
this.error()
|
||||
return false
|
||||
WxCloudApi.addGameDataField("gold",gold).then((result) => {
|
||||
if(result.result.code === 200) {
|
||||
this.vmdata.gold += gold;
|
||||
this.data.gold = this.vmdata.gold; // 同步到data字段
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
} else {
|
||||
console.warn(`[SMC]: 游戏数据增加失败: ${result.result.msg}`);
|
||||
this.error()
|
||||
return false
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(`[SMC]: 增加游戏数据异常:`, error);
|
||||
this.error()
|
||||
return false
|
||||
});
|
||||
}
|
||||
this.data.gold+=gold
|
||||
this.vmdata.gold += gold;
|
||||
this.data.gold = this.vmdata.gold; // 同步到data字段
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}
|
||||
|
||||
spendGold(gold:number,autoSave:boolean=true){
|
||||
if(this.isWxClient()){
|
||||
if(this.gameDataSyncManager.spendGameProperty("gold",gold)){
|
||||
this.data.gold-=gold
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
WxCloudApi.spendGameDataField("gold",gold).then((result) => {
|
||||
if(result.result.code === 200) {
|
||||
this.vmdata.gold -= gold;
|
||||
this.data.gold = this.vmdata.gold; // 同步到data字段
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
} else {
|
||||
console.warn(`[SMC]: 游戏数据消耗失败: ${result.result.msg}`);
|
||||
return false
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(`[SMC]: 消耗游戏数据异常:`, error);
|
||||
this.error()
|
||||
return false
|
||||
});
|
||||
}
|
||||
this.data.gold-=gold
|
||||
this.vmdata.gold -= gold;
|
||||
this.data.gold = this.vmdata.gold; // 同步到data字段
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user