GameData.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. var reGameStates = require('GameStates');
  2. var AConfig = require('../Config');
  3. import utils from "../Network/netUtils";
  4. import GameNet from "../Network/GameNet"
  5. import gameToast from "../Network/gameToast"
  6. //全局数据类
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. //记录全部道路的index
  11. GameData_highwayIndex: {
  12. default: [],
  13. type: [cc.Integer],
  14. visible: false,
  15. serializable: false,
  16. },
  17. //记录建筑物存储的信息
  18. GameData_buildings: {
  19. default: [],
  20. visible: false,
  21. serializable: false,
  22. },
  23. HighWayPrefabs: cc.Prefab,
  24. //MyMapNode
  25. //建筑物的节点
  26. BuildingsParent: {
  27. default: null,
  28. type: cc.Node,
  29. },
  30. ManageUI: {
  31. default: null,
  32. type: cc.Node,
  33. },
  34. GameVersion: {
  35. default: -1,
  36. type: cc.Integer,
  37. },
  38. //读取数据
  39. readData: {
  40. default: null,
  41. visible: false,
  42. },
  43. /**
  44. * 接口
  45. * todo cnt和snb 由钱包读取
  46. */
  47. CNT: {
  48. default: 0,
  49. visible: false,
  50. },
  51. SNB: {
  52. default: 0,
  53. visible: false,
  54. },
  55. //站内收益
  56. CNTDrawBalance: {
  57. default: 0,
  58. visible: false,
  59. },
  60. //读取的土地文件
  61. ConfigLand: {
  62. default: null,
  63. visible: false
  64. },
  65. toast: null,
  66. scheduleObj: null,
  67. toastCallback: null,
  68. isStart: true,
  69. isInit: false,
  70. /**
  71. * 限制重复触发支付, 正在支付中转态,
  72. */
  73. isPlayingCnt: false,
  74. isPlayingSnb: false,
  75. /**
  76. * 处理请求
  77. */
  78. //现在多次触发
  79. isOnAddFruit: false,
  80. isOnSaleFruit: false,
  81. isOnGrantFruit: false,
  82. //自动存储数据
  83. AutoSaveData: null
  84. },
  85. onLoad() {
  86. //初始化GameData全局变量
  87. GlobalD.GameData = this;
  88. this.GameConfig();
  89. },
  90. GameConfig() {
  91. this.AddBuildingCost = 5;
  92. this.RemoveBuildingCost = 5;
  93. //
  94. this.Dapp = {
  95. UserInfo: null
  96. }
  97. },
  98. start() {
  99. cc.loader.loadRes("prefab/gameToast", function (err, texture) {
  100. this.toast = cc.instantiate(texture);
  101. this.toast.parent = cc.find("Canvas/UICamera");
  102. this.toast.zIndex = 999;
  103. this.toast.active = false;
  104. this.scheduleObj = this.schedule(() => {
  105. this.updateToast();
  106. }, 1);
  107. }.bind(this));
  108. this._tiledMap = GlobalD.TiledMap._tiledMap;
  109. //调用初始化dapp
  110. this.isDebugMode(GlobalD.dapp);
  111. },
  112. /**
  113. * 根据 不存在dapp 的话,使用本地测试
  114. * @param {*} bInit
  115. */
  116. isDebugMode: function (bInit) {
  117. if (bInit) {
  118. //读取网络数据
  119. try {
  120. GlobalD.dapp.cntBalance().then((cntBalance) => {
  121. //会延迟返回
  122. // console.log("获取cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
  123. GlobalD.GameData.SetCNT(cntBalance);
  124. });
  125. } catch (err) {
  126. console.error(err) // 初始化失败,运行环境不是钱包环境
  127. }
  128. //登录时候已经初始化好snb了
  129. this.SNB = GlobalD.UserInfo.snb;
  130. this.readData = userData.readData;
  131. // cc.log('playerPullInfo 读取到数据?:', this.readData)
  132. this.InitNextworkData();
  133. this.InitSceneInfo();
  134. // 自动存储数据
  135. /**
  136. * todo 如果退出游戏,触发一次存储。
  137. */
  138. this.AutoSaveData = function () {
  139. this.pushData(true);
  140. };
  141. this.schedule(this.AutoSaveData, 15);
  142. } else {
  143. // 清除
  144. cc.log("本地数据重新开始,并且不初始化");
  145. // this.onClearAllData();
  146. this.Init();
  147. this.InitSceneInfo();
  148. this.AutoSaveData = function () {
  149. this.pushData(false);
  150. };
  151. this.schedule(this.AutoSaveData, 10);
  152. }
  153. },
  154. getData: function () {
  155. //如果开局没有读取到网络数据,就这里初始化
  156. if (userData.readData == null) {
  157. cc.log("本地数据:", cc.sys.localStorage.getItem('userdata'));
  158. let userdata = cc.sys.localStorage.getItem('userdata');
  159. if (userdata) {
  160. this.readData = JSON.parse(userdata);
  161. }
  162. this.Init();
  163. this.InitSceneInfo();
  164. } else {
  165. this.readData = userData.readData;
  166. cc.log('读取到数据?:', this.readData)
  167. this.Init();
  168. this.InitSceneInfo();
  169. }
  170. },
  171. InitSceneInfo() {
  172. //根据顺序生成
  173. //初始化时间ui
  174. cc.find("GameNode/ManageTimer").getComponent('ManageTimer').Init();
  175. //初始化金钱ui,gold,diamond, cnt,snb,
  176. cc.find("GameNode/ManageGolden").getComponent('ManageGolden').InitManageGlodenUI();
  177. //领取ui
  178. this.ManageUI.getComponent("ManageUI").DiamondNowlabel.string = this.GetDiamond() + '/30000'
  179. // //初始化地图物件
  180. // cc.find("GameNode/ManageMap").getComponent('ManageMap').InitManageMap();
  181. GlobalD.TiledMap.onInitSolid();
  182. //初始化生成房屋
  183. /**
  184. * dapp设定: 如果固定土地冲突,删除冲突的建筑和回收仓库
  185. */
  186. this.ManageUI.getComponent('ManageBuildings').InitBuildings();
  187. //初始化道路
  188. /**
  189. * 原本游戏设定是先生产道路,现在为了方便处理等房屋生产后,再处理公路
  190. * dapp设定: 如果固定土地冲突,删除对应的公路
  191. */
  192. this.onSpawnHighway();
  193. //初始化生成人物
  194. /**
  195. * dapp设定: 如果固定土地位置冲突,把人物生成位置放置到主路
  196. * onSpawnWorkerAIFromStoredData 此函数处理
  197. */
  198. cc.find('GameNode/ManageWorker').getComponent('ManageWorker').InitWorkerAI();
  199. let _BFirstLoadGame = this.readData ? this.readData.BFirstLoadGame : 0;
  200. //初始化新手教学
  201. GlobalD.ManageTask.InitTask(_BFirstLoadGame);
  202. this.ManageUI.getComponent('ManageUI').Init();
  203. },
  204. InitNextworkData() {
  205. // console.log("this.readData", this.readData);
  206. this.Golden = this.readData.Golden;
  207. this.Diamond = this.readData.Diamond;
  208. this.shareGive = this.readData.shareGive;
  209. //签到分享给钻石金币
  210. this.signInGive = this.readData.signInGive;
  211. //每个月 给的 低保
  212. this.EveryGive = this.readData.EveryGive;
  213. //公共分享
  214. this.publicGive = this.readData.publicGive;
  215. // cc.log('初始化网络数据');
  216. //读取日期数据
  217. this.GameYear = this.readData.GameYear;
  218. this.GameMonth = this.readData.GameMonth;
  219. this.GameDay = this.readData.GameDay;
  220. //读取金币
  221. this.Golden = this.readData.Golden;
  222. //读取钻石
  223. this.Diamond = this.readData.Diamond;
  224. // cc.log('this.readData.Diamond', this.readData.Diamond);
  225. //读取工人等级
  226. this.WorkerLV = this.readData.WorkerLV;
  227. //工人数量
  228. this.WorkerNum = this.readData.WorkerNum;
  229. //工人容量
  230. this.WorkerCapacity = this.readData.WorkerCapacity;
  231. this.GameDate = this.readData.GameDate;
  232. this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
  233. this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
  234. this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
  235. this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
  236. //面板状态
  237. this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
  238. //解锁设置为 不用解锁
  239. // this.BuildingLockStateArray.forEach((value, index, array) => {
  240. // array[index] = 1;
  241. // })
  242. this.BuildingLockStateArray = this.readData.BuildingLockStateArray;//.split('_');
  243. this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
  244. this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
  245. this.FoodTradeState = this.readData.FoodTradeState;
  246. this.WoodTradeState = this.readData.WoodTradeState;
  247. this.MineralTradeState = this.readData.MineralTradeState;
  248. //工人工作信息
  249. if (this.readData.characterInfoArray) {
  250. this.CharacterInfoArray = this.readData.characterInfoArray;
  251. } else {
  252. this.CharacterInfoArray = [];
  253. }
  254. //道路数据
  255. if (this.readData.highwayIndex)
  256. this.GameData_highwayIndex = this.readData.highwayIndex;
  257. //建筑物数据
  258. if (this.readData.buildingsInfo)
  259. this.GameData_buildings = this.readData.buildingsInfo;
  260. this.LotteryTimes = this.readData.LotteryTimes;
  261. //任务
  262. if (this.readData.TaskIconCountClick)
  263. task.TaskIconCountClick = this.readData.TaskIconCountClick;
  264. },
  265. /**
  266. * 初始化
  267. */
  268. Init: function () {
  269. let _BFirstLoadGame = false;// this.readData ? this.readData.BFirstLoadGame : 0;
  270. //date
  271. this.GameYear = 0;
  272. this.GameMonth = 0;
  273. this.GameDay = 0;
  274. this.GameDate = '0000/00/01';
  275. /**
  276. * 新增 CNT 和 SNB
  277. */
  278. this.CNT = 0;
  279. this.SNB = 0;
  280. this.Golden = 2000;
  281. this.Diamond = 100;
  282. this.WorkerLV = 0;
  283. this.WorkerNum = 0;
  284. this.CharacterInfoArray = [];
  285. this.WorkerCapacity = 5;
  286. this.TerritoryStateArray = [1, 1, 1, 1, 1, 1, 1, 1];//测试,全开地图
  287. /**
  288. * 添加建筑往后添加,到时候ManageBuildings 脚本 会初始化根据顺序
  289. * BuildingStateArray---相关 GetBuildingStateArray 获取建筑状态
  290. * BuildingNumArray ---相关 GetBuildingNumArray 获取建筑数量
  291. * BuildingFrameArray ---相关 ManageUI 添加面板是否解锁之类的相关预制
  292. */
  293. this.BuildingStateArray = [
  294. 1, //公路
  295. 1, //路铲
  296. 1, //拆迁
  297. 1, //农舍
  298. 1, //单元楼
  299. 1, //别墅
  300. 1, //农田
  301. 1,//伐木场
  302. 1, //矿坑
  303. 1, //加工厂
  304. 1,//冷饮摊
  305. 1,//贩卖机
  306. 1,//面包房
  307. 1,//早餐车
  308. 1, //饮茶店
  309. 1,//点心店
  310. 1, //美食店
  311. 1,//西餐厅
  312. 1, //花店
  313. 1, //美发店
  314. 1,//洋装店
  315. 1, //珠宝店
  316. 1, //电影院
  317. 1,//路灯
  318. 1, //绿化带
  319. 1, //花坛
  320. 1,//喷泉
  321. 1, //警察局
  322. 1,//游乐场
  323. 1, //蓝色城堡
  324. 1, //粉色城堡
  325. 1, //Holy Farmland
  326. 1 //Holy Farmland seed
  327. ];
  328. this.BuildingLockStateArray = [
  329. 1, //公路
  330. 1, //路铲
  331. 1, //拆迁
  332. 1, //农舍
  333. 0, //单元楼
  334. 0, //别墅
  335. 1, //农田
  336. 0,//伐木场
  337. 0, //矿坑
  338. 1, //加工厂
  339. 0,//冷饮摊
  340. 0,//贩卖机
  341. 0,//面包房
  342. 0,//早餐车
  343. 0, //饮茶店
  344. 0,//点心店
  345. 0, //美食店
  346. 0,//西餐厅
  347. 0, //花店
  348. 0, //美发店
  349. 0,//洋装店
  350. 0, //珠宝店
  351. 0, //电影院
  352. 1,//路灯
  353. 1, //绿化带
  354. 1, //花坛
  355. 0,//喷泉
  356. 0, //警察局
  357. 0,//游乐场
  358. 0, //蓝色城堡
  359. 0, //粉色城堡
  360. 1, //Holy Farmland
  361. 1, //Holy Farmland seed
  362. ];
  363. //建筑物数量
  364. this.BuildingNumArray = [
  365. 0, //公路
  366. 0, //路铲
  367. 0, //拆迁
  368. 4, //农舍
  369. 1, //单元楼
  370. 1, //别墅
  371. 4, //农田
  372. 1,//伐木场
  373. 1, //矿坑
  374. 2, //加工厂
  375. 2,//冷饮摊
  376. 1,//贩卖机
  377. 1,//面包房
  378. 1,//早餐车
  379. 1, //饮茶店
  380. 1,//点心店
  381. 1, //美食店
  382. 1,//西餐厅
  383. 1, //花店
  384. 1, //美发店
  385. 1,//洋装店
  386. 1, //珠宝店
  387. 1, //电影院
  388. 1,//路灯
  389. 1, //绿化带
  390. 1, //花坛
  391. 1,//喷泉
  392. 1, //警察局
  393. 1,//游乐场
  394. 1, //蓝色城堡
  395. 1, //粉色城堡
  396. 1, //Holy Farmland
  397. 1, //Holy Farmland seed
  398. ];
  399. this.DiamondNumArray = AConfig.DiamondArray;
  400. //todo 钻石消耗默认值
  401. this.DiamondNumArray.forEach((value, index, array) => {
  402. array[index] = 0;
  403. })
  404. cc.log('钻石消耗默认值', this.DiamondNumArray);
  405. this.EveryDayRewardsArray = [0, 0, 0, 0, 0, 0, 0];
  406. this.LastTimeEveryDayRewardsDate = '0000/00/00';
  407. this.LastTimeLuckDate = '0000/00/00';
  408. this.FoodTradeState = 1;
  409. this.WoodTradeState = 1;
  410. this.MineralTradeState = 1;
  411. //转盘分享给钻石金币
  412. this.shareGive = [200, 5];
  413. //签到分享给钻石金币
  414. this.signInGive = [200, 5];
  415. //每个月 给的 低保
  416. this.EveryGive = [500, 10];
  417. //公共分享
  418. this.publicGive = [200, 50];
  419. //每天抽奖次数
  420. this.LotteryTimes = 10;
  421. // 1 === _BFirstLoadGame
  422. if (_BFirstLoadGame) {
  423. console.log("this.readData", this.readData);
  424. // cc.log('初始化网络数据');
  425. //读取日期数据
  426. this.GameYear = this.readData.GameYear;
  427. this.GameMonth = this.readData.GameMonth;
  428. this.GameDay = this.readData.GameDay;
  429. /**
  430. * 接口
  431. * todo cnt和snb 由钱包读取
  432. */
  433. this.CNT = 0;
  434. this.SNB = 0;
  435. //读取金币
  436. this.Golden = this.readData.Golden;
  437. //读取钻石
  438. this.Diamond = this.readData.Diamond;
  439. // cc.log('this.readData.Diamond', this.readData.Diamond);
  440. //读取工人等级
  441. this.WorkerLV = this.readData.WorkerLV;
  442. //工人数量
  443. this.WorkerNum = this.readData.WorkerNum;
  444. //工人容量
  445. this.WorkerCapacity = this.readData.WorkerCapacity;
  446. this.GameDate = this.readData.GameDate;
  447. this.LastTimeEveryDayRewardsDate = this.readData.LastTimeEveryDayRewardsDate;
  448. this.LastTimeLuckDate = this.readData.LastTimeLuckDate;
  449. this.EveryDayRewardsArray = this.readData.EveryDayRewardsArray;//.split('_');
  450. this.TerritoryStateArray = this.readData.TerritoryStateArray;//.split('_');
  451. //面板状态
  452. this.BuildingStateArray = this.readData.BuildingStateArray;//.split('_');
  453. //解锁设置为 不用解锁
  454. this.BuildingLockStateArray.forEach((value, index, array) => {
  455. array[index] = 1;
  456. })
  457. // this.BuildingLockStateArray = this.readData.BuildingLockStateArray.split('_');
  458. this.BuildingNumArray = this.readData.BuildingNumArray;//.split('_');
  459. this.DiamondNumArray = this.readData.DiamondNumArray;//.split('_');
  460. this.FoodTradeState = this.readData.FoodTradeState;
  461. this.WoodTradeState = this.readData.WoodTradeState;
  462. this.MineralTradeState = this.readData.MineralTradeState;
  463. //工人工作信息
  464. if (this.readData.characterInfoArray)
  465. this.CharacterInfoArray = this.readData.characterInfoArray;
  466. //道路数据
  467. if (this.readData.highwayIndex)
  468. this.GameData_highwayIndex = this.readData.highwayIndex;
  469. //建筑物数据
  470. if (this.readData.buildingsInfo)
  471. this.GameData_buildings = this.readData.buildingsInfo;
  472. this.LotteryTimes = this.readData.LotteryTimes;
  473. //任务
  474. if (this.readData.TaskIconCountClick)
  475. task.TaskIconCountClick = this.readData.TaskIconCountClick;
  476. //测试5W
  477. // this.PlusDiamond(50000);
  478. }
  479. },
  480. //GET / SET /Plus
  481. GetGameDate: function () {
  482. return this.GameDate;
  483. },
  484. SetGameDate: function (num) {
  485. this.GameDate = num;
  486. },
  487. PlusGameDate: function (num) {
  488. this.GameDate += num;
  489. },
  490. GetGolden: function () {
  491. return parseInt(this.Golden);
  492. },
  493. GetGoldenCallBack(_CallBack) {
  494. if (_CallBack)
  495. _CallBack({ resGolden: parseInt(this.Golden) });
  496. },
  497. SetGolden: function (Num) {
  498. let LastMoney = this.Golden;
  499. this.Golden = Num;
  500. let CurrentMoney = Num;
  501. this.ManageUI.getComponent('ManageUI').GoldenChangeCallBack(this.GetGolden(), LastMoney, CurrentMoney);
  502. },
  503. PlusGolden: function (Num) {
  504. if (this.Golden + Num < 0) {
  505. this.SetGolden(0);
  506. }
  507. else {
  508. this.SetGolden(this.Golden + Num);
  509. }
  510. // task.task50W();
  511. },
  512. GetDiamond: function () {
  513. return parseInt(this.Diamond);
  514. },
  515. SetDiamond: function (num) {
  516. let LastMoney = this.Diamond;
  517. this.Diamond = num;
  518. let CurrentMoney = num;
  519. this.ManageUI.getComponent("ManageUI").DiamondNowlabel.string = this.Diamond + '/30000'
  520. this.ManageUI.getComponent('ManageUI').DiamondChangeCallBack(this.GetDiamond(), LastMoney, CurrentMoney);
  521. },
  522. PlusDiamond: function (num) {
  523. if (this.Diamond + num < 0) {
  524. this.SetDiamond(0);
  525. }
  526. else {
  527. this.SetDiamond(this.Diamond + num);
  528. }
  529. },
  530. /**
  531. * 游戏里面的cnt 和神农呗
  532. */
  533. GetCNT: function () {
  534. return parseFloat(this.CNT);
  535. },
  536. SetCNT: function (Num) {
  537. this.CNT = Num;
  538. this.ManageUI.getComponent('ManageUI').CNTChangeCallBack(this.CNT);
  539. },
  540. GetSNB: function () {
  541. return parseFloat(this.SNB);
  542. },
  543. SetSNB: function (Num) {
  544. this.SNB = Num;
  545. this.ManageUI.getComponent('ManageUI').SNBChangeCallBack(this.SNB);
  546. },
  547. GetWorkerLV: function () {
  548. return parseInt(this.WorkerLV);
  549. },
  550. SetWorkerLV: function (num) {
  551. this.WorkerLV = num;
  552. },
  553. PlusWorkerLV: function (num) {
  554. this.WorkerLV += num;
  555. },
  556. GetWorkerNum: function () {
  557. return parseInt(this.WorkerNum);
  558. },
  559. SetWorkerNum: function (num) {
  560. this.WorkerNum = num;
  561. },
  562. PlusWorkerNum: function (num) {
  563. this.WorkerNum += num;
  564. },
  565. GetLastTimeEveryDayRewardsDate: function () {
  566. return this.LastTimeEveryDayRewardsDate;
  567. },
  568. //获取转盘 时间
  569. GetLastTimeLuckDate: function () {
  570. return this.LastTimeLuckDate;
  571. },
  572. //设置转盘时间
  573. SetLastTimeLuckDate: function (DateString) {
  574. this.LastTimeLuckDate = DateString;
  575. },
  576. SetLastTimeEveryDayRewardsDate: function (DateString) {
  577. this.LastTimeEveryDayRewardsDate = DateString;
  578. },
  579. //工人信息数组
  580. GetWorkerCharacterInfoArray: function () {
  581. return this.CharacterInfoArray;
  582. },
  583. SetWorkerCharacterInfoArray: function (item) {
  584. this.CharacterInfoArray = item;
  585. },
  586. /*** */
  587. GetWorkerCapacity: function () {
  588. return parseInt((this.WorkerCapacity));
  589. },
  590. SetWorkerCapacity: function (num) {
  591. this.WorkerCapacity = num;
  592. },
  593. PlusWorkerCapacity: function (num) {
  594. this.WorkerCapacity += num;
  595. },
  596. GetTerritoryStateArray: function () {
  597. return this.TerritoryStateArray;
  598. },
  599. SetTerritoryStateArray: function (aTerritoryStateArray) {
  600. this.TerritoryStateArray = aTerritoryStateArray;
  601. },
  602. PlusTerritoryStateArray: function (Item) {
  603. this.TerritoryStateArray.push(Item);
  604. },
  605. GetBuildingStateArray: function () {
  606. return this.BuildingStateArray;
  607. },
  608. SetBuildingStateArray: function (aBuildingStateArray) {
  609. this.BuildingStateArray = aBuildingStateArray;
  610. },
  611. PlusBuildingStateArray: function (Item) {
  612. this.BuildingStateArray.push(Item);
  613. },
  614. GetBuildingLockStateArray: function () {
  615. return this.BuildingLockStateArray;
  616. },
  617. SetBuildingLockStateArray: function (aArray) {
  618. this.BuildingLockStateArray = aArray;
  619. },
  620. PlusBuildingLockStateArray: function (Item) {
  621. this.BuildingLockStateArray.push(Item);
  622. },
  623. GetBuildingNumArray: function () {
  624. return this.BuildingNumArray;
  625. },
  626. SetBuildingNumArray: function (aBuildingNumArray) {
  627. this.BuildingNumArray = aBuildingNumArray;
  628. },
  629. PlusBuildingNumArray: function (Item) {
  630. this.BuildingNumArray.push(Item);
  631. },
  632. //操作钻石数据
  633. GetDiamondNumArray: function () {
  634. return this.DiamondNumArray;
  635. },
  636. SetDiamondNumArray: function (aDiamondNumArray) {
  637. this.DiamondNumArray = aDiamondNumArray;
  638. },
  639. GetEveryDayRewardsArray: function () {
  640. return this.EveryDayRewardsArray;
  641. },
  642. SetEveryDayRewardsArray: function (aArray) {
  643. this.EveryDayRewardsArray = aArray;
  644. },
  645. PlusEveryDayRewardsArray: function (Item) {
  646. this.EveryDayRewardsArray.push(Item);
  647. },
  648. GetFoodTradeState: function () {
  649. return parseInt((this.FoodTradeState));
  650. },
  651. SetFoodTradeState: function (num) {
  652. this.FoodTradeState = num;
  653. },
  654. PlusFoodTradeState: function (num) {
  655. this.FoodTradeState += num;
  656. },
  657. GetWoodTradeState: function () {
  658. return parseInt((this.WoodTradeState));
  659. },
  660. SetWoodTradeState: function (num) {
  661. this.WoodTradeState = num;
  662. },
  663. PlusWoodTradeState: function (num) {
  664. this.WoodTradeState += num;
  665. },
  666. GetMineralTradeState: function () {
  667. return parseInt((this.MineralTradeState));
  668. },
  669. SetMineralTradeState: function (num) {
  670. this.MineralTradeState = num;
  671. },
  672. PlusMineralTradeState: function (num) {
  673. this.MineralTradeState += num;
  674. },
  675. GetLotteryTimes: function () {
  676. return parseInt((this.LotteryTimes));
  677. },
  678. SetLotteryTimes: function (num) {
  679. this.LotteryTimes = num;
  680. },
  681. PlusLotteryTimes: function (num) {
  682. this.LotteryTimes += num;
  683. },
  684. //初始化道路
  685. onSpawnHighway() {
  686. // 获取InitPoint层
  687. let InitPos = this._tiledMap.getLayer('InitPoint');
  688. InitPos.enabled = false;
  689. console.log("onSpawnHighway");
  690. //创建公路数据
  691. if (this.readData && this.readData.allHighwayStylesAndIndex) {
  692. //保存到内存中
  693. this.GameData_highwayIndex = this.readData.highwayIndex;
  694. //先赋值道路的对象数组
  695. let _AllHighwayAIndex = Object.assign([], this.readData.allHighwayStylesAndIndex);
  696. //获取最后建造公路的层级
  697. this.HighwayLayer = this._tiledMap.getLayer('Highway');
  698. for (let i = 0; i < _AllHighwayAIndex.length; i++) {
  699. let _tiledPos = GlobalD.TiledMap.analyticalIndexData(_AllHighwayAIndex[i].highwayInfoIndex);
  700. /**
  701. * 解决固定土地时候,道路 和房子 冲突
  702. *
  703. */
  704. if (GlobalD.game.getManageGameIndexArrayAt(_tiledPos)) {
  705. continue;
  706. }
  707. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  708. highwayTemp.parent = this.HighwayLayer.node;
  709. let tiledTile = highwayTemp.addComponent('TiledTile');
  710. tiledTile.x = _tiledPos.x;
  711. tiledTile.y = _tiledPos.y;
  712. //地图设置可行走区域公路设置
  713. AStar.setMapSolid(_tiledPos.x, _tiledPos.y, 0);
  714. // _buildId ==0 是公路
  715. let _buildId = 0;
  716. let occupyTemp = cc.v2(_buildId, _AllHighwayAIndex[i].highwayInfoIndex);
  717. GlobalD.game.OccupyArray.push(occupyTemp);
  718. highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(_AllHighwayAIndex[i].highwayInfoType);
  719. GlobalD.game.AllHighwayStylesAndIndex.push(_AllHighwayAIndex[i]);
  720. }
  721. // console.log("end");
  722. } else {
  723. //如果用户没有存储数据
  724. //根据InitPoint 的数据,创建初始化的地图数据 的初始化数据
  725. this.HighwayLayer = this._tiledMap.getLayer('Highway');
  726. for (var i = 0; i < 32; i++) {
  727. //去除中间两条路
  728. if (i == 16 || i == 17) continue;
  729. for (var j = 0; j < 32; j++) {
  730. let tilesPos = cc.v2(i, j);
  731. if (InitPos.getTileGIDAt(tilesPos)) {
  732. let index = GlobalD.TiledMap.getIndex(tilesPos);
  733. this.GameData_highwayIndex.push(index);
  734. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  735. highwayTemp.parent = this.HighwayLayer.node;
  736. let tiledTile = highwayTemp.addComponent('TiledTile');
  737. tiledTile.x = tilesPos.x;
  738. tiledTile.y = tilesPos.y;
  739. // cc.log('onSpawnHighway2');
  740. //地图设置可行走区域公路设置
  741. AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
  742. // _buildId ==0 是公路
  743. let _buildId = 0;
  744. let occupyTemp = cc.v2(_buildId, index);
  745. GlobalD.game.OccupyArray.push(occupyTemp);
  746. let tile = InitPos.getTiledTileAt(tilesPos.x, tilesPos.y, true);
  747. let _MoveType = reGameStates.HighwayType.moveX;
  748. // if (tile.gid == 15) {
  749. // _MoveType = reGameStates.HighwayType.moveX;
  750. // } else
  751. if (tile.gid == 16) {
  752. _MoveType = reGameStates.HighwayType.moveY;
  753. } else if (tile.gid == 11) {
  754. _MoveType = reGameStates.HighwayType.ZebraCrossingX;
  755. } else if (tile.gid == 12) {
  756. _MoveType = reGameStates.HighwayType.ZebraCrossingY;
  757. }
  758. // return;
  759. //更换公路样式
  760. GlobalD.game.onCreateDifferentRoadStyles({
  761. _buildId: _buildId,
  762. _highwayType: _MoveType,
  763. _roadIndex: index,
  764. _hightwayNode: highwayTemp
  765. });
  766. }
  767. }
  768. }
  769. }
  770. //外面的两条路
  771. let initRoad = this._tiledMap.getLayer('Road');
  772. initRoad.enabled = false;
  773. for (var i = 16; i < 18; i++) {
  774. for (var j = 0; j < 32; j++) {
  775. let tilesPos = cc.v2(i, j);
  776. // cc.log('road tilesPos',tilesPos)
  777. if (initRoad.getTileGIDAt(tilesPos)) {
  778. let index = GlobalD.TiledMap.getIndex(tilesPos);
  779. this.GameData_highwayIndex.push(index);
  780. let highwayTemp = cc.instantiate(this.HighWayPrefabs);
  781. highwayTemp.parent = this.HighwayLayer.node;
  782. let tiledTile = highwayTemp.addComponent('TiledTile');
  783. tiledTile.x = tilesPos.x;
  784. tiledTile.y = tilesPos.y;
  785. // cc.log('initRoad');
  786. //地图设置可行走区域公路设置
  787. AStar.setMapSolid(tilesPos.x, tilesPos.y, 0);
  788. let _buildId = 0;
  789. let occupyTemp = cc.v2(_buildId, index);
  790. GlobalD.game.OccupyArray.push(occupyTemp);
  791. highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.none);
  792. //更换公路样式
  793. // if (j >= 25 && j <= 27 || j >= 19 && j <= 21 || j >= 9 && j <= 11)
  794. // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.none);
  795. // else
  796. // highwayTemp.getComponent('HighwayInfo').onChangeHighwayStyles(reGameStates.HighwayType.moveY);
  797. }
  798. }
  799. }
  800. },
  801. //清除所有的数据
  802. onClearAllData() {
  803. this.unschedule(this.AutoSaveData);
  804. //任务索引
  805. cc.sys.localStorage.removeItem('userdata');
  806. //跳回登录场景
  807. // cc.director.loadScene('Login');
  808. },
  809. //保存数据请求
  810. /**
  811. *
  812. * 现在转服务器存储,原本所有相关操作通过服务器计算。
  813. * 一、目前涉及到的是 SNT 和 SNB 两个货币相关,和买田地操作 (可以理解为租聘,三种类型)
  814. *
  815. * isNetwork:true,上传到网络
  816. *
  817. */
  818. pushData: function (isNetwork) {
  819. // cc.log(this.EveryDayRewardsArray);
  820. // console.log(this.readData.BFirstLoadGame);
  821. var datas = {
  822. version: this.GameVersion,
  823. // 转后台处理
  824. BFirstLoadGame: 1,//只要push数据,就证明已经开始第一次游戏了
  825. /**游戏不修改,默认值 start */
  826. shareGive: this.shareGive,
  827. //签到分享给钻石金币
  828. signInGive: this.signInGive,
  829. //每个月 给的 低保
  830. EveryGive: this.EveryGive,
  831. //公共分享
  832. publicGive: this.publicGive,
  833. /**游戏不修改,默认值 end */
  834. //时间
  835. GameYear: this.GameYear,
  836. GameMonth: this.GameMonth,
  837. GameDay: this.GameDay,
  838. //这里只是单纯的存储起来
  839. CNT: this.CNT,
  840. SNB: this.SNB,
  841. Golden: this.Golden,
  842. Diamond: this.Diamond,
  843. WorkerLV: this.WorkerLV,
  844. WorkerNum: this.WorkerNum,
  845. WorkerCapacity: this.WorkerCapacity,
  846. GameDate: this.GameDate,
  847. LastTimeEveryDayRewardsDate: this.LastTimeEveryDayRewardsDate,
  848. LastTimeLuckDate: this.LastTimeLuckDate,
  849. EveryDayRewardsArray: this.EveryDayRewardsArray,//.join('_'),
  850. TerritoryStateArray: this.TerritoryStateArray,//.join('_'),
  851. //面板状态
  852. BuildingStateArray: this.BuildingStateArray,//.join('_'),
  853. BuildingLockStateArray: this.BuildingLockStateArray,//.join('_'),
  854. /**
  855. * SNB和CNT相关,需要后台筛选 田地 和 种子。单独处理
  856. */
  857. BuildingNumArray: this.BuildingNumArray,//.join('_'),
  858. DiamondNumArray: this.DiamondNumArray,//.join('_'),
  859. FoodTradeState: this.FoodTradeState,
  860. WoodTradeState: this.WoodTradeState,
  861. MineralTradeState: this.MineralTradeState,
  862. LotteryTimes: this.LotteryTimes,
  863. /**
  864. * 这些数据不用服务器初始化
  865. */
  866. //工人工作信息
  867. characterInfoArray: this.CharacterInfoArray,
  868. //道路数据
  869. highwayIndex: this.GameData_highwayIndex,
  870. allHighwayStylesAndIndex: GlobalD.game.AllHighwayStylesAndIndex,
  871. //建筑物数据
  872. buildingsInfo: this.GameData_buildings,
  873. //任务
  874. TaskIconCountClick: task.TaskIconCountClick
  875. }
  876. if (isNetwork) {
  877. var data = [];
  878. // data["openid"] = userData.openId;
  879. // data["userdata"] = JSON.stringify(datas);
  880. data["playerData"] = JSON.stringify(datas);
  881. /** 推送信息 */
  882. utils.post(utils.api.playerPushInfo, data, (res, playerPushInfoTemp) => {
  883. // console.log('playerPushInfoTemp', playerPushInfoTemp);
  884. })
  885. } else {
  886. console.log("存储本地:" + isNetwork);
  887. //保存用户数据到本地
  888. cc.sys.localStorage.setItem('userdata', JSON.stringify(datas));
  889. }
  890. },
  891. /**
  892. * 兑换种子时候,特殊处理数据
  893. * 扣除相应的钻石
  894. */
  895. exchangeSeed(diamondAmount, callback) {
  896. //先暂停存储
  897. this.unschedule(this.AutoSaveData);
  898. utils.post(utils.api.exchangeSeeds, { diamondAmount: diamondAmount, seedId: 8 }, (res, value) => {
  899. console.log("兑换种子", value.code, value.msg);
  900. //兑换成功后
  901. if (0 === value.code) {
  902. //更新一次数据
  903. let _playerData = JSON.parse(value.data.playerData);
  904. console.log("兑换后的钻石:", _playerData.Diamond);
  905. //更新本地钻石数据
  906. this.SetDiamond(_playerData.Diamond);
  907. }
  908. callback(res, value);
  909. //重新开启存储
  910. this.schedule(this.AutoSaveData, 15);
  911. })
  912. },
  913. /**
  914. * 获取是否租赁过的状态
  915. */
  916. getPlayerExchangeState(callback) {
  917. utils.get(utils.api.playerExchangeState, {}, (res, value) => {
  918. console.log("租赁状态:", value);
  919. callback(res, value);
  920. })
  921. },
  922. /**
  923. * 暂时不需要从这里读取
  924. */
  925. getLandConfig() {
  926. utils.get(utils.api.landConfig, {}, (res, value) => {
  927. if (0 === value.code) {
  928. this.ConfigLand = value.data;
  929. } else {
  930. console.warn("未能读取到config土地?");
  931. }
  932. })
  933. },
  934. //获取用户全部租赁土地
  935. getUserLandList() {
  936. utils.get(utils.api.userLandList, {}, (res, vaule) => {
  937. })
  938. },
  939. //获取用户已租赁土地的状态
  940. getLandState(context) {
  941. let { landId, callback } = context;
  942. utils.get(utils.api.landState, { landId: landId }, (res, vaule) => {
  943. callback(res, vaule);
  944. })
  945. },
  946. //获取商城种子
  947. getMallSeed(callback) {
  948. utils.get(utils.api.mallSeed, {}, (res, vaule) => {
  949. callback(res, vaule);
  950. })
  951. },
  952. getWarehouseSeedAndFruit(callback) {
  953. utils.get(utils.api.getSeedAndFruit, {}, (res, vaule) => {
  954. callback(res, vaule);
  955. })
  956. },
  957. //获取种子状态
  958. onGetSeedState(data, callback) {
  959. utils.get(utils.api.getSeedState, data, (res, vaule) => {
  960. callback(res, vaule);
  961. })
  962. },
  963. //种植种子
  964. // data:{landId:1,seedId:1}
  965. onPlant(data, callback) {
  966. utils.get(utils.api.plant, data, (res, vaule) => {
  967. callback(res, vaule);
  968. })
  969. },
  970. //收取果实
  971. onAddFruit(data, callback) {
  972. if (this.isOnAddFruit) {
  973. console.log("收取果实过快!");
  974. return;
  975. }
  976. this.isOnAddFruit = true;
  977. utils.post(utils.api.addFruit, data, (res, vaule) => {
  978. this.isOnAddFruit = false;
  979. callback(res, vaule);
  980. })
  981. },
  982. //出售果实
  983. onSaleFruit(data, callback) {
  984. if (this.isOnSaleFruit) {
  985. console.log("出售果实过快!");
  986. return;
  987. }
  988. this.isOnSaleFruit = true;
  989. utils.post(utils.api.saleFruit, data, (res, vaule) => {
  990. this.isOnSaleFruit = false;
  991. callback(res, vaule);
  992. })
  993. },
  994. //赠送果实
  995. onGrantFruit(data, callback) {
  996. if (this.isOnGrantFruit) {
  997. console.log("赠送果实过快!");
  998. return;
  999. }
  1000. this.isOnGrantFruit = true;
  1001. utils.post(utils.api.grantFruit, data, (res, vaule) => {
  1002. this.isOnGrantFruit = false;
  1003. callback(res, vaule);
  1004. })
  1005. },
  1006. //神农呗购买种子
  1007. onBuySeedsWithSNB(snbAmount, seedId, callback) {
  1008. utils.post(utils.api.snbBuySeeds, { payAmount: snbAmount, seedId: seedId }, (res, vaule) => {
  1009. callback(res, vaule);
  1010. })
  1011. },
  1012. //获取用户的snb
  1013. onGetUserSnb(callback) {
  1014. utils.get(utils.api.userSnbInfo, {}, (res, value) => {
  1015. GlobalD.GameData.SetSNB(value.data.SNB);
  1016. if (callback)
  1017. callback(res, value);
  1018. })
  1019. },
  1020. /**
  1021. * 支付cnt操作
  1022. * @param {*} amount // 需支付金额
  1023. * @param {*} payType 支付类型,1购买土地租凭, 2自然灾害防护,3野兽防护
  1024. * @param {*} itemType 操作物品的id 字符串
  1025. */
  1026. payCNT(cntAmount, payType, itemType, callback) {
  1027. console.log("购买金额:" + cntAmount + "购买类型PlayType:" + payType + "购买物品的id:" + itemType);
  1028. if (!GlobalD.dapp) {
  1029. console.warn("GlobalD.dapp未初始化,不能payCNT!");
  1030. return;
  1031. }
  1032. //
  1033. if (this.isPlayingCnt) {
  1034. console.warn("同时触发支付CNT过快!");
  1035. return;
  1036. }
  1037. if (GlobalD.GameData.GetCNT() < cntAmount) {
  1038. console.log("cnt 不足,GetCNT:" + GlobalD.GameData.GetCNT() + "消费的cntAmount" + cntAmount);
  1039. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "CNT不足!", 2, () => {
  1040. console.log("finish toast! CNT不足!");
  1041. });
  1042. return;
  1043. }
  1044. this.isPlayingCnt = true;
  1045. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "支付处理中...", 5, () => {
  1046. console.log("finish toast!支付处理中...");
  1047. });
  1048. GlobalD.dapp.payCnt(cntAmount, payType, itemType).then((data) => {
  1049. const [err, tx] = data;
  1050. this.isPlayingCnt = false;
  1051. if (err === null) {
  1052. // TODO 成功, 支付为链上操作,需要提供回调接口给这边服务端确认交易成功后修改购买订单结果
  1053. console.log(tx) // 交易hash,唯一标识符
  1054. GlobalD.dapp.cntBalance().then((cntBalance) => {
  1055. //会延迟返回
  1056. // console.log("更新cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
  1057. GlobalD.GameData.SetCNT(cntBalance);
  1058. });
  1059. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "支付成功!", 5, () => {
  1060. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 120);
  1061. });
  1062. } else {
  1063. console.log(err)
  1064. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2, () => {
  1065. console.log("finish toast!");
  1066. });
  1067. }
  1068. if (callback) {
  1069. callback(data);
  1070. }
  1071. });
  1072. },
  1073. /**
  1074. * 神农呗转换CNT ,比例是5:1
  1075. * @param {*} snbAmount
  1076. * @param {*} callback
  1077. * @returns
  1078. */
  1079. onSnbToCnt(snbAmount, callback) {
  1080. if (!GlobalD.dapp) {
  1081. console.warn("GlobalD.dapp未初始化,不能onSnbToCnt!");
  1082. return;
  1083. }
  1084. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "暂不支持兑换CNT!", 2, () => {
  1085. // console.log("finish toast!");
  1086. // });
  1087. // return;
  1088. if (this.isPlayingSnb) {
  1089. console.warn("同时触发支付SNB过快!");
  1090. return;
  1091. }
  1092. if (GlobalD.GameData.GetSNB() <= 0) {
  1093. console.log("神农呗数量GetSNB:", GlobalD.GameData.GetSNB(), "替换snbAmount:", snbAmount);
  1094. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "神农呗数量不足!", 2, () => {
  1095. console.log("finish toast!");
  1096. });
  1097. return;
  1098. }
  1099. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "已发起兑换!", 5, () => {
  1100. console.log("finish toast!");
  1101. });
  1102. this.isPlayingSnb = true;
  1103. GlobalD.dapp.snbToCnt(snbAmount).then((data) => {
  1104. //更新日志数据
  1105. cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();
  1106. const [err, tx] = data;
  1107. this.isPlayingSnb = false;
  1108. if (err === null) {
  1109. // TODO 成功, 兑换为链上操作,需要提供回调接口给这边服务端确认交易成功后修改扣除SNB数量
  1110. console.log(tx) // 交易hash,唯一标识符
  1111. //扣除对应的神农呗,本地修改显示
  1112. GlobalD.GameData.SetSNB(GlobalD.GameData.GetSNB() - snbAmount);
  1113. GlobalD.dapp.cntBalance().then((cntBalance) => {
  1114. //会延迟返回
  1115. // console.log("更新cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
  1116. GlobalD.GameData.SetCNT(cntBalance);
  1117. });
  1118. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "区块确认中,请耐心等待!", 5, () => {
  1119. console.log("finish toast!");
  1120. });
  1121. } else {
  1122. console.log(err)
  1123. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 2, () => {
  1124. console.log("finish toast!");
  1125. });
  1126. }
  1127. if (callback) {
  1128. callback(data);
  1129. }
  1130. });
  1131. },
  1132. //站内收益
  1133. onCntCanWithdrawBalance(callback) {
  1134. if (!GlobalD.dapp) {
  1135. console.warn("GlobalD.dapp未初始化,不能onGetCNTRevenue!");
  1136. return;
  1137. }
  1138. GlobalD.dapp.cntCanWithdrawBalance().then((data) => {
  1139. const { err, res } = data;
  1140. if (err === null) {
  1141. console.log(res) //Number 返回数字
  1142. GlobalD.GameData.CNTDrawBalance = res;
  1143. }
  1144. //todo ,记录一个信息
  1145. if (callback) {
  1146. callback(data);
  1147. }
  1148. });
  1149. },
  1150. //站内收益提现
  1151. onCntWithdraw(amount, callback) {
  1152. if (!GlobalD.dapp) {
  1153. console.warn("GlobalD.dapp未初始化,不能onCntWithdraw!");
  1154. return;
  1155. }
  1156. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "收益正在提现中", 10, () => {
  1157. console.log("finish toast!");
  1158. });
  1159. GlobalD.dapp.cntWithdraw(amount).then((data) => {
  1160. const [err, tx] = data;
  1161. console.log("onCntWithdraw:", data);
  1162. if (err === null) {
  1163. console.log(tx) //String|null 交易唯一哈市
  1164. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "提现成功!", 1, () => {
  1165. console.log("finish toast!");
  1166. });
  1167. } else {
  1168. GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), err, 1, () => {
  1169. console.log("finish toast!");
  1170. });
  1171. }
  1172. if (callback) {
  1173. callback(data);
  1174. }
  1175. });
  1176. },
  1177. //获取账户站内CNT明细(村长和镇长才有)
  1178. onCntLog(page, limit, callback) {
  1179. if (!GlobalD.dapp) {
  1180. console.warn("GlobalD.dapp未初始化,不能onGetSnbToCntInfo!");
  1181. return;
  1182. }
  1183. GlobalD.dapp.cntLog(page, limit).then((data) => {
  1184. const { err, res } = data;
  1185. // if (err === null) {
  1186. // console.log(res) //
  1187. // }
  1188. if (callback) {
  1189. callback(data);
  1190. }
  1191. });
  1192. },
  1193. //获取 神农呗转 CNT 日志
  1194. onGetSnbToCntInfo(page, limit, callback) {
  1195. if (!GlobalD.dapp) {
  1196. console.warn("GlobalD.dapp未初始化,不能onGetSnbToCntInfo!");
  1197. return;
  1198. }
  1199. GlobalD.dapp.swapLog(page, limit).then((data) => {
  1200. const { err, res } = data;
  1201. if (err === null) {
  1202. console.log(res) //
  1203. }
  1204. if (callback) {
  1205. callback(data);
  1206. }
  1207. });
  1208. },
  1209. //获取 snb 流水日志
  1210. onGetSnbInfoList(page, limit, callback) {
  1211. utils.get(utils.api.snbList, { page: page, limit: limit }, (res, value) => {
  1212. // console.log("snb操作日志", value);
  1213. if (0 === value.code) {
  1214. if (callback)
  1215. callback(value.data);
  1216. } else {
  1217. let mySnbList = [];
  1218. if (callback)
  1219. callback(mySnbList);
  1220. }
  1221. })
  1222. },
  1223. updateToast() {
  1224. if (this.toastEndTime <= 0) {
  1225. this.toast.active = false
  1226. if (this.isStart) {
  1227. if (this.toastCallback != null && this.toastCallback != undefined) {
  1228. this.toastCallback();
  1229. // console.log(this.toastCallback);
  1230. }
  1231. this.isStart = false;
  1232. }
  1233. // console.log("this.toast.active:" + this.toast.active);
  1234. }
  1235. this.toastEndTime--;
  1236. },
  1237. showToast(parent, content, time, callback) {
  1238. if (callback) {
  1239. this.toastCallback = callback;
  1240. } else {
  1241. this.toastCallback = null;
  1242. }
  1243. this.isStart = true;
  1244. if (this.toast) {
  1245. // console.log(this.toast.active);
  1246. this.toast.active = true;
  1247. this.toast.parent = parent;
  1248. this.toast.zIndex = 999;
  1249. let DetailLabel = this.toast.getChildByName('DetailLabel');
  1250. DetailLabel.getComponent(cc.Label).string = content;
  1251. this.toastEndTime = time;
  1252. }
  1253. },
  1254. onTestToken() {
  1255. utils.onTestToken();
  1256. },
  1257. onDestroy() {
  1258. if (this.AutoSaveData) {
  1259. this.unschedule(this.AutoSaveData);
  1260. this.AutoSaveData = null;
  1261. }
  1262. }
  1263. });