currency.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 skynet = require "lualib.skynet"
  6. local role
  7. local MODULE_NAME = "currency"
  8. local MAX_CHANGE = 20000000000
  9. local logger_trace = logger.trace
  10. local table_insert = table.insert
  11. local statistics_list = {
  12. CURRENCY_ID_COINS,
  13. CURRENCY_ID_STM,
  14. CURRENCY_ID_RELIC_SCORE,
  15. CURRENCY_ID_RELIC_COIN,
  16. CURRENCY_ID_EQUIP_COIN,
  17. CURRENCY_ID_DIA,
  18. CURRENCY_ID_IRON,
  19. CURRENCY_ID_WOOD,
  20. CURRENCY_ID_ROLE_EXP,
  21. CURRENCY_ID_EXP_STONE,
  22. CURRENCY_ID_EXP_BOOK,
  23. CURRENCY_ID_TALENT_STONE,
  24. CURRENCY_ID_WEAPON_SCROLL,
  25. CURRENCY_ID_JEWELRY_SCROLL,
  26. CURRENCY_ID_WRISTER_SCROLL,
  27. CURRENCY_ID_CUIRASS_SCROLL,
  28. CURRENCY_ID_BELT_SCROLL,
  29. CURRENCY_ID_SHOE_SCROLL,
  30. CURRENCY_ID_DRAW_COIN,
  31. CURRENCY_ID_DRAW_COIN2,
  32. }
  33. local _M = schema.new(MODULE_NAME, {
  34. first = false,
  35. data = {
  36. -- [id] = {
  37. -- id = 货币id;
  38. -- num = 货币数量;
  39. -- }
  40. }
  41. })
  42. local REQUEST = {}
  43. local CMD = {}
  44. local MODULE = {}
  45. local THIS = {}
  46. function MODULE.list_request_interests() return REQUEST end
  47. function MODULE.list_command_interests() return CMD end
  48. -- TODO: 解析/升级模块数据
  49. function MODULE.parse(character)
  50. local d = _M.load(character)
  51. if not d then
  52. return
  53. end
  54. if not d.first then
  55. d.data[CURRENCY_ID_STM] = d.data[CURRENCY_ID_STM] or {id = CURRENCY_ID_STM, num = asset.DataConfig_proto[1].data3}
  56. d.first = true
  57. _M.persist(character)
  58. end
  59. -- 加载货币配置 设置为0
  60. -- local conf = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
  61. -- for k, _ in pairs(conf) do
  62. -- if not d.data[k] then
  63. -- d.data[k] = {
  64. -- id = k,
  65. -- num = 0,
  66. -- }
  67. -- end
  68. -- end
  69. end
  70. -- TODO: 侦听事件
  71. function MODULE.monitor(character)
  72. local d = _M.assert_get(character)
  73. character.monitor("daily_refresh", function()
  74. local data = d.data[CURRENCY_ID_DACTIVITY]
  75. if data then
  76. local num = data.num
  77. d.data[CURRENCY_ID_DACTIVITY] = nil
  78. _M.persist(character)
  79. character.send("pay_currency", {now = {{id = CURRENCY_ID_DACTIVITY, num = 0}}, pay = {{id = CURRENCY_ID_DACTIVITY, num = num}}})
  80. end
  81. end)
  82. character.monitor("weekly_refresh", function()
  83. local data = d.data[CURRENCY_ID_WACTIVITY]
  84. if data then
  85. local num = data.num
  86. d.data[CURRENCY_ID_WACTIVITY] = nil
  87. _M.persist(character)
  88. character.send("pay_currency", {now = {{id = CURRENCY_ID_WACTIVITY, num = 0}}, pay = {{id = CURRENCY_ID_WACTIVITY, num = num}}})
  89. end
  90. end)
  91. end
  92. -- TODO: 类似泰利的 prepare 接口
  93. function MODULE.launch(character)
  94. local d = _M.assert_get(character)
  95. role = role or require "model.role"
  96. end
  97. -- TODO: 与客户端同步数据
  98. function MODULE.ready(character)
  99. local d = _M.assert_get(character)
  100. end
  101. -- TODO: 玩家下线时的处理
  102. function MODULE.saybye(character)
  103. local d = _M.assert_get(character)
  104. local ret = {
  105. level = character.level,
  106. list = {}
  107. }
  108. -- for _ , key in ipairs(statistics_list) do
  109. -- if d.data[key] then
  110. -- table.insert(ret.list, d.data[key])
  111. -- end
  112. -- end
  113. -- local itemavg = skynet.localname(".itemavg")
  114. -- if next(ret.list) then
  115. -- skynet.send(itemavg, "lua", "updata", ret)
  116. -- end
  117. end
  118. function MODULE.get_client_data(character)
  119. local d = _M.assert_get(character) or {}
  120. return common_fun.tqueue(d.data)
  121. end
  122. -- list = {{id =1 , num = 1}} 物品id, 物品数量
  123. function MODULE.add_money(character, id, num, collect, dispose, addition)
  124. local d = _M.assert_get(character)
  125. local conf_list = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
  126. local conf = conf_list[id]
  127. if not conf and dispose then
  128. dispose(nil, id, num)
  129. return
  130. end
  131. -- 一次超过2000亿是什么数值
  132. if num >= 1 and num < MAX_CHANGE then
  133. d.data[id] = d.data[id] or {id = id, num = 0}
  134. d.data[id].num = d.data[id].num + math.floor(num) -- 向下取下整避免意外
  135. character.dispatch("currency_add", id, num)
  136. if addition then
  137. addition(GOODS_MONEY, id, num)
  138. end
  139. if collect then
  140. collect(GOODS_MONEY, d.data[id])
  141. end
  142. _M.persist(character)
  143. return true, num, d.data[id].num
  144. else
  145. if dispose then
  146. dispose(nil, id, num)
  147. end
  148. end
  149. end
  150. -- 支付一旦异常玩家直接报错
  151. function MODULE.pay_money(character, id, num)
  152. local d = _M.assert_get(character)
  153. if num < 0 then
  154. assert(false)
  155. end
  156. local cur_num = 0
  157. if d.data and d.data[id] and d.data[id].num then
  158. cur_num = d.data[id].num
  159. else
  160. assert(false)
  161. end
  162. if cur_num < num then
  163. assert(false)
  164. end
  165. d.data[id].num = d.data[id].num - math.floor(num)
  166. _M.persist(character)
  167. if id == CURRENCY_ID_STM then
  168. role.stm_change(character)
  169. end
  170. character.dispatch("pay_money", id, num)
  171. return d.data[id].num
  172. end
  173. function MODULE.check_money(character, id, num)
  174. local d = _M.assert_get(character)
  175. -- 数量至少为1
  176. if num < 0 then
  177. return STD_ERR.PAYMENT_NUM_ERR
  178. end
  179. if not d.data or not d.data[id] then
  180. local conf = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
  181. if not conf[id] then
  182. return STD_ERR.PAYMENT_UNKNOWN_ITEM
  183. end
  184. return STD_ERR.PAYMENT_NOT_ENOUGH
  185. end
  186. if not d.data[id].num then
  187. return STD_ERR.COMMON_SYS_ERR
  188. end
  189. if d.data[id].num < num then
  190. return STD_ERR.PAYMENT_NOT_ENOUGH
  191. end
  192. return 0
  193. end
  194. function MODULE.set_money(character, id, num)
  195. local d = _M.assert_get(character)
  196. if num < 0 then
  197. return STD_ERR.PAYMENT_NUM_ERR
  198. end
  199. if not d.data or not d.data[id] then
  200. local conf = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
  201. if not conf[id] then
  202. return STD_ERR.PAYMENT_UNKNOWN_ITEM
  203. end
  204. end
  205. d.data[id].num = num
  206. _M.persist(character)
  207. return 0
  208. end
  209. function MODULE.get_money(character, id)
  210. local d = _M.assert_get(character)
  211. return d.data[id] and d.data[id].num or 0
  212. end
  213. -- 获取所有随机宝箱
  214. function MODULE.get_all_random_box(character, args)
  215. local d = _M.assert_get(character)
  216. local conf_list = assert(asset.GoodsConfig_proto, "GoodsConfig_proto")
  217. local temp = {}
  218. for _, data in pairs(d.data) do
  219. local conf = conf_list[data.id]
  220. if conf and conf.type == CURRENCY_TYPE_RANDOM_BOX and data.num > 0 then
  221. table.insert(temp, {id = data.id, num = data.num})
  222. end
  223. end
  224. return temp
  225. end
  226. return MODULE