local schema = require "model.schema" local logger = require "logger" local stringify = require "stringify" local asset = require "model.asset" local module_fun = require "model.module_fun" local payment = require "model.payment" local reward = require "model.reward" local util = require "util" local rechargemod = require "model.rechargemod" local mailbox local currency local Register_model = { [MODULE_ID_RELIC_MANUA] = require "model.manual.relic_manual" } local FIRST_MANUAL = 1 -- 免费 local SECOND_MANUAL = 2 -- 强者战令 local THIRD_MANUAL = 3 -- 王者战令 -- 请和altar中保持一致 local EQUIP_FREE = 2 local HERO_FREE = 3 local module_name = "manual" local module_id = 38 local _M = schema.new(module_name, { open = false, round = 1, open_time = 0, level = 0, exp = 0, list = { -- { -- lock = true, -- award = 0, -- } }, privilege = { -- 英雄置换特权和装备置换特权 } }) local REQUEST = {} local CMD = {} local MODULE = {} local THIS = {} local function func_ret(fname, character, args) if args.moduleid and Register_model[args.moduleid] then local f = Register_model[args.moduleid][fname] if not f then logger.error("func_ret not moduleid:%d, fname:%s !!!", args.moduleid, fname) return {errno = STD_ERR.COMMON_SYS_ERR} end return f(character, args) end local f = THIS[fname] if not f then logger.error("func_ret not fname:%s !!!", fname) return {errno = STD_ERR.COMMON_SYS_ERR} end local errno, ret = f(character, args) if errno ~= 0 then return {errno = errno} end ret = ret or {} ret.errno = 0 ret.moduleid = module_id return ret end function MODULE.list_request_interests() return REQUEST end function MODULE.list_command_interests() return CMD end -- TODO: 解析/升级模块数据 在这里把数据初始化好 function MODULE.parse(character) local d = _M.load(character) end -- TODO: 侦听事件 function MODULE.monitor(character) local d = _M.assert_get(character) or {} character.monitor("currency_add", function(_, id, num) if id == CURRENCY_ID_DACTIVITY then THIS.manual_upgrade(character, num) end end) character.monitor("daily_refresh", function() if d.open then THIS.reset_model(character) end end) -- 特权重置 character.monitor("weekly_refresh", function() if d.open and d.privilege then d.privilege = {} _M.persist(character) end end) character.monitor("simple_battle_pass", function(_, id) if id == asset.DataConfig_proto[12].data1 then THIS.reset_model(character) end end) character.monitor("recharge.money",function(_, cfid, orderid, moneynum, giftid, id, pos) if module_id == id then THIS.manual_unlock(character, pos+1) end end) end -- TODO: 类似泰利的 prepare 接口 function MODULE.launch(character) currency = currency or require "model.currency" mailbox = mailbox or require "model.mailbox" end -- TODO: 与客户端同步数据 function MODULE.ready(character) local d = _M.assert_get(character) or {} THIS.reset_model(character) if d.open and d.level > 0 and d.exp > 0 then THIS.manual_upgrade(character, 0) end logger.test("%s:ready, %s", module_name, stringify(d or {})) end -- TODO: 玩家下线时的处理 function MODULE.saybye(character) end -- 战令特权剩余次数 function MODULE.manual_privilege_times(character, ptype) local d = _M.assert_get(character) or {} if not d.open then return 0 end local data, data_conf if ptype == EQUIP_FREE then data = d.list[SECOND_MANUAL] data_conf = asset.DataConfig_proto[37] elseif ptype == HERO_FREE then data = d.list[THIRD_MANUAL] data_conf = asset.DataConfig_proto[38] else return 0 end if not data or data.lock then return 0 end d.privilege = d.privilege or {} d.privilege[ptype] = d.privilege[ptype] or 0 return math.max(0, data_conf.data1 - d.privilege[ptype]) end -- 战令特权使用 function MODULE.manual_privilege_use(character, ptype) local d = _M.assert_get(character) or {} d.privilege = d.privilege or {} d.privilege[ptype] = d.privilege[ptype] or 0 d.privilege[ptype] = d.privilege[ptype] + 1 _M.persist(character) return 0 end function THIS.reset_model(character) local d = _M.assert_get(character) or {} if not d.open then d.round = 1 d.open = true d.exp = currency.get_money(character, CURRENCY_ID_DACTIVITY) else local now = os.time() local open_time = util.today(d.open_time) local day = (now-open_time)/DAY_SEC if day < asset.DataConfig_proto[12].data2 then return end local award_list = THIS.get_award_list(character) if next(award_list) then local desc = { module = "manual", brief = "战令自动发奖", context = "战令自动发奖,round:"..d.round, } mailbox.send(character, 6, "", award_list, desc) end d.round = (d.round or 0) + 1 d.exp = 0 end d.privilege = {} d.open_time = util.today() + 12 * 3600 d.level = 0 d.list = {} for i = FIRST_MANUAL, THIRD_MANUAL do d.list[i] = { lock = true, award = -1, } end d.list[FIRST_MANUAL].lock = false if d.exp > 0 then THIS.manual_upgrade(character) else _M.persist(character) end character.send("manual_notify", {open = d.open, open_time = util.today(d.open_time), level = d.level, exp = d.exp, round = d.round, list = d.list}) end function THIS.get_round(character) local d = _M.assert_get(character) or {} local round = 1 for _, v in pairs(asset.ManualConfig_proto) do if v.sign > round then round = v.sign end end return math.min(round, d.round and d.round or 1) end function THIS.get_conf_list(round) local ret = {} for _, conf in pairs(asset.ManualConfig_proto) do if conf.sign == round then ret[conf.level] = conf end end return ret end function THIS.manual_upgrade(character, add) local d = _M.assert_get(character) or {} local round = THIS.get_round(character) local conf_list = THIS.get_conf_list(round) local level = d.level local exp = d.exp + (add or 0) while(true) do local conf = conf_list[level+1] if not conf then break end if conf.exp > exp then break else level = level + 1 exp = exp - conf.exp end end if level ~= d.level or exp ~= d.exp then d.level = level d.exp = exp _M.persist(character) end end function THIS.manual_buy_exp(character, args) local d = _M.assert_get(character) or {} if not d.open then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local data_conf = asset.DataConfig_proto[13] if not data_conf then return STD_ERR.COMMON_CONF_ERR -- 配置异常 end local round = THIS.get_round(character) local conf_list = THIS.get_conf_list(round) if not conf_list[d.level + 1] then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local pay_list = {{id = CURRENCY_ID_DIA, num = data_conf.data2}} local receipt = { module="manual", brief="购买战令经验", context="购买战令经验", notify={ flags="manual_buy_exp" }, detail=pay_list } -- 检查消耗 local errno = module_fun.paymeny_check(character, pay_list) if errno ~= 0 then return errno end payment(character, receipt) THIS.manual_upgrade(character, data_conf.data1) return 0, {level = d.level, exp = d.exp} end function THIS.manual_get_data(character, args) local d = _M.assert_get(character) return 0, {open = d.open, open_time = util.today(d.open_time), level = d.level, exp = d.exp, round = d.round, list = d.list} end function THIS.get_award_list(character) local d = _M.assert_get(character) or {} local award_list = {} if not d.open then return award_list end local round = THIS.get_round(character) local max_award = -1 for _, conf in pairs(asset.ManualConfig_proto) do if conf.sign == round then for k, v in ipairs(d.list) do if not v.lock and v.award < conf.level and d.level >= conf.level then local award_conf if k == FIRST_MANUAL then award_conf = conf.brave elseif k == SECOND_MANUAL then award_conf = conf.strong elseif k == THIRD_MANUAL then award_conf = conf.king else assert(false) end max_award = math.max(max_award, conf.level) for i = 1, #award_conf, 2 do local item_id = award_conf[i] local item_num = award_conf[i+1] if item_id and item_num and item_id > 0 and item_num > 0 then table.insert(award_list, {id = item_id, num = item_num}) end end end end end end return award_list, max_award end function THIS.manual_get_award(character, args) local d = _M.assert_get(character) or {} if not d.open then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local award_list, max_award = THIS.get_award_list(character) if not next(award_list) then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end if not max_award then return STD_ERR.COMMON_SYS_ERR -- 系统异常 end -- 发奖励 local desc = { module="manual", brief="战令奖励", context= "战令奖励", mailgen={subject=2, body=""}, notify={ flags="manual_get_award" }, detail=award_list } reward(character, desc) for k, v in ipairs(d.list) do if not v.lock then v.award = max_award end end _M.persist(character) return 0, {award = max_award} end function THIS.manual_unlock(character, type) local d = _M.assert_get(character) or {} if not d.open then return end local data = d.list[type] if not data then return end data.lock = false if d.privilege and d.privilege[type] and d.privilege[type] > 0 then d.privilege[type] = 0 end _M.persist(character) character.send("manual_notify", {open = d.open, open_time = util.today(d.open_time), level = d.level, exp = d.exp, round = d.round, list = d.list}) end function THIS.manual_buy(character, args) local d = _M.assert_get(character) or {} if not d.open then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end if not args.giftid then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local manual_type if args.giftid == 50001 then manual_type = SECOND_MANUAL elseif args.giftid == 50002 then manual_type = THIRD_MANUAL else return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local data = d.list[manual_type] if not data then return STD_ERR.COMMON_SYS_ERR -- 系统异常 end if not data.lock then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local conf = asset.GiftConfig_proto[args.giftid] if not conf then return STD_ERR.COMMON_CONF_ERR -- 配置异常 end if conf.type ~= module_id then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local pay_list = {} if conf.spendtype == PAY_TYPE_FREE then pay_list = nil elseif conf.spendtype == PAY_TYPE_ITEM then table.insert(pay_list, {id = conf.spend, num = conf.num}) elseif conf.spendtype == PAY_TYPE_GIFT then rechargemod.get_orderid(character, conf.ID) return 0 end if pay_list then if not next(pay_list) then return STD_ERR.COMMON_CONF_ERR -- 配置异常 end local receipt = { module=module_name, brief="购买遗迹战令", context= "购买遗迹战令", mailgen={subject=2, body=""}, notify={ flags="manual_buy" }, detail=pay_list } local errno = payment(character, receipt) if errno ~= 0 then return errno end end THIS.manual_unlock(character, manual_type) return 0 end function REQUEST.manual_get_award(character, args) return func_ret("manual_get_award", character, args) end function REQUEST.manual_buy_exp(character, args) return func_ret("manual_buy_exp", character, args) end function REQUEST.manual_get_data(character, args) return func_ret("manual_get_data", character, args) end function REQUEST.manual_buy(character, args) return func_ret("manual_buy", character, args) end function MODULE.get_red_point(character) local d = _M.assert_get(character) or {} if not d.open then return false end local round = THIS.get_round(character) for _, v in pairs(d.list) do if not v.lock then for _, conf in pairs(asset.ManualConfig_proto) do if d.level >= conf.level and v.award < conf.level and conf.sign == round then return true, module_id end end end end return false end return MODULE