- 新增 GameDataSync 类,封装数据同步逻辑,支持防抖与时间戳冲突解决 - 重构 SingletonModuleComp 的云端同步方法,统一调用 GameDataSync - 优化 TalentsComp 天赋升级流程,使用新的同步机制 - 添加本地缓存支持,提升离线体验与数据恢复能力
306 lines
9.4 KiB
TypeScript
306 lines
9.4 KiB
TypeScript
import { sys } from "cc";
|
||
import { VM } from "../../../../extensions/oops-plugin-framework/assets/libs/model-view/ViewModel";
|
||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||
import { Initialize } from "../initialize/Initialize";
|
||
import { GameMap } from "../map/GameMap";
|
||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||
import { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||
import { GameEvent } from "./config/GameEvent";
|
||
import { GameScoreStats } from "./config/HeroAttrs";
|
||
import { mLogger } from "./Logger";
|
||
import { TalentType } from "./config/TalentSet";
|
||
import { gameDataSync } from "./GameDataSync";
|
||
|
||
/**
|
||
* 用远程数据覆盖本地数据(统一方法)
|
||
* @param remoteData 远程数据(云端或本地调试)
|
||
*/
|
||
export interface GameDate{
|
||
gold:number,
|
||
heros:any,
|
||
fight_hero:number,
|
||
timestamp?: number, // 用于比对本地与云端数据的最新状态
|
||
collection?: {
|
||
talents: Partial<Record<TalentType, number>>,
|
||
player_level: number,
|
||
player_exp: number,
|
||
talent_points: number,
|
||
skills?: {uuid:0,count:0},
|
||
friend?: {uuid:0,count:0},
|
||
}
|
||
}
|
||
export interface CloudData {
|
||
openid: string;
|
||
data: GameDate;
|
||
}
|
||
/** 游戏模块 */
|
||
@ecs.register('SingletonModule')
|
||
export class SingletonModuleComp extends ecs.Comp {
|
||
private debugMode: boolean = false;
|
||
|
||
/** 游戏初始化模块 */
|
||
initialize: Initialize = null!;
|
||
/** 游戏地图 */
|
||
map: GameMap = null!;
|
||
openid:string=''
|
||
mission:any={
|
||
status:0, //0:未开始 1:进行中 2:胜利 3:失败
|
||
play:false,
|
||
pause:false,
|
||
in_select:false,
|
||
in_fight:false,
|
||
stop_mon_action:false,
|
||
};
|
||
data:any={
|
||
score:0,
|
||
mission:1,
|
||
diamond:100, //商店购买 及 双倍奖励资源
|
||
gold:1000,
|
||
task:0,
|
||
noStop:false,
|
||
showInfo:true,
|
||
}
|
||
|
||
|
||
guides:any=[0,0,0,0,0]
|
||
current_guide:number=0
|
||
fight_hero: number = 5001; // 单个出战英雄
|
||
heros:any= [5001]
|
||
|
||
collection: {
|
||
talents: Partial<Record<TalentType, number>>;
|
||
player_level: number;
|
||
player_exp: number;
|
||
talent_points: number;
|
||
} = {
|
||
talents: {}, // 存储各个天赋的等级: { talent_id: level }
|
||
player_level: 1, // 玩家等级
|
||
player_exp: 0, // 玩家当前经验
|
||
talent_points: 0, // 当前可用天赋点
|
||
};
|
||
|
||
vmdata: any = {
|
||
game_over:false,
|
||
game_pause:false,
|
||
mission_data:{
|
||
mon_num:0,//怪物数量
|
||
hero_num:0,//英雄数量
|
||
hero_max_num:5,//英雄可召唤上限
|
||
hero_extend_max_num:6,//英雄可拓展上限
|
||
wave_time_num:0,//波次时间
|
||
in_fight:false,
|
||
fight_time:0,//战斗时间
|
||
level:1,//关卡等级
|
||
max_mission:4,//最大关卡
|
||
coin:0,
|
||
time:15*60,//游戏时间
|
||
},
|
||
scores: {
|
||
score: 0, // 基础得分
|
||
// 战斗统计
|
||
crt_count: 0, // 暴击次数
|
||
wf_count: 0, // 风怒次数
|
||
dod_count: 0, // 闪避次数
|
||
back_count: 0, // 击退次数
|
||
stun_count: 0, // 击晕次数
|
||
freeze_count: 0, // 冰冻次数
|
||
|
||
// 伤害统计
|
||
total_dmg: 0, // 总伤害
|
||
atk_count: 0, // 攻击次数
|
||
avg_dmg: 0, // 平均伤害
|
||
thorns_dmg: 0, // 反伤伤害
|
||
crit_dmg_total: 0, // 暴击伤害总额
|
||
|
||
// 生存统计
|
||
heal_total: 0, // 治疗总量
|
||
lifesteal_total: 0, // 吸血总量
|
||
shield_block_count: 0,
|
||
dead_trigger_count: 0,
|
||
|
||
// 资源统计
|
||
exp_total: 0, // 经验总数
|
||
gold_total: 0, // 金币总数
|
||
gold_earned: 0,
|
||
gold_spent: 0,
|
||
refresh_count: 0,
|
||
refresh_hit_count: 0,
|
||
|
||
// 击杀统计
|
||
melee_kill_count: 0, // 近战怪击杀数量
|
||
remote_kill_count: 0, // 远程怪击杀数量
|
||
elite_kill_count: 0, // 精英怪击杀数量
|
||
boss_kill_count: 0, // Boss击杀数
|
||
|
||
// 战绩统计
|
||
wave_win_count: 0,
|
||
wave_remain_monsters: 0,
|
||
wave_all_alive_count: 0,
|
||
passed_wave_20: false,
|
||
highest_dmg: 0,
|
||
|
||
score_combat: 0,
|
||
score_output: 0,
|
||
score_defense: 0,
|
||
score_build: 0,
|
||
score_efficiency: 0,
|
||
|
||
achieved_highlights: [],
|
||
} as GameScoreStats,
|
||
|
||
gold: 0, // 金币数据(MVVM绑定字段)
|
||
};
|
||
|
||
|
||
|
||
|
||
vmAdd() {
|
||
VM.add(this.vmdata, "data");
|
||
// mLogger.log(this.debugMode, 'SMC', "[MissionComp]局内数据初始化",smc.vmdata.mission_data)
|
||
}
|
||
reset() {
|
||
for (var key in this.vmdata) {
|
||
delete this.vmdata[key];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 重置单局评分数据
|
||
* 在每次新战斗开始时调用,确保上一局的得分不会带入新一局
|
||
*/
|
||
resetScores() {
|
||
this.vmdata.scores = {
|
||
score: 0,
|
||
crt_count: 0,
|
||
wf_count: 0,
|
||
dod_count: 0,
|
||
back_count: 0,
|
||
stun_count: 0,
|
||
freeze_count: 0,
|
||
total_dmg: 0,
|
||
atk_count: 0,
|
||
avg_dmg: 0,
|
||
thorns_dmg: 0,
|
||
crit_dmg_total: 0,
|
||
heal_total: 0,
|
||
lifesteal_total: 0,
|
||
shield_block_count: 0,
|
||
dead_trigger_count: 0,
|
||
exp_total: 0,
|
||
gold_total: 0,
|
||
gold_earned: 0,
|
||
gold_spent: 0,
|
||
refresh_count: 0,
|
||
refresh_hit_count: 0,
|
||
melee_kill_count: 0,
|
||
remote_kill_count: 0,
|
||
elite_kill_count: 0,
|
||
boss_kill_count: 0,
|
||
wave_win_count: 0,
|
||
wave_remain_monsters: 0,
|
||
wave_all_alive_count: 0,
|
||
passed_wave_20: false,
|
||
highest_dmg: 0,
|
||
score_combat: 0,
|
||
score_output: 0,
|
||
score_defense: 0,
|
||
score_build: 0,
|
||
score_efficiency: 0,
|
||
achieved_highlights: [],
|
||
} as GameScoreStats;
|
||
}
|
||
|
||
// ==================== 数据管理方法 ====================
|
||
|
||
/**
|
||
* 判断是否为微信客户端
|
||
*/
|
||
isWxClient(): boolean {
|
||
return gameDataSync.isWxClient();
|
||
}
|
||
|
||
finishGuide(index:number){
|
||
smc.guides[index]=1
|
||
//存储到远程服务器 后续再添加
|
||
}
|
||
|
||
updateCloudData(){
|
||
return gameDataSync.updateCloudData();
|
||
}
|
||
|
||
getCloudData(){
|
||
gameDataSync.getCloudData();
|
||
}
|
||
public async overrideLocalDataWithRemote(cloudData: any) {
|
||
try {
|
||
// 直接覆盖基础游戏数据
|
||
if (cloudData.openid) {
|
||
this.openid = cloudData.openid;
|
||
}
|
||
// 直接覆盖出战英雄配置
|
||
if (cloudData.data) {
|
||
const data = cloudData.data;
|
||
// 同步金币
|
||
if (data.gold !== undefined) {
|
||
this.vmdata.gold = data.gold;
|
||
}
|
||
// 同步英雄
|
||
if (data.heros && Array.isArray(data.heros)) {
|
||
this.heros = data.heros;
|
||
}
|
||
// 同步出战英雄
|
||
if (data.fight_hero !== undefined) {
|
||
this.fight_hero = data.fight_hero;
|
||
}
|
||
// 恢复收集记录
|
||
if (data.collection) {
|
||
const remoteCol = data.collection;
|
||
if (remoteCol.talents) this.collection.talents = remoteCol.talents;
|
||
if (typeof remoteCol.player_level === 'number') this.collection.player_level = remoteCol.player_level;
|
||
if (typeof remoteCol.player_exp === 'number') this.collection.player_exp = remoteCol.player_exp;
|
||
if (typeof remoteCol.talent_points === 'number') this.collection.talent_points = remoteCol.talent_points;
|
||
}
|
||
}
|
||
|
||
// 触发UI更新
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE);
|
||
|
||
} catch (error) {
|
||
mLogger.error(this.debugMode, 'SMC', `[SMC]: 数据覆盖失败:`, error);
|
||
}
|
||
}
|
||
getGameDate(){
|
||
let data: GameDate = {
|
||
gold: this.vmdata.gold,
|
||
heros: this.heros,
|
||
fight_hero: this.fight_hero,
|
||
collection: this.collection,
|
||
timestamp: Date.now() // 每次获取当前数据结构时都附带最新的时间戳
|
||
};
|
||
return data;
|
||
}
|
||
addHero(hero_uuid:number){
|
||
if(this.heros.indexOf(hero_uuid)==-1){
|
||
this.heros.push(hero_uuid)
|
||
gameDataSync.markDataDirty();
|
||
}
|
||
return true
|
||
}
|
||
|
||
updateGold(gold:number, is_sync: boolean = true){
|
||
this.vmdata.gold += gold;
|
||
if (is_sync) {
|
||
gameDataSync.markDataDirty();
|
||
}
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}
|
||
|
||
error(){
|
||
oops.gui.toast("数据处理异常,请重试或重新登录")
|
||
}
|
||
|
||
}
|
||
|
||
export var smc: SingletonModuleComp = ecs.getSingleton(SingletonModuleComp);
|