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