云环境和本地调试 添加

This commit is contained in:
2025-08-18 17:00:40 +08:00
parent addc61e2a9
commit a824d9a124
155 changed files with 6531 additions and 997 deletions

View File

@@ -0,0 +1,443 @@
# WxCloudApi 客户端使用指南
## 📋 概述
`WxCloudApi` 是游戏客户端与微信云函数交互的统一接口层,提供了完整的类型安全和便捷的调用方法。
## 🚀 快速开始
### 1. 初始化
```typescript
// 在游戏启动时初始化云环境
WxCloudApi.init("your-cloud-env-id");
```
### 2. 用户登录
```typescript
// 用户登录,获取完整的游戏数据
const loginResult = await WxCloudApi.login();
if (loginResult.result.code === 200) {
const userData = loginResult.result.data;
console.log("用户ID:", userData.user_id);
console.log("金币:", userData.data.gold);
console.log("出战英雄:", userData.fight_heros);
}
```
## 🎮 基础游戏数据操作
### 获取游戏数据
```typescript
const gameData = await WxCloudApi.getGameData();
if (gameData.result.code === 200) {
console.log("当前金币:", gameData.result.data.gold);
console.log("当前钻石:", gameData.result.data.diamond);
}
```
### 增加金币
```typescript
// 方式1使用便捷方法
const result = await WxCloudApi.addGold(100);
// 方式2使用通用方法
const result = await WxCloudApi.addGameDataField('gold', 100);
if (result.result.code === 200) {
console.log("金币增加成功:", result.result.data);
}
```
### 消耗金币
```typescript
// 消耗金币(会检查是否足够)
const result = await WxCloudApi.spendGold(50);
if (result.result.code === 200) {
console.log("金币消耗成功");
} else if (result.result.code === -6) {
console.log("金币不足");
}
```
### 批量更新游戏数据
```typescript
const result = await WxCloudApi.updateGameData({
gold: 1000,
diamond: 200,
exp: 500
}, true); // true表示合并更新
```
## ⚔️ 出战英雄管理
### 获取出战英雄配置
```typescript
const fightHeros = await WxCloudApi.getFightHeros();
if (fightHeros.result.code === 200) {
console.log("出战配置:", fightHeros.result.data);
// { 0: 5001, 1: 5005, 2: 0, 3: 0, 4: 0 }
}
```
### 设置出战英雄
```typescript
// 将英雄5007设置到位置2
const result = await WxCloudApi.setFightHero(2, 5007);
if (result.result.code === 200) {
console.log("设置成功:", result.result.data);
}
// 移除位置2的英雄
await WxCloudApi.setFightHero(2, 0);
```
### 批量更新出战英雄
```typescript
const result = await WxCloudApi.updateFightHeros({
0: 5001,
1: 5005,
2: 5007
});
```
### 交换英雄位置
```typescript
// 交换位置0和位置2的英雄
const result = await WxCloudApi.swapFightHeros(0, 2);
```
### 获取激活的出战英雄
```typescript
const activeHeros = await WxCloudApi.getActiveFightHeros();
if (activeHeros.result.code === 200) {
const heros = activeHeros.result.data.active_heros;
heros.forEach(hero => {
console.log(`位置${hero.position}: 英雄${hero.hero_id}, 等级${hero.hero_data.lv}`);
});
}
```
## 🦸 英雄管理
### 获取所有英雄
```typescript
const heros = await WxCloudApi.getHeros();
if (heros.result.code === 200) {
Object.entries(heros.result.data).forEach(([heroId, heroData]) => {
console.log(`英雄${heroId}: 等级${heroData.lv}, 战力${heroData.power}`);
});
}
```
### 添加新英雄
```typescript
// 添加英雄(使用默认属性)
const result = await WxCloudApi.addHero(5008);
// 添加英雄(自定义属性)
const result = await WxCloudApi.addHero(5008, {
lv: 5,
exp: 1000,
star: 2,
power: 150
});
```
### 英雄升级
```typescript
// 升级1级
const result = await WxCloudApi.levelUpHero(5001);
// 升级3级
const result = await WxCloudApi.levelUpHero(5001, 3);
if (result.result.code === 200) {
console.log("升级成功:", result.result.data);
}
```
### 更新英雄属性
```typescript
// 批量更新
const result = await WxCloudApi.updateHero(5001, {
lv: 15,
exp: 2000,
star: 3
});
// 单个属性更新
const result = await WxCloudApi.setHeroProperty(5001, 'power', 200);
```
### 删除英雄
```typescript
const result = await WxCloudApi.deleteHero(5008);
if (result.result.code === -8) {
console.log("英雄正在出战中,无法删除");
}
```
## 🎒 库存管理
### 道具操作
```typescript
// 获取所有道具
const items = await WxCloudApi.getItems();
// 添加道具
const result = await WxCloudApi.addItem(1001, 10);
// 消耗道具
const result = await WxCloudApi.consumeItem(1001, 3);
// 获取指定道具数量
const itemInfo = await WxCloudApi.getInventoryItem('items', 1001);
console.log("道具1001数量:", itemInfo.result.data.count);
```
### 天赋操作
```typescript
// 获取所有天赋
const talents = await WxCloudApi.getTalents();
// 添加天赋点
const result = await WxCloudApi.addTalent(1001, 1);
// 消耗天赋点
const result = await WxCloudApi.consumeTalent(1001, 1);
```
### 装备操作
```typescript
// 获取所有装备
const equipments = await WxCloudApi.getEquipments();
// 添加装备
const result = await WxCloudApi.addEquipment(1001, 2);
// 消耗装备
const result = await WxCloudApi.consumeEquipment(1001, 1);
```
### 批量库存操作
```typescript
// 批量更新道具
const result = await WxCloudApi.updateInventory('items', {
1001: 20,
1002: 15,
1003: 10
});
// 获取拥有的道具列表(数量>0
const ownedItems = await WxCloudApi.getOwnedItems('items');
console.log("拥有的道具:", ownedItems.result.data.owned_items);
```
## 🔐 认证和版本管理
### 检查版本信息
```typescript
const versionInfo = await WxCloudApi.checkVersion();
if (versionInfo.result.code === 200) {
const compatibility = versionInfo.result.data.compatibility;
if (compatibility.needsUpgrade) {
console.log("需要升级数据:", compatibility.message);
// 可以选择自动升级
await WxCloudApi.upgradeUserData();
}
}
```
### 获取用户基本信息
```typescript
const userInfo = await WxCloudApi.getUserInfo();
if (userInfo.result.code === 200) {
console.log("用户信息:", userInfo.result.data);
}
```
## 🛠️ 错误处理
### 统一错误处理
```typescript
async function handleApiCall<T>(apiCall: Promise<CloudCallFunctionResult<CloudReturnType<T>>>) {
try {
const result = await apiCall;
const response = result.result;
switch (response.code) {
case 200:
console.log("操作成功:", response.data);
return response.data;
case -3:
console.error("参数错误:", response.msg);
break;
case -4:
console.error("用户未找到:", response.msg);
break;
case -5:
console.error("系统错误:", response.msg);
break;
case -6:
console.error("资源不足:", response.msg);
break;
default:
console.error("未知错误:", response.code, response.msg);
}
} catch (error) {
console.error("网络错误:", error);
}
return null;
}
// 使用示例
const gameData = await handleApiCall(WxCloudApi.getGameData());
```
### 资源不足处理示例
```typescript
async function trySpendGold(amount: number): Promise<boolean> {
const result = await WxCloudApi.spendGold(amount);
const response = result.result;
if (response.code === 200) {
console.log("金币消耗成功");
return true;
} else if (response.code === -6) {
console.log("金币不足,当前:", response.data?.current, "需要:", response.data?.required);
// 可以提示用户充值或其他操作
return false;
}
console.error("消耗失败:", response.msg);
return false;
}
```
## 📊 完整游戏流程示例
```typescript
export class GameDataManager {
// 游戏启动时初始化
public static async initialize() {
// 1. 初始化云环境
WxCloudApi.init("your-cloud-env-id");
// 2. 用户登录
const loginResult = await WxCloudApi.login();
if (loginResult.result.code !== 200) {
console.error("登录失败");
return false;
}
const userData = loginResult.result.data;
console.log("登录成功,用户数据:", userData);
// 3. 检查版本兼容性
const versionResult = await WxCloudApi.checkVersion();
if (versionResult.result.data.compatibility.needsUpgrade) {
console.log("正在升级数据...");
await WxCloudApi.upgradeUserData();
}
return true;
}
// 战斗结算
public static async settleBattle(rewards: {gold: number, exp: number, items: {[id: number]: number}}) {
try {
// 1. 增加金币和经验
await WxCloudApi.addGold(rewards.gold);
await WxCloudApi.addGameDataField('exp', rewards.exp);
// 2. 增加道具奖励
for (const [itemId, count] of Object.entries(rewards.items)) {
await WxCloudApi.addItem(parseInt(itemId), count);
}
console.log("战斗结算完成");
return true;
} catch (error) {
console.error("战斗结算失败:", error);
return false;
}
}
// 英雄升级
public static async upgradeHero(heroId: number, levels: number): Promise<boolean> {
// 假设每级需要100金币
const cost = levels * 100;
// 1. 检查金币是否足够
const spendResult = await WxCloudApi.spendGold(cost);
if (spendResult.result.code !== 200) {
console.log("金币不足,无法升级");
return false;
}
// 2. 升级英雄
const upgradeResult = await WxCloudApi.levelUpHero(heroId, levels);
if (upgradeResult.result.code === 200) {
console.log("英雄升级成功");
return true;
} else {
// 升级失败,退还金币
await WxCloudApi.addGold(cost);
console.error("英雄升级失败");
return false;
}
}
// 设置出战阵容
public static async setupFightTeam(heroIds: number[]): Promise<boolean> {
const fightHeros: Partial<FightHeros> = {};
// 最多5个位置
for (let i = 0; i < Math.min(heroIds.length, 5); i++) {
fightHeros[i] = heroIds[i];
}
// 清空剩余位置
for (let i = heroIds.length; i < 5; i++) {
fightHeros[i] = 0;
}
const result = await WxCloudApi.updateFightHeros(fightHeros);
return result.result.code === 200;
}
}
```
## 🎯 最佳实践
### 1. 错误处理
- 始终检查返回的 `code` 字段
- 对不同错误码进行相应处理
- 网络错误要有重试机制
### 2. 性能优化
- 批量操作优于多次单独操作
- 合理使用缓存,避免频繁请求
- 关键操作添加加载状态
### 3. 数据一致性
- 重要操作后及时同步本地数据
- 使用乐观锁机制处理并发
- 定期全量同步数据
### 4. 用户体验
- 操作前进行客户端验证
- 提供友好的错误提示
- 重要操作添加确认对话框
---
**版本**: 1.0.0
**更新时间**: 2024年
**维护者**: 游戏开发团队

View File

@@ -0,0 +1,11 @@
{
"ver": "1.0.1",
"importer": "text",
"imported": true,
"uuid": "f07f89b5-9257-48a8-a6ab-06f662e5e726",
"files": [
".json"
],
"subMetas": {},
"userData": {}
}

View File

@@ -1,9 +1,92 @@
// 云函数返回类型定义
export type CloudReturnType<T = any> = {
code: number,// 200成功
msg?:string,
data?:T
code: number, // 200成功,其他都是失败
msg?: string, // 消息信息
data?: T, // 返回数据
timestamp?: number, // 时间戳
version?: string, // 数据版本
execution_time?: number // 执行时间(ms)
}
// 用户信息类型
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,
ghstone: number, // 绿色英雄石
bhstone: number, // 蓝色英雄石
phlestone: number, // 紫色英雄石
rhstone: number, // 红色英雄石
herocard: number, // 英雄卡
ckey: number, // 铜钥匙
skey: number, // 银钥匙
gkey: 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
@@ -16,30 +99,14 @@ export class WxCloudApi{
});
}
// ==================== 认证相关接口 ====================
/**
* @en Login to the cloud
* @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>
* @zh 用户登录,获取完整的用户和游戏数据
* @return Promise<CloudCallFunctionResult<CloudReturnType<UserGameData>>>
*/
public static async login(): Promise<CloudCallFunctionResult<CloudReturnType<{
openid:string,
regist_time:number,
game_data:any
}>>>{
public static async login(): Promise<CloudCallFunctionResult<CloudReturnType<UserGameData>>> {
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
data: {
@@ -49,29 +116,488 @@ export class WxCloudApi{
}
/**
* @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>
* @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: keyof GameData, 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: keyof GameData, 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_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 英雄ID0表示移除
* @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, exp:number,gold: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,
exp: exp,
gold:gold,
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[]}>>>
*/
public static async save(gameData: any): Promise<CloudCallFunctionResult<CloudReturnType<{
errMsg: string,
status:{
updated: number
}
results: any[]
}>>> {
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
@@ -81,4 +607,284 @@ 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();
}
}

View File

@@ -1,5 +1,5 @@
{
"ver": "4.0.23",
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "0b5acaf6-aa85-428d-a38c-97bf645cc1db",

View File

@@ -1,5 +1,5 @@
{
"ver": "4.0.23",
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "845c60fa-24b5-49c9-85d3-ef55ed459817",