Browse Source

新增兑换码

huangyuhao 1 year ago
parent
commit
abec0ccd56

+ 10 - 2
common/utils/redisUtil.lua

@@ -84,6 +84,14 @@ function root.hgetall(key)
84 84
         return ret
85 85
     end
86 86
 end
87
+-- 自增
88
+function root.hincrby(key, subKey, steps)
89
+    if is_nil(key) or is_nil(subKey) then
90
+        return
91
+    end
92
+    steps = steps or 1
93
+    return lib_game_redis:hincrby(key, subKey, steps)
94
+end
87 95
 
88 96
 function root.do_eval(evalkey, keyCount, ...)
89 97
     local data = lib_game_redis:eval(root.evalStr[evalkey], keyCount, ...)
@@ -91,11 +99,11 @@ function root.do_eval(evalkey, keyCount, ...)
91 99
 end
92 100
 
93 101
 -- redis 执行 lua 脚本.
94
-root.evalStr    = {
102
+root.evalStr = {
95 103
     ["aaa"] = [[redis.call('lpush', KEYS[1], ARGV[1]); redis.call('ltrim', KEYS[1], 0, ARGV[2]);]],
96 104
     ["bbb"] = [["redis.call('zincrby',KEYS[1], ARGV[1], ARGV[2]);
97 105
         local scores    = redis.call('zscore', KEYS[1], ARGV[2]);
98
-        local rank      = redis.call('zrevrank', KEYS[1], ARGV[2]); 
106
+        local rank      = redis.call('zrevrank', KEYS[1], ARGV[2]);
99 107
         return {rank, scores};]]
100 108
 }
101 109
 

+ 62 - 0
dev/modules/exchangecode.lua

@@ -0,0 +1,62 @@
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
+
12
+local bagData = require("data.bag")
13
+
14
+local root = class("exchangecode", require("base.baseModule"))
15
+
16
+function root:ctor(uid)
17
+    root.super.ctor(self, uid, "exchangecode", "uid", true)
18
+    self.uid = uid
19
+end
20
+
21
+function root:mysql_get_init_columns()
22
+    return {
23
+        uid = "int(11) NOT NULL COMMENT '用户id'",
24
+        awardList = "JSON COMMENT '已领取兑换码列表'"
25
+    }
26
+end
27
+
28
+-- 兑换码
29
+function root:itf_exchange_code(role, msg)
30
+    local pCode = msg.pCode
31
+    if is_empty(pCode) then
32
+        return code.PARAMTER_ERROR
33
+    end
34
+    local awardList = self:redis_get_key_info("awardList")
35
+    if table.include(awardList, pCode) then
36
+        -- 已领取奖励
37
+        return code.EXCHANGE_CODE.AWARDED
38
+    end
39
+    -- 获取兑换码奖励权重物品列表
40
+    local errcode, weightItems = util_exchange_code:get_exchange_code_items(pCode)
41
+    if code.is_not_ok(errcode) then
42
+        return errcode
43
+    end
44
+    -- 增加领奖次数
45
+    errcode = util_exchange_code:add_award_times(pCode)
46
+    if code.is_not_ok(errcode) then
47
+        return errcode
48
+    end
49
+
50
+    table.insert(awardList, pCode)
51
+    self:redis_update_key_info("awardList", awardList)
52
+    -- 减少领奖次数
53
+
54
+    -- 随机物品
55
+    local index = random_list_by_weight(weightItems)
56
+    local items = {{id = weightItems[index].id, count = weightItems[index].count}}
57
+    local keyEvent = string.format("exchange-code-%s", tostring(pCode))
58
+    bagData:add_items(self.uid, items, keyEvent)
59
+    return code.OK, {items = items}
60
+end
61
+
62
+return root

+ 92 - 0
dev/utils/util_exchange_code.lua

@@ -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

+ 38 - 0
nodes/game/interface/exchange.lua

@@ -0,0 +1,38 @@
1
+--[[
2
+Descripttion:兑换码
3
+version:
4
+Author: Neo,Huang
5
+Date: 2023-11-19 22:58:58
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2023-11-19 22:59:25
8
+--]]
9
+local code = require "code"
10
+local util_exchange_code = require("utils.util_exchange_code")
11
+local util_player = require("utils.util_player")
12
+
13
+local root = {}
14
+
15
+-- 模块信息
16
+function root:exchange_get_info(role, msg)
17
+    local list = util_exchange_code:get_records()
18
+    local ret = {}
19
+    if not is_empty(list) then
20
+        for _, v in ipairs(list) do
21
+            local info = {
22
+                playerInfo = util_player:get_base_info(v.uid),
23
+                items = v.items
24
+            }
25
+            if is_empty(ret.records) then
26
+                ret.records = {}
27
+            end
28
+            table.insert(ret.records, info)
29
+        end
30
+    end
31
+    return code.OK, ret
32
+end
33
+-- 领取
34
+function root:exchange_get_award(role, msg)
35
+    return role.exchangecode:itf_exchange_code(role, msg)
36
+end
37
+
38
+return root