JinbiExchangeLogic.lua 5.6 KB

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