KLTradeMonthCardPanel.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ---@class KLTradeMonthCardPanel:UIKmlLuaPanelBase
  2. ---@field view KLTradeMonthCardPanelView
  3. local KLTradeMonthCardPanel = class(UIKmlLuaPanelBase)
  4. local this = KLTradeMonthCardPanel
  5. EMonthCardTradeToggle = {
  6. MonthCard = 1,
  7. Coin = 2
  8. }
  9. ---创建时调用一次
  10. function this:Init()
  11. self.message = nil
  12. GUI:DataListInitData(self.view.datalist_goods, function()
  13. return self:ItemCountFunc()
  14. end, function(realIndex)
  15. return self:ItemGetFunc(realIndex)
  16. end, nil, function(realIndex, kmlCtrl)
  17. return self:ItemUpdateFunc(realIndex, kmlCtrl)
  18. end)
  19. ---@type table<UIKmlLuaControl,KLTradeGoodsItem>
  20. self.goodsItems = {}
  21. self.goodsItemDataList = {}
  22. self.tickTimer = SL:Schedule(self.tickTimer, 0, 1, -1, function()
  23. return self:Tick()
  24. end)
  25. --GUI:SetActive(self.view.toggle_coin, false)
  26. self.CurSelect = EMonthCardTradeToggle.MonthCard
  27. GUI:Toggle_setIsOn(self.view.toggle_month_card, true)
  28. end
  29. function this:ItemCountFunc()
  30. return #self.goodsItemDataList
  31. end
  32. function this:ItemGetFunc(realIndex)
  33. ---@type KLTradeGoodsItem
  34. local item = GUI:UIPanel_Open("dev/outui/Trade/Item/KLTradeGoods/KLTradeGoodsItem", self.view.datalist_goods, self, nil, true)
  35. self.goodsItems[item.view.root] = item
  36. return item.view.root
  37. end
  38. function this:ItemUpdateFunc(realIndex, kmlCtrl)
  39. local data = self.goodsItemDataList[realIndex + 1]
  40. local item = self.goodsItems[kmlCtrl]
  41. item:UpdateData(data)
  42. local id = SL:GetMetaValue(EMetaVarGetKey.MAIN_ACTOR_ID)
  43. local minPrice = data.publicity == "1" and self.minPricePublic or self.minPrice
  44. if id ~= data.ownid and (data.totalprice/data.count) > minPrice then
  45. item:UpdateBuyClickFun(self.MinPriceBuyCheck,self)
  46. end
  47. end
  48. ---@param itemData table
  49. function this:MinPriceBuyCheck(item)
  50. local itemData = item.itemData
  51. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_TRADE_GOODS_INFO, {itemData.itemid, itemData.ownid })
  52. GUI:UIPanel_Open("dev/outui/Trade/Panel/KLMonthCardBuyTip/KLMonthCardBuyTipPanel")
  53. end
  54. ---注册UI事件和服务器消息
  55. function this:RegistEvents()
  56. GUI:SetToggleOnValueChange(self.view.toggle_month_card, self, self.MonthCardToggle)
  57. GUI:SetToggleOnValueChange(self.view.toggle_coin, self, self.CoinToggle)
  58. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_GET_TRADE_GOODS, self.RES_GET_TRADE_GOODS, self)
  59. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_TRADE_BUY_GOODS, self.Refresh, self)
  60. --SL:RegisterLUAEvent(LUA_EVENT_TRADE_BUY_REQUEST, self.Refresh, self)
  61. end
  62. function this:RES_GET_TRADE_GOODS(_, message)
  63. if not message then
  64. return
  65. end
  66. self.message = message
  67. if self.CurSelect == EMonthCardTradeToggle.MonthCard then
  68. self:MonthCardToggle()
  69. elseif self.CurSelect == EMonthCardTradeToggle.Coin then
  70. self:CoinToggle()
  71. end
  72. end
  73. function this:MonthCardToggle()
  74. self.CurSelect = EMonthCardTradeToggle.MonthCard
  75. if not self.message then
  76. return
  77. end
  78. table.clear(self.goodsItemDataList)
  79. for k, v in pairs(self.message) do
  80. local stallTbl = SL:GetConfig("cfg_stall", v.itemcfgid)
  81. if stallTbl and stallTbl.type == 5 then
  82. table.insert(self.goodsItemDataList, v)
  83. end
  84. end
  85. table.sort(self.goodsItemDataList,self.SortGoodsByPriceFun)
  86. local id = SL:GetMetaValue(EMetaVarGetKey.MAIN_ACTOR_ID)
  87. self.minPrice = math.maxinteger
  88. self.minPricePublic = math.maxinteger
  89. if #self.goodsItemDataList > 0 then
  90. self.minPrice = self.goodsItemDataList[1].totalprice/self.goodsItemDataList[1].count
  91. end
  92. for k, v in pairs(self.goodsItemDataList) do
  93. if v.ownid ~= id then
  94. local unitPrice = v.totalprice/v.count
  95. if v.publicity == "1" then
  96. if unitPrice < self.minPricePublic then
  97. self.minPricePublic = unitPrice
  98. end
  99. else
  100. if unitPrice < self.minPrice then
  101. self.minPrice = unitPrice
  102. end
  103. end
  104. end
  105. end
  106. GUI:DataListUpdateData(self.view.datalist_goods)
  107. end
  108. function this.SortGoodsByPriceFun(a,b)
  109. return a.totalprice/a.count < b.totalprice/b.count
  110. end
  111. function this:CoinToggle()
  112. self.CurSelect = EMonthCardTradeToggle.Coin
  113. if not self.message then
  114. return
  115. end
  116. table.clear(self.goodsItemDataList)
  117. for k, v in pairs(self.message) do
  118. local stallTbl = SL:GetConfig("cfg_stall", v.itemcfgid)
  119. if stallTbl and stallTbl.type == 1 then
  120. table.insert(self.goodsItemDataList, v)
  121. end
  122. end
  123. GUI:DataListUpdateData(self.view.datalist_goods)
  124. end
  125. ---创建或者刷新界面数据时调用
  126. function this:Refresh()
  127. self:RequestRefreshGoodsList(0, 0, 0, 0, 0, 0)
  128. end
  129. function this:Close()
  130. SL:UnSchedule(self.tickTimer)
  131. table.clear(self.goodsItemDataList)
  132. GUI:DataListUpdateData(self.view.datalist_goods)
  133. end
  134. function this:RequestRefreshGoodsList(type, subType, sortType, careerType, filterLevel, filterQuality)
  135. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_GET_TRADE_GOODS, { type, subType, sortType, careerType, filterLevel, filterQuality })
  136. end
  137. function this:Tick()
  138. local refresh = false
  139. for _, v in pairs(self.goodsItems) do
  140. refresh = refresh or v:Tick()
  141. end
  142. if refresh then
  143. self:RequestRefreshGoodsList(0, 0, 0, 0, 0, 0)
  144. end
  145. end
  146. return this