源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.4 KiB

  1. -- <*
  2. -- @Author: Saber
  3. -- @Description: 藏宝图
  4. -- *>
  5. TreasureMapShopItem = TreasureMapShopItem or BaseClass(BaseItem)
  6. local TreasureMapShopItem = TreasureMapShopItem
  7. function TreasureMapShopItem:__init(parent_wnd,prefab_asset,layer_name)
  8. self.base_file = "treasureMap"
  9. self.layout_file = "TreasureMapShopItem"
  10. self.parent_wnd = parent_wnd
  11. self.layer_name = layer_name
  12. self.has_receive_free_goods = false
  13. self.need_show_leftime = true
  14. self.model = TreasureMapModel:getInstance()
  15. self:Load()
  16. end
  17. function TreasureMapShopItem:Load_callback()
  18. local nodes = {
  19. "bg:obj:img",
  20. "left_time_icon:obj",
  21. "item_con",
  22. "reddot:obj",
  23. "name:tmp",
  24. "price_con/price:tmp", "price_con/old_price:tmp", "price_con/delete_icon:obj",
  25. "day_limit:tmp",
  26. "left_time:tmp",
  27. }
  28. self:GetChildren(nodes)
  29. self.delete_icon_obj:SetActive(false)
  30. self.awardItem = UIObjPool:getInstance():PopItem(UIObjPool.UIType.AwardItem, self.item_con)
  31. self.awardItem:SetItemSize(78, 78)
  32. self.awardItem:SetAnchoredPosition(0, 0)
  33. self:AddEvents()
  34. if self.need_refreshData then
  35. self:UpdateView()
  36. end
  37. end
  38. function TreasureMapShopItem:AddEvents( )
  39. local function click_event(target, x, y)
  40. if target == self.bg_obj then -- 点击节点为购买表现
  41. if self.data then
  42. if self.data.is_free then -- 免费道具走不同的协议
  43. TreasureMapModel:getInstance():Fire(TreasureMapConst.REQUEST_CCMD_EVENT, 42412)
  44. else
  45. -- 限购个数
  46. local limit_num = self.data.reward[2][4]
  47. -- 获取剩余个数
  48. local rest_num = self.model:GetTreasureMapShopRestNumData(self.data.reward[1]) or limit_num
  49. if rest_num > 0 then
  50. local reward = self.data.reward[2][1]
  51. local goods_id = reward[2]
  52. local data = {
  53. shop_data = {
  54. price = self.data.reward[2][3],
  55. money_type = self.data.reward[2][2] == "gold" and 1 or 2,
  56. goods_id = goods_id,
  57. quota_num = limit_num,
  58. sold_out = limit_num - rest_num,
  59. quota_type = 1,
  60. is_tm_shop = true,
  61. discount = 100,
  62. reward_id = self.reward_id,
  63. reward_index = self.data.reward[1],
  64. },
  65. }
  66. UIToolTipMgr:getInstance():AppendGoodsTips(goods_id, x, y, nil, nil, data, true)
  67. else
  68. Message.show("该物品已售罄", "fault")
  69. end
  70. end
  71. end
  72. end
  73. end
  74. AddClickEvent(self.bg_obj, click_event)
  75. local function updateFreeStatus(status)
  76. self.has_receive_free_goods = status
  77. self:UpdateLeftNum()
  78. self:StartLeftTimeFunc()
  79. end
  80. self:BindEvent(self.model, TreasureMapConst.UPDATE_SHOP_FREE_RECEIVE_STATUS, updateFreeStatus)
  81. local function updateGoodsLeftNum()
  82. self:UpdateLeftNum()
  83. self:StartLeftTimeFunc()
  84. end
  85. self:BindEvent(self.model, TreasureMapConst.UPDATE_TREASUREMAP_SHOP_VIEW, updateGoodsLeftNum)
  86. end
  87. function TreasureMapShopItem:SetData(data, expire_time, reward_id)
  88. self.data = data
  89. self.expire_time = expire_time
  90. self.reward_id = reward_id
  91. if self.is_loaded then
  92. self.need_refreshData = false
  93. self:UpdateView()
  94. else
  95. self.need_refreshData = true
  96. end
  97. end
  98. function TreasureMapShopItem:UpdateView( )
  99. self:UpdateBasicData()
  100. self:StartLeftTimeFunc()
  101. self:UpdateLeftNum()
  102. end
  103. function TreasureMapShopItem:UpdateBasicData( )
  104. if self.data then
  105. local reward_data = self.data.is_free and self.data.reward or self.data.reward[2][1]
  106. local goods_Id, lock = GoodsModel:getInstance():GetMappingTypeId(reward_data[1] , reward_data[2])
  107. self.awardItem:SetData(goods_Id, reward_data[3], nil, nil, lock)
  108. self.name_tmp.text = GoodsModel:getInstance():getGoodsName(goods_Id)
  109. end
  110. end
  111. -- 更新剩余可购次数
  112. function TreasureMapShopItem:UpdateLeftNum( )
  113. if self.data.is_free then -- 免费道具
  114. else
  115. end
  116. if self.data then
  117. if self.data.is_free then -- 免费道具
  118. self.reddot_obj:SetActive(not self.has_receive_free_goods)
  119. self.day_limit_tmp.text = string.format("限购<color=%s>%s</color>次",
  120. self.has_receive_free_goods and ColorUtil.RED_DARK or ColorUtil.GREEN_DARK,
  121. self.has_receive_free_goods and 0 or 1)
  122. self.price_tmp.text = self.has_receive_free_goods
  123. and string.format("<color=%s>已售完</color>", ColorUtil.GRAY_DARK)
  124. or "免费"
  125. SetImageGray(self.bg_img, self.has_receive_free_goods)
  126. self.need_show_leftime = not self.has_receive_free_goods
  127. else -- 购买道具
  128. self.reddot_obj:SetActive(false)
  129. -- 限购个数
  130. local limit_num = self.data.reward[2][4]
  131. -- 获取剩余个数
  132. local rest_num = self.model:GetTreasureMapShopRestNumData(self.data.reward[1]) or limit_num
  133. self.day_limit_tmp.text = string.format("限购<color=%s>%s</color>/%s",
  134. rest_num > 0 and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, rest_num, limit_num)
  135. if rest_num > 0 then
  136. self.need_show_leftime = true
  137. local price_type = self.data.reward[2][2] == "gold" and 1 or 2
  138. self.price_tmp.text = WordManager:GetMoneyFaceStr(price_type) .. self.data.reward[2][3]
  139. else
  140. self.need_show_leftime = false
  141. self.price_tmp.text = string.format("<color=%s>已售完</color>", ColorUtil.GRAY_DARK)
  142. end
  143. SetImageGray(self.bg_img, rest_num <= 0)
  144. end
  145. end
  146. end
  147. function TreasureMapShopItem:StartLeftTimeFunc( )
  148. self:ClearLeftTimeFunc()
  149. if self.need_show_leftime then
  150. local function left_time_func()
  151. local left_time = self.expire_time - TimeUtil:getServerTime()
  152. if left_time > 0 then
  153. self.left_time_tmp.text = string.format("<color=%s>%s</color>", ColorUtil.GREEN_DARK, TimeUtil:convertTimeWithoutHour(left_time))
  154. else
  155. self.left_time_tmp.text = string.format("<color=%s>已超时</color>", ColorUtil.RED_DARK)
  156. self:ClearLeftTimeFunc()
  157. end
  158. end
  159. self.left_time_icon_obj:SetActive(true)
  160. left_time_func()
  161. self.left_time_func_id = GlobalTimerQuest:AddPeriodQuest(left_time_func, 0.5, -1)
  162. else
  163. self.left_time_icon_obj:SetActive(false)
  164. self.left_time_tmp.text = ""
  165. end
  166. end
  167. function TreasureMapShopItem:ClearLeftTimeFunc( )
  168. if self.left_time_func_id then
  169. GlobalTimerQuest:CancelQuest(self.left_time_func_id)
  170. self.left_time_func_id = nil
  171. end
  172. end
  173. function TreasureMapShopItem:__delete( )
  174. if self.awardItem then
  175. UIObjPool:getInstance():PushItem(UIObjPool.UIType.AwardItem, self.awardItem)
  176. self.awardItem = nil
  177. end
  178. self:ClearLeftTimeFunc()
  179. end