GameData.js 45 KB

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