time_box.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. local schema = require "model.schema"
  2. local logger = require "logger"
  3. local asset = require "model.asset"
  4. local common_fun = require "model.common_fun"
  5. local payment = require "model.payment"
  6. local reward = require "model.reward"
  7. local stringify = require "stringify"
  8. local MODULE_NAME = "time_box"
  9. local MAX_CHANGE = 20000000000
  10. local _M = schema.new(MODULE_NAME, {
  11. data = {
  12. --[[
  13. [pos]
  14. {
  15. tm = 可开启时间
  16. }
  17. ]]
  18. },
  19. history = {}
  20. })
  21. local REQUEST = {}
  22. local CMD = {}
  23. local MODULE = {}
  24. local THIS = {}
  25. local ret_func = common_fun.get_ret_func(THIS)
  26. function MODULE.list_request_interests() return REQUEST end
  27. function MODULE.list_command_interests() return CMD end
  28. -- TODO: 解析/升级模块数据
  29. function MODULE.parse(character)
  30. local d = _M.load(character)
  31. end
  32. -- TODO: 侦听事件
  33. function MODULE.monitor(character)
  34. local d = _M.assert_get(character)
  35. end
  36. -- TODO: 类似泰利的 prepare 接口
  37. function MODULE.launch(character)
  38. local d = _M.assert_get(character) or {}
  39. d.history = d.history or {}
  40. end
  41. -- TODO: 与客户端同步数据
  42. function MODULE.ready(character)
  43. local d = _M.assert_get(character)
  44. logger.test("%s: ready, d:%s", MODULE_NAME, stringify(d))
  45. end
  46. -- TODO: 玩家下线时的处理
  47. function MODULE.saybye(character)
  48. local d = _M.assert_get(character)
  49. end
  50. local function get_max_pos()
  51. return 4
  52. end
  53. local function get_box_data_one(pos, data)
  54. local temp = common_fun.scopy(data)
  55. temp.pos = pos
  56. return temp
  57. end
  58. function MODULE.get_client_data(character)
  59. local d = _M.assert_get(character) or {}
  60. local max_pos = get_max_pos()
  61. local ret = {}
  62. for i = 1, max_pos do
  63. if d.data[i] then
  64. table.insert(ret, get_box_data_one(i, d.data[i]))
  65. end
  66. end
  67. return ret
  68. end
  69. function MODULE.get_box_surplus(character)
  70. local d = _M.assert_get(character) or {}
  71. local max_pos = get_max_pos()
  72. local surplus_num = max_pos
  73. for i = 1, max_pos do
  74. if d.data[i] then
  75. surplus_num = surplus_num - 1
  76. end
  77. end
  78. return surplus_num
  79. end
  80. function MODULE.add_box(character, id, num, collect, dispose, addition)
  81. local d = _M.assert_get(character) or {}
  82. local conf_list = assert(asset.BoxConfig_proto, "BoxConfig_proto")
  83. local conf = conf_list[id]
  84. if not conf and dispose then
  85. dispose(nil, id, num)
  86. return
  87. end
  88. local add_num = 0
  89. local max_pos = get_max_pos()
  90. if num >= 1 and num < MAX_CHANGE then
  91. for i = 1, max_pos do
  92. if add_num >= num then
  93. break
  94. end
  95. if not d.data[i] then
  96. d.data[i] = {
  97. id = id,
  98. tm = 0,
  99. }
  100. collect(GOODS_BOX, get_box_data_one(i, d.data[i]))
  101. add_num = add_num + 1
  102. end
  103. end
  104. logger.trace(stringify(d.data or {}))
  105. addition(GOODS_BOX, id, add_num)
  106. _M.persist(character)
  107. return true
  108. else
  109. if dispose then
  110. dispose(nil, id, num)
  111. end
  112. end
  113. end
  114. function THIS.time_box_unlock(character, args)
  115. local d = _M.assert_get(character) or {}
  116. local pos = args.pos
  117. if not pos or pos <= 0 then
  118. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  119. end
  120. local data = d.data[pos]
  121. if not data or not data.tm or data.tm > 0 then
  122. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  123. end
  124. local conf_list = assert(asset.BoxConfig_proto, "BoxConfig_proto")
  125. local conf = conf_list[data.id]
  126. data.tm = os.time()+ conf.cd*MIN_SEC
  127. _M.persist(character)
  128. return 0, {data = get_box_data_one(pos, data)}
  129. end
  130. function THIS.time_box_get_award(character, args)
  131. local d = _M.assert_get(character) or {}
  132. local pos = args.pos
  133. if not pos or pos <= 0 then
  134. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  135. end
  136. local data = d.data[pos]
  137. if not data or not data.tm or data.tm <= 0 then
  138. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  139. end
  140. local conf_list = assert(asset.BoxConfig_proto, "BoxConfig_proto")
  141. local conf = conf_list[data.id]
  142. if not conf then
  143. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  144. end
  145. d.history[data.id] = (d.history[data.id] or 0)
  146. local history_num = d.history[data.id] + 1
  147. local awardID = conf.award
  148. for k, v in ipairs(conf.single) do
  149. if v == history_num then
  150. awardID = conf.replace[k] or awardID
  151. break
  152. end
  153. end
  154. local now = os.time()
  155. local pay_list = {}
  156. local errno, award_list = common_fun.get_award(awardID)
  157. if errno ~= 0 then
  158. return errno
  159. end
  160. if now < data.tm then
  161. for i = 1, #conf.consume, 2 do
  162. local item_id = conf.consume[i]
  163. local item_num = conf.consume[i+1]
  164. if item_id and item_num and item_id > 0 and item_num > 0 then
  165. table.insert(pay_list, {id = item_id, num = item_num})
  166. end
  167. end
  168. end
  169. if next(pay_list) then
  170. local receipt = {
  171. module="box",
  172. brief="开启宝箱",
  173. context=string.format("开启宝箱id:%d, 奖励库id%s, 开启次数%d", data.id, awardID or 0, history_num),
  174. notify={
  175. flags="time_box_get_award"
  176. },
  177. detail=pay_list
  178. }
  179. local errno = payment(character, receipt)
  180. if errno ~= 0 then
  181. return errno
  182. end
  183. end
  184. local desc = {
  185. module="box",
  186. brief="开启宝箱",
  187. context="开启宝箱:"..data.id,
  188. mailgen={subject=2, body=""},
  189. notify={
  190. flags="time_box_get_award"
  191. },
  192. detail=award_list
  193. }
  194. reward(character, desc)
  195. d.data[pos] = nil
  196. d.history[data.id] = history_num
  197. _M.persist(character)
  198. return 0, {pos = pos}
  199. end
  200. function THIS.time_box_data(character, args)
  201. return 0, {list = MODULE.get_client_data(character)}
  202. end
  203. function REQUEST.time_box_unlock(character, args)
  204. return ret_func("time_box_unlock", character, args)
  205. end
  206. function REQUEST.time_box_get_award(character, args)
  207. return ret_func("time_box_get_award", character, args)
  208. end
  209. function REQUEST.time_box_data(character, args)
  210. return ret_func("time_box_data", character, args)
  211. end
  212. return MODULE