GameData.js 52 KB

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