GameData.js 60 KB

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