time_box.lua 6.0 KB

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