GameData.js 51 KB

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