local schema = require "model.schema" local map = { -- character login = 1001, -- 登录次数 recharge_times = 1002, -- 充值次数 recharge_num = 1003, -- 充值数量 buy_stm_times = 1004, -- 购买体力次数 add_stm_times = 1005, -- 购买或看视频获得体力 buy_shop_item = 1006, -- 购买商店物品 watch_ad_times = 1101, -- 看广告次数 -- advanture simple_battle = 2001, -- 挑战简单关卡次数 sweep_simple_battle = 2002, -- 扫荡简单关卡次数 enter_simple_battle = 2003, -- 进入简单关卡次数 simple_battle_pass = 2004, -- 通关简单关卡 elite_battle = 2101, -- 挑战精英管卡次数 elite_battle_pass = 2102, -- 通关精英关卡 daily_battle = 2201, -- 日常副本挑战次数 relic_battle_num = 2301, -- 挑战遗迹关卡次数 -- hero hero_upgrade = 3001, -- 角色升级次数 hero_upstart = 3002, -- 角色突破次数 hero_max_level = 3003, -- 角色最大等级 hero_add_num = 3004, -- 英雄增加数量 skill_card_upgrade = 3101, -- 卡牌技能升级 skill_card_upstart = 3102, -- 卡牌技能升星 skill_card_add_num = 3103, -- 卡牌激活数量 --kill monster kill_monster = 4001, -- 击杀怪物 kill_boss = 4002, -- 击杀boss -- equip equip_upgrade = 5001, -- 装备升级 equip_upstart = 5002, -- 装备突破 equip_add_num = 5003, -- 装备增加数量 -- telent telent_activate = 6001, -- 天赋激活次数 -- building building_upgrade = 7001, -- 建筑升级次数 city_skill_upgrade = 7002, -- 城镇技能升级 building_explore_times = 7003, -- 建筑探索次数 -- draw draw_times = 8001, -- 抽奖次数 draw_hero_times = 8002, -- 抽取英雄次数 draw_equip_times = 8003, -- 抽取装备次数 -- currency consume_coin = 9001, -- 消耗金币数量 consume_diamond = 9002, -- 消耗钻石数量 add_coin = 9003, -- 获得金币 add_diamond = 9003, -- 获得金币 } local _M = schema.new('stats', { list = {}, hero_lv = {}, }) local stats = {} local incident = {} function stats.parse(character) local d = _M.load(character) d.list = d.list or {} d.hero_lv = d.hero_lv or {} local change = false for _, v in pairs(map) do if not d.list[v] then d.list[v] = 0 if not change then change = true end end end if change then _M.persist(character) end end function stats.inform(character, name, ...) local str = "stats.".. name character.dispatch(str,...) end function incident.character(character) local d = _M.assert_get(character) character.monitor("daily_refresh", function() local key = "login" local index = map[key] d.list[index] = d.list[index] + 1 _M.persist(character) stats.inform(character, key, 1) end) character.monitor("recharge.money",function(_, ...) local key = "recharge_times" local index = map[key] d.list[index] = d.list[index] + 1 _M.persist(character) stats.inform(character, key, 1) end) character.monitor("recharge.money",function(_, cfid, orderid, moneynum, ...) local key = "recharge_num" local index = map[key] d.list[index] = d.list[index] + moneynum _M.persist(character) stats.inform(character, key, moneynum) end) character.monitor("buy_stm", function(_, bfree) if not bfree then local key = "buy_stm_times" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) end local key = "add_stm_times" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("shop_buy", function(_, num) local key = "buy_shop_item" local index = map[key] if num > 0 then d.list[index] = (d.list[index] or 0) + num stats.inform(character, key, num) _M.persist(character) end end) character.monitor("watch_ad", function() local key = "watch_ad_times" local index = map[key] d.list[index] = (d.list[index] or 0) + 1 stats.inform(character, key, 1) _M.persist(character) end) end function incident.advanture(character) local d = _M.assert_get(character) character.monitor("simple_battle", function(_, id) local key = "simple_battle" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) key = "enter_simple_battle" index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("sweep_simple_battle", function(_, id) local key = "sweep_simple_battle" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) key = "enter_simple_battle" index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("simple_battle_pass", function(_, id) local key = "simple_battle_pass" local index = map[key] if id > d.list[index] then d.list[index] = id stats.inform(character, key, id) end _M.persist(character) end) character.monitor("elite_battle", function(_, id) local key = "elite_battle" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("elite_battle_pass", function(_, id) local key = "elite_battle_pass" local index = map[key] if id > d.list[index] then d.list[index] = id stats.inform(character, key, id) end _M.persist(character) end) character.monitor("relic_battle", function() local key = "relic_battle_num" local index = map[key] d.list[index] = (d.list[index] or 0) + 1 stats.inform(character, key, 1) _M.persist(character) end) -- daily_battle = 2201, -- 日常副本挑战次数 local function join_daily_battle() local key = "daily_battle" local index = map[key] d.list[index] = (d.list[index] or 0) + 1 stats.inform(character, key, 1) _M.persist(character) end character.monitor("daily_dungeons_pass", function() join_daily_battle() end) character.monitor("daily_dungeons_sweep", function() join_daily_battle() end) end function incident.hero(character) local d = _M.assert_get(character) character.monitor("hero_upgrade", function(_, id, pre, cur) if pre >= cur then return end local add = cur - pre local key = "hero_upgrade" local index = map[key] d.list[index] = d.list[index] + add stats.inform(character, key, add) -- 10级以下不处理, for i = math.min(10, pre+1), cur do d.hero_lv[i] = (d.hero_lv[i] or 0) + 1 stats.inform(character, "hero_lv", d.hero_lv[i], i) end key = "hero_max_level" index = map[key] if d.list[index] < cur then d.list[index] = cur stats.inform(character, key, cur) end _M.persist(character) end) character.monitor("hero_upstart", function(_, id, pre, cur) local key = "hero_upstart" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("hero_upstart_times", function(_, num, quality) local key = "hero_upstart" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end) character.monitor("hero_add_num", function(_, id, num) local key = "hero_add_num" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end) character.monitor("skill_card_upgrade", function(_, id, pre, cur) if pre >= cur then return end local add = cur - pre local key = "skill_card_upgrade" local index = map[key] d.list[index] = d.list[index] + add stats.inform(character, key, add) _M.persist(character) end) character.monitor("skill_card_upstart", function(_, id, pre, cur) if pre >= cur then return end local add = cur - pre local key = "skill_card_upstart" local index = map[key] d.list[index] = d.list[index] + add stats.inform(character, key, add) _M.persist(character) end) character.monitor("skill_card_add_num", function(_, id) local key = "skill_card_add_num" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) end function incident.monster(character) local d = _M.assert_get(character) character.monitor("kill_monster", function(_, num) local key = "kill_monster" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("kill_boss", function(_, num) local key = "kill_boss" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) end function incident.equip(character) local d = _M.assert_get(character) character.monitor("equip_upgrade", function(_, id, pre, cur) if pre >= cur then return end local add = cur - pre local key = "equip_upgrade" local index = map[key] d.list[index] = d.list[index] + add stats.inform(character, key, add) _M.persist(character) end) character.monitor("equip_upstart", function(_, id, pre, cur) local key = "equip_upstart" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("equip_upstart_times", function(_, num, quality) local key = "equip_upstart" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end) character.monitor("equip_add_num", function(_, id, num) local key = "equip_add_num" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end) end function incident.telent(character) local d = _M.assert_get(character) character.monitor("telent_activate", function(_) local key = "telent_activate" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) end function incident.building(character) local d = _M.assert_get(character) character.monitor("building_upgrade", function(_) local key = "building_upgrade" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("city_skill_upgrade", function(_) local key = "city_skill_upgrade" local index = map[key] d.list[index] = d.list[index] + 1 stats.inform(character, key, 1) _M.persist(character) end) character.monitor("building_explore", function(_, num) local key = "building_explore_times" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end) end function incident.draw(character) local d = _M.assert_get(character) character.monitor("draw_times", function(_, num, type) local key = "draw_times" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) if type == 1 or type == 2 then local key = "draw_hero_times" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) end if type == 3 then local key = "draw_equip_times" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) end _M.persist(character) end) end function incident.currency(character) local d = _M.assert_get(character) character.monitor("pay_money", function(_, id, num) if id == CURRENCY_ID_COINS then local key = "consume_coin" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) elseif id == CURRENCY_ID_DIA then local key = "consume_diamond" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end end) character.monitor("currency_add", function(_, id, num) if id == CURRENCY_ID_COINS then local key = "add_coin" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) elseif id == CURRENCY_ID_DIA then local key = "add_diamond" local index = map[key] d.list[index] = d.list[index] + num stats.inform(character, key, num) _M.persist(character) end end) end function stats.monitor(character) if incident then for _, v in pairs(incident) do if v then local f = v f(character) end end end end function stats.launch(character) end function stats.ready(character) local d = _M.assert_get(character) end function stats.saybye(character) end function stats.fetch(character, key) local d = _M.assert_get(character) local index = map[key] if not index then return 0 end return d.list[index] or 0 end function stats.hero_lv(character, lv) local d = _M.assert_get(character) return d.hero_lv[lv] or 0 end return stats