JinbiExchangeLogic.lua 7.8 KB

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