refactor(game): 重构云端数据同步逻辑并提取公共方法
- 将 `getCloudData` 中的冲突解决逻辑提取为独立的 `syncWithCloudData` 方法 - 修改 `getCloudData` 返回 Promise 以支持调用方等待 - 在 `Initialize` 中统一使用 `gameDataSync.syncWithCloudData` 处理数据合并 - 移除 `Initialize` 中重复的 `isWxClient` 方法,复用 `GameDataSync` 中的实现 - 简化 `loadFromCloud` 方法,将登录后数据处理委托给 `syncWithCloudData`
This commit is contained in:
@@ -14,6 +14,7 @@ import { LoadingViewComp } from "./view/LoadingViewComp";
|
||||
import { smc } from "../common/SingletonModuleComp";
|
||||
import { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { mLogger } from "../common/Logger";
|
||||
import { gameDataSync } from "../common/GameDataSync";
|
||||
|
||||
/**
|
||||
* 游戏进入初始化模块
|
||||
@@ -85,13 +86,6 @@ export class Initialize extends ecs.Entity {
|
||||
oops.res.loadDir("common", next);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 判断是否为微信客户端
|
||||
*/
|
||||
private isWxClient(): boolean {
|
||||
// 检查是否存在微信API
|
||||
return typeof wx !== 'undefined' && typeof (wx as any).getSystemInfoSync === 'function';
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一的游戏数据加载流程
|
||||
@@ -102,7 +96,7 @@ export class Initialize extends ecs.Entity {
|
||||
try {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 开始统一数据加载流程...");
|
||||
|
||||
if (this.isWxClient()) {
|
||||
if (gameDataSync.isWxClient()) {
|
||||
// 微信客户端:加载云端数据
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 检测到微信客户端,使用云端数据");
|
||||
await this.loadFromCloud();
|
||||
@@ -130,36 +124,33 @@ export class Initialize extends ecs.Entity {
|
||||
const loginResult = await WxCloudApi.login();
|
||||
const response = loginResult.result;
|
||||
|
||||
if (loginResult.result.code === 200) {
|
||||
if (response && response.code === 200) {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端登录成功");
|
||||
smc.updateCloudData()
|
||||
const cloudData = loginResult.result.data;
|
||||
try {
|
||||
// 直接覆盖基础游戏数据
|
||||
if (cloudData.openid) {
|
||||
smc.openid=cloudData.openid
|
||||
}
|
||||
// 直接覆盖出战英雄配置
|
||||
if (cloudData.game_data) {
|
||||
let gameDate=cloudData.game_data
|
||||
if( gameDate.gold ) smc.vmdata.gold=gameDate.gold
|
||||
if( gameDate.heros ) smc.heros=gameDate.heros
|
||||
if( gameDate.fight_hero ) smc.fight_hero=gameDate.fight_hero
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
mLogger.log(this.debugMode, 'Initialize', `[SMC]: 数据覆盖失败:`, error);
|
||||
|
||||
const cloudData = response.data;
|
||||
|
||||
if (cloudData && cloudData.openid) {
|
||||
smc.openid = cloudData.openid;
|
||||
}
|
||||
|
||||
// 将 login 获取的 game_data 封装为统一下发格式,交给 gameDataSync 对比与合并
|
||||
const formattedCloudData = {
|
||||
openid: cloudData?.openid || '',
|
||||
data: cloudData?.game_data || {}
|
||||
};
|
||||
|
||||
gameDataSync.syncWithCloudData(formattedCloudData);
|
||||
|
||||
} else {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端登录失败:", response.msg);
|
||||
// 登录失败时使用本地数据 游戏需要退出
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端登录失败:", response.msg);
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端登录失败:", response?.msg);
|
||||
// 登录失败时使用本地数据
|
||||
gameDataSync.syncWithCloudData(null);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端数据加载异常:", error);
|
||||
// 异常时使用本地数据 游戏需要退出
|
||||
// 异常时使用本地数据兜底
|
||||
gameDataSync.syncWithCloudData(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,10 +159,8 @@ export class Initialize extends ecs.Entity {
|
||||
*/
|
||||
private async loadFromLocalDebug() {
|
||||
try {
|
||||
// 使用本地调试API,模拟云端接口
|
||||
|
||||
// 用本地调试数据覆盖客户端数据
|
||||
|
||||
gameDataSync.syncWithCloudData(null);
|
||||
} catch (error) {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 本地调试数据加载异常:", error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user