ValleyTask.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ---------------------------------------------------
  2. -- 荣耀峡谷 个人成就/团队成就
  3. -- db.valleyTask.list 领取记录 [id]=true
  4. -- db.valleyTask.record 成就进度 [type]=value
  5. ---------------------------------------------------
  6. local ValleyExcel = require("excel.valley")
  7. local Util = require("common.Util")
  8. local Lang = require("common.Lang")
  9. local Msg = require("core.Msg")
  10. local Broadcast = require("broadcast.Broadcast")
  11. local Grid = require("bag.Grid")
  12. local BagLogic = require("bag.BagLogic")
  13. local RoleDBLogic = require("role.RoleDBLogic")
  14. local ValleyLogic = require("valley.ValleyLogic")
  15. local TASK_TYPE_KILLCNT = 1 -- 杀敌个数
  16. local TASK_TYPE_ROADCNT = 2 -- 攻破营地个数
  17. -- 状态
  18. local STATE_NONE = 0 -- 不可领
  19. local STATE_GET = 1 -- 可领
  20. local STATE_HAD = 2 -- 已领
  21. -- 封装成就结构体
  22. local function fontTaskNet(net, id, config, human)
  23. local descConfig = ValleyExcel.taskDesc[config.taskType]
  24. net.id = id
  25. net.desc = Util.format(descConfig.desc, config.taskValue)
  26. net.state = getTaskState(human, id)
  27. net.items[0] = #config.items
  28. for i = 1, net.items[0] do
  29. local itemID = config.items[i][1]
  30. local itemCnt = config.items[i][2]
  31. Grid.makeItem(net.items[i], itemID, itemCnt)
  32. end
  33. net.cnt = getValue(human, config.taskType)
  34. net.maxCnt = config.taskValue
  35. end
  36. -- 查看列表
  37. function sendQuery(human, taskType)
  38. local msgRet = Msg.gc.GC_VALLEY_TASK_QUERY
  39. msgRet.taskType = taskType
  40. msgRet.reds[0] = TASK_TYPE_ROADCNT
  41. for i = 1, msgRet.reds[0] do
  42. msgRet.reds[i] = isRedByType(i) and 1 or 0
  43. end
  44. local list = getIDsByType(taskType)
  45. msgRet.list[0] = list and #list or 0
  46. for i = 1, msgRet.list[0] do
  47. local id = list[i]
  48. local config = ValleyExcel.task[id]
  49. fontTaskNet(msgRet.list[i], id, config, human)
  50. end
  51. -- Msg.trace(msgRet)
  52. Msg.send(msgRet, human.fd)
  53. end
  54. -- 领取奖励
  55. function getReward(human, id)
  56. local state = getTaskState(human, id)
  57. if state == STATE_HAD then
  58. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
  59. end
  60. if state == STATE_NONE then
  61. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
  62. end
  63. local config = ValleyExcel.task[id]
  64. setGet(human, id)
  65. for i = 1, #config.items do
  66. local itemID = config.items[i][1]
  67. local itemCnt = config.items[i][2]
  68. BagLogic.addItem(human, itemID, itemCnt, "valley")
  69. end
  70. local msgRet = Msg.gc.GC_VALLEY_TASK_GET
  71. msgRet.id = id
  72. msgRet.reds[0] = TASK_TYPE_ROADCNT
  73. for i = 1, msgRet.reds[0] do
  74. msgRet.reds[i] = isRedByType(i) and 1 or 0
  75. end
  76. Msg.send(msgRet, human.fd)
  77. if not isRed(human) then -- 刷新主界面红点
  78. ValleyLogic.query(human)
  79. end
  80. end
  81. -- 是否有红点
  82. function isRed(human)
  83. for taskType in pairs(ValleyExcel.taskDesc) do
  84. if isRedByType(human, taskType) then
  85. return true
  86. end
  87. end
  88. end
  89. -- 是否有红点,根据任务类型
  90. function isRedByType(human, taskType)
  91. local list = getIDsByType(taskType)
  92. if not list then return end
  93. local value = getValue(human, taskType)
  94. for _, id in ipairs(list) do
  95. local state = getTaskState(human, id)
  96. if state == STATE_NONE then
  97. break
  98. end
  99. if state == STATE_GET then
  100. return true
  101. end
  102. end
  103. end
  104. -- 获取任务状态
  105. function getTaskState(human, id)
  106. local config = ValleyExcel.task[id]
  107. if not config then
  108. return STATE_NONE
  109. end
  110. if getValue(human, config.taskType) < config.taskValue then
  111. return STATE_NONE
  112. end
  113. if isGet(human, id) then
  114. return STATE_HAD
  115. end
  116. return STATE_GET
  117. end
  118. local TASKTYPE_2_IDS = nil
  119. function getIDsByType(taskType)
  120. if not TASKTYPE_2_IDS then
  121. TASKTYPE_2_IDS = {}
  122. for id, config in Util.pairsByKeys(ValleyExcel.task) do
  123. if not TASKTYPE_2_IDS[config.taskType] then
  124. TASKTYPE_2_IDS[config.taskType] = {}
  125. end
  126. local len = #TASKTYPE_2_IDS[config.taskType]
  127. TASKTYPE_2_IDS[config.taskType][len + 1] = id
  128. end
  129. end
  130. return TASKTYPE_2_IDS[taskType]
  131. end
  132. -- 是否已领取
  133. function isGet(human, id)
  134. if not human.db.valleyTask then
  135. return
  136. end
  137. if not human.db.valleyTask.list then
  138. return
  139. end
  140. return human.db.valleyTask.list[id]
  141. end
  142. -- 设置已领取
  143. function setGet(human, id)
  144. if not human.db.valleyTask then
  145. human.db.valleyTask = {}
  146. end
  147. if not human.db.valleyTask.list then
  148. human.db.valleyTask.list = {}
  149. end
  150. human.db.valleyTask.list[id] = true
  151. end
  152. -- 获取进度值
  153. function getValue(human, taskType)
  154. if not human.db.valleyTask then
  155. return 0
  156. end
  157. if not human.db.valleyTask.record then
  158. return 0
  159. end
  160. return human.db.valleyTask.record[taskType] or 0
  161. end
  162. -- 刷新任务进度
  163. function updateValue(uuid, killCnt, winCnt)
  164. if killCnt < 1 and winCnt < 1 then
  165. return
  166. end
  167. local db = RoleDBLogic.getDb(uuid, "valleyTask")
  168. if not db then return end
  169. db.valleyTask = db.valleyTask or {}
  170. db.valleyTask.record = db.valleyTask.record or {}
  171. local oldKillCnt = db.valleyTask.record[TASK_TYPE_KILLCNT] or 0
  172. db.valleyTask.record[TASK_TYPE_KILLCNT] = oldKillCnt + killCnt
  173. local oldWinCnt = db.valleyTask.record[TASK_TYPE_ROADCNT] or 0
  174. db.valleyTask.record[TASK_TYPE_ROADCNT] = oldWinCnt + winCnt
  175. RoleDBLogic.saveRoleSset(db)
  176. end