BillboardAim.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. ------------------------------------------
  2. -- 进度奖励
  3. -- [aimID] = {[rank] = {uuid, time}}
  4. -- db.billboardAim[aimID] = 是否领取
  5. ------------------------------------------
  6. local BillboardExcel = require("excel.billboard")
  7. local Util = require("common.Util")
  8. local CommonDB = require("common.CommonDB")
  9. local Lang = require("common.Lang")
  10. local Msg = require("core.Msg")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local Grid = require("bag.Grid")
  13. local BagLogic = require("bag.BagLogic")
  14. local BillboardDefine = require("billboard.BillboardDefine")
  15. local BattleLogic = require("battle.BattleLogic")
  16. local RoleLogic = require("role.RoleLogic")
  17. local BillboardLogic = require("billboard.BillboardLogic")
  18. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  19. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  20. local AIM_LOG_MAX_CNT = 5 -- 达标最多记录x条
  21. local STATUS_CANTGET = 0 -- 不可领
  22. local STATUS_CANGET = 1 -- 可领
  23. local STATUS_HADGET = 2 -- 已领
  24. -- 获取达标信息
  25. function getAimData(aimID)
  26. local data = CommonDB.getValueByKey(CommonDB.KEY_BILLBOARD_AIM)
  27. if not data then return end
  28. return data[aimID]
  29. end
  30. -- 获取达标玩家信息
  31. function getAimRoleData(aimID, rank)
  32. local aimData = getAimData(aimID)
  33. if not aimData then return end
  34. return aimData[rank]
  35. end
  36. -- 是否在记录里
  37. local function isOnAimLog(aimData, uuid)
  38. if not aimData then return end
  39. for _, roleAim in ipairs(aimData) do
  40. if roleAim.uuid == uuid then
  41. return true
  42. end
  43. end
  44. end
  45. -- 完成目标回调
  46. local function onAimFinish(aimID, uuid)
  47. local data = CommonDB.getValueByKey(CommonDB.KEY_BILLBOARD_AIM)
  48. data = data or {}
  49. data[aimID] = data[aimID] or {}
  50. local aimData = data[aimID]
  51. if #aimData >= AIM_LOG_MAX_CNT then
  52. return
  53. end
  54. if isOnAimLog(aimData, uuid) then
  55. return
  56. end
  57. local roleAim = {}
  58. roleAim.uuid = uuid
  59. roleAim.time = os.time()
  60. aimData[#aimData + 1] = roleAim
  61. CommonDB.updateValue(CommonDB.KEY_BILLBOARD_AIM, data)
  62. end
  63. -- 排行榜变化回调
  64. function onCallback(boardType, db, value)
  65. local list = getAimIDsByType(boardType)
  66. if not list then return end
  67. for _, aimID in ipairs(list) do
  68. local config = BillboardExcel.aim[aimID]
  69. if config.value > value then
  70. break
  71. end
  72. onAimFinish(aimID, db._id)
  73. end
  74. end
  75. -- 是否领取奖励
  76. function isGetReward(human, aimID)
  77. if not human.db.billboardAim then
  78. return
  79. end
  80. return human.db.billboardAim[aimID]
  81. end
  82. -- 设置领取奖励
  83. function setGetReward(human, aimID)
  84. if not human.db.billboardAim then
  85. human.db.billboardAim = {}
  86. end
  87. human.db.billboardAim[aimID] = true
  88. end
  89. --------------------------------------- excel ----------------------------------------------
  90. local BOARDTYPE_2_LIST = nil
  91. function getAimIDsByType(boardType)
  92. if not BOARDTYPE_2_LIST then
  93. BOARDTYPE_2_LIST = {}
  94. for id, config in Util.pairsByKeys(BillboardExcel.aim) do
  95. if not BOARDTYPE_2_LIST[config.boardType] then
  96. BOARDTYPE_2_LIST[config.boardType] = {}
  97. end
  98. local len = #BOARDTYPE_2_LIST[config.boardType]
  99. BOARDTYPE_2_LIST[config.boardType][len + 1] = id
  100. end
  101. end
  102. return BOARDTYPE_2_LIST[boardType]
  103. end
  104. --------------------------------------- msg ------------------------------------------------
  105. local function getAimStatus(human, aimID)
  106. local config = BillboardExcel.aim[aimID]
  107. if not config then
  108. return STATUS_CANTGET
  109. end
  110. if not getAimRoleData(aimID, 1) then
  111. return STATUS_CANTGET
  112. end
  113. if isGetReward(human, aimID) then
  114. return STATUS_HADGET
  115. end
  116. return STATUS_CANGET
  117. end
  118. local function fontAimNet(net, aimID, human)
  119. local config = BillboardExcel.aim[aimID]
  120. net.id = aimID
  121. if config.boardType == BillboardDefine.TYPE_BATTLE then
  122. net.value = BattleLogic.getBattleName(human, config.value)
  123. else
  124. net.value = tostring(config.value)
  125. end
  126. net.roleBase[0] = 0
  127. local roleAim = getAimRoleData(aimID, 1)
  128. if roleAim then
  129. net.roleBase[0] = 1
  130. RoleLogic.getRoleBaseByUuid(roleAim.uuid, net.roleBase[1])
  131. end
  132. net.status = getAimStatus(human, aimID)
  133. Grid.makeItem(net.item, config.reward[1], config.reward[2])
  134. end
  135. function sendAimList(human, boardType)
  136. local config = BillboardExcel.board[boardType]
  137. if not config then
  138. return
  139. end
  140. local list = getAimIDsByType(boardType)
  141. if not list then return end
  142. local msgRet = Msg.gc.GC_BILLBOARD_AIM_LIST
  143. msgRet.boardType = boardType
  144. msgRet.desc = config.aimDesc
  145. msgRet.list[0] = math.min(#msgRet.list, #list)
  146. for i = 1, msgRet.list[0] do
  147. local id = list[i]
  148. fontAimNet(msgRet.list[i], id, human)
  149. end
  150. -- Msg.trace(msgRet)
  151. Msg.send(msgRet, human.fd)
  152. end
  153. local function fontAimRoleNet(net, rank, roleAim)
  154. net.rank = rank
  155. RoleLogic.getRoleBaseByUuid(roleAim.uuid, net.roleBase)
  156. net.time = roleAim.time
  157. end
  158. -- 查看前5达标的玩家列表
  159. function sendAimDetial(human, aimID)
  160. local aimData = getAimData(aimID)
  161. local msgRet = Msg.gc.GC_BILLBOARD_AIM_DETAIL
  162. msgRet.id = aimID
  163. msgRet.list[0] = aimData and #aimData or 0
  164. for i = 1, msgRet.list[0] do
  165. fontAimRoleNet(msgRet.list[i], i, aimData[i])
  166. end
  167. -- Msg.trace(msgRet)
  168. Msg.send(msgRet, human.fd)
  169. end
  170. -- 领取奖励
  171. function getAimReward(human, aimID)
  172. local status = getAimStatus(human, aimID)
  173. if status == STATUS_CANTGET then
  174. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
  175. end
  176. if status == STATUS_HADGET then
  177. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
  178. end
  179. setGetReward(human, aimID)
  180. local config = BillboardExcel.aim[aimID]
  181. BagLogic.cleanMomentItemList()
  182. BagLogic.updateMomentItem(1, config.reward[1], config.reward[2])
  183. BagLogic.addMomentItemList(human, "billboard_aim")
  184. local msgRet = Msg.gc.GC_BILLBOARD_AIM_GET
  185. msgRet.id = aimID
  186. Msg.send(msgRet, human.fd)
  187. BillboardLogic.sendMainList(human)
  188. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_203)
  189. end
  190. function isAimRed(human, boardType)
  191. local list = getAimIDsByType(boardType)
  192. if not list then return end
  193. for i = 1, #list do
  194. local aimID = list[i]
  195. local status = getAimStatus(human, aimID)
  196. if status == STATUS_CANTGET then
  197. break
  198. end
  199. if status == STATUS_CANGET then
  200. return true
  201. end
  202. end
  203. end