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 skynet = require "skynet" local s_relic_manual = skynet.localname(".relic_manual") local mailbox local currency local FIRST_MANUAL = 1 -- 免费 local SECOND_MANUAL = 2 -- 遗迹通行证 local module_name = "relic_manual" local module_id = MODULE_ID_RELIC_MANUA local _M = schema.new(module_name, { open = false, round = 1, open_time = 0, level = 0, exp = 0, list = { -- { -- lock = true, -- award = 0, -- } }, }) local CMD = {} local MODULE = {} local THIS = {} local function func_ret(fname, character, args) 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_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) character.monitor("currency_add", function(_, id, num) if id == CURRENCY_ID_RELIC_SCORE 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("recharge.money",function(_, cfid, orderid, moneynum, giftid, id, pos) if module_id == id then THIS.manual_unlock(character) 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) 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 THIS.reset_model(character) local d = _M.assert_get(character) if not d.open then local manual_data = skynet.call(s_relic_manual, "lua", "relic_manual_data") if not manual_data then return end d.round = manual_data.round d.open = true d.exp = 0 d.open_time = manual_data.open_time 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[22].data2 then return end local manual_data = skynet.call(s_relic_manual, "lua", "relic_manual_data") if manual_data.round == d.round 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 = manual_data.round d.exp = 0 d.open_time = manual_data.open_time end d.level = 0 d.list = {} for i = FIRST_MANUAL, SECOND_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 THIS.send_data(character) end function THIS.get_round(character) local d = _M.assert_get(character) local round = 1 for _, v in pairs(asset.RelicmanualConfig_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.RelicmanualConfig_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) 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) -- 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="relic_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) or {} 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.RelicmanualConfig_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.ordinary elseif k == SECOND_MANUAL then award_conf = conf.noble 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="relic_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.send_data(character) local d = _M.assert_get(character) character.send("manual_notify", {moduleid = module_id, 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_unlock(character) local d = _M.assert_get(character) if not d.open then return end local data = d.list[SECOND_MANUAL] if not data then return end data.lock = false _M.persist(character) THIS.send_data(character) end function THIS.manual_buy(character, args) local d = _M.assert_get(character) if not d.open then return STD_ERR.COMMON_PARM_ERR -- 参数异常 end local data = d.list[SECOND_MANUAL] 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[50050] if not conf then return STD_ERR.COMMON_CONF_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) return 0 end function MODULE.manual_get_award(character, args) return func_ret("manual_get_award", character, args) end -- function MODULE.manual_buy_exp(character, args) -- return func_ret("manual_buy_exp", character, args) -- end function MODULE.manual_get_data(character, args) return func_ret("manual_get_data", character, args) end function MODULE.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 k, v in pairs(d.list) do if not v.lock then for _, conf in pairs(asset.RelicmanualConfig_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