GameData.js 47 KB

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