relic_manual.lua 12 KB

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