stats.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. local schema = require "model.schema"
  2. local map = {
  3. -- character
  4. login = 1001, -- 登录次数
  5. recharge_times = 1002, -- 充值次数
  6. recharge_num = 1003, -- 充值数量
  7. buy_stm_times = 1004, -- 购买体力次数
  8. add_stm_times = 1005, -- 购买或看视频获得体力
  9. buy_shop_item = 1006, -- 购买商店物品
  10. watch_ad_times = 1101, -- 看广告次数
  11. -- advanture
  12. simple_battle = 2001, -- 挑战简单关卡次数
  13. sweep_simple_battle = 2002, -- 扫荡简单关卡次数
  14. enter_simple_battle = 2003, -- 进入简单关卡次数
  15. simple_battle_pass = 2004, -- 通关简单关卡
  16. elite_battle = 2101, -- 挑战精英管卡次数
  17. elite_battle_pass = 2102, -- 通关精英关卡
  18. daily_battle = 2201, -- 日常副本挑战次数
  19. relic_battle_num = 2301, -- 挑战遗迹关卡次数
  20. -- hero
  21. hero_upgrade = 3001, -- 角色升级次数
  22. hero_upstart = 3002, -- 角色突破次数
  23. hero_max_level = 3003, -- 角色最大等级
  24. hero_add_num = 3004, -- 英雄增加数量
  25. skill_card_upgrade = 3101, -- 卡牌技能升级
  26. skill_card_upstart = 3102, -- 卡牌技能升星
  27. skill_card_add_num = 3103, -- 卡牌激活数量
  28. --kill monster
  29. kill_monster = 4001, -- 击杀怪物
  30. kill_boss = 4002, -- 击杀boss
  31. -- equip
  32. equip_upgrade = 5001, -- 装备升级
  33. equip_upstart = 5002, -- 装备突破
  34. equip_add_num = 5003, -- 装备增加数量
  35. -- telent
  36. telent_activate = 6001, -- 天赋激活次数
  37. -- building
  38. building_upgrade = 7001, -- 建筑升级次数
  39. city_skill_upgrade = 7002, -- 城镇技能升级
  40. building_explore_times = 7003, -- 建筑探索次数
  41. -- draw
  42. draw_times = 8001, -- 抽奖次数
  43. draw_hero_times = 8002, -- 抽取英雄次数
  44. draw_equip_times = 8003, -- 抽取装备次数
  45. -- currency
  46. consume_coin = 9001, -- 消耗金币数量
  47. consume_diamond = 9002, -- 消耗钻石数量
  48. add_coin = 9003, -- 获得金币
  49. add_diamond = 9003, -- 获得金币
  50. }
  51. local _M = schema.new('stats', {
  52. list = {},
  53. hero_lv = {},
  54. })
  55. local stats = {}
  56. local incident = {}
  57. function stats.parse(character)
  58. local d = _M.load(character)
  59. d.list = d.list or {}
  60. d.hero_lv = d.hero_lv or {}
  61. local change = false
  62. for _, v in pairs(map) do
  63. if not d.list[v] then
  64. d.list[v] = 0
  65. if not change then
  66. change = true
  67. end
  68. end
  69. end
  70. if change then
  71. _M.persist(character)
  72. end
  73. end
  74. function stats.inform(character, name, ...)
  75. local str = "stats.".. name
  76. character.dispatch(str,...)
  77. end
  78. function incident.character(character)
  79. local d = _M.assert_get(character)
  80. character.monitor("daily_refresh", function()
  81. local key = "login"
  82. local index = map[key]
  83. d.list[index] = d.list[index] + 1
  84. _M.persist(character)
  85. stats.inform(character, key, 1)
  86. end)
  87. character.monitor("recharge.money",function(_, ...)
  88. local key = "recharge_times"
  89. local index = map[key]
  90. d.list[index] = d.list[index] + 1
  91. _M.persist(character)
  92. stats.inform(character, key, 1)
  93. end)
  94. character.monitor("recharge.money",function(_, cfid, orderid, moneynum, ...)
  95. local key = "recharge_num"
  96. local index = map[key]
  97. d.list[index] = d.list[index] + moneynum
  98. _M.persist(character)
  99. stats.inform(character, key, moneynum)
  100. end)
  101. character.monitor("buy_stm", function(_, bfree)
  102. if not bfree then
  103. local key = "buy_stm_times"
  104. local index = map[key]
  105. d.list[index] = d.list[index] + 1
  106. stats.inform(character, key, 1)
  107. end
  108. local key = "add_stm_times"
  109. local index = map[key]
  110. d.list[index] = d.list[index] + 1
  111. stats.inform(character, key, 1)
  112. _M.persist(character)
  113. end)
  114. character.monitor("shop_buy", function(_, num)
  115. local key = "buy_shop_item"
  116. local index = map[key]
  117. if num > 0 then
  118. d.list[index] = (d.list[index] or 0) + num
  119. stats.inform(character, key, num)
  120. _M.persist(character)
  121. end
  122. end)
  123. character.monitor("watch_ad", function()
  124. local key = "watch_ad_times"
  125. local index = map[key]
  126. d.list[index] = (d.list[index] or 0) + 1
  127. stats.inform(character, key, 1)
  128. _M.persist(character)
  129. end)
  130. end
  131. function incident.advanture(character)
  132. local d = _M.assert_get(character)
  133. character.monitor("simple_battle", function(_, id)
  134. local key = "simple_battle"
  135. local index = map[key]
  136. d.list[index] = d.list[index] + 1
  137. stats.inform(character, key, 1)
  138. key = "enter_simple_battle"
  139. index = map[key]
  140. d.list[index] = d.list[index] + 1
  141. stats.inform(character, key, 1)
  142. _M.persist(character)
  143. end)
  144. character.monitor("sweep_simple_battle", function(_, id)
  145. local key = "sweep_simple_battle"
  146. local index = map[key]
  147. d.list[index] = d.list[index] + 1
  148. stats.inform(character, key, 1)
  149. key = "enter_simple_battle"
  150. index = map[key]
  151. d.list[index] = d.list[index] + 1
  152. stats.inform(character, key, 1)
  153. _M.persist(character)
  154. end)
  155. character.monitor("simple_battle_pass", function(_, id)
  156. local key = "simple_battle_pass"
  157. local index = map[key]
  158. if id > d.list[index] then
  159. d.list[index] = id
  160. stats.inform(character, key, id)
  161. end
  162. _M.persist(character)
  163. end)
  164. character.monitor("elite_battle", function(_, id)
  165. local key = "elite_battle"
  166. local index = map[key]
  167. d.list[index] = d.list[index] + 1
  168. stats.inform(character, key, 1)
  169. _M.persist(character)
  170. end)
  171. character.monitor("elite_battle_pass", function(_, id)
  172. local key = "elite_battle_pass"
  173. local index = map[key]
  174. if id > d.list[index] then
  175. d.list[index] = id
  176. stats.inform(character, key, id)
  177. end
  178. _M.persist(character)
  179. end)
  180. character.monitor("relic_battle", function()
  181. local key = "relic_battle_num"
  182. local index = map[key]
  183. d.list[index] = (d.list[index] or 0) + 1
  184. stats.inform(character, key, 1)
  185. _M.persist(character)
  186. end)
  187. -- daily_battle = 2201, -- 日常副本挑战次数
  188. local function join_daily_battle()
  189. local key = "daily_battle"
  190. local index = map[key]
  191. d.list[index] = (d.list[index] or 0) + 1
  192. stats.inform(character, key, 1)
  193. _M.persist(character)
  194. end
  195. character.monitor("daily_dungeons_pass", function()
  196. join_daily_battle()
  197. end)
  198. character.monitor("daily_dungeons_sweep", function()
  199. join_daily_battle()
  200. end)
  201. end
  202. function incident.hero(character)
  203. local d = _M.assert_get(character)
  204. character.monitor("hero_upgrade", function(_, id, pre, cur)
  205. if pre >= cur then
  206. return
  207. end
  208. local add = cur - pre
  209. local key = "hero_upgrade"
  210. local index = map[key]
  211. d.list[index] = d.list[index] + add
  212. stats.inform(character, key, add)
  213. -- 10级以下不处理,
  214. for i = math.min(10, pre+1), cur do
  215. d.hero_lv[i] = (d.hero_lv[i] or 0) + 1
  216. stats.inform(character, "hero_lv", d.hero_lv[i], i)
  217. end
  218. key = "hero_max_level"
  219. index = map[key]
  220. if d.list[index] < cur then
  221. d.list[index] = cur
  222. stats.inform(character, key, cur)
  223. end
  224. _M.persist(character)
  225. end)
  226. character.monitor("hero_upstart", function(_, id, pre, cur)
  227. local key = "hero_upstart"
  228. local index = map[key]
  229. d.list[index] = d.list[index] + 1
  230. stats.inform(character, key, 1)
  231. _M.persist(character)
  232. end)
  233. character.monitor("hero_upstart_times", function(_, num, quality)
  234. local key = "hero_upstart"
  235. local index = map[key]
  236. d.list[index] = d.list[index] + num
  237. stats.inform(character, key, num)
  238. _M.persist(character)
  239. end)
  240. character.monitor("hero_add_num", function(_, id, num)
  241. local key = "hero_add_num"
  242. local index = map[key]
  243. d.list[index] = d.list[index] + num
  244. stats.inform(character, key, num)
  245. _M.persist(character)
  246. end)
  247. character.monitor("skill_card_upgrade", function(_, id, pre, cur)
  248. if pre >= cur then
  249. return
  250. end
  251. local add = cur - pre
  252. local key = "skill_card_upgrade"
  253. local index = map[key]
  254. d.list[index] = d.list[index] + add
  255. stats.inform(character, key, add)
  256. _M.persist(character)
  257. end)
  258. character.monitor("skill_card_upstart", function(_, id, pre, cur)
  259. if pre >= cur then
  260. return
  261. end
  262. local add = cur - pre
  263. local key = "skill_card_upstart"
  264. local index = map[key]
  265. d.list[index] = d.list[index] + add
  266. stats.inform(character, key, add)
  267. _M.persist(character)
  268. end)
  269. character.monitor("skill_card_add_num", function(_, id)
  270. local key = "skill_card_add_num"
  271. local index = map[key]
  272. d.list[index] = d.list[index] + 1
  273. stats.inform(character, key, 1)
  274. _M.persist(character)
  275. end)
  276. end
  277. function incident.monster(character)
  278. local d = _M.assert_get(character)
  279. character.monitor("kill_monster", function(_, num)
  280. local key = "kill_monster"
  281. local index = map[key]
  282. d.list[index] = d.list[index] + 1
  283. stats.inform(character, key, 1)
  284. _M.persist(character)
  285. end)
  286. character.monitor("kill_boss", function(_, num)
  287. local key = "kill_boss"
  288. local index = map[key]
  289. d.list[index] = d.list[index] + 1
  290. stats.inform(character, key, 1)
  291. _M.persist(character)
  292. end)
  293. end
  294. function incident.equip(character)
  295. local d = _M.assert_get(character)
  296. character.monitor("equip_upgrade", function(_, id, pre, cur)
  297. if pre >= cur then
  298. return
  299. end
  300. local add = cur - pre
  301. local key = "equip_upgrade"
  302. local index = map[key]
  303. d.list[index] = d.list[index] + add
  304. stats.inform(character, key, add)
  305. _M.persist(character)
  306. end)
  307. character.monitor("equip_upstart", function(_, id, pre, cur)
  308. local key = "equip_upstart"
  309. local index = map[key]
  310. d.list[index] = d.list[index] + 1
  311. stats.inform(character, key, 1)
  312. _M.persist(character)
  313. end)
  314. character.monitor("equip_upstart_times", function(_, num, quality)
  315. local key = "equip_upstart"
  316. local index = map[key]
  317. d.list[index] = d.list[index] + num
  318. stats.inform(character, key, num)
  319. _M.persist(character)
  320. end)
  321. character.monitor("equip_add_num", function(_, id, num)
  322. local key = "equip_add_num"
  323. local index = map[key]
  324. d.list[index] = d.list[index] + num
  325. stats.inform(character, key, num)
  326. _M.persist(character)
  327. end)
  328. end
  329. function incident.telent(character)
  330. local d = _M.assert_get(character)
  331. character.monitor("telent_activate", function(_)
  332. local key = "telent_activate"
  333. local index = map[key]
  334. d.list[index] = d.list[index] + 1
  335. stats.inform(character, key, 1)
  336. _M.persist(character)
  337. end)
  338. end
  339. function incident.building(character)
  340. local d = _M.assert_get(character)
  341. character.monitor("building_upgrade", function(_)
  342. local key = "building_upgrade"
  343. local index = map[key]
  344. d.list[index] = d.list[index] + 1
  345. stats.inform(character, key, 1)
  346. _M.persist(character)
  347. end)
  348. character.monitor("city_skill_upgrade", function(_)
  349. local key = "city_skill_upgrade"
  350. local index = map[key]
  351. d.list[index] = d.list[index] + 1
  352. stats.inform(character, key, 1)
  353. _M.persist(character)
  354. end)
  355. character.monitor("building_explore", function(_, num)
  356. local key = "building_explore_times"
  357. local index = map[key]
  358. d.list[index] = d.list[index] + num
  359. stats.inform(character, key, num)
  360. _M.persist(character)
  361. end)
  362. end
  363. function incident.draw(character)
  364. local d = _M.assert_get(character)
  365. character.monitor("draw_times", function(_, num, type)
  366. local key = "draw_times"
  367. local index = map[key]
  368. d.list[index] = d.list[index] + num
  369. stats.inform(character, key, num)
  370. if type == 1 or type == 2 then
  371. local key = "draw_hero_times"
  372. local index = map[key]
  373. d.list[index] = d.list[index] + num
  374. stats.inform(character, key, num)
  375. end
  376. if type == 3 then
  377. local key = "draw_equip_times"
  378. local index = map[key]
  379. d.list[index] = d.list[index] + num
  380. stats.inform(character, key, num)
  381. end
  382. _M.persist(character)
  383. end)
  384. end
  385. function incident.currency(character)
  386. local d = _M.assert_get(character)
  387. character.monitor("pay_money", function(_, id, num)
  388. if id == CURRENCY_ID_COINS then
  389. local key = "consume_coin"
  390. local index = map[key]
  391. d.list[index] = d.list[index] + num
  392. stats.inform(character, key, num)
  393. _M.persist(character)
  394. elseif id == CURRENCY_ID_DIA then
  395. local key = "consume_diamond"
  396. local index = map[key]
  397. d.list[index] = d.list[index] + num
  398. stats.inform(character, key, num)
  399. _M.persist(character)
  400. end
  401. end)
  402. character.monitor("currency_add", function(_, id, num)
  403. if id == CURRENCY_ID_COINS then
  404. local key = "add_coin"
  405. local index = map[key]
  406. d.list[index] = d.list[index] + num
  407. stats.inform(character, key, num)
  408. _M.persist(character)
  409. elseif id == CURRENCY_ID_DIA then
  410. local key = "add_diamond"
  411. local index = map[key]
  412. d.list[index] = d.list[index] + num
  413. stats.inform(character, key, num)
  414. _M.persist(character)
  415. end
  416. end)
  417. end
  418. function stats.monitor(character)
  419. if incident then
  420. for _, v in pairs(incident) do
  421. if v then
  422. local f = v
  423. f(character)
  424. end
  425. end
  426. end
  427. end
  428. function stats.launch(character)
  429. end
  430. function stats.ready(character)
  431. local d = _M.assert_get(character)
  432. end
  433. function stats.saybye(character)
  434. end
  435. function stats.fetch(character, key)
  436. local d = _M.assert_get(character)
  437. local index = map[key]
  438. if not index then
  439. return 0
  440. end
  441. return d.list[index] or 0
  442. end
  443. function stats.hero_lv(character, lv)
  444. local d = _M.assert_get(character)
  445. return d.hero_lv[lv] or 0
  446. end
  447. return stats