GameData.js 46 KB

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