exchangecode.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. --[[
  2. Descripttion:兑换码
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-19 21:28:04
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-19 21:29:10
  8. --]]
  9. local code = require("code")
  10. local util_exchange_code = require("utils.util_exchange_code")
  11. local bagData = require("data.bag")
  12. local root = class("exchangecode", require("base.baseModule"))
  13. function root:ctor(uid)
  14. root.super.ctor(self, uid, "exchangecode", "uid", true)
  15. self.uid = uid
  16. end
  17. function root:mysql_get_init_columns()
  18. return {
  19. uid = "int(11) NOT NULL COMMENT '用户id'",
  20. awardList = "JSON COMMENT '已领取兑换码列表'"
  21. }
  22. end
  23. -- 兑换码
  24. function root:itf_exchange_code(role, msg)
  25. local pCode = msg.pCode
  26. if is_empty(pCode) then
  27. return code.PARAMTER_ERROR
  28. end
  29. local awardList = self:redis_get_key_info("awardList")
  30. if table.include(awardList, pCode) then
  31. -- 已领取奖励
  32. return code.EXCHANGE_CODE.AWARDED
  33. end
  34. -- 获取兑换码奖励权重物品列表
  35. local errcode, weightItems = util_exchange_code:get_exchange_code_items(pCode)
  36. if code.is_not_ok(errcode) then
  37. return errcode
  38. end
  39. -- 增加领奖次数
  40. errcode = util_exchange_code:add_award_times(pCode)
  41. if code.is_not_ok(errcode) then
  42. return errcode
  43. end
  44. table.insert(awardList, pCode)
  45. self:redis_update_key_info("awardList", awardList)
  46. -- 减少领奖次数
  47. -- 随机物品
  48. local index = random_list_by_weight(weightItems)
  49. local items = {{id = weightItems[index].id, count = weightItems[index].count}}
  50. local keyEvent = string.format("exchange-code-%s", tostring(pCode))
  51. bagData:add_items(self.uid, items, keyEvent)
  52. return code.OK, {items = items}
  53. end
  54. return root