GameData.js 57 KB

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