123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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 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
-
- 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,
-
-
- flags=notify.flags
- })
- end
- if errlist then
- logger.error("REWARD ERR !!!! module:%s, brief:%s, errlist:%s", module, brief, stringify(errlist or {}))
- end
-
- 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
|