refactor(common): 重构游戏数据同步与单例模块代码
- 移除 GameDataSyncManager 类及相关依赖,简化数据同步管理逻辑 - 在 SingletonModuleComp 中集成数据管理功能,使用本地数组替代字典结构存储英雄数据 - 优化本地与云端数据同步方法,适配云函数接口改动 - 修改英雄判断逻辑,支持基于数组的查询方式 - 修正金币数据的增减接口,增加异步云调用与本地更新的统一处理 - 删除冗余注释及无用代码,提升代码可读性和维护性 - 调整数据结构定义和类型声明,保障类型安全与代码健壮性
This commit is contained in:
9
assets/Scripts.meta
Normal file
9
assets/Scripts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "f784d67d-edb7-4b84-9ea8-de8d2eb4c174",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,527 +0,0 @@
|
||||
import { oops } from "db://oops-framework/core/Oops";
|
||||
import { WxCloudApi ,UserGameData} from "../wx_clound_client_api/WxCloudApi";
|
||||
import { smc } from "./SingletonModuleComp";
|
||||
import { GameData } from "../wx_clound_client_api/WxCloudApi";
|
||||
|
||||
/**
|
||||
* 游戏数据同步管理器
|
||||
* 负责管理fight_heros、heros、items、tals、equips的远程操作和本地同步
|
||||
* 只有在远程修改成功后才同步修改本地数据
|
||||
*/
|
||||
export class GameDataSyncManager {
|
||||
private static instance: GameDataSyncManager;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): GameDataSyncManager {
|
||||
if (!GameDataSyncManager.instance) {
|
||||
GameDataSyncManager.instance = new GameDataSyncManager();
|
||||
}
|
||||
return GameDataSyncManager.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用远程数据覆盖本地数据(统一方法)
|
||||
* @param remoteData 远程数据(云端或本地调试)
|
||||
* @param dataSource 数据源描述
|
||||
*/
|
||||
async overrideLocalDataWithRemote(remoteData: UserGameData, dataSource: string) {
|
||||
try {
|
||||
// console.log(`[Initialize]: 开始用${dataSource}数据覆盖客户端数据...`);
|
||||
|
||||
// 直接覆盖基础游戏数据
|
||||
if (remoteData.data) {
|
||||
Object.assign(smc.data, remoteData.data);
|
||||
// console.log(`[Initialize]: 基础游戏数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖出战英雄配置
|
||||
if (remoteData.fight_heros) {
|
||||
Object.assign(smc.fight_heros, remoteData.fight_heros);
|
||||
// console.log(`[Initialize]: 出战英雄配置已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖英雄数据
|
||||
if (remoteData.heros) {
|
||||
smc.heros = { ...remoteData.heros };
|
||||
// console.log(`[Initialize]: 英雄数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖道具数据
|
||||
if (remoteData.items) {
|
||||
Object.assign(smc.items, remoteData.items);
|
||||
// console.log(`[Initialize]: 道具数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖天赋数据
|
||||
if (remoteData.tals) {
|
||||
Object.assign(smc.tals, remoteData.tals);
|
||||
// console.log(`[Initialize]: 天赋数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖装备数据
|
||||
if (remoteData.equips) {
|
||||
Object.assign(smc.equips, remoteData.equips);
|
||||
// console.log(`[Initialize]: 装备数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 保存到本地存储(确保数据持久化)
|
||||
// smc.saveGameData();
|
||||
|
||||
// console.log(`[Initialize]: ${dataSource}数据覆盖完成,已保存到本地`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[Initialize]: ${dataSource}数据覆盖失败:`, error);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 批量更新出战英雄配置
|
||||
* @param fightHeros 出战英雄配置对象
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async updateFightHeros(fightHero: any): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 批量更新出战英雄配置:`, fightHeros);
|
||||
|
||||
const result = await WxCloudApi.updateFightHeros(fightHero);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
Object.assign(smc.fight_heros, fightHeros);
|
||||
// console.log(`[GameDataSyncManager]: 出战英雄配置更新成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 出战英雄配置更新失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 更新出战英雄配置异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重置出战英雄配置为默认值
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async resetFightHeros(): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 重置出战英雄配置`);
|
||||
|
||||
const result = await WxCloudApi.resetFightHeros();
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.fight_heros = result.result.data;
|
||||
// console.log(`[GameDataSyncManager]: 出战英雄配置重置成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 出战英雄配置重置失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 重置出战英雄配置异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 英雄管理 ====================
|
||||
|
||||
/**
|
||||
* 添加新英雄到用户库存
|
||||
* @param heroId 英雄ID
|
||||
* @param heroData 英雄数据(可选)
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addHero(heroId: number, heroData?: any): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 添加英雄 ID:${heroId}, 数据:`, heroData);
|
||||
|
||||
const result = await WxCloudApi.addHero(heroId, heroData);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.heros[heroId] = result.result.data;
|
||||
// console.log(`[GameDataSyncManager]: 英雄添加成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄添加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 添加英雄异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新英雄的多个属性
|
||||
* @param heroId 英雄ID
|
||||
* @param updateData 要更新的属性
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async updateHero(heroId: number, updateData: any): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 更新英雄 ID:${heroId}, 更新数据:`, updateData);
|
||||
|
||||
const result = await WxCloudApi.updateHero(heroId, updateData);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
Object.assign(smc.heros[heroId], result.result.data.new_data);
|
||||
// console.log(`[GameDataSyncManager]: 英雄更新成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄更新失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 更新英雄异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置英雄的单个属性值
|
||||
* @param heroId 英雄ID
|
||||
* @param property 属性名
|
||||
* @param value 属性值
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async setHeroProperty(heroId: number, property: any, value: any): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 设置英雄属性 ID:${heroId}, 属性:${property}, 值:${value}`);
|
||||
|
||||
const result = await WxCloudApi.setHeroProperty(heroId, property, value);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.heros[heroId][property] = value;
|
||||
// console.log(`[GameDataSyncManager]: 英雄属性设置成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄属性设置失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 设置英雄属性异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 英雄升级指定级数
|
||||
* @param heroId 英雄ID
|
||||
* @param levels 升级级数(默认1级)
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async levelUpHero(heroId: number,levels: number = 1,): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 英雄升级 ID:${heroId}, 级数:${levels}`);
|
||||
|
||||
const result = await WxCloudApi.levelUpHero(heroId,levels);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.heros[heroId].lv = result.result.data.new_value;
|
||||
// console.log(`[GameDataSyncManager]: 英雄升级成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄升级失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 英雄升级异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 便捷方法 ====================
|
||||
|
||||
/**
|
||||
* 增加道具
|
||||
* @param itemId 道具ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addItem(itemId: number, count: number): Promise<boolean> {
|
||||
smc.items[itemId] = (smc.items[itemId] || 0) + count;
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 增加道具 ID:${itemId}, 数量:${count}`);
|
||||
const result = await WxCloudApi.addInventoryItem('items', itemId, count);
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
// console.log(`[GameDataSyncManager]: 道具增加成功`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 道具增加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 增加道具异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗道具
|
||||
* @param itemId 道具ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeItem(itemId: number, count: number): Promise<boolean> {
|
||||
if(!smc.items[itemId]||smc.items[itemId]<count){
|
||||
oops.gui.toast("道具数量不足")
|
||||
return false
|
||||
}
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 消耗道具 ID:${itemId}, 数量:${count}`);
|
||||
const result = await WxCloudApi.consumeInventoryItem('items', itemId, count);
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.items[itemId] = Math.max(0, (smc.items[itemId] || 0) - count);
|
||||
// console.log(`[GameDataSyncManager]: 道具消耗成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 道具消耗失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 消耗道具异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 增加天赋点
|
||||
* @param talId 天赋ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addTalent(talId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 增加天赋点 ID:${talId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.addInventoryItem('tals', talId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.tals[talId] = (smc.tals[talId] || 0) + count;
|
||||
// console.log(`[GameDataSyncManager]: 天赋点增加成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 天赋点增加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 增加天赋点异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗天赋点
|
||||
* @param talId 天赋ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeTalent(talId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 消耗天赋点 ID:${talId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.consumeInventoryItem('tals', talId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.tals[talId] = Math.max(0, (smc.tals[talId] || 0) - count);
|
||||
// console.log(`[GameDataSyncManager]: 天赋点消耗成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 天赋点消耗失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 消耗天赋点异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加装备
|
||||
* @param equipId 装备ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addEquipment(equipId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 增加装备 ID:${equipId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.addInventoryItem('equips', equipId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.equips[equipId] = (smc.equips[equipId] || 0) + count;
|
||||
// console.log(`[GameDataSyncManager]: 装备增加成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 装备增加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 增加装备异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗装备
|
||||
* @param equipId 装备ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeEquipment(equipId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 消耗装备 ID:${equipId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.consumeInventoryItem('equips', equipId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.equips[equipId] = Math.max(0, (smc.equips[equipId] || 0) - count);
|
||||
// console.log(`[GameDataSyncManager]: 装备消耗成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 装备消耗失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 消耗装备异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async addGameProperty(property: string, value: any): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 增加游戏数据 ${property} = ${value}`);
|
||||
const result = await WxCloudApi.addGameDataField(property, value);
|
||||
if (result.result.code === 200) {
|
||||
// console.log(`[GameDataSyncManager]: 游戏数据增加成功`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 游戏数据增加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 增加游戏数据异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async spendGameProperty(property: string|Record<string, number>, value: any = undefined ): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 消耗游戏数据 ${property} = ${value}`);
|
||||
const result = await WxCloudApi.spendGameDataField(property, value);
|
||||
if (result.result.code === 200) {
|
||||
// console.log(`[GameDataSyncManager]: 游戏数据消耗成功`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 消耗游戏数据异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从云端加载所有游戏数据并同步到本地
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async loadAllGameData(): Promise<boolean> {
|
||||
try {
|
||||
// console.log(`[GameDataSyncManager]: 从云端加载所有游戏数据`);
|
||||
|
||||
const result = await WxCloudApi.getAllGameData();
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程数据获取成功,同步本地数据
|
||||
const cloudData = result.result.data;
|
||||
smc.data = cloudData.data;
|
||||
smc.fight_heros = cloudData.fight_heros;
|
||||
smc.heros = cloudData.heros;
|
||||
smc.items = cloudData.items;
|
||||
smc.tals = cloudData.tals;
|
||||
smc.equips = cloudData.equips;
|
||||
|
||||
// console.log(`[GameDataSyncManager]: 云端数据加载成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 云端数据加载失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 加载云端数据异常:`, error);
|
||||
smc.error()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 导出单例实例
|
||||
export const gameDataSyncManager = GameDataSyncManager.getInstance();
|
||||
|
||||
/*
|
||||
使用示例:
|
||||
|
||||
// 1. 出战英雄管理
|
||||
await gameDataSyncManager.updateFightHeros({0: 5001, 1: 5005}); // 批量更新
|
||||
await gameDataSyncManager.resetFightHeros(); // 重置为默认配置
|
||||
|
||||
// 2. 英雄管理
|
||||
await gameDataSyncManager.addHero(5008, {uuid: 5008, lv: 1}); // 添加新英雄
|
||||
await gameDataSyncManager.levelUpHero(5001, 100, 50, 5); // 英雄升级5级
|
||||
await gameDataSyncManager.setHeroProperty(5001, "exp", 1000); // 设置英雄经验
|
||||
|
||||
// 3. 道具管理 (items)
|
||||
await gameDataSyncManager.addItem(1001, 10); // 增加道具
|
||||
await gameDataSyncManager.consumeItem(1001, 2); // 消耗道具
|
||||
await gameDataSyncManager.setItem(1001, 5); // 设置道具数量
|
||||
|
||||
// 4. 天赋点管理 (tals)
|
||||
await gameDataSyncManager.addTalent(2001, 5); // 增加天赋点
|
||||
await gameDataSyncManager.consumeTalent(2001, 2); // 消耗天赋点
|
||||
await gameDataSyncManager.setTalent(2001, 10); // 设置天赋点数量
|
||||
|
||||
// 5. 装备管理 (equips)
|
||||
await gameDataSyncManager.addEquipment(3001, 2); // 增加装备
|
||||
await gameDataSyncManager.consumeEquipment(3001, 1); // 消耗装备
|
||||
await gameDataSyncManager.setEquipment(3001, 3); // 设置装备数量
|
||||
|
||||
// 6. 数据加载
|
||||
await gameDataSyncManager.loadAllGameData(); // 从云端加载所有数据
|
||||
|
||||
注意:所有方法都返回 Promise<boolean>,true表示成功,false表示失败
|
||||
只有在远程修改成功后,本地数据才会被同步修改
|
||||
*/
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "89c45a3b-d0bf-4e45-9e27-b7d714ba7e29",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ export const getHeroList = (quality:number=0)=>{
|
||||
});
|
||||
|
||||
// 分离拥有和未拥有的英雄
|
||||
const ownedHeros = filteredHeros.filter(item => smc.heros[item.uuid]);
|
||||
const unownedHeros = filteredHeros.filter(item => !smc.heros[item.uuid]);
|
||||
const ownedHeros = filteredHeros.filter(item => smc.heros.some(hero => hero.uuid === item.uuid));
|
||||
const unownedHeros = filteredHeros.filter(item => !smc.heros.some(hero => hero.uuid === item.uuid));
|
||||
|
||||
// 合并列表:拥有的在前,未拥有的在后
|
||||
return [...ownedHeros, ...unownedHeros].map(item => item.uuid);
|
||||
|
||||
@@ -12,8 +12,8 @@ import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ec
|
||||
import { UIID } from "../common/config/GameUIConfig";
|
||||
import { LoadingViewComp } from "./view/LoadingViewComp";
|
||||
import { smc } from "../common/SingletonModuleComp";
|
||||
import { WxCloudApi, UserGameData } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { GameDataSyncManager } from "../common/GameDataSyncManager";
|
||||
import { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||||
|
||||
import { Test } from "../common/Test";
|
||||
|
||||
// import {data} from "../data/data";
|
||||
@@ -141,7 +141,7 @@ export class Initialize extends ecs.Entity {
|
||||
const cloudData = response.data;
|
||||
|
||||
// 3. 用云端数据覆盖本地数据
|
||||
GameDataSyncManager.getInstance().overrideLocalDataWithRemote(cloudData, "云端");
|
||||
|
||||
} else {
|
||||
console.warn("[Initialize]: 云端登录失败:", response.msg);
|
||||
// 登录失败时使用本地数据 游戏需要退出
|
||||
@@ -163,7 +163,7 @@ export class Initialize extends ecs.Entity {
|
||||
const loginResult = new Test().load_data_from_local()
|
||||
|
||||
// 用本地调试数据覆盖客户端数据
|
||||
GameDataSyncManager.getInstance().overrideLocalDataWithRemote(loginResult, "本地调试");
|
||||
|
||||
} catch (error) {
|
||||
console.error("[Initialize]: 本地调试数据加载异常:", error);
|
||||
}
|
||||
|
||||
@@ -1,84 +1,9 @@
|
||||
// 云函数返回类型定义
|
||||
export type CloudReturnType<T = any> = {
|
||||
code: number, // 200成功,其他都是失败
|
||||
msg?: string, // 消息信息
|
||||
data?: T, // 返回数据
|
||||
timestamp?: number, // 时间戳
|
||||
version?: string, // 数据版本
|
||||
execution_time?: number // 执行时间(ms)
|
||||
code: number,// 200成功
|
||||
msg?:string,
|
||||
data?:T
|
||||
}
|
||||
|
||||
// 用户信息类型
|
||||
export type UserInfo = {
|
||||
user_id: string,
|
||||
openid: string,
|
||||
regist_time: number,
|
||||
init_time?: number,
|
||||
data_version?: string,
|
||||
last_save_time?: number
|
||||
}
|
||||
|
||||
// 完整用户数据类型
|
||||
export type UserGameData = UserInfo & {
|
||||
data: GameData,
|
||||
fight_heros: FightHeros,
|
||||
heros: Heros,
|
||||
items: Items,
|
||||
tals: Tals,
|
||||
equips: Equips
|
||||
}
|
||||
|
||||
// 基础游戏数据类型
|
||||
export type GameData = {
|
||||
score: number,
|
||||
mission: number,
|
||||
gold: number,
|
||||
diamond: number,
|
||||
meat: number,
|
||||
exp: number,
|
||||
}
|
||||
|
||||
// 出战英雄类型
|
||||
export type FightHeros = {
|
||||
[position: number]: number // 位置 -> 英雄ID
|
||||
}
|
||||
|
||||
// 英雄数据类型
|
||||
export type HeroData = {
|
||||
uuid: number,
|
||||
lv: number,
|
||||
exp?: number,
|
||||
star?: number,
|
||||
power?: number
|
||||
}
|
||||
|
||||
export type Heros = {
|
||||
[heroId: number]: HeroData
|
||||
}
|
||||
|
||||
// 库存类型
|
||||
export type Items = {
|
||||
[itemId: number]: number
|
||||
}
|
||||
|
||||
export type Tals = {
|
||||
[talId: number]: number
|
||||
}
|
||||
|
||||
export type Equips = {
|
||||
[equipId: number]: number
|
||||
}
|
||||
|
||||
// 版本兼容性检查结果
|
||||
export type VersionCompatibility = {
|
||||
compatible: boolean,
|
||||
needsUpgrade: boolean,
|
||||
message: string
|
||||
}
|
||||
|
||||
// 库存类型枚举
|
||||
export type InventoryType = 'items' | 'tals' | 'equips'
|
||||
|
||||
export class WxCloudApi{
|
||||
/**
|
||||
* @en init the cloud
|
||||
@@ -91,14 +16,30 @@ export class WxCloudApi{
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 认证相关接口 ====================
|
||||
|
||||
/**
|
||||
* @en Login to the cloud
|
||||
* @zh 用户登录,获取完整的用户和游戏数据
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<UserGameData>>>
|
||||
* @zh 登录云
|
||||
* @return 返回结果
|
||||
* 参考:
|
||||
* result.result = {
|
||||
* code: number, // 200成功,其他都是失败
|
||||
* msg: string, // 如果失败,这里是失败原因等信息
|
||||
* data: { // 成功才有
|
||||
* openid: string, // 用户微信平台openid
|
||||
* regist_time: number, // 时间戳,用户注册时间
|
||||
* game_data: object, // 开发者自己保存的数据
|
||||
* }
|
||||
* }
|
||||
* 如果这个泛型令你报错(一般是因为你删了wx.api.d.ts导致),请使用以下签名:
|
||||
* login():Promise<{result: CloudReturnType<{openid: string, regist_time: number, game_data: object}>}>
|
||||
* 或者你是个“不拘小节”的老哥,可以用以下简洁版签名(参考上方的数据结构例子使用即可)
|
||||
* login():Promise<any>
|
||||
*/
|
||||
public static async login(): Promise<CloudCallFunctionResult<CloudReturnType<UserGameData>>> {
|
||||
public static async login(): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
openid:string,
|
||||
regist_time:number,
|
||||
game_data:any
|
||||
}>>>{
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
@@ -108,486 +49,29 @@ export class WxCloudApi{
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get user basic info
|
||||
* @zh 获取用户基本信息(不包含游戏数据)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<UserInfo>>>
|
||||
*/
|
||||
public static async getUserInfo(): Promise<CloudCallFunctionResult<CloudReturnType<UserInfo>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "user_info"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Check data version compatibility
|
||||
* @zh 检查数据版本兼容性
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{user_version: string, current_version: string, compatibility: VersionCompatibility}>>>
|
||||
*/
|
||||
public static async checkVersion(): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
user_version: string,
|
||||
current_version: string,
|
||||
compatibility: VersionCompatibility,
|
||||
init_time: number,
|
||||
regist_time: number,
|
||||
last_save_time: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "version"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Force upgrade user data
|
||||
* @zh 强制升级用户数据结构
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{old_version: string, new_version: string, upgrade_time: number} & UserGameData>>>
|
||||
*/
|
||||
public static async upgradeUserData(): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
old_version: string,
|
||||
new_version: string,
|
||||
upgrade_time: number
|
||||
} & UserGameData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "upgrade"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 基础游戏数据接口 ====================
|
||||
|
||||
/**
|
||||
* @en Get game data
|
||||
* @zh 获取基础游戏数据(金币、钻石、经验等)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<GameData>>>
|
||||
*/
|
||||
public static async getGameData(): Promise<CloudCallFunctionResult<CloudReturnType<GameData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "data_get"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Update game data
|
||||
* @zh 批量更新基础游戏数据
|
||||
* @param data 要更新的数据
|
||||
* @param merge 是否合并更新(默认true)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<GameData>>>
|
||||
*/
|
||||
public static async updateGameData(data: Partial<GameData>, merge: boolean = true): Promise<CloudCallFunctionResult<CloudReturnType<GameData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "data_update",
|
||||
data: data,
|
||||
merge: merge
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Add to game data field
|
||||
* @zh 增加指定字段的数值
|
||||
* @param field 字段名
|
||||
* @param amount 增加的数量
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{field: string, old_value: number, new_value: number, change: number}>>>
|
||||
*/
|
||||
public static async addGameDataField(field:string, amount: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
field: string,
|
||||
old_value: number,
|
||||
new_value: number,
|
||||
change: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "data_add",
|
||||
field: field,
|
||||
amount: amount
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Spend game data field
|
||||
* @zh 消耗指定字段的数值(会检查是否足够)
|
||||
* @param field 字段名
|
||||
* @param amount 消耗的数量
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{field: string, old_value: number, new_value: number, change: number}>>>
|
||||
*/
|
||||
public static async spendGameDataField(field:string|Record<string, number>, amount: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
field: string|Record<string, number>,
|
||||
old_value: number,
|
||||
new_value: number,
|
||||
change: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "data_spend",
|
||||
field: field,
|
||||
amount: amount
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Set game data field
|
||||
* @zh 直接设置某个字段的值
|
||||
* @param field 字段名
|
||||
* @param value 新的值
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{field: string, old_value: any, new_value: any}>>>
|
||||
*/
|
||||
public static async setGameDataField(field: keyof GameData, value: any): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
field: string,
|
||||
old_value: any,
|
||||
new_value: any
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "data_set",
|
||||
field: field,
|
||||
value: value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Reset game data
|
||||
* @zh 重置基础游戏数据为默认值
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<GameData>>>
|
||||
*/
|
||||
public static async resetGameData(): Promise<CloudCallFunctionResult<CloudReturnType<GameData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "data_reset"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 便捷方法:金币操作
|
||||
/**
|
||||
* @en Add gold
|
||||
* @zh 增加金币
|
||||
* @param amount 金币数量
|
||||
*/
|
||||
public static async addGold(amount: number) {
|
||||
return await this.addGameDataField('gold', amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Spend gold
|
||||
* @zh 消耗金币
|
||||
* @param amount 金币数量
|
||||
*/
|
||||
public static async spendGold(amount: number) {
|
||||
return await this.spendGameDataField('gold', amount);
|
||||
}
|
||||
|
||||
// 便捷方法:钻石操作
|
||||
/**
|
||||
* @en Add diamond
|
||||
* @zh 增加钻石
|
||||
* @param amount 钻石数量
|
||||
*/
|
||||
public static async addDiamond(amount: number) {
|
||||
return await this.addGameDataField('diamond', amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Spend diamond
|
||||
* @zh 消耗钻石
|
||||
* @param amount 钻石数量
|
||||
*/
|
||||
public static async spendDiamond(amount: number) {
|
||||
return await this.spendGameDataField('diamond', amount);
|
||||
}
|
||||
|
||||
// ==================== 出战英雄接口 ====================
|
||||
|
||||
/**
|
||||
* @en Get fight heros
|
||||
* @zh 获取出战英雄配置
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<FightHeros>>>
|
||||
*/
|
||||
public static async getFightHeros(): Promise<CloudCallFunctionResult<CloudReturnType<FightHeros>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "fight_heros_get"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Set fight hero
|
||||
* @zh 设置指定位置的出战英雄
|
||||
* @param position 出战位置 (0-4)
|
||||
* @param heroId 英雄ID,0表示移除
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{position: number, old_hero_id: number, new_hero_id: number}>>>
|
||||
*/
|
||||
public static async setFightHero(position: number, heroId: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
position: number,
|
||||
old_hero_id: number,
|
||||
new_hero_id: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "fight_hero_set",
|
||||
position: position,
|
||||
hero_id: heroId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Update fight heros
|
||||
* @zh 批量更新出战英雄配置
|
||||
* @param fightHeros 出战英雄配置对象
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<FightHeros>>>
|
||||
*/
|
||||
public static async updateFightHeros(fightHeros: Partial<FightHeros>): Promise<CloudCallFunctionResult<CloudReturnType<FightHeros>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "fight_heros_update",
|
||||
fight_heros: fightHeros
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get active fight heros
|
||||
* @zh 获取当前出战的英雄列表(不包含空位)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{active_heros: Array<{position: number, hero_id: number, hero_data: HeroData}>, total_count: number}>>>
|
||||
*/
|
||||
public static async getActiveFightHeros(): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
active_heros: Array<{
|
||||
position: number,
|
||||
hero_id: number,
|
||||
hero_data: HeroData
|
||||
}>,
|
||||
total_count: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "fight_heros_active"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Swap fight heros
|
||||
* @zh 交换两个出战位置的英雄
|
||||
* @param position1 位置1 (0-4)
|
||||
* @param position2 位置2 (0-4)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{position1: number, position2: number, hero1_moved_to: number, hero2_moved_to: number}>>>
|
||||
*/
|
||||
public static async swapFightHeros(position1: number, position2: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
position1: number,
|
||||
position2: number,
|
||||
hero1_moved_to: number,
|
||||
hero2_moved_to: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "fight_heros_swap",
|
||||
position1: position1,
|
||||
position2: position2
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Reset fight heros
|
||||
* @zh 重置出战英雄配置为默认值
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<FightHeros>>>
|
||||
*/
|
||||
public static async resetFightHeros(): Promise<CloudCallFunctionResult<CloudReturnType<FightHeros>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "fight_heros_reset"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 英雄管理接口 ====================
|
||||
|
||||
/**
|
||||
* @en Get all heros
|
||||
* @zh 获取用户拥有的所有英雄数据
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<Heros>>>
|
||||
*/
|
||||
public static async getHeros(): Promise<CloudCallFunctionResult<CloudReturnType<Heros>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "heros_get"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get single hero
|
||||
* @zh 获取指定英雄的详细数据
|
||||
* @param heroId 英雄ID
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<HeroData>>>
|
||||
*/
|
||||
public static async getHero(heroId: number): Promise<CloudCallFunctionResult<CloudReturnType<HeroData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "hero_get",
|
||||
hero_id: heroId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Add new hero
|
||||
* @zh 添加新英雄到用户库存
|
||||
* @param heroId 英雄ID
|
||||
* @param heroData 英雄数据(可选)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<HeroData>>>
|
||||
*/
|
||||
public static async addHero(heroId: number, heroData?: Partial<HeroData>): Promise<CloudCallFunctionResult<CloudReturnType<HeroData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "hero_add",
|
||||
hero_id: heroId,
|
||||
hero_data: heroData
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Update hero
|
||||
* @zh 批量更新英雄的多个属性
|
||||
* @param heroId 英雄ID
|
||||
* @param updateData 要更新的属性
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{old_data: HeroData, new_data: HeroData}>>>
|
||||
*/
|
||||
public static async updateHero(heroId: number, updateData: Partial<HeroData>): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
old_data: HeroData,
|
||||
new_data: HeroData
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "hero_update",
|
||||
hero_id: heroId,
|
||||
update_data: updateData
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Set hero property
|
||||
* @zh 设置英雄的单个属性值
|
||||
* @param heroId 英雄ID
|
||||
* @param property 属性名
|
||||
* @param value 属性值
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{hero_id: number, property: string, old_value: any, new_value: any}>>>
|
||||
*/
|
||||
public static async setHeroProperty(heroId: number, property: keyof HeroData, value: any): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
hero_id: number,
|
||||
property: string,
|
||||
old_value: any,
|
||||
new_value: any
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "hero_property_set",
|
||||
hero_id: heroId,
|
||||
property: property,
|
||||
value: value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Level up hero
|
||||
* @zh 英雄升级指定级数
|
||||
* @param heroId 英雄ID
|
||||
* @param levels 升级级数(默认1级)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{hero_id: number, property: string, old_value: number, new_value: number}>>>
|
||||
*/
|
||||
public static async levelUpHero(heroId: number,levels: number = 1): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
hero_id: number,
|
||||
property: string,
|
||||
old_value: number,
|
||||
new_value: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "hero_levelup",
|
||||
hero_id: heroId,
|
||||
levels: levels
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Delete hero
|
||||
* @zh 删除指定英雄(会检查是否在出战阵容中)
|
||||
* @param heroId 英雄ID
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<HeroData>>>
|
||||
*/
|
||||
public static async deleteHero(heroId: number): Promise<CloudCallFunctionResult<CloudReturnType<HeroData>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "hero_delete",
|
||||
hero_id: heroId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get owned hero IDs
|
||||
* @zh 获取用户拥有的所有英雄ID
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{hero_ids: number[], total_count: number}>>>
|
||||
*/
|
||||
public static async getOwnedHeroIds(): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
hero_ids: number[],
|
||||
total_count: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "heros_owned"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 兼容旧接口 ====================
|
||||
|
||||
/**
|
||||
* @en Save game data to the cloud (Legacy)
|
||||
* @zh 把客户端数据写入云,此为全覆盖写入,请自行管理完整数据(兼容旧接口)
|
||||
* @param gameData 游戏数据
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{results: any[]}>>>
|
||||
* @en Save game data to the cloud
|
||||
* @zh 把客户端数据写入云,此为全覆盖写入,请自行管理完整数据
|
||||
* @return 返回结果
|
||||
* 参考:
|
||||
* result.result = {
|
||||
* code: number, // 200成功,其他都是失败
|
||||
* msg: string, // 如果失败,这里是失败原因等信息
|
||||
* data: {
|
||||
* errMsg: "document.update:ok", // 数据库返回结果
|
||||
* stats: {
|
||||
* updated: number, // 更新了几条数据(正常是1)
|
||||
* }
|
||||
* }
|
||||
* 如果这个泛型令你报错(一般是因为你删了wx.api.d.ts导致),请使用以下签名:
|
||||
* save(gameData: any): Promise<{resoult:CloudReturnType}>
|
||||
* 或者你是个“不拘小节”的老哥,可以用以下简洁版签名(参考上方的数据结构例子使用即可)
|
||||
* login():Promise<any>
|
||||
*/
|
||||
public static async save(gameData: any): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
results: any[]
|
||||
errMsg: string,
|
||||
status:{
|
||||
updated: number
|
||||
}
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
@@ -597,284 +81,4 @@ export class WxCloudApi{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 库存管理接口 (items, tals, equips) ====================
|
||||
|
||||
/**
|
||||
* @en Get inventory
|
||||
* @zh 获取指定类型的所有库存数据
|
||||
* @param type 库存类型 ('items', 'tals', 'equips')
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<Items | Tals | Equips>>>
|
||||
*/
|
||||
public static async getInventory(type: InventoryType): Promise<CloudCallFunctionResult<CloudReturnType<Items | Tals | Equips>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_get",
|
||||
type: type
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get inventory item
|
||||
* @zh 获取指定物品的数量
|
||||
* @param type 库存类型
|
||||
* @param itemId 物品ID
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{item_id: number, count: number}>>>
|
||||
*/
|
||||
public static async getInventoryItem(type: InventoryType, itemId: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
item_id: number,
|
||||
count: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_item_get",
|
||||
type: type,
|
||||
item_id: itemId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Add inventory item
|
||||
* @zh 增加指定物品的数量
|
||||
* @param type 库存类型
|
||||
* @param itemId 物品ID
|
||||
* @param count 添加数量
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{item_id: number, old_count: number, new_count: number, added: number}>>>
|
||||
*/
|
||||
public static async addInventoryItem(type: InventoryType, itemId: number, count: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
item_id: number,
|
||||
old_count: number,
|
||||
new_count: number,
|
||||
added: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_item_add",
|
||||
type: type,
|
||||
item_id: itemId,
|
||||
count: count
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Consume inventory item
|
||||
* @zh 消耗指定数量的物品(会检查是否足够)
|
||||
* @param type 库存类型
|
||||
* @param itemId 物品ID
|
||||
* @param count 消耗数量
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{item_id: number, old_count: number, new_count: number, added: number}>>>
|
||||
*/
|
||||
public static async consumeInventoryItem(type: InventoryType, itemId: number, count: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
item_id: number,
|
||||
old_count: number,
|
||||
new_count: number,
|
||||
added: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_item_consume",
|
||||
type: type,
|
||||
item_id: itemId,
|
||||
count: count
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Set inventory item
|
||||
* @zh 直接设置物品的数量
|
||||
* @param type 库存类型
|
||||
* @param itemId 物品ID
|
||||
* @param count 新的数量
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{item_id: number, old_count: number, new_count: number}>>>
|
||||
*/
|
||||
public static async setInventoryItem(type: InventoryType, itemId: number, count: number): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
item_id: number,
|
||||
old_count: number,
|
||||
new_count: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_item_set",
|
||||
type: type,
|
||||
item_id: itemId,
|
||||
count: count
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Update inventory
|
||||
* @zh 批量更新多个物品的数量
|
||||
* @param type 库存类型
|
||||
* @param data 更新数据对象
|
||||
* @param merge 是否合并更新(默认true)
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<Items | Tals | Equips>>>
|
||||
*/
|
||||
public static async updateInventory(type: InventoryType, data: Partial<Items | Tals | Equips>, merge: boolean = true): Promise<CloudCallFunctionResult<CloudReturnType<Items | Tals | Equips>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_update",
|
||||
type: type,
|
||||
data: data,
|
||||
merge: merge
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Reset inventory
|
||||
* @zh 重置指定类型的库存为默认值
|
||||
* @param type 库存类型
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<Items | Tals | Equips>>>
|
||||
*/
|
||||
public static async resetInventory(type: InventoryType): Promise<CloudCallFunctionResult<CloudReturnType<Items | Tals | Equips>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_reset",
|
||||
type: type
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get owned items
|
||||
* @zh 获取数量大于0的物品列表
|
||||
* @param type 库存类型
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<{owned_items: Array<{item_id: number, count: number}>, total_types: number}>>>
|
||||
*/
|
||||
public static async getOwnedItems(type: InventoryType): Promise<CloudCallFunctionResult<CloudReturnType<{
|
||||
owned_items: Array<{
|
||||
item_id: number,
|
||||
count: number
|
||||
}>,
|
||||
total_types: number
|
||||
}>>> {
|
||||
return await wx?.cloud.callFunction({
|
||||
name: 'cocos_cloud',
|
||||
data: {
|
||||
cmd: "inventory_owned",
|
||||
type: type
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 便捷方法:道具操作 ====================
|
||||
|
||||
/**
|
||||
* @en Get items
|
||||
* @zh 获取所有道具数据
|
||||
*/
|
||||
public static async getItems() {
|
||||
return await this.getInventory('items');
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Add item
|
||||
* @zh 添加道具
|
||||
* @param itemId 道具ID
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async addItem(itemId: number, count: number) {
|
||||
return await this.addInventoryItem('items', itemId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Consume item
|
||||
* @zh 消耗道具
|
||||
* @param itemId 道具ID
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async consumeItem(itemId: number, count: number) {
|
||||
return await this.consumeInventoryItem('items', itemId, count);
|
||||
}
|
||||
|
||||
// ==================== 便捷方法:天赋操作 ====================
|
||||
|
||||
/**
|
||||
* @en Get talents
|
||||
* @zh 获取所有天赋数据
|
||||
*/
|
||||
public static async getTalents() {
|
||||
return await this.getInventory('tals');
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Add talent
|
||||
* @zh 添加天赋点
|
||||
* @param talId 天赋ID
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async addTalent(talId: number, count: number) {
|
||||
return await this.addInventoryItem('tals', talId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Consume talent
|
||||
* @zh 消耗天赋点
|
||||
* @param talId 天赋ID
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async consumeTalent(talId: number, count: number) {
|
||||
return await this.consumeInventoryItem('tals', talId, count);
|
||||
}
|
||||
|
||||
// ==================== 便捷方法:装备操作 ====================
|
||||
|
||||
/**
|
||||
* @en Get equipments
|
||||
* @zh 获取所有装备数据
|
||||
*/
|
||||
public static async getEquipments() {
|
||||
return await this.getInventory('equips');
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Add equipment
|
||||
* @zh 添加装备
|
||||
* @param equipId 装备ID
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async addEquipment(equipId: number, count: number) {
|
||||
return await this.addInventoryItem('equips', equipId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Consume equipment
|
||||
* @zh 消耗装备
|
||||
* @param equipId 装备ID
|
||||
* @param count 数量
|
||||
*/
|
||||
public static async consumeEquipment(equipId: number, count: number) {
|
||||
return await this.consumeInventoryItem('equips', equipId, count);
|
||||
}
|
||||
|
||||
// ==================== 其他便捷方法 ====================
|
||||
|
||||
/**
|
||||
* @en Load game data (Legacy compatible)
|
||||
* @zh 加载游戏数据(兼容旧接口)
|
||||
*/
|
||||
public static async load() {
|
||||
return await this.login();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Get all game data at once
|
||||
* @zh 一次性获取所有游戏数据
|
||||
* @return Promise<CloudCallFunctionResult<CloudReturnType<UserGameData>>>
|
||||
*/
|
||||
public static async getAllGameData(): Promise<CloudCallFunctionResult<CloudReturnType<UserGameData>>> {
|
||||
return await this.login();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user