123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- local schema = require "model.schema"
- local logger = require "logger"
- local asset = require "model.asset"
- local common_fun = require "model.common_fun"
- local payment = require "model.payment"
- local reward = require "model.reward"
- local stringify = require "stringify"
- local MODULE_NAME = "time_box"
- local MAX_CHANGE = 20000000000
- local _M = schema.new(MODULE_NAME, {
- data = {
- }
- })
- local REQUEST = {}
- local CMD = {}
- local MODULE = {}
- local THIS = {}
- local ret_func = common_fun.get_ret_func(THIS)
- 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)
- end
- -- TODO: 类似泰利的 prepare 接口
- function MODULE.launch(character)
- local d = _M.assert_get(character)
- end
- -- TODO: 与客户端同步数据
- function MODULE.ready(character)
- local d = _M.assert_get(character)
- logger.test("%s: ready, d:%s", MODULE_NAME, stringify(d))
- end
- -- TODO: 玩家下线时的处理
- function MODULE.saybye(character)
- local d = _M.assert_get(character)
- end
- local function get_max_pos()
- return 4
- end
- local function get_box_data_one(pos, data)
- local temp = common_fun.scopy(data)
- temp.pos = pos
- return temp
- end
- function MODULE.get_client_data(character)
- local d = _M.assert_get(character) or {}
- local max_pos = get_max_pos()
- local ret = {}
- for i = 1, max_pos do
- if d.data[i] then
- table.insert(ret, get_box_data_one(i, d.data[i]))
- end
- end
- return ret
- end
- function MODULE.get_box_surplus(character)
- local d = _M.assert_get(character) or {}
- local max_pos = get_max_pos()
- local surplus_num = max_pos
- for i = 1, max_pos do
- if d.data[i] then
- surplus_num = surplus_num - 1
- end
- end
- return surplus_num
- end
- function MODULE.add_box(character, id, num, collect, dispose, addition)
- local d = _M.assert_get(character) or {}
- local conf_list = assert(asset.BoxConfig_proto, "BoxConfig_proto")
- local conf = conf_list[id]
- if not conf and dispose then
- dispose(nil, id, num)
- return
- end
- local add_num = 0
- local max_pos = get_max_pos()
- if num >= 1 and num < MAX_CHANGE then
- for i = 1, max_pos do
- if add_num >= num then
- break
- end
- if not d.data[i] then
- d.data[i] = {
- id = id,
- tm = 0,
- }
- collect(GOODS_BOX, get_box_data_one(i, d.data[i]))
- add_num = add_num + 1
- end
- end
- logger.trace(stringify(d.data or {}))
- addition(GOODS_BOX, id, add_num)
- _M.persist(character)
- return true
- else
- if dispose then
- dispose(nil, id, num)
- end
- end
- end
- function THIS.time_box_unlock(character, args)
- local d = _M.assert_get(character) or {}
- local pos = args.pos
- if not pos or pos <= 0 then
- return STD_ERR.COMMON_PARM_ERR -- 参数异常
- end
-
- local data = d.data[pos]
- if not data or not data.tm or data.tm > 0 then
- return STD_ERR.COMMON_PARM_ERR -- 参数异常
- end
-
- local conf_list = assert(asset.BoxConfig_proto, "BoxConfig_proto")
- local conf = conf_list[data.id]
- data.tm = os.time()+ conf.cd*MIN_SEC
- _M.persist(character)
- return 0, {data = get_box_data_one(pos, data)}
- end
- function THIS.time_box_get_award(character, args)
- local d = _M.assert_get(character) or {}
- local pos = args.pos
- if not pos or pos <= 0 then
- return STD_ERR.COMMON_PARM_ERR -- 参数异常
- end
-
- local data = d.data[pos]
- if not data or not data.tm or data.tm <= 0 then
- return STD_ERR.COMMON_PARM_ERR -- 参数异常
- end
- local conf_list = assert(asset.BoxConfig_proto, "BoxConfig_proto")
- local conf = conf_list[data.id]
- if not conf then
- return STD_ERR.COMMON_CONF_ERR -- 配置异常
- end
- local now = os.time()
- local pay_list = {}
- local errno, award_list = common_fun.get_award(conf.award)
- if errno ~= 0 then
- return errno
- end
- if now < data.tm then
- for i = 1, #conf.consume, 2 do
- local item_id = conf.consume[i]
- local item_num = conf.consume[i+1]
- if item_id and item_num and item_id > 0 and item_num > 0 then
- table.insert(pay_list, {id = item_id, num = item_num})
- end
- end
- end
- if next(pay_list) then
- local receipt = {
- module="box",
- brief="开启宝箱",
- context="开启宝箱:"..data.id,
- notify={
- flags="time_box_get_award"
- },
- detail=pay_list
- }
- local errno = payment(character, receipt)
- if errno ~= 0 then
- return errno
- end
- end
- local desc = {
- module="box",
- brief="开启宝箱",
- context="开启宝箱:"..data.id,
- mailgen={subject=2, body=""},
- notify={
- flags="time_box_get_award"
- },
- detail=award_list
- }
- reward(character, desc)
- d.data[pos] = nil
- _M.persist(character)
- return 0, {pos = pos}
- end
- function THIS.time_box_data(character, args)
- return 0, {list = MODULE.get_client_data(character)}
- end
- function REQUEST.time_box_unlock(character, args)
- return ret_func("time_box_unlock", character, args)
- end
- function REQUEST.time_box_get_award(character, args)
- return ret_func("time_box_get_award", character, args)
- end
- function REQUEST.time_box_data(character, args)
- return ret_func("time_box_data", character, args)
- end
- return MODULE
|