TequanShopLogic.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. local TEQUAN_FASTGIFT = 601
  21. -- 是否激活征战扫荡特权
  22. function isActive(human, YYInfo, funcConfig)
  23. return isActiveMopup(human)
  24. end
  25. -- 是否激活征战扫荡特权
  26. function isActiveMopup(human)
  27. if not human.db.tequanMopup then
  28. return
  29. end
  30. if human.db.tequanMopup < os.time() then
  31. human.db.tequanMopup = nil
  32. return
  33. end
  34. return true
  35. end
  36. -- 已购买次数
  37. function getCnt(human, id)
  38. if not human.db.tequanShop then
  39. return 0
  40. end
  41. local data = human.db.tequanShop[id]
  42. if not data then return 0 end
  43. return data.cnt or 0
  44. end
  45. -- 记录购买次数
  46. function addCnt(human, id)
  47. if not human.db.tequanShop then
  48. human.db.tequanShop = {}
  49. end
  50. if not human.db.tequanShop[id] then
  51. human.db.tequanShop[id] = {}
  52. end
  53. local data = human.db.tequanShop[id]
  54. data.cnt = (data.cnt or 0) + 1
  55. data.time = data.time or os.time()
  56. end
  57. -- 剩余购买次数
  58. function getLeftCnt(human, id)
  59. local config = TequanShopExcel[id]
  60. if not config then
  61. return 0
  62. end
  63. return math.max(config.limitCnt - getCnt(human, id), 0)
  64. end
  65. -- 刷新剩余时间
  66. function getLeftTime(human, id)
  67. local config = TequanShopExcel[id]
  68. if not config then
  69. return DEF_FOREVER_TIME
  70. end
  71. if not human.db.tequanShop then
  72. return 0
  73. end
  74. local data = human.db.tequanShop[id]
  75. if not data then return 0 end
  76. local leftTime = DEF_FOREVER_TIME
  77. if config.refreshTime > 0 then
  78. local refreshTimeTs = config.refreshTime * 86400
  79. leftTime = math.max(refreshTimeTs - (os.time() - data.time), 0)
  80. end
  81. return leftTime
  82. end
  83. -- 检查是否需要刷新
  84. function checkData(human)
  85. if not human.db.tequanShop then
  86. return
  87. end
  88. for id in pairs(human.db.tequanShop) do
  89. if getLeftTime(human, id) == 0 then
  90. human.db.tequanShop[id] = nil
  91. end
  92. end
  93. end
  94. -- 封装礼包结构体
  95. function fontShopNet(net, id, config, human)
  96. local leftTime = getLeftTime(human, id)
  97. local leftCnt = getLeftCnt(human, id)
  98. net.id = id
  99. net.buyItem[0] = 0
  100. net.needZuanshi = 0
  101. local costType = config.cost[1]
  102. if costType == COST_RMB then
  103. net.buyItem[0] = 1
  104. BuyLogic.fontBuyItem(human, net.buyItem[1], config.cost[2])
  105. else
  106. net.needZuanshi = config.cost[2]
  107. end
  108. net.cnt = getCnt(human, id)
  109. net.maxCnt = config.limitCnt
  110. net.name = config.name
  111. local len = #config.items
  112. for i = 1,len do
  113. Grid.makeItem(net.item[i],config.items[i][1],config.items[i][2])
  114. end
  115. net.item[0] = len
  116. net.desc = config.desc
  117. net.leftTime = math.max(leftTime, 0)
  118. net.limitDesc = config.limitDesc
  119. return true
  120. end
  121. -- 发送
  122. function sendQuery(human)
  123. checkData(human)
  124. local msgRet = Msg.gc.GC_TEQUANSHOP_QUERY
  125. msgRet.list[0] = 0
  126. for id, config in ipairs(TequanShopExcel) do
  127. local net = msgRet.list[msgRet.list[0] + 1]
  128. if fontShopNet(net, id, config, human) then
  129. msgRet.list[0] = msgRet.list[0] + 1
  130. end
  131. end
  132. --Msg.trace(msgRet)
  133. Msg.send(msgRet, human.fd)
  134. end
  135. -- 购买
  136. function buyLibao(human, id, costType, nBuyNum)
  137. local config = TequanShopExcel[id]
  138. if not config then return end
  139. if config.cost[1] ~= costType then
  140. return
  141. end
  142. checkData(human)
  143. if getLeftCnt(human, id) < 1 then
  144. -- if getLeftCnt(human, id) < nBuyNum then
  145. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  146. end
  147. if costType == COST_ZUANSHI then
  148. local needZuanshi = config.cost[2]
  149. if not ObjHuman.checkRMB(human, needZuanshi) then
  150. return
  151. end
  152. ObjHuman.decZuanshi(human, -needZuanshi, "tequan_shop")
  153. end
  154. addCnt(human, id)
  155. BagLogic.addItemList(human, config.items, "tequan_shop")
  156. -- addCnt(human, nBuyNum)
  157. -- local tItemList = {}
  158. -- for _, v in ipairs(config.items) do
  159. -- table.insert(tItemList, {v[1], v[2]*nBuyNum})
  160. -- end
  161. -- BagLogic.addItemList(human, tItemList, "tequan_shop")
  162. sendQuery(human)
  163. end
  164. -- function GetRemainNum(human, nBuyID)
  165. -- checkData(human)
  166. -- for id, config in ipairs(TequanShopExcel) do
  167. -- local costType = config.cost[1]
  168. -- if costType == COST_RMB then
  169. -- local nCofBuyID = config.cost[2]
  170. -- if nCofBuyID == nBuyID then
  171. -- local nLeftCnt = getLeftCnt(human, id)
  172. -- return nLeftCnt
  173. -- end
  174. -- end
  175. -- end
  176. -- return 0
  177. -- end