local logger = require "logger" local log = require "model.log" local stringify = require "stringify" local common_fun = require "model.common_fun" local currency local hero local equip local mailbox local time_box -- local role local table_insert = table.insert local function batch(character, detail, collect, dispose, addition) currency = currency or require "model.currency" hero = hero or require "model.hero" equip = equip or require "model.equip" time_box = time_box or require "model.time_box" if detail then for _, v in ipairs(detail) do local id = v.id local num = v.num local gtype = common_fun.goods_type(id) if gtype == GOODS_NONE then dispose(gtype, id, num) elseif gtype == GOODS_MONEY then currency.add_money(character, id, num, collect, dispose, addition) elseif gtype == GOODS_HERO then hero.add_hero(character, id, num, collect, dispose, addition) elseif gtype == GOODS_EQUIP then equip.add_equip(character, id, num, collect, dispose, addition) elseif gtype == GOODS_BOX then time_box.add_box(character, id, num, collect, dispose, addition) end end end end local function _simple(character, detail, module, brief, context) assert(character) assert(detail) -- 新增数量,用于日志存储 local added = {} local addition = function(type, id, num) local list = {id = id, num = num} if type == GOODS_MONEY then added.currency = added.currency or {} table_insert(added.currency, list) elseif type == GOODS_HERO then added.heroes = added.heroes or {} table_insert(added.heroes, list) elseif type == GOODS_EQUIP then added.equip = added.equip or {} table_insert(added.equip, list) elseif type == GOODS_BOX then added.box = added.box or {} table_insert(added.box, list) end end -- 将立即发放物品, 发送给客户端 local cache = {} local collect = function(type, list) if type == GOODS_MONEY then cache.currency = cache.currency or {} table_insert(cache.currency, list) elseif type == GOODS_HERO then cache.heroes = cache.heroes or {} table_insert(cache.heroes, list) elseif type == GOODS_EQUIP then cache.equip = cache.equip or {} table_insert(cache.equip, list) elseif type == GOODS_BOX then cache.box = cache.box or {} table_insert(cache.box, list) end end local lost = nil -- 丢弃的道具/装备(背包满) local errlist = nil -- 配置中不存在的道具统一放这里 local dispose = function(type, id, num) local list = {id = id, num = num} if type == nil or type == GOODS_NONE then errlist = errlist or {} table_insert(errlist, list) else lost = lost or {} table_insert(lost, list) end end -- 开始发放逻辑 batch(character, detail, collect, dispose, addition) if added then -- TODO: 在这里记录奖励日志 if next(added) then log.reward(character, module, brief, context, added) end end return cache, lost, errlist end local function reward(character, desc) assert(character) assert(desc) assert(type(desc.detail) == "table") local detail = common_fun.merge_pay(desc.detail) local module = assert(desc.module) local brief = assert(desc.brief) local notify = assert(desc.notify) local context = desc.context local client, lost, errlist = _simple( character, detail, module, brief, context) if notify.flags and next(client) then -- 立刻与客户端同步奖励数据 character.send("reward_info", { added=client, -- 客户端需要当前量 -- lost= lost, -- extra=notify.extra, flags=notify.flags }) end if errlist then logger.error("REWARD ERR !!!! module:%s, brief:%s, errlist:%s", module, brief, stringify(errlist or {})) end -- 当存在丢弃物品时(装备道具), 通过邮件发送(mailgen有效) local mailgen = desc.mailgen local falg = false if lost and mailgen then falg = true local subject = assert(mailgen.subject) local body = assert(mailgen.body) local specific = { module=module, brief=brief, context=context } mailbox = mailbox or require "model.mailbox" mailbox.send(character, subject, body, lost, specific, desc.must_mail) end return falg end return reward