GameData.js 52 KB

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