init.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. local schema = require "model.schema"
  2. local logger = require "logger"
  3. local stringify = require "stringify"
  4. local asset = require "model.asset"
  5. local module_fun = require "model.module_fun"
  6. local payment = require "model.payment"
  7. local reward = require "model.reward"
  8. local util = require "util"
  9. local rechargemod = require "model.rechargemod"
  10. local mailbox
  11. local currency
  12. local Register_model = {
  13. [MODULE_ID_RELIC_MANUA] = require "model.manual.relic_manual"
  14. }
  15. local FIRST_MANUAL = 1 -- 免费
  16. local SECOND_MANUAL = 2 -- 强者战令
  17. local THIRD_MANUAL = 3 -- 王者战令
  18. -- 请和altar中保持一致
  19. local EQUIP_FREE = 2
  20. local HERO_FREE = 3
  21. local module_name = "manual"
  22. local module_id = 38
  23. local _M = schema.new(module_name, {
  24. open = false,
  25. round = 1,
  26. open_time = 0,
  27. level = 0,
  28. exp = 0,
  29. list = {
  30. -- {
  31. -- lock = true,
  32. -- award = 0,
  33. -- }
  34. },
  35. privilege = { -- 英雄置换特权和装备置换特权
  36. }
  37. })
  38. local REQUEST = {}
  39. local CMD = {}
  40. local MODULE = {}
  41. local THIS = {}
  42. local function func_ret(fname, character, args)
  43. if args.moduleid and Register_model[args.moduleid] then
  44. local f = Register_model[args.moduleid][fname]
  45. if not f then
  46. logger.error("func_ret not moduleid:%d, fname:%s !!!", args.moduleid, fname)
  47. return {errno = STD_ERR.COMMON_SYS_ERR}
  48. end
  49. return f(character, args)
  50. end
  51. local f = THIS[fname]
  52. if not f then
  53. logger.error("func_ret not fname:%s !!!", fname)
  54. return {errno = STD_ERR.COMMON_SYS_ERR}
  55. end
  56. local errno, ret = f(character, args)
  57. if errno ~= 0 then
  58. return {errno = errno}
  59. end
  60. ret = ret or {}
  61. ret.errno = 0
  62. ret.moduleid = module_id
  63. return ret
  64. end
  65. function MODULE.list_request_interests() return REQUEST end
  66. function MODULE.list_command_interests() return CMD end
  67. -- TODO: 解析/升级模块数据 在这里把数据初始化好
  68. function MODULE.parse(character)
  69. local d = _M.load(character)
  70. end
  71. -- TODO: 侦听事件
  72. function MODULE.monitor(character)
  73. local d = _M.assert_get(character) or {}
  74. character.monitor("currency_add", function(_, id, num)
  75. if id == CURRENCY_ID_DACTIVITY then
  76. THIS.manual_upgrade(character, num)
  77. end
  78. end)
  79. character.monitor("daily_refresh", function()
  80. if d.open then
  81. THIS.reset_model(character)
  82. end
  83. end)
  84. -- 特权重置
  85. character.monitor("weekly_refresh", function()
  86. if d.open and d.privilege then
  87. d.privilege = {}
  88. _M.persist(character)
  89. end
  90. end)
  91. character.monitor("simple_battle_pass", function(_, id)
  92. if id == asset.DataConfig_proto[12].data1 then
  93. THIS.reset_model(character)
  94. end
  95. end)
  96. character.monitor("recharge.money",function(_, cfid, orderid, moneynum, giftid, id, pos)
  97. if module_id == id then
  98. THIS.manual_unlock(character, pos+1)
  99. end
  100. end)
  101. end
  102. -- TODO: 类似泰利的 prepare 接口
  103. function MODULE.launch(character)
  104. currency = currency or require "model.currency"
  105. mailbox = mailbox or require "model.mailbox"
  106. end
  107. -- TODO: 与客户端同步数据
  108. function MODULE.ready(character)
  109. local d = _M.assert_get(character) or {}
  110. THIS.reset_model(character)
  111. if d.open and d.level > 0 and d.exp > 0 then
  112. THIS.manual_upgrade(character, 0)
  113. end
  114. logger.test("%s:ready, %s", module_name, stringify(d or {}))
  115. end
  116. -- TODO: 玩家下线时的处理
  117. function MODULE.saybye(character)
  118. end
  119. -- 战令特权剩余次数
  120. function MODULE.manual_privilege_times(character, ptype)
  121. local d = _M.assert_get(character) or {}
  122. if not d.open then
  123. return 0
  124. end
  125. local data, data_conf
  126. if ptype == EQUIP_FREE then
  127. data = d.list[SECOND_MANUAL]
  128. data_conf = asset.DataConfig_proto[37]
  129. elseif ptype == HERO_FREE then
  130. data = d.list[THIRD_MANUAL]
  131. data_conf = asset.DataConfig_proto[38]
  132. else
  133. return 0
  134. end
  135. if not data or data.lock then
  136. return 0
  137. end
  138. d.privilege = d.privilege or {}
  139. d.privilege[ptype] = d.privilege[ptype] or 0
  140. return math.max(0, data_conf.data1 - d.privilege[ptype])
  141. end
  142. -- 战令特权使用
  143. function MODULE.manual_privilege_use(character, ptype)
  144. local d = _M.assert_get(character) or {}
  145. d.privilege = d.privilege or {}
  146. d.privilege[ptype] = d.privilege[ptype] or 0
  147. d.privilege[ptype] = d.privilege[ptype] + 1
  148. _M.persist(character)
  149. return 0
  150. end
  151. function THIS.reset_model(character)
  152. local d = _M.assert_get(character) or {}
  153. if not d.open then
  154. d.round = 1
  155. d.open = true
  156. d.exp = currency.get_money(character, CURRENCY_ID_DACTIVITY)
  157. else
  158. local now = os.time()
  159. local open_time = util.today(d.open_time)
  160. local day = (now-open_time)/DAY_SEC
  161. if day < asset.DataConfig_proto[12].data2 then
  162. return
  163. end
  164. local award_list = THIS.get_award_list(character)
  165. if next(award_list) then
  166. local desc = {
  167. module = "manual",
  168. brief = "战令自动发奖",
  169. context = "战令自动发奖,round:"..d.round,
  170. }
  171. mailbox.send(character, 6, "", award_list, desc)
  172. end
  173. d.round = (d.round or 0) + 1
  174. d.exp = 0
  175. end
  176. d.privilege = {}
  177. d.open_time = util.today() + 12 * 3600
  178. d.level = 0
  179. d.list = {}
  180. for i = FIRST_MANUAL, THIRD_MANUAL do
  181. d.list[i] = {
  182. lock = true,
  183. award = -1,
  184. }
  185. end
  186. d.list[FIRST_MANUAL].lock = false
  187. if d.exp > 0 then
  188. THIS.manual_upgrade(character)
  189. else
  190. _M.persist(character)
  191. end
  192. character.send("manual_notify", {open = d.open, open_time = util.today(d.open_time), level = d.level, exp = d.exp, round = d.round, list = d.list})
  193. end
  194. function THIS.get_round(character)
  195. local d = _M.assert_get(character) or {}
  196. local round = 1
  197. for _, v in pairs(asset.ManualConfig_proto) do
  198. if v.sign > round then
  199. round = v.sign
  200. end
  201. end
  202. return math.min(round, d.round and d.round or 1)
  203. end
  204. function THIS.get_conf_list(round)
  205. local ret = {}
  206. for _, conf in pairs(asset.ManualConfig_proto) do
  207. if conf.sign == round then
  208. ret[conf.level] = conf
  209. end
  210. end
  211. return ret
  212. end
  213. function THIS.manual_upgrade(character, add)
  214. local d = _M.assert_get(character) or {}
  215. local round = THIS.get_round(character)
  216. local conf_list = THIS.get_conf_list(round)
  217. local level = d.level
  218. local exp = d.exp + (add or 0)
  219. while(true) do
  220. local conf = conf_list[level+1]
  221. if not conf then
  222. break
  223. end
  224. if conf.exp > exp then
  225. break
  226. else
  227. level = level + 1
  228. exp = exp - conf.exp
  229. end
  230. end
  231. if level ~= d.level or exp ~= d.exp then
  232. d.level = level
  233. d.exp = exp
  234. _M.persist(character)
  235. end
  236. end
  237. function THIS.manual_buy_exp(character, args)
  238. local d = _M.assert_get(character) or {}
  239. if not d.open then
  240. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  241. end
  242. local data_conf = asset.DataConfig_proto[13]
  243. if not data_conf then
  244. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  245. end
  246. local round = THIS.get_round(character)
  247. local conf_list = THIS.get_conf_list(round)
  248. if not conf_list[d.level + 1] then
  249. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  250. end
  251. local pay_list = {{id = CURRENCY_ID_DIA, num = data_conf.data2}}
  252. local receipt = {
  253. module="manual",
  254. brief="购买战令经验",
  255. context="购买战令经验",
  256. notify={
  257. flags="manual_buy_exp"
  258. },
  259. detail=pay_list
  260. }
  261. -- 检查消耗
  262. local errno = module_fun.paymeny_check(character, pay_list)
  263. if errno ~= 0 then
  264. return errno
  265. end
  266. payment(character, receipt)
  267. THIS.manual_upgrade(character, data_conf.data1)
  268. return 0, {level = d.level, exp = d.exp}
  269. end
  270. function THIS.manual_get_data(character, args)
  271. local d = _M.assert_get(character)
  272. return 0, {open = d.open, open_time = util.today(d.open_time), level = d.level, exp = d.exp, round = d.round, list = d.list}
  273. end
  274. function THIS.get_award_list(character)
  275. local d = _M.assert_get(character) or {}
  276. local award_list = {}
  277. if not d.open then
  278. return award_list
  279. end
  280. local round = THIS.get_round(character)
  281. local max_award = -1
  282. for _, conf in pairs(asset.ManualConfig_proto) do
  283. if conf.sign == round then
  284. for k, v in ipairs(d.list) do
  285. if not v.lock and v.award < conf.level and d.level >= conf.level then
  286. local award_conf
  287. if k == FIRST_MANUAL then
  288. award_conf = conf.brave
  289. elseif k == SECOND_MANUAL then
  290. award_conf = conf.strong
  291. elseif k == THIRD_MANUAL then
  292. award_conf = conf.king
  293. else
  294. assert(false)
  295. end
  296. max_award = math.max(max_award, conf.level)
  297. for i = 1, #award_conf, 2 do
  298. local item_id = award_conf[i]
  299. local item_num = award_conf[i+1]
  300. if item_id and item_num and item_id > 0 and item_num > 0 then
  301. table.insert(award_list, {id = item_id, num = item_num})
  302. end
  303. end
  304. end
  305. end
  306. end
  307. end
  308. return award_list, max_award
  309. end
  310. function THIS.manual_get_award(character, args)
  311. local d = _M.assert_get(character) or {}
  312. if not d.open then
  313. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  314. end
  315. local award_list, max_award = THIS.get_award_list(character)
  316. if not next(award_list) then
  317. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  318. end
  319. if not max_award then
  320. return STD_ERR.COMMON_SYS_ERR -- 系统异常
  321. end
  322. -- 发奖励
  323. local desc = {
  324. module="manual",
  325. brief="战令奖励",
  326. context= "战令奖励",
  327. mailgen={subject=2, body=""},
  328. notify={
  329. flags="manual_get_award"
  330. },
  331. detail=award_list
  332. }
  333. reward(character, desc)
  334. for k, v in ipairs(d.list) do
  335. if not v.lock then
  336. v.award = max_award
  337. end
  338. end
  339. _M.persist(character)
  340. return 0, {award = max_award}
  341. end
  342. function THIS.manual_unlock(character, type)
  343. local d = _M.assert_get(character) or {}
  344. if not d.open then
  345. return
  346. end
  347. local data = d.list[type]
  348. if not data then
  349. return
  350. end
  351. data.lock = false
  352. if d.privilege and d.privilege[type] and d.privilege[type] > 0 then
  353. d.privilege[type] = 0
  354. end
  355. _M.persist(character)
  356. character.send("manual_notify", {open = d.open, open_time = util.today(d.open_time), level = d.level, exp = d.exp, round = d.round, list = d.list})
  357. end
  358. function THIS.manual_buy(character, args)
  359. local d = _M.assert_get(character) or {}
  360. if not d.open then
  361. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  362. end
  363. if not args.giftid then
  364. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  365. end
  366. local manual_type
  367. if args.giftid == 50001 then
  368. manual_type = SECOND_MANUAL
  369. elseif args.giftid == 50002 then
  370. manual_type = THIRD_MANUAL
  371. else
  372. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  373. end
  374. local data = d.list[manual_type]
  375. if not data then
  376. return STD_ERR.COMMON_SYS_ERR -- 系统异常
  377. end
  378. if not data.lock then
  379. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  380. end
  381. local conf = asset.GiftConfig_proto[args.giftid]
  382. if not conf then
  383. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  384. end
  385. if conf.type ~= module_id then
  386. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  387. end
  388. local pay_list = {}
  389. if conf.spendtype == PAY_TYPE_FREE then
  390. pay_list = nil
  391. elseif conf.spendtype == PAY_TYPE_ITEM then
  392. table.insert(pay_list, {id = conf.spend, num = conf.num})
  393. elseif conf.spendtype == PAY_TYPE_GIFT then
  394. rechargemod.get_orderid(character, conf.ID)
  395. return 0
  396. end
  397. if pay_list then
  398. if not next(pay_list) then
  399. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  400. end
  401. local receipt = {
  402. module=module_name,
  403. brief="购买遗迹战令",
  404. context= "购买遗迹战令",
  405. mailgen={subject=2, body=""},
  406. notify={
  407. flags="manual_buy"
  408. },
  409. detail=pay_list
  410. }
  411. local errno = payment(character, receipt)
  412. if errno ~= 0 then
  413. return errno
  414. end
  415. end
  416. THIS.manual_unlock(character, manual_type)
  417. return 0
  418. end
  419. function REQUEST.manual_get_award(character, args)
  420. return func_ret("manual_get_award", character, args)
  421. end
  422. function REQUEST.manual_buy_exp(character, args)
  423. return func_ret("manual_buy_exp", character, args)
  424. end
  425. function REQUEST.manual_get_data(character, args)
  426. return func_ret("manual_get_data", character, args)
  427. end
  428. function REQUEST.manual_buy(character, args)
  429. return func_ret("manual_buy", character, args)
  430. end
  431. function MODULE.get_red_point(character)
  432. local d = _M.assert_get(character) or {}
  433. if not d.open then
  434. return false
  435. end
  436. local round = THIS.get_round(character)
  437. for _, v in pairs(d.list) do
  438. if not v.lock then
  439. for _, conf in pairs(asset.ManualConfig_proto) do
  440. if d.level >= conf.level and v.award < conf.level and conf.sign == round then
  441. return true, module_id
  442. end
  443. end
  444. end
  445. end
  446. return false
  447. end
  448. return MODULE