GameData.js 46 KB

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