JinbiExchangeLogic.lua 5.8 KB

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