JinbiExchangeLogic.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. local Lang = require("common.Lang")
  2. local Msg = require("core.Msg")
  3. local ObjHuman = require("core.ObjHuman")
  4. local Broadcast = require("broadcast.Broadcast")
  5. local JinbiExchangeExcel = require("excel.JinbiExchange")
  6. local ItemDefine = require("bag.ItemDefine")
  7. local Grid = require("bag.Grid")
  8. local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
  9. local Util = require("common.Util")
  10. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  11. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  12. local VipLogic = require("vip.VipLogic")
  13. local YunYingLogic = require("yunying.YunYingLogic")
  14. local TalismanLogic = require("talisman.TalismanLogic")
  15. local TriggerDefine = require("trigger.TriggerDefine")
  16. local TriggerLogic = require("trigger.TriggerLogic")
  17. MAINTYPE_FREE = 1
  18. REFRESH_HOURS = {0, 9, 19}
  19. function getExchangeCnt(human, mainType)
  20. if not human.db.jinbiExchange then
  21. return 0
  22. end
  23. return human.db.jinbiExchange[mainType] or 0
  24. end
  25. function addExchangeCnt(human, mainType)
  26. if not human.db.jinbiExchange then
  27. human.db.jinbiExchange = {}
  28. end
  29. human.db.jinbiExchange.ts = os.time()
  30. local oldcnt = human.db.jinbiExchange[mainType] or 0
  31. human.db.jinbiExchange[mainType] = oldcnt + 1
  32. end
  33. function getLeftCnt(human, mainType)
  34. local cf = JinbiExchangeExcel.define[mainType]
  35. if not cf then return 0 end
  36. local leftCnt = cf.cnt - getExchangeCnt(human, mainType)
  37. return math.max(leftCnt, 0)
  38. end
  39. local function checkNeedRefresh(ts)
  40. local nowTime = os.time()
  41. local dayStartTime = Util.getDayStartTime(ts)
  42. local leftTime = nil
  43. for _, hour in ipairs(REFRESH_HOURS) do
  44. local refreshTime = dayStartTime + hour * 3600
  45. if nowTime >= refreshTime and ts < refreshTime then
  46. return true
  47. else
  48. local tleftTime = refreshTime - nowTime
  49. if tleftTime >= 0 and (leftTime == nil or tleftTime < leftTime) then
  50. leftTime = tleftTime
  51. end
  52. end
  53. local nextRefreshTime = refreshTime + 86400
  54. if nowTime >= nextRefreshTime and ts < nextRefreshTime then
  55. return true
  56. else
  57. local tleftTime = nextRefreshTime - nowTime
  58. if tleftTime >= 0 and (leftTime == nil or tleftTime < leftTime) then
  59. leftTime = tleftTime
  60. end
  61. end
  62. end
  63. return false, leftTime
  64. end
  65. function checkRefresh(human)
  66. local jinbiExchange = human.db.jinbiExchange
  67. if not jinbiExchange then return end
  68. local dirty = nil
  69. for i = 1, #JinbiExchangeExcel.define do
  70. if jinbiExchange[i] then
  71. dirty = true
  72. break
  73. end
  74. end
  75. if not dirty then return end
  76. local bRefresh, leftTime = checkNeedRefresh(jinbiExchange.ts)
  77. if bRefresh then
  78. human.db.jinbiExchange = nil
  79. end
  80. return leftTime
  81. end
  82. -- 获取金币数量
  83. function getJinbiCnt(human, mainType)
  84. local jinbiExchangeConfig = JinbiExchangeExcel.jinbiExchange[human.db.lv]
  85. if not jinbiExchangeConfig then return end
  86. --local vipAdd = (VipLogic.getPowerArgs(human, VipLogic.VIP_POWER11) or 0) / 100
  87. local talismanAdd = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.DJ) or 0) / 100
  88. local baseCnt = jinbiExchangeConfig[mainType] or 0
  89. local jinbiCnt = baseCnt + math.ceil(baseCnt * talismanAdd)
  90. return jinbiCnt
  91. end
  92. -- 金币兑换查询
  93. function jinbiExchangeQuery(human)
  94. local leftTime = checkRefresh(human)
  95. local msgRet = Msg.gc.GC_JINBI_EXCHANGE_QUERY
  96. -- 获得金币
  97. msgRet.jinbiCnt[0] = #JinbiExchangeExcel.define
  98. for i = 1, msgRet.jinbiCnt[0] do
  99. msgRet.jinbiCnt[i] = getJinbiCnt(human, i)
  100. end
  101. -- 消耗元宝
  102. msgRet.needItemCnt[0] = #JinbiExchangeExcel.define - 1
  103. for i = 1, msgRet.needItemCnt[0] do
  104. local cf = JinbiExchangeExcel.define[i+1]
  105. msgRet.needItemCnt[i] = cf.cost
  106. end
  107. -- 剩余次数
  108. msgRet.leftExchange[0] = #JinbiExchangeExcel.define
  109. for i = 1, msgRet.leftExchange[0] do
  110. msgRet.leftExchange[i] = getLeftCnt(human, i)
  111. end
  112. msgRet.leftTime = leftTime or 0
  113. --Msg.trace(msgRet)
  114. Msg.send(msgRet, human.fd)
  115. end
  116. -- 金币兑换
  117. function jinbiExchangDo(human, mainType)
  118. local config = JinbiExchangeExcel.define[mainType]
  119. if not config then return end
  120. checkRefresh(human)
  121. if getLeftCnt(human, mainType) < 1 then
  122. return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
  123. end
  124. local needZuanshi = config.cost
  125. if not ObjHuman.checkRMB(human, needZuanshi) then
  126. return
  127. end
  128. -- 加金币
  129. local jinbiCnt = getJinbiCnt(human, mainType)
  130. if not ObjHuman.canAddJinbi(human, jinbiCnt) then
  131. return
  132. end
  133. -- 扣消耗
  134. if needZuanshi > 0 then
  135. ObjHuman.decZuanshi(human, -needZuanshi, "jinbi_exchange")
  136. end
  137. addExchangeCnt(human, mainType)
  138. ObjHuman.updateJinbi(human, jinbiCnt, "jinbi_exchange")
  139. -- 通知客户端
  140. local msgRet = Msg.gc.GC_JINBI_EXCHANGE_DO
  141. Grid.makeItem(msgRet.item, ItemDefine.ITEM_JINBI_ID, jinbiCnt)
  142. msgRet.vipLv = human.db.vipLv or 0
  143. Msg.send(msgRet, human.fd)
  144. jinbiExchangeQuery(human)
  145. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_103)
  146. YunYingLogic.onCallBack(human, "onExchangeJinbi",1)
  147. TriggerLogic.PublishEvent(TriggerDefine.GOLD_POINTS_NUM, human.db._id, 1)
  148. end
  149. function isDot(human)
  150. checkRefresh(human)
  151. return getLeftCnt(human, MAINTYPE_FREE) > 0
  152. end
  153. function onHour(hour)
  154. do return end
  155. local isFind = nil
  156. for _, thour in ipairs(REFRESH_HOURS) do
  157. if thour == hour then
  158. isFind = true
  159. break
  160. end
  161. end
  162. if not isFind then return end
  163. -- for _, human in pairs(ObjHuman.onlineUuid) do
  164. -- RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_403)
  165. -- end
  166. end