123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- --[[
- Descripttion:兑换码
- version:
- Author: Neo,Huang
- Date: 2023-11-19 21:20:05
- LastEditors: Neo,Huang
- LastEditTime: 2023-11-19 21:23:40
- --]]
- local code = require("code")
- local timeUtil = require("utils.timeUtil")
- local redisUtil = require("utils.redisUtil")
- local lib_game_redis = require("lib_game_redis")
- local MODULE_KEY = "exchange:code"
- local root = {}
- local function _get_exchange_code_key(pcode)
- return string.format("%s:%s", tostring(MODULE_KEY), tostring(pcode))
- end
- -- 获取兑换码配置
- function root:get_exchange_code_items(pcode)
- if is_empty(pcode) then
- return code.PARAMTER_ERROR
- end
- local key = _get_exchange_code_key(pcode)
- local isExist = lib_game_redis:exists(key)
- if not isExist then
- -- 兑换码已不存在
- return code.EXCHANGE_CODE.NOT_FOUND
- end
- local awardTimes = redisUtil.hget_int(key, "awardTimes")
- if is_empty(awardTimes) or awardTimes <= 0 then
- -- 已领完
- return code.EXCHANGE_CODE.AWARD_NOT_MORE
- end
- local currTime = timeUtil.now()
- local expireTime = redisUtil.hget_int(key, "expireTime")
- if is_empty(expireTime) or currTime > expireTime then
- -- 活动结束
- return code.EXCHANGE_CODE.TIME_OUT
- end
- return code.OK, redisUtil.hget_json(key, "items")
- end
- -- 增加领奖次数
- function root:add_award_times(pcode)
- local key = _get_exchange_code_key(pcode)
- local isExist = lib_game_redis:exists(key)
- if not isExist then
- -- 兑换码已不存在
- return code.EXCHANGE_CODE.NOT_FOUND
- end
- local subKey = "awardTimes"
- local awardTimes = redisUtil.hincrby(key, subKey, -1)
- if awardTimes < 0 then
- redisUtil.hset(key, subKey, 0)
- -- 已领完
- return code.EXCHANGE_CODE.AWARD_NOT_MORE
- end
- return code.OK
- end
- ----------------------------------------
- -- 记录
- ----------------------------------------
- local function _get_record_key()
- return string.format("%s:record", tostring(MODULE_KEY))
- end
- -- 新增
- function root:add_record(uid, items)
- if is_empty(uid) or is_empty(items) then
- return false
- end
- local key = _get_record_key()
- local subKey = "records"
- local records = redisUtil.hget_json(key, subKey)
- table.insert(records, {uid = uid, items = items})
- if #records > 10 then
- table.remove(records, 1)
- end
- redisUtil.hset(key, subKey, records)
- return false
- end
- -- 获取记录列表
- function root:get_records()
- local key = _get_record_key()
- local subKey = "records"
- return redisUtil.hget_json(key, subKey)
- end
- return root
|