ShopUI.ts 29 KB

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