--[[ Descripttion:全局 version: Author: Neo,Huang Date: 2023-11-15 22:34:35 LastEditors: Neo,Huang LastEditTime: 2023-11-16 23:30:37 --]] local lib_game_redis = require("lib_game_redis") local lib_game_mysql = require("lib_game_mysql") local userData = require("data.user") local MODULE_NAME = "global" local root = {} -- 创建玩家id function root:gen_user_id() local subKey = "user:max:id" local uid = lib_game_redis:hincrby(MODULE_NAME, subKey, 1) if uid < 1000000 then uid = uid + 1000000 lib_game_redis:hset(MODULE_NAME, subKey, uid) end return uid end -- 创建邮件id function root:gen_mail_id() local id = lib_game_redis:hincrby(MODULE_NAME, "mail:max:id", 1) return id end -- 创建邀请码 function root:gen_share_code() local function _gen_code() local r = {} for i = 1, 6 do table.insert(r, string.char(math.random(65, 90))) end return table.concat(r) end local sharecode = _gen_code() while self:is_sharecode_active(sharecode) do sharecode = _gen_code() end -- 绑定 lib_game_redis:sadd("sharecode", sharecode) return sharecode end -- 邀请码是否使用 function root:is_sharecode_active(sharecode) if is_empty(sharecode) then return false end local ret = lib_game_redis:sismember("sharecode", sharecode) return ret == 1 end -- 删除邀请码 function root:del_sharecode(sharecode) local sql = string.format("delete from `mdl_sharecode` where `sharecode` = '%s'", tostring(sharecode)) lib_game_mysql:query(sql) lib_game_redis:srem("sharecode", sharecode) end -- 更新邀请码 function root:update_sharecode(uid, sharecode) if is_empty(uid) or is_empty(sharecode) then return false end -- 删除旧邀请码 local shareCode = userData:get_share_code(uid) if not is_empty(shareCode) then self:del_sharecode(shareCode) end -- 更新 lib_game_redis:sadd("sharecode", sharecode) local values = string.format("sharecode='%s', uid=%s", tostring(sharecode), tostring(uid)) local sql = string.format("insert into mdl_sharecode set %s;", tostring(values)) lib_game_mysql:query(sql) return true end return root