GameData.js 40 KB

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