GameData.js 54 KB

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