重构了云函数
This commit is contained in:
@@ -3,10 +3,22 @@ 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 { WxCloudApi, UserGameData } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { Test } from "./Test";
|
||||
import { GameEvent } from "./config/GameEvent";
|
||||
|
||||
/**
|
||||
* 用远程数据覆盖本地数据(统一方法)
|
||||
* @param remoteData 远程数据(云端或本地调试)
|
||||
*/
|
||||
interface GameDate{
|
||||
gold:number,
|
||||
heros:any,
|
||||
fight_hero:number
|
||||
}
|
||||
interface CloudData {
|
||||
openid: string;
|
||||
data: GameDate;
|
||||
}
|
||||
/** 游戏模块 */
|
||||
@ecs.register('SingletonModule')
|
||||
export class SingletonModuleComp extends ecs.Comp {
|
||||
@@ -14,6 +26,7 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
initialize: Initialize = null!;
|
||||
/** 游戏地图 */
|
||||
map: GameMap = null!;
|
||||
openid:string=''
|
||||
mission:any={
|
||||
status:0, //0:未开始 1:进行中 2:胜利 3:失败
|
||||
play:false,
|
||||
@@ -32,10 +45,7 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
task:0,
|
||||
}
|
||||
fight_hero: number = 5001; // 单个出战英雄
|
||||
heros:any = {
|
||||
5001:{uuid:5001,lv:1},
|
||||
5005:{uuid:5005,lv:1},
|
||||
};
|
||||
heros:any= [5001,5002]
|
||||
monsters:any = [];
|
||||
vmdata: any = {
|
||||
game_over:false,
|
||||
@@ -76,148 +86,105 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
//存储到远程服务器 后续再添加
|
||||
}
|
||||
|
||||
//调试用
|
||||
syncDataFromLocal(){
|
||||
if(this.isWxClient()) return
|
||||
const loginResult = new Test().load_data_from_local()
|
||||
this.overrideLocalDataWithRemote(loginResult, "本地调试");
|
||||
updateCloudData(){
|
||||
let gemeDate=this.getGameDate()
|
||||
WxCloudApi.save(gemeDate).then((result) => {
|
||||
if(result.result.code === 200) {
|
||||
return true
|
||||
} else {
|
||||
console.warn(`[SMC]: 游戏数据增加失败: ${result.result.msg}`);
|
||||
return false
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(`[SMC]: 增加游戏数据异常:`, error);
|
||||
return false
|
||||
});
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 用远程数据覆盖本地数据(统一方法)
|
||||
* @param remoteData 远程数据(云端或本地调试)
|
||||
* @param dataSource 数据源描述
|
||||
*/
|
||||
async overrideLocalDataWithRemote(remoteData: UserGameData, dataSource: string) {
|
||||
getCloudData(){
|
||||
WxCloudApi.get().then(async (result) => {
|
||||
if(result.result.code === 200) {
|
||||
let data=result.result.data
|
||||
this.overrideLocalDataWithRemote(data)
|
||||
return true
|
||||
} else {
|
||||
console.warn(`[SMC]: 游戏数据增加失败: ${result.result.msg}`);
|
||||
return false
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(`[SMC]: 获取游戏数据异常:`, error);
|
||||
});
|
||||
}
|
||||
public async overrideLocalDataWithRemote(CloudData) {
|
||||
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 (CloudData.openid) {
|
||||
this.openid=CloudData.openid
|
||||
}
|
||||
|
||||
// 直接覆盖出战英雄配置
|
||||
if (remoteData.fight_heros) {
|
||||
this.fight_hero = remoteData.fight_heros[0] || this.fight_hero;
|
||||
}
|
||||
|
||||
// 直接覆盖英雄数据
|
||||
if (remoteData.heros) {
|
||||
this.heros = { ...remoteData.heros };
|
||||
if (CloudData.data) {
|
||||
if(CloudData.data.gold) this.vmdata.gold=CloudData.data.gold
|
||||
if(CloudData.data.heros) this.heros=CloudData.data.heros
|
||||
if(CloudData.data.fight_hero) this.fight_hero=CloudData.data.fight_hero
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[SMC]: ${dataSource}数据覆盖失败:`, error);
|
||||
console.error(`[SMC]: 数据覆盖失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
addHero(hero_uuid:number,autoSave:boolean=true){
|
||||
if(this.isWxClient()){
|
||||
// 适配原有接口,保持与云函数的兼容性
|
||||
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
|
||||
});
|
||||
getGameDate(){
|
||||
return {gold:this.vmdata.gold,heros:this.heros,fight_hero:this.fight_hero}
|
||||
}
|
||||
addHero(hero_uuid:number){
|
||||
if(this.heros.indexOf(hero_uuid)==-1){
|
||||
this.heros.push(hero_uuid)
|
||||
}
|
||||
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
|
||||
if(this.isWxClient()){
|
||||
let res = this.updateCloudData()
|
||||
if (res){
|
||||
return true
|
||||
}else{
|
||||
// 同步不成功,删除uuid
|
||||
this.heros.splice(this.heros.indexOf(hero_uuid), 1);
|
||||
oops.gui.toast("数据同步失败,已回滚操作");
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 设置单个出战英雄
|
||||
setFightHero(heroId: number, autoSave: boolean = true) {
|
||||
updateFihgtHero(heroId: number) {
|
||||
this.fight_hero = heroId;
|
||||
if (this.isWxClient()) {
|
||||
// 适配原有接口,保持与云函数的兼容性
|
||||
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()
|
||||
});
|
||||
if(this.isWxClient()){
|
||||
let res = this.updateCloudData()
|
||||
if (res){
|
||||
return true
|
||||
}else{
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 获取出战英雄
|
||||
getFightHero(): number {
|
||||
return this.fight_hero;
|
||||
}
|
||||
|
||||
getHasHeroUUID(){
|
||||
let heros=this.heros
|
||||
let heros_uuid=[]
|
||||
for(let key in heros){
|
||||
heros_uuid.push(heros[key].uuid)
|
||||
updateGold(gold:number){
|
||||
this.vmdata.gold += gold;
|
||||
if(this.isWxClient()){
|
||||
let res = this.updateCloudData()
|
||||
if (res){
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}else{
|
||||
this.vmdata.gold -= gold
|
||||
return false
|
||||
}
|
||||
}
|
||||
return heros_uuid
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}
|
||||
|
||||
error(){
|
||||
oops.gui.toast("数据处理异常,请重试或重新登录")
|
||||
}
|
||||
|
||||
addGold(gold:number,autoSave:boolean=true){
|
||||
if(this.isWxClient()){
|
||||
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.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()){
|
||||
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.vmdata.gold -= gold;
|
||||
this.data.gold = this.vmdata.gold; // 同步到data字段
|
||||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user