ShopUI.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /** @format */
  2. import {BaseUI} from './BaseUI'
  3. import {Data, Mgr} from '../GameControl'
  4. import {ccUtils} from '../utils/ccUtils'
  5. import {list, node, observer, render} from '../mobx/observer'
  6. import {msgCmd} from '../proto/msg_cmd'
  7. import {advGiftBuy, shopBuy, shopBuyRsp, shopDataRsp, shopNotify, speedUpBuy} from '../proto/game'
  8. import List from '../uiutils/List'
  9. import {GiftConfig, IGiftConfig} from '../config/GiftConfig'
  10. import {IItemshopConfig, ItemshopConfig} from '../config/ItemshopConfig'
  11. import {IIntegralshopConfig, IntegralshopConfig} from '../config/IntegralshopConfig'
  12. import {
  13. AD_ID,
  14. DIS_ID,
  15. EVENT,
  16. GOODS,
  17. Language,
  18. LANGUAGE_TYPE,
  19. LIMIT_TYPE,
  20. LOCAL,
  21. MOD,
  22. QUALITY_COLOR,
  23. SHOP_TYPE,
  24. SPEND_TYPE,
  25. } from '../enums/Enum'
  26. import {GoodsConfig} from '../config/GoodsConfig'
  27. import {i18nLabel} from '../uiutils/i18nLabel'
  28. import {UI} from '../enums/UI'
  29. import {idNum} from '../proto/typedef'
  30. import {DataConfig} from '../config/DataConfig'
  31. import {IRewardNty, IShopItemPop} from '../interface/UIInterface'
  32. import {CenterGridCell} from '../cell/CenterGridCell'
  33. import {SOUND} from '../enums/Sound'
  34. import {AdvertisementConfig} from '../config/AdvertisementConfig'
  35. const {ccclass, property} = cc._decorator
  36. interface shopItem {
  37. buyNum: number
  38. spend: SPEND_TYPE
  39. price: number
  40. currency: number
  41. remain: number
  42. cfg: IGiftConfig | IItemshopConfig | IIntegralshopConfig
  43. }
  44. interface shopTipData {
  45. list: shopItem[]
  46. name: string
  47. redActive: boolean
  48. }
  49. enum SHOP_POINTS_TYPE {
  50. relic = 120, //遗迹
  51. }
  52. @ccclass
  53. @observer
  54. export class ShopUI extends BaseUI {
  55. @list('bottom1/daily_list')
  56. dailyList: List
  57. @list('bottom1/gift_list')
  58. giftList: List
  59. @list('bottom1/relic_list')
  60. relicList: List
  61. @node('bottom1')
  62. bottomBox: cc.Node
  63. @node('bottom1/resource_list')
  64. resourceList: cc.Node
  65. @node('bottom1/resource_list/shop_bottom_4')
  66. resourceItem: cc.Node
  67. @node('tipToggle_box')
  68. tipToggleBox: cc.Node
  69. @node('tipToggle_box/toggle_item')
  70. tipToggleItem: cc.Node
  71. @node('toggle_box')
  72. toggleBox: cc.Node
  73. @node('block1')
  74. refreshBox: cc.Node
  75. @node('toggle_box/tog_relic')
  76. togRelic: cc.Node //遗迹商店选项
  77. selStoreType: number
  78. selModType: number = MOD.dailyGift //没有默认值 给一个默认值、 如果onShow args为空就会为空
  79. allListName: string[] = ['gift_list', 'daily_list', 'resource_list', 'relic_list']
  80. giftAllData: Map<MOD, shopTipData> = new Map()
  81. dailyAllData: shopItem[] = []
  82. resourceAllData: shopItem[] = []
  83. relicAllData: shopItem[] = []
  84. //免费广告节点
  85. freeNodeMap: Map<MOD, cc.Node[]> = new Map()
  86. checkMod: MOD | null
  87. adIDs: Map<number, number> = new Map()
  88. onShow(args: MOD, fromUI: number) {
  89. this.checkMod = args
  90. Mgr.net.add(msgCmd.cmd_shop_data_rsp, this, this.onShopRsp)
  91. Mgr.net.add(msgCmd.cmd_shop_buy_rsp, this, this.onBuyRsp)
  92. Mgr.net.add(msgCmd.cmd_shop_notify, this, this.onNotifyRsp)
  93. Mgr.net.send(msgCmd.cmd_shop_data)
  94. //设置toggles
  95. this.initToggles()
  96. //广告位ID
  97. Object.values(AdvertisementConfig).forEach(v => {
  98. if (v.parameter) {
  99. this.adIDs.set(v.parameter, v.ID)
  100. }
  101. })
  102. Mgr.event.add(EVENT.goodsChangeSync, this, this.initUI)
  103. }
  104. onHide(): any {
  105. Mgr.event.removeAll(this)
  106. }
  107. initToggles(selStore?: SHOP_TYPE) {
  108. this.selStoreType = selStore == null ? SHOP_TYPE.gift : selStore
  109. this.checkStore()
  110. if (this.selModType == MOD.dailyGift) {
  111. //对底部页签初始化
  112. ccUtils.setTogglesChecked(0, this.tipToggleBox)
  113. }
  114. ccUtils.setTogglesChecked(this.selStoreType - 1, this.toggleBox)
  115. Mgr.ui.showTop(this, this.selStoreType == SHOP_TYPE.relic ? [GOODS.relicCoin] : [GOODS.coin, GOODS.diamond])
  116. }
  117. getAdID(id) {
  118. let adID = this.adIDs.get(id)
  119. if (!adID) cc.error('没有找到广告ID')
  120. return adID
  121. }
  122. //红点系统
  123. redActiveControl() {
  124. let redActive: boolean[] = [false, false, false, false]
  125. let freeGift = []
  126. let freeDaily = []
  127. //检测礼包商店红点状态
  128. //确定左侧大类页签红点状态
  129. this.giftAllData.forEach(value => {
  130. let list = value.list
  131. let modRed = []
  132. for (let i = 0; i < list.length; i++) {
  133. if (list[i].spend == SPEND_TYPE.adFree) {
  134. let inTime = Mgr.global.getAdInfo(this.getAdID(list[i].cfg.ID)).inTime
  135. modRed.push(list[i].remain > 0 && !inTime)
  136. }
  137. }
  138. //一旦对应mod(页签)有红点则返回真
  139. value.redActive = modRed.includes(true)
  140. freeGift.push(value.redActive)
  141. })
  142. //礼品商店一旦有免费可购买商品则返回真
  143. redActive[SHOP_TYPE.gift - 1] = freeGift.includes(true)
  144. //确定每日商店红点状态
  145. let dailyArr = this.dailyAllData
  146. for (let i = 0; i < dailyArr.length; i++) {
  147. if (dailyArr[i].spend == SPEND_TYPE.adFree) {
  148. let inTime = Mgr.global.getAdInfo(this.getAdID(dailyArr[i].cfg.ID)).inTime
  149. freeDaily.push(dailyArr[i].remain > 0 && !inTime)
  150. }
  151. }
  152. //每日商店一旦有买免费可购买商品返回真
  153. redActive[SHOP_TYPE.daily - 1] = freeDaily.includes(true)
  154. //显示左侧toggle红点状态
  155. let children = this.toggleBox.children
  156. for (let i = 0; i < children.length; i++) {
  157. cc.find('red_dot', children[i]).active = redActive[i]
  158. }
  159. Data.main.modRedMap.set(
  160. UI.ShopUI,
  161. redActive.some(v => v),
  162. )
  163. }
  164. //动态根据此时选择商店类型显示列表
  165. checkStore() {
  166. for (let i = 0; i < this.allListName.length; i++) {
  167. cc.find(this.allListName[i], this.bottomBox).active = i + 1 == this.selStoreType
  168. }
  169. }
  170. //动态根据此时mod返回数据
  171. checkModData(index): shopItem {
  172. let allData = [this.dailyAllData, this.resourceAllData, this.relicAllData]
  173. return this.selStoreType == SHOP_TYPE.gift
  174. ? this.giftAllData.get(this.selModType).list[index]
  175. : allData[this.selStoreType - 2][index]
  176. }
  177. //确定购买方式
  178. checkPurMethod(node: cc.Node, item: shopItem, button: cc.Node) {
  179. let free = cc.find('free', node)
  180. let recharge = cc.find('recharge', node)
  181. let resource = cc.find('resource', node)
  182. free.active = item.spend == SPEND_TYPE.adFree
  183. recharge.active = item.spend == SPEND_TYPE.recharge
  184. resource.active = item.spend == SPEND_TYPE.resource
  185. if (free.active) {
  186. let label =
  187. this.selStoreType == SHOP_TYPE.gift
  188. ? LANGUAGE_TYPE.free
  189. : `${Mgr.i18n.getLabel(LANGUAGE_TYPE.free)}${item.remain}/${item.cfg.num}`
  190. ccUtils.setLabel(label, free, 'lb')
  191. this.storeAdData(node, free)
  192. }
  193. if (recharge.active) {
  194. cc.find('time', node).active = false
  195. ccUtils.setLabel(Mgr.i18n.getMoneyLabel(item.price), recharge, 'lb')
  196. cc.find('lb', recharge).active = item.remain > 0
  197. ccUtils.setSpriteGray(item.remain <= 0, button)
  198. ccUtils.setBtnInteract(item.remain > 0, button)
  199. }
  200. if (resource.active) {
  201. let payIconUrl = `Public/goods/${GoodsConfig[item.currency].icon}`
  202. cc.find('time', node).active = false
  203. let isEnable = Data.user.goods.get(item.currency) >= item.price
  204. ccUtils.setLabel(item.price.toString(), resource, 'lb')
  205. let color = this.selStoreType == SHOP_TYPE.gift ? '#000D20' : '#FFFFFF'
  206. ccUtils.setColor(isEnable ? color : '#FF2525', resource, 'lb')
  207. this.loadTexImg(payIconUrl, resource, 'icon')
  208. cc.find('icon', resource).active = item.remain > 0
  209. cc.find('lb', resource).active = item.remain > 0
  210. ccUtils.setSpriteGray(isEnable && item.remain <= 0, button)
  211. ccUtils.setBtnInteract(isEnable && item.remain > 0, button)
  212. }
  213. cc.find('sold', node).active = item.remain <= 0
  214. }
  215. //存入存在观看广告的节点数据
  216. storeAdData(node: cc.Node, free: cc.Node) {
  217. let array = this.freeNodeMap.get(this.selModType) || []
  218. let index = node['index']
  219. if (!array.includes(node)) {
  220. array.push(node)
  221. }
  222. let isShopGift = this.selStoreType == SHOP_TYPE.gift
  223. let item: shopItem = this.checkModData(index)
  224. let inTime = Mgr.global.getAdInfo(this.getAdID(item.cfg.ID)).inTime
  225. let button = isShopGift ? cc.find('btn_blue', node) : node
  226. let interact = item.remain > 0 && !inTime
  227. ccUtils.setSpriteGray(isShopGift && !interact, button)
  228. ccUtils.setSpriteGray(!interact, free, 'icon')
  229. //显示点击状态
  230. ccUtils.setBtnInteract(interact, button)
  231. this.freeNodeMap.set(this.selModType, array)
  232. cc.find('icon', free).active = interact
  233. cc.find('lb', free).active = interact
  234. cc.find('time', node).active = inTime
  235. cc.find('red_dot', node).active = interact
  236. //刷新
  237. if (inTime) {
  238. this.showUpdateRemain()
  239. }
  240. }
  241. //初始化底部页签
  242. initTipItem(type: number, len: number) {
  243. this.tipToggleBox.active = len > 0
  244. if (len == 0) return
  245. ccUtils.instantChildren(this.tipToggleItem, len, this.tipToggleBox)
  246. let data: number[] = [],
  247. map = null
  248. switch (type) {
  249. case SHOP_TYPE.gift:
  250. data = [MOD.dailyGift, MOD.weekGift, MOD.noviceGift]
  251. map = this.giftAllData
  252. break
  253. default:
  254. return
  255. }
  256. let children = this.tipToggleBox.children,
  257. index = 0
  258. map.forEach(value => {
  259. ccUtils.setLabel(value.name, children[index], 'paging3/label')
  260. ccUtils.setLabel(value.name, children[index], 'paging4/label')
  261. children[index]['mod'] = data[index]
  262. cc.find('red_dot', children[index]).active = value.redActive
  263. index += 1
  264. })
  265. //置换Layout顺序 l->r
  266. this.tipToggleBox.getComponent(cc.Layout).horizontalDirection = cc.Layout.HorizontalDirection.LEFT_TO_RIGHT
  267. }
  268. initUI() {
  269. //当args不为空时,进入对应商店
  270. if (this.checkMod != null) {
  271. let selStore: SHOP_TYPE = SHOP_TYPE.gift
  272. switch (this.checkMod) {
  273. case MOD.giftStore:
  274. selStore = SHOP_TYPE.gift
  275. this.selModType = MOD.giftStore
  276. break
  277. case MOD.dailyGift:
  278. selStore = SHOP_TYPE.gift
  279. this.selModType = MOD.dailyGift
  280. break
  281. case MOD.weekGift:
  282. selStore = SHOP_TYPE.gift
  283. this.selModType = MOD.weekGift
  284. break
  285. case MOD.noviceGift:
  286. selStore = SHOP_TYPE.gift
  287. this.selModType = MOD.noviceGift
  288. break
  289. case MOD.dailyStore:
  290. selStore = SHOP_TYPE.daily
  291. break
  292. case MOD.resourceStore:
  293. selStore = SHOP_TYPE.resource
  294. break
  295. case MOD.relicStore:
  296. selStore = SHOP_TYPE.relic
  297. break
  298. }
  299. this.initToggles(selStore)
  300. this.checkMod = null
  301. }
  302. switch (this.selStoreType) {
  303. //初始化礼包商店
  304. case SHOP_TYPE.gift:
  305. this.initTipItem(SHOP_TYPE.gift, this.giftAllData.size)
  306. let selType = this.giftAllData.get(this.selModType)
  307. if (!selType) {
  308. selType = this.giftAllData.get(MOD.dailyGift)
  309. this.selModType = MOD.dailyGift
  310. }
  311. this.giftList.numItems = selType.list.length
  312. if (this.selModType != MOD.noviceGift) this.handleGetAwardRemainTime()
  313. break
  314. //初始化日常商店
  315. case SHOP_TYPE.daily:
  316. this.initTipItem(SHOP_TYPE.daily, 0)
  317. this.handleGetAwardRemainTime()
  318. this.dailyList.numItems = this.dailyAllData.length
  319. this.selModType = MOD.dailyStore
  320. break
  321. //初始化资源商店
  322. case SHOP_TYPE.resource:
  323. this.initTipItem(SHOP_TYPE.resource, 0)
  324. ccUtils.instantChildren(this.resourceItem, this.resourceAllData.length, this.resourceList)
  325. this.initResourceItem()
  326. break
  327. //初始化积分商店
  328. case SHOP_TYPE.relic:
  329. this.initTipItem(SHOP_TYPE.relic, 0)
  330. this.relicList.numItems = this.relicAllData.length
  331. break
  332. }
  333. //检查资源商店是否当前开启
  334. this.refreshBox.active =
  335. this.selModType == MOD.dailyGift || this.selModType == MOD.weekGift || this.selModType == MOD.dailyStore
  336. }
  337. //初始化礼包页签内容
  338. initGiftItem(node: cc.Node, index: number) {
  339. node['index'] = index
  340. let data = this.giftAllData.get(this.selModType).list[index]
  341. let cfg = data.cfg as IGiftConfig
  342. //初始化上方购买次数
  343. let num = data.remain + data.buyNum
  344. let purchaseTimeInfo =
  345. this.selModType == MOD.dailyGift
  346. ? LANGUAGE_TYPE.dailyPurchase
  347. : this.selModType == MOD.weekGift
  348. ? LANGUAGE_TYPE.weeklyPurchase
  349. : LANGUAGE_TYPE.novicePurchase
  350. cc.find('prop_info', node)
  351. .getComponent(i18nLabel)
  352. .init(data.remain == 0 ? LANGUAGE_TYPE.noBuyTime : LANGUAGE_TYPE.readyBuyTime, [
  353. Mgr.i18n.getLabel(purchaseTimeInfo),
  354. data.remain.toString(),
  355. num.toString(),
  356. ])
  357. //初始化商品数量
  358. let library = Mgr.goods.getIdNumByCfgArr(cfg.library)
  359. let iconBox = cc.find('icon_box', node)
  360. let centerGrid = iconBox.getComponent(CenterGridCell)
  361. let goods = centerGrid.init(library.length)
  362. Mgr.goods.initGoods(
  363. library,
  364. goods.map(goodsParent => cc.find('goods', goodsParent)),
  365. this,
  366. )
  367. //确定购买方式
  368. let button = data.spend == SPEND_TYPE.adFree ? cc.find('btn_blue', node) : cc.find('btn_yellow', node)
  369. cc.find('btn_blue', node).active = data.spend == SPEND_TYPE.adFree
  370. cc.find('btn_yellow', node).active = !(data.spend == SPEND_TYPE.adFree)
  371. //确认购买方式 adFree: 广告/recharge: 付费/resource: 资源
  372. this.checkPurMethod(node, data, button)
  373. //红点显示
  374. if (data.spend == SPEND_TYPE.recharge || data.spend == SPEND_TYPE.resource) {
  375. cc.find('red_dot', node).active = false
  376. }
  377. }
  378. //初始化日常商店
  379. initDailyItem(node: cc.Node, index: number) {
  380. node['index'] = index
  381. let data = this.dailyAllData[index],
  382. cfg = data.cfg as IItemshopConfig,
  383. goodsInfo = Mgr.goods.getGoodShowInfo(cfg.item[0])
  384. //显示商品名称
  385. ccUtils.setLabel(goodsInfo.name, node, 'prop_lb')
  386. //根据品质显示颜色
  387. ccUtils.setColor(QUALITY_COLOR[goodsInfo.quality], node, 'prop_lb')
  388. //显示商品图标
  389. Mgr.goods.initGoods([{id: cfg.item[0], num: cfg.item[1]}], [cc.find('good_item/goods', node)], this)
  390. //显示折扣
  391. let discountNode = cc.find('discount_bottom', node)
  392. discountNode.active = data.price > 0
  393. if (discountNode.active) {
  394. if (Mgr.storage.getString(LOCAL.selectLanguage) == Language.en) {
  395. ccUtils.setLabel(cfg.discount * 10 + '%', discountNode, 'num_lb')
  396. } else {
  397. ccUtils.setLabel(cfg.discount.toString(), discountNode, 'num_lb')
  398. }
  399. }
  400. //显示block中购买次数
  401. this.checkPurMethod(node, data, node)
  402. }
  403. //初始化资源商店
  404. initResourceItem() {
  405. let children = this.resourceList.children
  406. for (let index = 0; index < children.length; index++) {
  407. let block = cc.find('block1', children[index]),
  408. discount_bottom = cc.find('discount_bottom', children[index]),
  409. button = cc.find('btn_yellow', children[index]),
  410. cfg = this.resourceAllData[index].cfg as IGiftConfig,
  411. buyNum = this.resourceAllData[index].buyNum,
  412. library = Mgr.goods.getIdNumByCfgArr(cfg.library)
  413. children[index]['index'] = index
  414. //填入获得钻石
  415. ccUtils.setLabel(library[0].num.toString(), block, 'label')
  416. //是否显示首充双倍
  417. discount_bottom.active = buyNum == 0
  418. if (discount_bottom.active) {
  419. ccUtils.setLabel(`+${library[0].num}`, discount_bottom, 'charge_lb')
  420. }
  421. //初始化钻石图标
  422. this.loadTexImg(`StoreUI/diamond_${cfg.parameter[0]}`, children[index], 'icon')
  423. //显示购买金额
  424. ccUtils.setLabel(Mgr.i18n.getMoneyLabel(cfg.spend), button, 'lb')
  425. }
  426. }
  427. initPointsItem(node: cc.Node, index: number) {
  428. let data = this.relicAllData[index]
  429. let cfg = data.cfg as IIntegralshopConfig,
  430. goodsInfo = Mgr.goods.getGoodShowInfo(cfg.item[0]),
  431. limitNode = cc.find('prop_info', node)
  432. node['index'] = index
  433. //显示商品名称
  434. ccUtils.setLabel(goodsInfo.name, node, 'prop_lb')
  435. //根据品质显示颜色
  436. ccUtils.setColor(QUALITY_COLOR[goodsInfo.quality], node, 'prop_lb')
  437. //显示商品图标
  438. Mgr.goods.initGoods([{id: cfg.item[0], num: cfg.item[1]}], [cc.find('good_item/goods', node)], this)
  439. limitNode.active = cfg.limit == LIMIT_TYPE.daily || cfg.limit == LIMIT_TYPE.weekly
  440. if (limitNode.active) {
  441. //显示限购类型
  442. let purchaseTimeInfo =
  443. cfg.limit == LIMIT_TYPE.daily ? LANGUAGE_TYPE.dailyPurchase : LANGUAGE_TYPE.weeklyPurchase
  444. limitNode
  445. .getComponent(i18nLabel)
  446. .init(data.remain == 0 ? LANGUAGE_TYPE.noBuyTime : LANGUAGE_TYPE.readyBuyTime, [
  447. Mgr.i18n.getLabel(purchaseTimeInfo),
  448. data.remain.toString(),
  449. cfg.num.toString(),
  450. ])
  451. }
  452. this.checkPurMethod(node, data, node)
  453. }
  454. //UI或者其他函数=======================================
  455. //网络事件=======================================
  456. onShopRsp(data: shopDataRsp) {
  457. //数据初始化
  458. this.giftAllData.clear()
  459. this.dailyAllData = []
  460. this.resourceAllData = []
  461. this.relicAllData = []
  462. let allBuyData: Map<SHOP_TYPE, idNum[]> = new Map()
  463. //填入购买记录
  464. for (let i = SHOP_TYPE.gift; i <= SHOP_TYPE.relic; i++) {
  465. allBuyData.set(i, data.list[i - 1].list)
  466. }
  467. //初始化礼品商店&&资源商店
  468. //查看新手商店用品是否卖尽
  469. let giftBuy = allBuyData.get(SHOP_TYPE.gift)
  470. let resourceBuy = allBuyData.get(SHOP_TYPE.resource)
  471. let allNovice: boolean[] = []
  472. let limit = 1
  473. let name = ''
  474. for (let id in GiftConfig) {
  475. let list: shopItem[] = [],
  476. type: MOD = MOD.dailyGift,
  477. hasBuy = 0
  478. switch (GiftConfig[id].type) {
  479. case MOD.dailyGift:
  480. type = MOD.dailyGift
  481. limit = GiftConfig[id].daylimit
  482. hasBuy = giftBuy.find(value => value.id == parseInt(id))?.num || 0
  483. name = Mgr.i18n.getLabel(LANGUAGE_TYPE.dailyGift)
  484. break
  485. case MOD.weekGift:
  486. type = MOD.weekGift
  487. limit = GiftConfig[id].weeklimit
  488. hasBuy = giftBuy.find(value => value.id == parseInt(id))?.num || 0
  489. name = Mgr.i18n.getLabel(LANGUAGE_TYPE.weeklyGift)
  490. break
  491. case MOD.noviceGift:
  492. type = MOD.noviceGift
  493. hasBuy = giftBuy.find(value => value.id == parseInt(id))?.num || 0
  494. allNovice.push(limit - hasBuy > 0)
  495. name = Mgr.i18n.getLabel(LANGUAGE_TYPE.noviceGift)
  496. break
  497. case MOD.resourceStore:
  498. type = MOD.resourceStore
  499. hasBuy = resourceBuy.find(value => value.id == parseInt(id))?.num || 0
  500. limit = Number.POSITIVE_INFINITY
  501. break
  502. default:
  503. continue
  504. }
  505. let cfg = GiftConfig[id]
  506. //初始化存入对象
  507. let item: shopItem = {
  508. buyNum: hasBuy,
  509. cfg: cfg,
  510. spend: cfg.spendtype,
  511. price: cfg.spendtype == SPEND_TYPE.resource ? cfg.num : cfg.spend,
  512. currency: cfg.spend,
  513. remain: limit - hasBuy,
  514. }
  515. //若是资源商店物品,则数据存入资源商店,否则为礼包商店
  516. if (type == MOD.resourceStore) {
  517. this.resourceAllData.push(item)
  518. } else {
  519. list = this.giftAllData.get(type)?.list || []
  520. list.push(item)
  521. this.giftAllData.set(type, {redActive: false, list: list, name: name})
  522. }
  523. }
  524. //检查新手商店物品是否售尽
  525. if (allNovice.every(item => item == false)) {
  526. this.giftAllData.delete(MOD.noviceGift)
  527. }
  528. //对礼包商店商品排序
  529. this.giftAllData.forEach((value: shopTipData) => {
  530. value.list.sort((a: shopItem, b: shopItem) => a.cfg.sort - b.cfg.sort)
  531. })
  532. //对资源商店进行排序
  533. this.resourceAllData.sort((a: shopItem, b: shopItem) => a.cfg.sort - b.cfg.sort)
  534. //初始化每日商店
  535. let dailyBuy = allBuyData.get(SHOP_TYPE.daily)
  536. for (let index = 0; index < dailyBuy.length; index++) {
  537. let id = dailyBuy[index].id,
  538. hasBuy = dailyBuy.find(value => value.id == id)?.num || 0,
  539. cfg = ItemshopConfig[id]
  540. let item: shopItem = {
  541. buyNum: hasBuy,
  542. cfg: cfg,
  543. spend: cfg.price[0] == 0 ? SPEND_TYPE.adFree : SPEND_TYPE.resource,
  544. price: cfg.price[0] == 0 ? 0 : cfg.price[1],
  545. currency: cfg.price[0] == 0 ? 0 : cfg.price[0],
  546. remain: cfg.num - hasBuy,
  547. }
  548. this.dailyAllData.push(item)
  549. }
  550. //对每日商店商品进行排序
  551. this.dailyAllData.sort((a: shopItem, b: shopItem) => {
  552. return a.cfg.sort - b.cfg.sort
  553. })
  554. //初始化遗迹商店
  555. let relicBuy = allBuyData.get(SHOP_TYPE.relic)
  556. for (let id in IntegralshopConfig) {
  557. if (IntegralshopConfig[id].module == SHOP_POINTS_TYPE.relic) {
  558. let hasBuy = relicBuy.find(value => value.id == parseInt(id))?.num || 0
  559. let cfg = IntegralshopConfig[id]
  560. let item: shopItem = {
  561. buyNum: hasBuy,
  562. cfg: cfg,
  563. spend: cfg.price[0] == 0 ? SPEND_TYPE.adFree : SPEND_TYPE.resource,
  564. price: cfg.price[0] == 0 ? 0 : cfg.price[1],
  565. currency: cfg.price[0] == 0 ? 0 : cfg.price[0],
  566. remain: cfg.limit == LIMIT_TYPE.free ? Infinity : cfg.num - hasBuy,
  567. }
  568. this.relicAllData.push(item)
  569. }
  570. }
  571. //对遗迹商店进行排序
  572. this.resourceAllData.sort((a: shopItem, b: shopItem) => a.cfg.sort - b.cfg.sort)
  573. this.redActiveControl()
  574. this.initUI()
  575. }
  576. onBuyRsp(data: shopBuyRsp) {
  577. this.redActiveControl()
  578. this.initUI()
  579. }
  580. onNotifyRsp(data: shopNotify) {
  581. let list = [this.dailyAllData, this.resourceAllData, this.relicAllData]
  582. let array =
  583. this.selStoreType == SHOP_TYPE.gift
  584. ? this.giftAllData.get(this.selModType)?.list
  585. : list[this.selStoreType - 2]
  586. //对数据进行更新
  587. for (let i = 0; i < array.length; i++) {
  588. if (array[i].cfg.ID == data.id) {
  589. array[i].buyNum += data.num
  590. array[i].remain -= data.num
  591. }
  592. //当新手礼包购尽时,直接跳转至每日礼包(daily gift)
  593. if (this.selModType == MOD.noviceGift) {
  594. //新手商店是否全部购买
  595. if (array.every(item => item.remain == 0)) {
  596. this.giftAllData.delete(MOD.noviceGift)
  597. this.selModType = MOD.dailyGift
  598. //对底部页签初始化
  599. ccUtils.setTogglesChecked(0, this.tipToggleBox)
  600. }
  601. }
  602. }
  603. this.initUI()
  604. }
  605. //触发事件=======================================
  606. @render
  607. showUpdateRemain() {
  608. let time = Data.main.serverTime,
  609. list = this.freeNodeMap.get(this.selModType) || []
  610. //查看节点是否挂载再当前MOD上
  611. list.forEach(node => {
  612. let data = this.checkModData(node['index'])
  613. let info = data.cfg.ID,
  614. remain = data.remain,
  615. ads = Mgr.global.getAdInfo(this.getAdID(info)),
  616. grayInteract = remain <= 0 || ads.inTime,
  617. isShopGift = this.selStoreType == SHOP_TYPE.gift
  618. cc.find('red_dot', node).active = !grayInteract
  619. //显示灰色状态
  620. let free = cc.find('free', node)
  621. let button = isShopGift ? cc.find('btn_blue', node) : node
  622. ccUtils.setSpriteGray(isShopGift && grayInteract, button)
  623. ccUtils.setSpriteGray(grayInteract, free, 'icon')
  624. //显示点击状态
  625. ccUtils.setBtnInteract(!grayInteract, button)
  626. if (ads.isZero) {
  627. this.redActiveControl()
  628. this.initUI()
  629. } else if (ads.inTime) {
  630. //格式化数字
  631. ccUtils.setLabel(ads.min + ':' + ads.sec, node, 'time')
  632. }
  633. })
  634. }
  635. @render
  636. //处理剩余时间
  637. handleGetAwardRemainTime() {
  638. let time = Data.main.serverTime,
  639. endOfDay = this.selModType == MOD.weekGift ? Date.getWeekTime(time, 0) : Date.getDayTime(time),
  640. remain = Math.floor(endOfDay - time) < 60 ? 60 : Math.floor(endOfDay - time)
  641. if (remain == 0) {
  642. Mgr.net.send(msgCmd.cmd_shop_data)
  643. } else {
  644. ccUtils.setLabel(`${Mgr.i18n.getTimeLabel(remain)}`, this.refreshBox, 'time_info')
  645. }
  646. }
  647. // 点击事件=======================================
  648. //左侧总类页签点击
  649. onToggleClick(event, data: string) {
  650. Mgr.audio.playSFX(SOUND.toggleClick)
  651. this.selStoreType = parseInt(data)
  652. this.checkStore()
  653. let allShopType = [MOD.dailyGift, MOD.dailyStore, MOD.resourceStore, MOD.relicStore]
  654. this.selModType = allShopType[this.selStoreType - 1]
  655. //对底部页签初始化
  656. ccUtils.setTogglesChecked(0, this.tipToggleBox)
  657. Mgr.ui.showTop(
  658. this,
  659. this.selStoreType == SHOP_TYPE.relic ? [GOODS.relicCoin, GOODS.diamond] : [GOODS.coin, GOODS.diamond],
  660. )
  661. this.initUI()
  662. }
  663. //底部页签点击
  664. onTipToggleClick(event) {
  665. Mgr.audio.playSFX(SOUND.toggleClick)
  666. this.selModType = event.target['mod']
  667. this.initUI()
  668. }
  669. //商品购买
  670. onGoodsBuy(event) {
  671. if (event.currentTarget == event.target) {
  672. let index =
  673. this.selStoreType == SHOP_TYPE.gift || this.selStoreType == SHOP_TYPE.resource
  674. ? event.currentTarget.parent['index']
  675. : event.currentTarget['index'],
  676. item = this.checkModData(index)
  677. let data: shopBuy = {type: this.selStoreType, id: item.cfg.ID, num: 1}
  678. switch (item.spend) {
  679. //广告(免费)商品
  680. case SPEND_TYPE.adFree:
  681. let value = Data.main.serverTime + DataConfig[DIS_ID.adFreeTime].data3
  682. Mgr.global.storageAdInfo(this.getAdID(item.cfg.ID), value, item.remain - 1)
  683. Mgr.platform.playVideoAD(this.getAdID(item.cfg.ID), () => {
  684. Mgr.net.send(msgCmd.cmd_shop_buy, data)
  685. })
  686. break
  687. //礼金商品
  688. case SPEND_TYPE.recharge:
  689. Mgr.net.send(msgCmd.cmd_shop_buy, data)
  690. break
  691. }
  692. }
  693. }
  694. }