util_exchange_code.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. --[[
  2. Descripttion:兑换码
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-19 21:20:05
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-19 21:23:40
  8. --]]
  9. local code = require("code")
  10. local timeUtil = require("utils.timeUtil")
  11. local redisUtil = require("utils.redisUtil")
  12. local lib_game_redis = require("lib_game_redis")
  13. local MODULE_KEY = "exchange:code"
  14. local root = {}
  15. local function _get_exchange_code_key(pcode)
  16. return string.format("%s:%s", tostring(MODULE_KEY), tostring(pcode))
  17. end
  18. -- 获取兑换码配置
  19. function root:get_exchange_code_items(pcode)
  20. if is_empty(pcode) then
  21. return code.PARAMTER_ERROR
  22. end
  23. local key = _get_exchange_code_key(pcode)
  24. local isExist = lib_game_redis:exists(key)
  25. if not isExist then
  26. -- 兑换码已不存在
  27. return code.EXCHANGE_CODE.NOT_FOUND
  28. end
  29. local awardTimes = redisUtil.hget_int(key, "awardTimes")
  30. if is_empty(awardTimes) or awardTimes <= 0 then
  31. -- 已领完
  32. return code.EXCHANGE_CODE.AWARD_NOT_MORE
  33. end
  34. local currTime = timeUtil.now()
  35. local expireTime = redisUtil.hget_int(key, "expireTime")
  36. if is_empty(expireTime) or currTime > expireTime then
  37. -- 活动结束
  38. return code.EXCHANGE_CODE.TIME_OUT
  39. end
  40. return code.OK, redisUtil.hget_json(key, "items")
  41. end
  42. -- 增加领奖次数
  43. function root:add_award_times(pcode)
  44. local key = _get_exchange_code_key(pcode)
  45. local isExist = lib_game_redis:exists(key)
  46. if not isExist then
  47. -- 兑换码已不存在
  48. return code.EXCHANGE_CODE.NOT_FOUND
  49. end
  50. local subKey = "awardTimes"
  51. local awardTimes = redisUtil.hincrby(key, subKey, -1)
  52. if awardTimes < 0 then
  53. redisUtil.hset(key, subKey, 0)
  54. -- 已领完
  55. return code.EXCHANGE_CODE.AWARD_NOT_MORE
  56. end
  57. return code.OK
  58. end
  59. ----------------------------------------
  60. -- 记录
  61. ----------------------------------------
  62. local function _get_record_key()
  63. return string.format("%s:record", tostring(MODULE_KEY))
  64. end
  65. -- 新增
  66. function root:add_record(uid, items)
  67. if is_empty(uid) or is_empty(items) then
  68. return false
  69. end
  70. local key = _get_record_key()
  71. local subKey = "records"
  72. local records = redisUtil.hget_json(key, subKey)
  73. table.insert(records, {uid = uid, items = items})
  74. if #records > 10 then
  75. table.remove(records, 1)
  76. end
  77. redisUtil.hset(key, subKey, records)
  78. return false
  79. end
  80. -- 获取记录列表
  81. function root:get_records()
  82. local key = _get_record_key()
  83. local subKey = "records"
  84. return redisUtil.hget_json(key, subKey)
  85. end
  86. return root