reward.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. local logger = require "logger"
  2. local log = require "model.log"
  3. local stringify = require "stringify"
  4. local common_fun = require "model.common_fun"
  5. local currency
  6. local hero
  7. local equip
  8. local mailbox
  9. local time_box
  10. -- local role
  11. local table_insert = table.insert
  12. local function batch(character, detail, collect, dispose, addition)
  13. currency = currency or require "model.currency"
  14. hero = hero or require "model.hero"
  15. equip = equip or require "model.equip"
  16. time_box = time_box or require "model.time_box"
  17. if detail then
  18. for _, v in ipairs(detail) do
  19. local id = v.id
  20. local num = v.num
  21. local gtype = common_fun.goods_type(id)
  22. if gtype == GOODS_NONE then
  23. dispose(gtype, id, num)
  24. elseif gtype == GOODS_MONEY then
  25. currency.add_money(character, id, num, collect, dispose, addition)
  26. elseif gtype == GOODS_HERO then
  27. hero.add_hero(character, id, num, collect, dispose, addition)
  28. elseif gtype == GOODS_EQUIP then
  29. equip.add_equip(character, id, num, collect, dispose, addition)
  30. elseif gtype == GOODS_BOX then
  31. time_box.add_box(character, id, num, collect, dispose, addition)
  32. end
  33. end
  34. end
  35. end
  36. local function _simple(character, detail, module, brief, context)
  37. assert(character)
  38. assert(detail)
  39. -- 新增数量,用于日志存储
  40. local added = {}
  41. local addition = function(type, id, num)
  42. local list = {id = id, num = num}
  43. if type == GOODS_MONEY then
  44. added.currency = added.currency or {}
  45. table_insert(added.currency, list)
  46. elseif type == GOODS_HERO then
  47. added.heroes = added.heroes or {}
  48. table_insert(added.heroes, list)
  49. elseif type == GOODS_EQUIP then
  50. added.equip = added.equip or {}
  51. table_insert(added.equip, list)
  52. elseif type == GOODS_BOX then
  53. added.box = added.box or {}
  54. table_insert(added.box, list)
  55. end
  56. end
  57. -- 将立即发放物品, 发送给客户端
  58. local cache = {}
  59. local collect = function(type, list)
  60. if type == GOODS_MONEY then
  61. cache.currency = cache.currency or {}
  62. table_insert(cache.currency, list)
  63. elseif type == GOODS_HERO then
  64. cache.heroes = cache.heroes or {}
  65. table_insert(cache.heroes, list)
  66. elseif type == GOODS_EQUIP then
  67. cache.equip = cache.equip or {}
  68. table_insert(cache.equip, list)
  69. elseif type == GOODS_BOX then
  70. cache.box = cache.box or {}
  71. table_insert(cache.box, list)
  72. end
  73. end
  74. local lost = nil -- 丢弃的道具/装备(背包满)
  75. local errlist = nil -- 配置中不存在的道具统一放这里
  76. local dispose = function(type, id, num)
  77. local list = {id = id, num = num}
  78. if type == nil or type == GOODS_NONE then
  79. errlist = errlist or {}
  80. table_insert(errlist, list)
  81. else
  82. lost = lost or {}
  83. table_insert(lost, list)
  84. end
  85. end
  86. -- 开始发放逻辑
  87. batch(character, detail, collect, dispose, addition)
  88. if added then
  89. -- TODO: 在这里记录奖励日志
  90. if next(added) then
  91. log.reward(character, module, brief, context, added)
  92. end
  93. end
  94. return cache, lost, errlist
  95. end
  96. local function reward(character, desc)
  97. assert(character)
  98. assert(desc)
  99. assert(type(desc.detail) == "table")
  100. local detail = common_fun.merge_pay(desc.detail)
  101. local module = assert(desc.module)
  102. local brief = assert(desc.brief)
  103. local notify = assert(desc.notify)
  104. local context = desc.context
  105. local client, lost, errlist = _simple(
  106. character, detail, module, brief, context)
  107. if notify.flags and next(client) then
  108. -- 立刻与客户端同步奖励数据
  109. character.send("reward_info", {
  110. added=client, -- 客户端需要当前量
  111. -- lost= lost,
  112. -- extra=notify.extra,
  113. flags=notify.flags
  114. })
  115. end
  116. if errlist then
  117. logger.error("REWARD ERR !!!! module:%s, brief:%s, errlist:%s", module, brief, stringify(errlist or {}))
  118. end
  119. -- 当存在丢弃物品时(装备道具), 通过邮件发送(mailgen有效)
  120. local mailgen = desc.mailgen
  121. local falg = false
  122. if lost and mailgen then
  123. falg = true
  124. local subject = assert(mailgen.subject)
  125. local body = assert(mailgen.body)
  126. local specific = {
  127. module=module,
  128. brief=brief,
  129. context=context
  130. }
  131. mailbox = mailbox or require "model.mailbox"
  132. mailbox.send(character, subject, body, lost, specific, desc.must_mail)
  133. end
  134. return falg
  135. end
  136. return reward