| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780 |
- var reGameStates = require('GameStates');
- var AConfig = require('../Config');
- import utils from "../Network/netUtils";
- // import GameNet from "../Network/GameNet"
- // import gameToast from "../Network/gameToast"
- //全局数据类
- cc.Class({
- extends: cc.Component,
- properties: {
- //记录全部道路的index
- GameData_highwayIndex: {
- default: [],
- type: [cc.Integer],
- visible: false,
- serializable: false,
- },
- //记录建筑物存储的信息
- GameData_buildings: {
- default: [],
- visible: false,
- serializable: false,
- },
- HighWayPrefabs: cc.Prefab,
- //MyMapNode
- //建筑物的节点
- BuildingsParent: {
- default: null,
- type: cc.Node,
- },
- ManageUI: {
- default: null,
- type: cc.Node,
- },
- GameVersion: {
- default: -1,
- type: cc.Integer,
- },
- //读取数据
- readData: {
- default: null,
- visible: false,
- },
- /**
- * 接口
- * todo cnt和snb 由钱包读取
- */
- CNT: {
- default: 0,
- visible: false,
- },
- SNB: {
- default: 0,
- visible: false,
- },
- /**
- * 用户属性
- */
- //体力
- strength: {
- default: 0,
- visible: false,
- },
- //幸运值
- lucky: {
- default: 0,
- visible: false,
- },
- //站内收益
- CNTDrawBalance: {
- default: 0,
- visible: false,
- },
- //读取的土地文件
- ConfigLand: {
- default: null,
- visible: false
- },
- /**
- * 制作计时器
- */
- toast: null,
- scheduleObj: null,
- toastCallback: null,
- isStart: true,
- isInit: false,
- /**
- * 限制重复触发支付, 正在支付中转态,
- */
- isPlayingCnt: false,
- isPlayingSnb: false,
- isPlayingLucky:false,
- /**
- * 处理请求
- */
- //现在多次触发
- isOnAddFruit: false,
- isOnSaleFruit: false,
- isOnGrantFruit: false,
- isExchangeSeed: false,
- //是否申请
- isApplying: false,
- //自动存储数据
- AutoSaveData: null
- },
- onLoad() {
- //初始化GameData全局变量
- GlobalD.GameData = this;
- this.GameConfig();
- },
- GameConfig() {
- this.AddBuildingCost = 5;
- this.RemoveBuildingCost = 5;
- //
- // this.Dapp = {
- // UserInfo: null
- // }
- },
- start() {
- cc.loader.loadRes("prefab/gameToast", function (err, texture) {
- this.toast = cc.instantiate(texture);
- this.toast.parent = cc.find("Canvas/UICamera");
- this.toast.zIndex = 999;
- this.toast.active = false;
- }.bind(this));
- this._tiledMap = GlobalD.TiledMap._tiledMap;
- //调用初始化dapp
- this.isDebugMode(GlobalD.dapp);
- },
- /**
- * 根据 不存在dapp 的话,使用本地测试
- * @param {*} bInit
- */
- isDebugMode: function (bInit) {
- if (bInit) {
- //读取可领取的snb
- this.onGetCntToSnb((value) => {
- // console.log("当前用户转换的snb", value);
- if (value.data != null) {
- this.ManageUI.getComponent("ManageUI").onSetVillageReceivesSNBViewInfo(value.data);
- }
- })
- //读取网络数据
- try {
- GlobalD.dapp.cntBalance().then((cntBalance) => {
- //会延迟返回
- // console.log("获取cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
- GlobalD.GameData.SetCNT(cntBalance);
- });
- } catch (err) {
- console.error(err) // 初始化失败,运行环境不是钱包环境
- }
- //登录时候已经初始化好snb了
- this.SNB = GlobalD.UserInfo.snb + GlobalD.UserInfo.snbPart;
- // console.log("this.SNB==", this.SNB);
- this.readData = userData.readData;
- // cc.log('playerPullInfo 读取到数据?:', this.readData)
- this.InitNextworkData();
- this.InitSceneInfo();
- // 自动存储数据
- /**
- * remove version: todo 去除自动存储
- */
- // this.AutoSaveData = function () {
- // this.pushData(true);
- // };
- // this.schedule(this.AutoSaveData, 15);
- } else {
- // 清除
- cc.log("本地数据重新开始,并且不初始化");
- // this.onClearAllData();
- this.Init();
- this.InitSceneInfo();
- // this.AutoSaveData = function () {
- // this.pushData(false);
- // };
- // this.schedule(this.AutoSaveData, 10);
- }
- },
- getData: function () {
- //如果开局没有读取到网络数据,就这里初始化
- if (userData.readData == null) {
- cc.log("本地数据:", cc.sys.localStorage.getItem('userdata'));
- let userdata = cc.sys.localStorage.getItem('userdata');
- if (userdata) {
- this.readData = JSON.parse(userdata);
- }
- this.Init();
- this.InitSceneInfo();
- } else {
- this.readData = userData.readData;
- cc.log('读取到数据?:', this.readData)
- this.Init();
- this.InitSceneInfo();
- }
- },
- /**
- * 处理用户生成的场景,新增处理跳转其他用户界面,
- * 其他用户界面 不同点:
- * 1.土地信息:显示对应用户的土地信息
- * 2.界面按钮:显示对应用户所需要的操作按钮
- */
- InitSceneInfo() {
- //根据顺序生成
- //remove version:todo 去除初始化时间ui
- // cc.find("GameNode/ManageTimer").getComponent('ManageTimer').Init();
- //初始化金钱ui,gold,diamond, cnt,snb,
- cc.find("GameNode/ManageGolden").getComponent('ManageGolden').InitManageGlodenUI();
- //领取ui
- this.ManageUI.getComponent("ManageUI").DiamondNowlabel.string = this.GetDiamond() + '/30000'
- //设置体力值
- this.SetStrength(GlobalD.PlayerAttribute.currentStrength);
- //设置幸运值
- this.SetLucky(GlobalD.PlayerAttribute.lucky);
- // todo 去除公路 Barrier
- // GlobalD.TiledMap.onInitSolid();
- //初始化生成房屋
- /**
- * dapp设定: 如果固定土地冲突,删除冲突的建筑和回收仓库
- * 新增:
- * 当进入其他用户农场时候,只处理土地显示信息。其他的都隐藏起来,不进行操作
- * 更新的数据:1.土地
- * 隐藏的数据:1.隐藏游客
- * 2.隐藏用户当前建筑
- * 3.隐藏当前用户菜单,显示对应农场按钮
- */
- this.ManageUI.getComponent('ManageBuildings').InitBuildings();
- //初始化道路
- /**
- * 原本游戏设定是先生产道路,现在为了方便处理等房屋生产后,再处理公路
- * dapp设定: 如果固定土地冲突,删除对应的公路
- * remove version:todo 去除生成公路
- */
- // this.onSpawnHighway();
- //初始化生成人物
- /**
- * dapp设定: 如果固定土地位置冲突,把人物生成位置放置到主路
- * onSpawnWorkerAIFromStoredData 此函数处理
- *
- */
- // remove version:todo 去除生成公人
- // cc.find('GameNode/ManageWorker').getComponent('ManageWorker').InitWorkerAI();
- /**
- * remove version:todo 去除新手教程
- */
- //初始化新手教学
- // let _BFirstLoadGame = this.readData ? this.readData.BFirstLoadGame : 0;
- // GlobalD.ManageTask.InitTask(_BFirstLoadGame);
- this.ManageUI.getComponent('ManageUI').Init();
- },
- InitNextworkData() {
- // console.log("this.readData", this.readData);
- this.Golden = this.readData.Golden;
- this.Diamond = this.readData.Diamond;
- this.shareGive = this.readData.shareGive;
- //签到分享给钻石金币
- this.signInGive = this.readData.signInGive;
- //每个月 给的 低保
- this.EveryGive = this.readData.EveryGive;
- //公共分享
- this.publicGive = this.readData.publicGive;
- // cc.log('初始化网络数据');
- //读取日期数据
- this.GameYear = this.readData.GameYear;
- this.GameMonth = this.readData.GameMonth;
- this.GameDay = this.readData.GameDay;
- //读取金币
- this.Golden = this.readData.Golden;
- //读取钻石
- this.Diamond = this.readData.Diamond;
- // cc.log('this.readData.Diamond', this.readData.Diamond);
- //读取工人等级
- this.WorkerLV = this.readData.WorkerLV;
- //工人数量
- this.WorkerNum = this.readData.WorkerNum;
- //工人容量
- this.WorkerCapacity = this.readData.WorkerCapacity;
- this.GameDate = this.readData.GameDate;
- this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
- this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
- this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
- this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
- //面板状态
- this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
- //解锁设置为 不用解锁
- // this.BuildingLockStateArray.forEach((value, index, array) => {
- // array[index] = 1;
- // })
- this.BuildingLockStateArray = this.readData.BuildingLockStateArray;//.split('_');
- this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
- this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
- this.FoodTradeState = this.readData.FoodTradeState;
- this.WoodTradeState = this.readData.WoodTradeState;
- this.MineralTradeState = this.readData.MineralTradeState;
- //工人工作信息
- if (this.readData.characterInfoArray) {
- this.CharacterInfoArray = this.readData.characterInfoArray;
- } else {
- this.CharacterInfoArray = [];
- }
- //道路数据
- if (this.readData.highwayIndex)
- this.GameData_highwayIndex = this.readData.highwayIndex;
- //建筑物数据
- if (this.readData.buildingsInfo)
- this.GameData_buildings = this.readData.buildingsInfo;
- this.LotteryTimes = this.readData.LotteryTimes;
- //任务
- if (this.readData.TaskIconCountClick)
- task.TaskIconCountClick = this.readData.TaskIconCountClick;
- },
- /**
- * 初始化
- */
- Init: function () {
- let _BFirstLoadGame = false;// this.readData ? this.readData.BFirstLoadGame : 0;
- //date
- this.GameYear = 0;
- this.GameMonth = 0;
- this.GameDay = 0;
- this.GameDate = '0000/00/01';
- /**
- * 新增 CNT 和 SNB
- */
- this.CNT = 0;
- this.SNB = 0;
- this.Golden = 2000;
- this.Diamond = 100;
- this.WorkerLV = 0;
- this.WorkerNum = 0;
- this.CharacterInfoArray = [];
- this.WorkerCapacity = 5;
- this.TerritoryStateArray = [1, 1, 1, 1, 1, 1, 1, 1];//测试,全开地图
- /**
- * 添加建筑往后添加,到时候ManageBuildings 脚本 会初始化根据顺序
- * BuildingStateArray---相关 GetBuildingStateArray 获取建筑状态
- * BuildingNumArray ---相关 GetBuildingNumArray 获取建筑数量
- * BuildingFrameArray ---相关 ManageUI 添加面板是否解锁之类的相关预制
- */
- this.BuildingStateArray = [
- 1, //公路
- 1, //路铲
- 1, //拆迁
- 1, //农舍
- 1, //单元楼
- 1, //别墅
- 1, //农田
- 1,//伐木场
- 1, //矿坑
- 1, //加工厂
- 1,//冷饮摊
- 1,//贩卖机
- 1,//面包房
- 1,//早餐车
- 1, //饮茶店
- 1,//点心店
- 1, //美食店
- 1,//西餐厅
- 1, //花店
- 1, //美发店
- 1,//洋装店
- 1, //珠宝店
- 1, //电影院
- 1,//路灯
- 1, //绿化带
- 1, //花坛
- 1,//喷泉
- 1, //警察局
- 1,//游乐场
- 1, //蓝色城堡
- 1, //粉色城堡
- 1, //Holy Farmland
- 1 //Holy Farmland seed
- ];
- this.BuildingLockStateArray = [
- 1, //公路
- 1, //路铲
- 1, //拆迁
- 1, //农舍
- 0, //单元楼
- 0, //别墅
- 1, //农田
- 0,//伐木场
- 0, //矿坑
- 1, //加工厂
- 0,//冷饮摊
- 0,//贩卖机
- 0,//面包房
- 0,//早餐车
- 0, //饮茶店
- 0,//点心店
- 0, //美食店
- 0,//西餐厅
- 0, //花店
- 0, //美发店
- 0,//洋装店
- 0, //珠宝店
- 0, //电影院
- 1,//路灯
- 1, //绿化带
- 1, //花坛
- 0,//喷泉
- 0, //警察局
- 0,//游乐场
- 0, //蓝色城堡
- 0, //粉色城堡
- 1, //Holy Farmland
- 1, //Holy Farmland seed
- ];
- //建筑物数量
- this.BuildingNumArray = [
- 0, //公路
- 0, //路铲
- 0, //拆迁
- 4, //农舍
- 1, //单元楼
- 1, //别墅
- 4, //农田
- 1,//伐木场
- 1, //矿坑
- 2, //加工厂
- 2,//冷饮摊
- 1,//贩卖机
- 1,//面包房
- 1,//早餐车
- 1, //饮茶店
- 1,//点心店
- 1, //美食店
- 1,//西餐厅
- 1, //花店
- 1, //美发店
- 1,//洋装店
- 1, //珠宝店
- 1, //电影院
- 1,//路灯
- 1, //绿化带
- 1, //花坛
- 1,//喷泉
- 1, //警察局
- 1,//游乐场
- 1, //蓝色城堡
- 1, //粉色城堡
- 1, //Holy Farmland
- 1, //Holy Farmland seed
- ];
- this.DiamondNumArray = AConfig.DiamondArray;
- //todo 钻石消耗默认值
- this.DiamondNumArray.forEach((value, index, array) => {
- array[index] = 0;
- })
- cc.log('钻石消耗默认值', this.DiamondNumArray);
- this.EveryDayRewardsArray = [0, 0, 0, 0, 0, 0, 0];
- this.LastTimeEveryDayRewardsDate = '0000/00/00';
- this.LastTimeLuckDate = '0000/00/00';
- this.FoodTradeState = 1;
- this.WoodTradeState = 1;
- this.MineralTradeState = 1;
- //转盘分享给钻石金币
- this.shareGive = [200, 5];
- //签到分享给钻石金币
- this.signInGive = [200, 5];
- //每个月 给的 低保
- this.EveryGive = [500, 10];
- //公共分享
- this.publicGive = [200, 50];
- //每天抽奖次数
- this.LotteryTimes = 10;
- // 1 === _BFirstLoadGame
- if (_BFirstLoadGame) {
- console.log("this.readData", this.readData);
- // cc.log('初始化网络数据');
- //读取日期数据
- this.GameYear = this.readData.GameYear;
- this.GameMonth = this.readData.GameMonth;
- this.GameDay = this.readData.GameDay;
- /**
- * 接口
- * todo cnt和snb 由钱包读取
- */
- this.CNT = 0;
- this.SNB = 0;
- //读取金币
- this.Golden = this.readData.Golden;
- //读取钻石
- this.Diamond = this.readData.Diamond;
- // cc.log('this.readData.Diamond', this.readData.Diamond);
- //读取工人等级
- this.WorkerLV = this.readData.WorkerLV;
- //工人数量
- this.WorkerNum = this.readData.WorkerNum;
- //工人容量
- this.WorkerCapacity = this.readData.WorkerCapacity;
- this.GameDate = this.readData.GameDate;
- this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
- this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
- this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
- this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
- //面板状态
- this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
- //解锁设置为 不用解锁
- this.BuildingLockStateArray.forEach((value, index, array) => {
- array[index] = 1;
- })
- // this.BuildingLockStateArray = this.readData.BuildingLockStateArray.split('_');
- this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
- this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
- this.FoodTradeState = this.readData.FoodTradeState;
- this.WoodTradeState = this.readData.WoodTradeState;
- this.MineralTradeState = this.readData.MineralTradeState;
- //工人工作信息
- if (this.readData.characterInfoArray)
- this.CharacterInfoArray = this.readData.characterInfoArray;
- //道路数据
- if (this.readData.highwayIndex)
- this.GameData_highwayIndex = this.readData.highwayIndex;
- //建筑物数据
- if (this.readData.buildingsInfo)
- this.GameData_buildings = this.readData.buildingsInfo;
- this.LotteryTimes = this.readData.LotteryTimes;
- //任务
- if (this.readData.TaskIconCountClick)
- task.TaskIconCountClick = this.readData.TaskIconCountClick;
- //测试5W
- // this.PlusDiamond(50000);
- }
- },
- //GET / SET /Plus
- GetGameDate: function () {
- return this.GameDate;
- },
- SetGameDate: function (num) {
- this.GameDate = num;
- },
- PlusGameDate: function (num) {
- this.GameDate += num;
- },
- GetGolden: function () {
- return parseInt(this.Golden);
- },
- GetGoldenCallBack(_CallBack) {
- if (_CallBack)
- _CallBack({ resGolden: parseInt(this.Golden) });
- },
- SetGolden: function (Num) {
- let LastMoney = this.Golden;
- this.Golden = Num;
- let CurrentMoney = Num;
- this.ManageUI.getComponent('ManageUI').GoldenChangeCallBack(this.GetGolden(), LastMoney, CurrentMoney);
- },
- PlusGolden: function (Num) {
- if (this.Golden + Num < 0) {
- this.SetGolden(0);
- }
- else {
- this.SetGolden(this.Golden + Num);
- }
- // task.task50W();
- },
- GetDiamond: function () {
- return parseInt(this.Diamond);
- },
- SetDiamond: function (num, bPush) {
- let LastMoney = this.Diamond;
- this.Diamond = num;
- let CurrentMoney = num;
- this.ManageUI.getComponent("ManageUI").DiamondNowlabel.string = this.Diamond + '/30000'
- this.ManageUI.getComponent('ManageUI').DiamondChangeCallBack(this.GetDiamond(), LastMoney, CurrentMoney);
- console.log("Diamond:", bPush);
- if (bPush) {
- //每次砖石操作。更新一次数据
- this.pushData(true);
- }
- },
- PlusDiamond: function (num) {
- if (this.Diamond + num < 0) {
- this.SetDiamond(0, true);
- }
- else {
- this.SetDiamond(this.Diamond + num, true);
- }
- },
- /**
- * 游戏里面的cnt 和神农呗
- */
- GetCNT: function () {
- return parseFloat(this.CNT);
- },
- SetCNT: function (Num) {
- this.CNT = Num;
- this.ManageUI.getComponent('ManageUI').CNTChangeCallBack(this.CNT);
- },
- GetSNB: function () {
- return parseFloat(this.SNB);
- },
- SetSNB: function (Num) {
- this.SNB = Num;
- this.ManageUI.getComponent('ManageUI').SNBChangeCallBack(this.SNB);
- },
- GetStrength: function () {
- return parseFloat(this.strength);
- },
- SetStrength: function (Num) {
- this.strength = Num;
- this.ManageUI.getComponent('ManageUI').StrengthChangeCallBack(this.strength);
- },
- GetLucky: function () {
- return parseFloat(this.lucky);
- },
- SetLucky: function (Num) {
- this.lucky = Num;
- this.ManageUI.getComponent('ManageUI').LuckyChangeCallBack(this.lucky);
- },
- GetWorkerLV: function () {
- return parseInt(this.WorkerLV);
- },
- SetWorkerLV: function (num) {
- this.WorkerLV = num;
- },
- PlusWorkerLV: function (num) {
- this.WorkerLV += num;
- },
- GetWorkerNum: function () {
- return parseInt(this.WorkerNum);
- },
- SetWorkerNum: function (num) {
- this.WorkerNum = num;
- },
- PlusWorkerNum: function (num) {
- this.WorkerNum += num;
- },
- GetLastTimeEveryDayRewardsDate: function () {
- return this.LastTimeEveryDayRewardsDate;
- },
- //获取转盘 时间
- GetLastTimeLuckDate: function () {
- return this.LastTimeLuckDate;
- },
- //设置转盘时间
- SetLastTimeLuckDate: function (DateString) {
- this.LastTimeLuckDate = DateString;
- },
- SetLastTimeEveryDayRewardsDate: function (DateString) {
- this.LastTimeEveryDayRewardsDate = DateString;
- },
- //工人信息数组
- GetWorkerCharacterInfoArray: function () {
- return this.CharacterInfoArray;
- },
- SetWorkerCharacterInfoArray: function (item) {
- this.CharacterInfoArray = item;
- },
- /*** */
- GetWorkerCapacity: function () {
- return parseInt((this.WorkerCapacity));
- },
- SetWorkerCapacity: function (num) {
- this.WorkerCapacity = num;
- },
- PlusWorkerCapacity: function (num) {
- this.WorkerCapacity += num;
- },
- GetTerritoryStateArray: function () {
- return this.TerritoryStateArray;
- },
- SetTerritoryStateArray: function (aTerritoryStateArray) {
- this.TerritoryStateArray = aTerritoryStateArray;
- },
- PlusTerritoryStateArray: function (Item) {
- this.TerritoryStateArray.push(Item);
- },
- GetBuildingStateArray: function () {
- return this.BuildingStateArray;
- },
- SetBuildingStateArray: function (aBuildingStateArray) {
- this.BuildingStateArray = aBuildingStateArray;
- },
- PlusBuildingStateArray: function (Item) {
- this.BuildingStateArray.push(Item);
- },
- GetBuildingLockStateArray: function () {
- return this.BuildingLockStateArray;
- },
- SetBuildingLockStateArray: function (aArray) {
- this.BuildingLockStateArray = aArray;
- },
- PlusBuildingLockStateArray: function (Item) {
- this.BuildingLockStateArray.push(Item);
- },
- GetBuildingNumArray: function () {
- return this.BuildingNumArray;
- },
- SetBuildingNumArray: function (aBuildingNumArray) {
- this.BuildingNumArray = aBuildingNumArray;
- },
- PlusBuildingNumArray: function (Item) {
- this.BuildingNumArray.push(Item);
- },
- //操作钻石数据
- GetDiamondNumArray: function () {
- return this.DiamondNumArray;
- },
- SetDiamondNumArray: function (aDiamondNumArray) {
- this.DiamondNumArray = aDiamondNumArray;
- },
- GetEveryDayRewardsArray: function () {
- return this.EveryDayRewardsArray;
- },
- SetEveryDayRewardsArray: function (aArray) {
- this.EveryDayRewardsArray = aArray;
- },
- PlusEveryDayRewardsArray: function (Item) {
- this.EveryDayRewardsArray.push(Item);
- },
- GetFoodTradeState: function () {
- return parseInt((this.FoodTradeState));
- },
- SetFoodTradeState: function (num) {
- this.FoodTradeState = num;
- },
- PlusFoodTradeState: function (num) {
- this.FoodTradeState += num;
- },
- GetWoodTradeState: function () {
- return parseInt((this.WoodTradeState));
- },
- SetWoodTradeState: function (num) {
- this.WoodTradeState = num;
- },
- PlusWoodTradeState: function (num) {
- this.WoodTradeState += num;
- },
- GetMineralTradeState: function () {
- return parseInt((this.MineralTradeState));
- },
- SetMineralTradeState: function (num) {
- this.MineralTradeState = num;
- },
- PlusMineralTradeState: function (num) {
- this.MineralTradeState += num;
- },
- GetLotteryTimes: function () {
- return parseInt((this.LotteryTimes));
- },
- SetLotteryTimes: function (num) {
- this.LotteryTimes = num;
- },
- PlusLotteryTimes: function (num) {
- this.LotteryTimes += num;
- },
- //初始化道路
- onSpawnHighway() {
- // 获取InitPoint层
- let InitPos = this._tiledMap.getLayer('InitPoint');
- InitPos.enabled = false;
- console.log("onSpawnHighway");
- //创建公路数据
- if (this.readData && this.readData.allHighwayStylesAndIndex) {
- //保存到内存中
- this.GameData_highwayIndex = this.readData.highwayIndex;
- //先赋值道路的对象数组
- let _AllHighwayAIndex = Object.assign([], this.readData.allHighwayStylesAndIndex);
- //获取最后建造公路的层级
- this.HighwayLayer = this._tiledMap.getLayer('Highway');
- for (let i = 0; i < _AllHighwayAIndex.length; i++) {
- let _tiledPos = GlobalD.TiledMap.analyticalIndexData(_AllHighwayAIndex[i].highwayInfoIndex);
- /**
- * 解决固定土地时候,道路 和房子 冲突
- *
- */
- if (GlobalD.game.getManageGameIndexArrayAt(_tiledPos)) {
- continue;
- }
- let highwayTemp = cc.instantiate(this.HighWayPrefabs);
- highwayTemp.parent = this.HighwayLayer.node;
- let tiledTile = highwayTemp.addComponent('TiledTile');
- tiledTile.x = _tiledPos.x;
- tiledTile.y = _tiledPos.y;
- //地图设置可行走区域公路设置
- AStar.setMapSolid(_tiledPos.x, _tiledPos.y, 0);
- // _buildId ==0 是公路
- let _buildId = 0;
- let occupyTemp = cc.v2(_buildId, _AllHighwayAIndex[i].highwayInfoIndex);
- GlobalD.game.OccupyArray.push(occupyTemp);
- highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(_AllHighwayAIndex[i].highwayInfoType);
- GlobalD.game.AllHighwayStylesAndIndex.push(_AllHighwayAIndex[i]);
- }
- // console.log("end");
- } else {
- //如果用户没有存储数据
- //根据InitPoint 的数据,创建初始化的地图数据 的初始化数据
- this.HighwayLayer = this._tiledMap.getLayer('Highway');
- for (var i = 0; i < 32; i++) {
- //去除中间两条路
- if (i == 16 || i == 17) continue;
- for (var j = 0; j < 32; j++) {
- let tilesPos = cc.v2(i, j);
- if (InitPos.getTileGIDAt(tilesPos)) {
- let index = GlobalD.TiledMap.getIndex(tilesPos);
- this.GameData_highwayIndex.push(index);
- let highwayTemp = cc.instantiate(this.HighWayPrefabs);
- highwayTemp.parent = this.HighwayLayer.node;
- let tiledTile = highwayTemp.addComponent('TiledTile');
- tiledTile.x = tilesPos.x;
- tiledTile.y = tilesPos.y;
- // cc.log('onSpawnHighway2');
- //地图设置可行走区域公路设置
- AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
- // _buildId ==0 是公路
- let _buildId = 0;
- let occupyTemp = cc.v2(_buildId, index);
- GlobalD.game.OccupyArray.push(occupyTemp);
- let tile = InitPos.getTiledTileAt(tilesPos.x, tilesPos.y, true);
- let _MoveType = reGameStates.HighwayType.moveX;
- // if (tile.gid == 15) {
- // _MoveType = reGameStates.HighwayType.moveX;
- // } else
- if (tile.gid == 16) {
- _MoveType = reGameStates.HighwayType.moveY;
- } else if (tile.gid == 11) {
- _MoveType = reGameStates.HighwayType.ZebraCrossingX;
- } else if (tile.gid == 12) {
- _MoveType = reGameStates.HighwayType.ZebraCrossingY;
- }
- // return;
- //更换公路样式
- GlobalD.game.onCreateDifferentRoadStyles({
- _buildId: _buildId,
- _highwayType: _MoveType,
- _roadIndex: index,
- _hightwayNode: highwayTemp
- });
- }
- }
- }
- }
- //外面的两条路
- let initRoad = this._tiledMap.getLayer('Road');
- initRoad.enabled = false;
- for (var i = 16; i < 18; i++) {
- for (var j = 0; j < 32; j++) {
- let tilesPos = cc.v2(i, j);
- // cc.log('road tilesPos',tilesPos)
- if (initRoad.getTileGIDAt(tilesPos)) {
- let index = GlobalD.TiledMap.getIndex(tilesPos);
- this.GameData_highwayIndex.push(index);
- let highwayTemp = cc.instantiate(this.HighWayPrefabs);
- highwayTemp.parent = this.HighwayLayer.node;
- let tiledTile = highwayTemp.addComponent('TiledTile');
- tiledTile.x = tilesPos.x;
- tiledTile.y = tilesPos.y;
- // cc.log('initRoad');
- //地图设置可行走区域公路设置
- AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
- let _buildId = 0;
- let occupyTemp = cc.v2(_buildId, index);
- GlobalD.game.OccupyArray.push(occupyTemp);
- highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.none);
- //更换公路样式
- // if (j >= 25 && j <= 27 || j >= 19 && j <= 21 || j >= 9 && j <= 11)
- // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.none);
- // else
- // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.moveY);
- }
- }
- }
- },
- //清除所有的数据
- onClearAllData() {
- // this.unschedule(this.AutoSaveData);
- //任务索引
- cc.sys.localStorage.removeItem('userdata');
- //跳回登录场景
- // cc.director.loadScene('Login');
- },
- //保存数据请求
- /**
- *
- * 现在转服务器存储,原本所有相关操作通过服务器计算。
- * 一、目前涉及到的是 SNT 和 SNB 两个货币相关,和买田地操作 (可以理解为租聘,三种类型)
- *
- * isNetwork:true,上传到网络
- *
- */
- pushData: function (isNetwork) {
- // cc.log(this.EveryDayRewardsArray);
- // console.log(this.readData.BFirstLoadGame);
- var datas = {
- version: this.GameVersion,
- // 转后台处理
- BFirstLoadGame: 1,//只要push数据,就证明已经开始第一次游戏了
- /**游戏不修改,默认值 start */
- shareGive: this.shareGive,
- //签到分享给钻石金币
- signInGive: this.signInGive,
- //每个月 给的 低保
- EveryGive: this.EveryGive,
- //公共分享
- publicGive: this.publicGive,
- /**游戏不修改,默认值 end */
- //时间
- GameYear: this.GameYear,
- GameMonth: this.GameMonth,
- GameDay: this.GameDay,
- //这里只是单纯的存储起来
- CNT: this.CNT,
- SNB: this.SNB,
- Golden: this.Golden,
- Diamond: this.Diamond,
- WorkerLV: this.WorkerLV,
- WorkerNum: this.WorkerNum,
- WorkerCapacity: this.WorkerCapacity,
- GameDate: this.GameDate,
- LastTimeEveryDayRewardsDate: this.LastTimeEveryDayRewardsDate,
- LastTimeLuckDate: this.LastTimeLuckDate,
- EveryDayRewardsArray: this.EveryDayRewardsArray,//.join('_'),
- TerritoryStateArray: this.TerritoryStateArray,//.join('_'),
- //面板状态
- BuildingStateArray: this.BuildingStateArray,//.join('_'),
- BuildingLockStateArray: this.BuildingLockStateArray,//.join('_'),
- /**
- * SNB和CNT相关,需要后台筛选 田地 和 种子。单独处理
- */
- BuildingNumArray: this.BuildingNumArray,//.join('_'),
- DiamondNumArray: this.DiamondNumArray,//.join('_'),
- FoodTradeState: this.FoodTradeState,
- WoodTradeState: this.WoodTradeState,
- MineralTradeState: this.MineralTradeState,
- LotteryTimes: this.LotteryTimes,
- /**
- * 这些数据不用服务器初始化
- */
- //工人工作信息
- characterInfoArray: this.CharacterInfoArray,
- //道路数据
- highwayIndex: this.GameData_highwayIndex,
- allHighwayStylesAndIndex: GlobalD.game.AllHighwayStylesAndIndex,
- //建筑物数据
- buildingsInfo: this.GameData_buildings,
- //任务
- TaskIconCountClick: task.TaskIconCountClick
- }
- if (isNetwork) {
- var data = [];
- // data["openid"] = userData.openId;
- // data["userdata"] = JSON.stringify(datas);
- data["playerData"] = JSON.stringify(datas);
- /** 推送信息 */
- utils.post(utils.api.playerPushInfo, data, (flag, playerPushInfoTemp) => {
- // console.log('playerPushInfoTemp');
- })
- } else {
- console.log("存储本地:" + isNetwork);
- //保存用户数据到本地
- cc.sys.localStorage.setItem('userdata', JSON.stringify(datas));
- }
- },
- /**
- * 兑换种子时候,特殊处理数据
- * 扣除相应的钻石
- */
- exchangeSeed(diamondAmount, callback) {
- if (this.isExchangeSeed) {
- console.warn("正在兑换中...");
- return;
- }
- this.isExchangeSeed = true;
- //先暂停存储
- // this.unschedule(this.AutoSaveData);
- utils.post(utils.api.exchangeSeeds, { diamondAmount: diamondAmount, seedId: 8 }, (flag, value) => {
- console.log("兑换flag", flag);
- if (flag) {
- console.log("兑换种子", value.code, value.msg);
- //兑换成功后
- if (0 === value.code) {
- //更新一次数据
- let _playerData = JSON.parse(value.data.playerData);
- console.log("兑换后的钻石:", _playerData.Diamond);
- //更新本地钻石数据
- this.SetDiamond(_playerData.Diamond, false);
- }
- callback(value);
- }
- this.isExchangeSeed = false;
- //重新开启存储
- // this.schedule(this.AutoSaveData, 15);
- })
- },
- /**
- * 获取是否租赁过的状态
- */
- getPlayerExchangeState(callback) {
- utils.get(utils.api.playerExchangeState, {}, (flag, value) => {
- console.log("租赁状态:", value);
- if (flag)
- callback(flag, value);
- })
- },
- /**
- * 暂时不需要从这里读取
- */
- getLandConfig() {
- utils.get(utils.api.landConfig, {}, (flag, value) => {
- if (flag && 0 === value.code) {
- this.ConfigLand = value.data;
- } else {
- console.warn("未能读取到config土地?");
- }
- })
- },
- //获取自己全部租赁土地
- getSelfUserLandList(callback) {
- utils.get(utils.api.userLandList, {}, (flag, value) => {
- if (flag && 0 === value.code) {
- GlobalD.UserLeaseLand = value.data;
- if (callback)
- callback();
- } else {
- console.warn("读取到用户的土地错误?");
- }
- })
- },
- //获取用户全部租赁土地
- getOtherUserLandList(otherUserId, callback) {
- this.showToast(cc.find("Canvas/UICamera"), "载入玩家数据中..", 5);
- utils.get(utils.api.userLandAndCanStealList, { otherUserId: otherUserId }, (flag, value) => {
- console.log(otherUserId + ",获取其他用户土地数据:", value);
- if (flag && 0 === value.code) {
- GlobalD.OtherLeaseland = value.data;
- //todo 获取数据成功,进入切换流程,设置 false,处理其他用户对应的土地
- this.ManageUI.getComponent('ManageBuildings').spawnConfigLand(GlobalD.OtherLeaseland, false);
- //收起菜单
- GlobalD.game._ManageUIScript.onHideMenu();
- //收起底部菜单栏
- GlobalD.game._ManageUIScript.onBottomMenuView(false);
- //显示进入其他用户场景后菜单操作
- GlobalD.game._ManageUIScript.onOtherFarmerView(true);
- //关闭当前列表
- GlobalD.game._ManageUIScript.onListUserInfo_Close();
- // this.showToast(cc.find("Canvas/UICamera"), "载入成功..", 1);
- this.hideToast();
- if (callback)
- callback();
- } else {
- this.showToast(cc.find("Canvas/UICamera"), "获取数据失败..", 1);
- console.warn("获取其他用户土地数据错误?");
- }
- })
- },
- //获取用户可以偷取的列表
- onGetCanStealUserList(page, limit, callback) {
- this.showToast(cc.find("Canvas/UICamera"), "刷新列表中..", 5);
- utils.get(utils.api.canStealUserList, { page: page, limit: limit }, (flag, value) => {
- this.showToast(cc.find("Canvas/UICamera"), "刷新完成!", 1);
- if (flag && 0 === value.code) {
- if (callback)
- callback(value.data);
- } else {
- let myList = [];
- if (callback)
- callback(myList);
- }
- })
- },
- // onGetSnbInfoList(page, limit, callback) {
- // utils.get(utils.api.snbList, { page: page, limit: limit }, (flag, value) => {
- // // console.log("snb操作日志", value);
- // if (flag && 0 === value.code) {
- // if (callback)
- // callback(value.data);
- // } else {
- // let mySnbList = [];
- // if (callback)
- // callback(mySnbList);
- // }
- // })
- // },
- //狗的相关处理
- //获取状态
- onGetDogState(data, callback) {
- utils.get(utils.api.getEquipmentByType, data, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- onGetEquipmentListState(data, callback) {
- utils.get(utils.api.getListEquipment, data, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- onGetAddDogFood(data, callback) {
- utils.post(utils.api.addFood, data, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- onSetDogShowState(data, callback) {
- utils.post(utils.api.setDogShowState, data, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //获取狗粮商城列表
- onGetMallFoodList(callback) {
- utils.get(utils.api.getMallFoodList, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //商城装备列表
- onGetMallEquipmentList(callback) {
- utils.get(utils.api.getMallEquipmentList, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- onGetAddEquipment(data, callback) {
- utils.post(utils.api.addEquipment, data, (flag, value) => {
- console.log(value);
- callback(flag, value);
- })
- },
- //获取用户已租赁土地的状态
- getLandState(context) {
- let { landId, callback } = context;
- utils.get(utils.api.landState, { landId: landId }, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //获取商城种子
- getMallSeed(callback) {
- utils.get(utils.api.mallSeed, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- getWarehouseSeedAndFruit(callback) {
- utils.get(utils.api.getSeedAndFruit, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //获取种子状态
- onGetSeedState(data, callback) {
- utils.get(utils.api.getSeedState, data, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //种植种子
- // data:{landId:1,seedId:1}
- onPlant(data, callback) {
- utils.get(utils.api.plant, data, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //收取果实
- onAddFruit(data, callback) {
- if (this.isOnAddFruit) {
- console.log("收取果实过快!");
- return;
- }
- this.isOnAddFruit = true;
- utils.post(utils.api.addFruit, data, (flag, value) => {
- this.isOnAddFruit = false;
- if (flag)
- callback(value);
- })
- },
- //一键收取果实,偷取
- onStealAllFruit(data, callback) {
- if (this.isOnAddFruit) {
- console.log("一键收取果实过快!");
- return;
- }
- this.isOnAddFruit = true;
- this.showToast(cc.find("Canvas/UICamera"), "收取果实中..", 5);
- utils.post(utils.api.stealAllFruit, data, (flag, value) => {
- this.isOnAddFruit = false;
- this.hideToast();
- if (0 === value.code) {
- //如果返回一个体力信息,更新对应的新的用户信息。
- if (value.data.playerAttribute) {
- GlobalD.PlayerAttribute = value.data.playerAttribute;
- //这里需要更新体力值
- this.SetStrength(GlobalD.PlayerAttribute.currentStrength);
- }
- }
- if (flag)
- callback(value);
- })
- },
- //单个收取
- onStealFruit(data, callback) {
- if (this.isOnAddFruit) {
- console.log("偷取果实过快!");
- return;
- }
- this.isOnAddFruit = true;
- this.showToast(cc.find("Canvas/UICamera"), "偷取果实中..", 5);
- utils.post(utils.api.stealFruit, data, (flag, value) => {
- this.isOnAddFruit = false;
- this.hideToast();
- if (0 === value.code) {
- //如果返回一个体力信息,更新对应的新的用户信息。
- if (value.data.playerAttribute) {
- GlobalD.PlayerAttribute = value.data.playerAttribute;
- //这里需要更新体力值
- this.SetStrength(GlobalD.PlayerAttribute.currentStrength);
- }
- }
- if (flag)
- callback(value);
- })
- },
- //出售果实
- onSaleFruit(data, callback) {
- if (this.isOnSaleFruit) {
- console.log("出售果实过快!");
- return;
- }
- this.isOnSaleFruit = true;
- utils.post(utils.api.saleFruit, data, (flag, value) => {
- this.isOnSaleFruit = false;
- if (flag)
- callback(value);
- })
- },
- //赠送果实
- onGrantFruit(data, callback) {
- if (this.isOnGrantFruit) {
- console.log("赠送果实过快!");
- return;
- }
- this.isOnGrantFruit = true;
- utils.post(utils.api.grantFruit, data, (flag, value) => {
- this.isOnGrantFruit = false;
- if (flag)
- callback(value);
- })
- },
- //神农呗购买种子
- onBuySeedsWithSNB(snbAmount, seedId, callback) {
- utils.post(utils.api.snbBuySeeds, { payAmount: snbAmount, seedId: seedId }, (flag, value) => {
- //callback里面处理flag判断
- callback(flag, value);
- })
- },
- //获取用户的snb
- onGetUserSnb(callback) {
- utils.get(utils.api.userSnbInfo, {}, (flag, value) => {
- if (flag) {
- GlobalD.GameData.SetSNB(value.data.SNB + value.data.snbPart);
- }
- if (flag && callback)
- callback(value);
- })
- },
- /**
- * 支付cnt操作
- * @param {*} amount // 需支付金额
- * @param {*} payType 支付类型,1购买土地租凭, 2自然灾害防护,3野兽防护, 4种子
- * @param {*} itemType 操作物品的id 字符串
- */
- payCNT(cntAmount, payType, itemType, callback) {
- console.log("购买金额:" + cntAmount + "购买类型PlayType:" + payType + "购买物品的id:" + itemType);
- if (!GlobalD.dapp) {
- console.warn("GlobalD.dapp未初始化,不能payCNT!");
- return;
- }
- //
- if (this.isPlayingCnt) {
- console.warn("同时触发支付CNT过快!");
- return;
- }
- if (GlobalD.GameData.GetCNT() < cntAmount) {
- console.log("cnt 不足,GetCNT:" + GlobalD.GameData.GetCNT() + "消费的cntAmount" + cntAmount);
- this.showToast(cc.find("Canvas/UICamera"), "CNT不足!", 2);
- return;
- }
- this.isPlayingCnt = true;
- this.showToast(cc.find("Canvas/UICamera"), "支付处理中...", 5);
- GlobalD.dapp.payCnt(cntAmount, payType, itemType).then((data) => {
- const [err, tx] = data;
- this.isPlayingCnt = false;
- if (err === null) {
- // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
- console.log(tx) // 交易hash,唯一标识符
- GlobalD.dapp.cntBalance().then((cntBalance) => {
- //会延迟返回
- // console.log("更新cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
- GlobalD.GameData.SetCNT(cntBalance);
- });
- this.showToast(cc.find("Canvas/UICamera"), "支付成功!", 5, () => {
- this.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
- });
- } else {
- console.log(err)
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2);
- }
- if (callback) {
- callback(data);
- }
- });
- },
- /**
- * 神农呗转换CNT ,比例是5:1
- * @param {*} snbAmount
- * @param {*} callback
- * @returns
- */
- onSnbToCnt(snbAmount, callback) {
- if (!GlobalD.dapp) {
- console.warn("GlobalD.dapp未初始化,不能onSnbToCnt!");
- return;
- }
- // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "暂不支持兑换CNT!", 2, () => {
- // console.log("finish toast!");
- // });
- // return;
- if (this.isPlayingSnb) {
- console.warn("同时触发支付SNB过快!");
- return;
- }
- if (GlobalD.GameData.GetSNB() <= 0) {
- console.log("神农呗数量GetSNB:", GlobalD.GameData.GetSNB(), "替换snbAmount:", snbAmount);
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "神农呗数量不足!", 2);
- return;
- }
- this.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 10);
- this.isPlayingSnb = true;
- GlobalD.dapp.snbToCnt(snbAmount).then((data) => {
- //更新日志数据
- cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
- const [err, tx] = data;
- this.isPlayingSnb = false;
- if (err === null) {
- // TODO 成功, 兑换为链上操作,需要提供回调接口给这边服务端确认交易成功后修改扣除SNB数量
- console.log(tx) // 交易hash,唯一标识符
- //扣除对应的神农呗,本地修改显示
- GlobalD.GameData.SetSNB(GlobalD.GameData.GetSNB() - snbAmount);
- GlobalD.dapp.cntBalance().then((cntBalance) => {
- //会延迟返回
- // console.log("更新cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
- GlobalD.GameData.SetCNT(cntBalance);
- });
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 2);
- } else {
- console.log(err)
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2);
- }
- if (callback) {
- callback(data);
- }
- });
- },
- //站内收益
- onCntCanWithdrawBalance(callback) {
- if (!GlobalD.dapp) {
- console.warn("GlobalD.dapp未初始化,不能onGetCNTRevenue!");
- return;
- }
- GlobalD.dapp.cntCanWithdrawBalance().then((data) => {
- const { err, res } = data;
- if (err === null) {
- console.log(res) //Number 返回数字
- GlobalD.GameData.CNTDrawBalance = res;
- }
- //todo ,记录一个信息
- if (callback) {
- callback(data);
- }
- });
- },
- //站内收益提现
- onCntWithdraw(amount, callback) {
- if (!GlobalD.dapp) {
- console.warn("GlobalD.dapp未初始化,不能onCntWithdraw!");
- return;
- }
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收益正在提现中", 10, () => {
- console.log("finish toast!");
- });
- GlobalD.dapp.cntWithdraw(amount).then((data) => {
- const [err, tx] = data;
- console.log("onCntWithdraw:", data);
- if (err === null) {
- console.log(tx) //String|null 交易唯一哈市
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "提现成功!", 1, () => {
- console.log("finish toast!");
- });
- } else {
- GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 1, () => {
- console.log("finish toast!");
- });
- }
- if (callback) {
- callback(data);
- }
- });
- },
- //获取账户站内CNT明细(村长和镇长才有)
- onCntLog(page, limit, callback) {
- if (!GlobalD.dapp) {
- console.warn("GlobalD.dapp未初始化,不能onGetSnbToCntInfo!");
- return;
- }
- GlobalD.dapp.cntLog(page, limit).then((data) => {
- const { err, res } = data;
- // if (err === null) {
- // console.log(res) //
- // }
- if (callback) {
- callback(data);
- }
- });
- },
- //获取 神农呗转 CNT 日志
- onGetSnbToCntInfo(page, limit, callback) {
- if (!GlobalD.dapp) {
- console.warn("GlobalD.dapp未初始化,不能onGetSnbToCntInfo!");
- return;
- }
- GlobalD.dapp.swapLog(page, limit).then((data) => {
- const { err, res } = data;
- if (err === null) {
- console.log(res) //
- }
- if (callback) {
- callback(data);
- }
- });
- },
- //获取 snb 流水日志
- onGetSnbInfoList(page, limit, callback) {
- utils.get(utils.api.snbList, { page: page, limit: limit }, (flag, value) => {
- // console.log("snb操作日志", value);
- if (flag && 0 === value.code) {
- if (callback)
- callback(value.data);
- } else {
- let mySnbList = [];
- if (callback)
- callback(mySnbList);
- }
- })
- },
- //获取snb申请列表
- onGetSnbApplyPage(isState, page, limit, callback) {
- utils.get(utils.api.getSnbApplyPage, { isState: isState, page: page, limit: limit }, (flag, value) => {
- if (flag && 0 === value.code) {
- if (callback)
- callback(value.data);
- } else {
- let getSnbApplyPage = [];
- if (callback)
- callback(getSnbApplyPage);
- }
- })
- },
- //提交申请
- onPostApplySnbToCnt(applyAmount, callback) {
- if (this.isApplying) {
- this.showToast(cc.find("Canvas/UICamera"), "正在申请中..", 1);
- return;
- }
- this.isApplying = true;
- utils.post(utils.api.applySnbToCnt, { applyAmount: applyAmount }, (flag, value) => {
- this.isApplying = false;
- if (flag)
- callback(value);
- })
- },
- //获取转换的snb
- onGetCntToSnb(callback) {
- utils.get(utils.api.getCntToSnb, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //领取SNB接口
- onReceiveCntToSnb(callback) {
- utils.get(utils.api.receiveCntToSnb, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //校验snb凭证
- onCheckApplySnbToCnt(certificateId, callback) {
- utils.post(utils.api.checkApplySnbToCnt, { certificateId: certificateId }, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- //切换不同身份的场景,这里处理
- onSwitchAddressBg(_level) {
- //设置背景
- GlobalD.TiledMap.setMapInfo(_level);
- //设置土地
- let _landBuildingsInfo = this.BuildingsParent.children;
- for (let i = _landBuildingsInfo.length - 1; i >= 0; i--) {
- let resetLandInfo = _landBuildingsInfo[i];
- if (resetLandInfo.name == "Labour_205_Holy_Farmland") {
- let leaseFarmlandInfoScript = resetLandInfo.getComponent("LeaseFarmlandInfo");
- leaseFarmlandInfoScript.onSwitchEnvBgFromLevel(_level);
- }
- }
- },
- /**
- * 获取灾难
- */
- onGetDisasterHistory(callback) {
- utils.get(utils.api.disasterHistory, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- /**
- * 获取灾难防护
- * @param {*} callback
- */
- onGetDisasterProtected(callback) {
- utils.get(utils.api.getDisasterProtectedMap, {}, (flag, value) => {
- if (flag)
- callback(value);
- })
- },
- /**
- * 操作工具
- */
- //获取工具
- onGetTools(callback) {
- utils.get(utils.api.getTools, {}, (flag, value) => {
- console.log("工具:", flag, value);
- if (flag)
- callback(value);
- })
- },
- //使用工具
- onPostToolToLand(data, callback) {
- utils.post(utils.api.postUseLuckyTools, data, (flag, value) => {
- console.log(data, "=使用工具:", flag, value);
- if (flag && 0 === value.code && value.flag) {
- GlobalD.PlayerAttribute = value.data.playerAttribute;
- this.SetLucky(GlobalD.PlayerAttribute.lucky);
- this.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
- callback(value);
- } else {
- this.showToast(cc.find("Canvas/UICamera"), value.msg, 1);
- }
- })
- },
- //获取土地等级
- onGetLevel(callback) {
- utils.get(utils.api.getLevel, {}, (flag, value) => {
- console.log("获取土地", flag, value);
- if (flag)
- callback(value);
- })
- },
- //升级土地
- onPostLandLevelUp(data, callback) {
- utils.post(utils.api.postLandLevelUp, data, (flag, value) => {
- console.log(data, "=升级土地", flag, value);
- if (flag) {
- callback(value);
- }
- })
- },
- /**
- * 注意限制检查snb
- * @param {*} data
- * @param {*} callback
- */
- onPostLuckToSnb(data, callback) {
- if(this.isPlayingLucky)return;
- this.isPlayingLucky = true;
- this.showToast(cc.find("Canvas/UICamera"), "兑换SNB中", 2);
- utils.post(utils.api.luckToSnb, data, (flag, value) => {
- this.isPlayingLucky = false;
- console.log(data, "=兑换snb", flag, value);
- if (flag && 0 === value.code && value.flag) {
- //如果返回一个体力信息,更新对应的新的用户信息。
- //更新日志数据
- cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
- GlobalD.PlayerAttribute = value.data.playerAttribute;
- this.SetLucky(GlobalD.PlayerAttribute.lucky);
- GlobalD.UserInfo = value.data.userInfo;
- this.SetSNB(GlobalD.UserInfo.snb + GlobalD.UserInfo.snbPart);
- this.showToast(cc.find("Canvas/UICamera"), value.msg, 2);
- callback(value);
- }
- else {
- this.showToast(cc.find("Canvas/UICamera"), value.msg, 2);
- }
- })
- },
- //获取一个toast
- getToast() {
- return this.toast;
- },
- hideToast() {
- if (this.scheduleObj) {
- this.unschedule(this.scheduleObj);
- this.scheduleObj = null;
- }
- this.toastCallback = null;
- },
- showToast(parent, content, time, callback) {
- if (this.toast) {
- //如果有旧的callback,返回callback
- if (this.toastCallback) {
- this.toastCallback();
- this.toastCallback = null;
- }
- this.toastCallback = callback;
- if (this.scheduleObj) {
- this.unschedule(this.scheduleObj);
- this.scheduleObj = null;
- }
- this.scheduleObj = this.scheduleOnce(() => {
- this.toast.active = false
- if (this.toastCallback != null && this.toastCallback != undefined) {
- this.toastCallback();
- }
- }, time);
- this.toast.active = true;
- this.toast.parent = parent;
- this.toast.zIndex = 999;
- let DetailLabel = this.toast.getChildByName('DetailLabel');
- DetailLabel.getComponent(cc.Label).string = content;
- this.toastEndTime = time;
- } else {
- console.error("Toast 加载出错!");
- }
- },
- onTestToken() {
- utils.onTestToken();
- },
- onDestroy() {
- if (this.AutoSaveData) {
- this.unschedule(this.AutoSaveData);
- this.AutoSaveData = null;
- }
- }
- });
|