TequanShopLogic.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. --------------------------------------------
  2. -- 充值-特权商店
  3. -- 购买后开始计时 x天后刷新
  4. -- db.tequanShop[id] = {cnt, time}
  5. --------------------------------------------
  6. local TequanShopExcel = require("excel.present").tequanShop
  7. local Util = require("common.Util")
  8. local Lang = require("common.Lang")
  9. local Msg = require("core.Msg")
  10. local ObjHuman = require("core.ObjHuman")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local Grid = require("bag.Grid")
  13. local BagLogic = require("bag.BagLogic")
  14. local BuyLogic = require("topup.BuyLogic")
  15. -- 花费类型
  16. COST_RMB = 1
  17. COST_ZUANSHI = 2
  18. -- 剩余永久
  19. local DEF_FOREVER_TIME = -1
  20. -- 是否激活征战扫荡特权
  21. function isActive(human, YYInfo, funcConfig)
  22. return isActiveMopup(human)
  23. end
  24. -- 是否激活征战扫荡特权
  25. function isActiveMopup(human)
  26. if not human.db.tequanMopup then
  27. return
  28. end
  29. if human.db.tequanMopup < os.time() then
  30. human.db.tequanMopup = nil
  31. return
  32. end
  33. return true
  34. end
  35. -- 已购买次数
  36. function getCnt(human, id)
  37. if not human.db.tequanShop then
  38. return 0
  39. end
  40. local data = human.db.tequanShop[id]
  41. if not data then return 0 end
  42. return data.cnt or 0
  43. end
  44. -- 记录购买次数
  45. function addCnt(human, id)
  46. if not human.db.tequanShop then
  47. human.db.tequanShop = {}
  48. end
  49. if not human.db.tequanShop[id] then
  50. human.db.tequanShop[id] = {}
  51. end
  52. local data = human.db.tequanShop[id]
  53. data.cnt = (data.cnt or 0) + 1
  54. data.time = data.time or os.time()
  55. end
  56. -- 剩余购买次数
  57. function getLeftCnt(human, id)
  58. local config = TequanShopExcel[id]
  59. if not config then
  60. return 0
  61. end
  62. return math.max(config.limitCnt - getCnt(human, id), 0)
  63. end
  64. -- 刷新剩余时间
  65. function getLeftTime(human, id)
  66. local config = TequanShopExcel[id]
  67. if not config then
  68. return DEF_FOREVER_TIME
  69. end
  70. if not human.db.tequanShop then
  71. return 0
  72. end
  73. local data = human.db.tequanShop[id]
  74. if not data then return 0 end
  75. local leftTime = DEF_FOREVER_TIME
  76. if config.refreshTime > 0 then
  77. local refreshTimeTs = config.refreshTime * 86400
  78. leftTime = math.max(refreshTimeTs - (os.time() - data.time), 0)
  79. end
  80. return leftTime
  81. end
  82. -- 检查是否需要刷新
  83. function checkData(human)
  84. if not human.db.tequanShop then
  85. return
  86. end
  87. for id in pairs(human.db.tequanShop) do
  88. if getLeftTime(human, id) == 0 then
  89. human.db.tequanShop[id] = nil
  90. end
  91. end
  92. end
  93. -- 封装礼包结构体
  94. function fontShopNet(net, id, config, human)
  95. local leftTime = getLeftTime(human, id)
  96. local leftCnt = getLeftCnt(human, id)
  97. net.id = id
  98. net.buyItem[0] = 0
  99. net.needZuanshi = 0
  100. local costType = config.cost[1]
  101. if costType == COST_RMB then
  102. net.buyItem[0] = 1
  103. BuyLogic.fontBuyItem(human, net.buyItem[1], config.cost[2])
  104. else
  105. net.needZuanshi = config.cost[2]
  106. end
  107. net.cnt = getCnt(human, id)
  108. net.maxCnt = config.limitCnt
  109. net.name = config.name
  110. local len = #config.items
  111. for i = 1,len do
  112. Grid.makeItem(net.item[i],config.items[i][1],config.items[i][2])
  113. end
  114. net.item[0] = len
  115. net.desc = config.desc
  116. net.leftTime = math.max(leftTime, 0)
  117. net.limitDesc = config.limitDesc
  118. return true
  119. end
  120. -- 发送
  121. function sendQuery(human)
  122. checkData(human)
  123. local msgRet = Msg.gc.GC_TEQUANSHOP_QUERY
  124. msgRet.list[0] = 0
  125. for id, config in ipairs(TequanShopExcel) do
  126. local net = msgRet.list[msgRet.list[0] + 1]
  127. if fontShopNet(net, id, config, human) then
  128. msgRet.list[0] = msgRet.list[0] + 1
  129. end
  130. end
  131. --Msg.trace(msgRet)
  132. Msg.send(msgRet, human.fd)
  133. end
  134. -- 购买
  135. function buyLibao(human, id, costType)
  136. local config = TequanShopExcel[id]
  137. if not config then return end
  138. if config.cost[1] ~= costType then
  139. return
  140. end
  141. checkData(human)
  142. if getLeftCnt(human, id) < 1 then
  143. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  144. end
  145. if costType == COST_ZUANSHI then
  146. local needZuanshi = config.cost[2]
  147. if not ObjHuman.checkRMB(human, needZuanshi) then
  148. return
  149. end
  150. ObjHuman.decZuanshi(human, -needZuanshi, "tequan_shop")
  151. end
  152. addCnt(human, id)
  153. BagLogic.addItemList(human, config.items, "tequan_shop")
  154. sendQuery(human)
  155. end