123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- local schema = require "model.schema"
- local logger = require "logger"
- local asset = require "model.asset"
- local common_fun = require "model.common_fun"
- local skynet = require "lualib.skynet"
- local role
- local MODULE_NAME = "currency"
- local MAX_CHANGE = 20000000000
- local logger_trace = logger.trace
- local table_insert = table.insert
- local statistics_list = {
- CURRENCY_ID_COINS,
- CURRENCY_ID_STM,
- CURRENCY_ID_RELIC_SCORE,
- CURRENCY_ID_RELIC_COIN,
- CURRENCY_ID_EQUIP_COIN,
- CURRENCY_ID_DIA,
- CURRENCY_ID_IRON,
- CURRENCY_ID_WOOD,
- CURRENCY_ID_ROLE_EXP,
- CURRENCY_ID_EXP_STONE,
- CURRENCY_ID_EXP_BOOK,
- CURRENCY_ID_TALENT_STONE,
- CURRENCY_ID_WEAPON_SCROLL,
- CURRENCY_ID_JEWELRY_SCROLL,
- CURRENCY_ID_WRISTER_SCROLL,
- CURRENCY_ID_CUIRASS_SCROLL,
- CURRENCY_ID_BELT_SCROLL,
- CURRENCY_ID_SHOE_SCROLL,
- CURRENCY_ID_DRAW_COIN,
- CURRENCY_ID_DRAW_COIN2,
- }
- local _M = schema.new(MODULE_NAME, {
- first = false,
- data = {
- -- [id] = {
- -- id = 货币id;
- -- num = 货币数量;
- -- }
- }
- })
- local REQUEST = {}
- local CMD = {}
- local MODULE = {}
- local 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)
- if not d then
- return
- end
- if not d.first then
- d.data[CURRENCY_ID_STM] = d.data[CURRENCY_ID_STM] or {id = CURRENCY_ID_STM, num = asset.DataConfig_proto[1].data3}
- d.first = true
- _M.persist(character)
- end
- -- 加载货币配置 设置为0
- -- local conf = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
- -- for k, _ in pairs(conf) do
- -- if not d.data[k] then
- -- d.data[k] = {
- -- id = k,
- -- num = 0,
- -- }
- -- end
- -- end
- end
- -- TODO: 侦听事件
- function MODULE.monitor(character)
- local d = _M.assert_get(character)
- character.monitor("daily_refresh", function()
- local data = d.data[CURRENCY_ID_DACTIVITY]
- if data then
- local num = data.num
- d.data[CURRENCY_ID_DACTIVITY] = nil
- _M.persist(character)
- character.send("pay_currency", {now = {{id = CURRENCY_ID_DACTIVITY, num = 0}}, pay = {{id = CURRENCY_ID_DACTIVITY, num = num}}})
- end
- end)
-
- character.monitor("weekly_refresh", function()
- local data = d.data[CURRENCY_ID_WACTIVITY]
- if data then
- local num = data.num
- d.data[CURRENCY_ID_WACTIVITY] = nil
- _M.persist(character)
- character.send("pay_currency", {now = {{id = CURRENCY_ID_WACTIVITY, num = 0}}, pay = {{id = CURRENCY_ID_WACTIVITY, num = num}}})
- end
- end)
- end
- -- TODO: 类似泰利的 prepare 接口
- function MODULE.launch(character)
- local d = _M.assert_get(character)
- role = role or require "model.role"
- end
- -- TODO: 与客户端同步数据
- function MODULE.ready(character)
- local d = _M.assert_get(character)
- end
- -- TODO: 玩家下线时的处理
- function MODULE.saybye(character)
- local d = _M.assert_get(character)
- local ret = {
- level = character.level,
- list = {}
- }
- -- for _ , key in ipairs(statistics_list) do
- -- if d.data[key] then
- -- table.insert(ret.list, d.data[key])
- -- end
- -- end
- -- local itemavg = skynet.localname(".itemavg")
- -- if next(ret.list) then
- -- skynet.send(itemavg, "lua", "updata", ret)
- -- end
- end
- function MODULE.get_client_data(character)
- local d = _M.assert_get(character) or {}
- return common_fun.tqueue(d.data)
- end
- -- list = {{id =1 , num = 1}} 物品id, 物品数量
- function MODULE.add_money(character, id, num, collect, dispose, addition)
- local d = _M.assert_get(character)
- local conf_list = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
- local conf = conf_list[id]
- if not conf and dispose then
- dispose(nil, id, num)
- return
- end
- -- 一次超过2000亿是什么数值
- if num >= 1 and num < MAX_CHANGE then
- d.data[id] = d.data[id] or {id = id, num = 0}
- d.data[id].num = d.data[id].num + math.floor(num) -- 向下取下整避免意外
- character.dispatch("currency_add", id, num)
- if addition then
- addition(GOODS_MONEY, id, num)
- end
- if collect then
- collect(GOODS_MONEY, d.data[id])
- end
- _M.persist(character)
- return true, num, d.data[id].num
- else
- if dispose then
- dispose(nil, id, num)
- end
- end
- end
- -- 支付一旦异常玩家直接报错
- function MODULE.pay_money(character, id, num)
- local d = _M.assert_get(character)
- if num < 0 then
- assert(false)
- end
- local cur_num = 0
- if d.data and d.data[id] and d.data[id].num then
- cur_num = d.data[id].num
- else
- assert(false)
- end
- if cur_num < num then
- assert(false)
- end
- d.data[id].num = d.data[id].num - math.floor(num)
- _M.persist(character)
- if id == CURRENCY_ID_STM then
- role.stm_change(character)
- end
- character.dispatch("pay_money", id, num)
- return d.data[id].num
- end
- function MODULE.check_money(character, id, num)
- local d = _M.assert_get(character)
- -- 数量至少为1
- if num < 0 then
- return STD_ERR.PAYMENT_NUM_ERR
- end
- if not d.data or not d.data[id] then
- local conf = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
- if not conf[id] then
- return STD_ERR.PAYMENT_UNKNOWN_ITEM
- end
- return STD_ERR.PAYMENT_NOT_ENOUGH
- end
- if not d.data[id].num then
- return STD_ERR.COMMON_SYS_ERR
- end
- if d.data[id].num < num then
- return STD_ERR.PAYMENT_NOT_ENOUGH
- end
- return 0
- end
- function MODULE.set_money(character, id, num)
- local d = _M.assert_get(character)
- if num < 0 then
- return STD_ERR.PAYMENT_NUM_ERR
- end
- if not d.data or not d.data[id] then
- local conf = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
- if not conf[id] then
- return STD_ERR.PAYMENT_UNKNOWN_ITEM
- end
- end
- d.data[id].num = num
- _M.persist(character)
- return 0
- end
- function MODULE.get_money(character, id)
- local d = _M.assert_get(character)
- return d.data[id] and d.data[id].num or 0
- end
- -- 获取所有随机宝箱
- function MODULE.get_all_random_box(character, args)
- local d = _M.assert_get(character)
- local conf_list = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
- local temp = {}
- for _, data in pairs(d.data) do
- local conf = conf_list[data.id]
- if conf and conf.type == CURRENCY_TYPE_RANDOM_BOX and data.num > 0 then
- table.insert(temp, {id = data.id, num = data.num})
- end
- end
- return temp
- end
- return MODULE
|