GameData.js 50 KB

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