--[[ Descripttion:玩家数据,不能引用非干净模块 version: Author: Neo,Huang Date: 2021-09-06 20:27:20 LastEditors: Neo,Huang LastEditTime: 2021-09-06 20:27:42 --]] local lib_game_redis = require("lib_game_redis") local root = {} ---------------------------------------- -- 实时数据 ---------------------------------------- -- 玩家key local function l_user_get_module_key(uid, mdl) if uid == nil or mdl == nil then return end return string.format("mdl:%s:%s", tostring(mdl), tostring(uid)) end -- 同步玩家模块数据 local function l_reload_user_module_info(uid, mdl) if uid == nil or mdl == nil then return false end -- 机器人 if is_robot(uid) then return false end -- 玩家模块数据是否已存在 local isExist = lib_game_redis:exists(l_user_get_module_key(uid, mdl)) -- log.info("l_reload_user_module_info uid[%s] mdl[%s] exist[%s]", tostring(uid), tostring(mdl), tostring(exist)) if not isExist then -- 再检查一次 local val = lib_game_redis:hget(l_user_get_module_key(uid, "user"), "uid") if not is_empty(val) then return true end -- TODO:从数据库载入玩家数据 return true else return true end return false end -- 删除玩家信息 function root:hdel(uid, mdl, key) if is_nil(uid) or is_nil(mdl) or is_nil(key) then return end return lib_game_redis:hdel(l_user_get_module_key(uid, mdl), key) end -- 设置玩家信息 function root:hset(uid, mdl, key, info) if is_nil(uid) or is_nil(mdl) or is_nil(key) then return false end if is_nil(info) then return self:hdel(uid, mdl, key) end local val = info if type(info) == "table" then val = cjson_encode(info) end return lib_game_redis:hset(l_user_get_module_key(uid, mdl), key, val) end -- 获取玩家信息 function root:hget(uid, mdl, key) if is_nil(uid) or is_nil(mdl) or is_nil(key) then return end local val = lib_game_redis:hget(l_user_get_module_key(uid, mdl), key) if is_nil(val) then local ok = l_reload_user_module_info(uid, mdl) if ok then val = lib_game_redis:hget(l_user_get_module_key(uid, mdl), key) end end return val end -- 获取玩家信息 function root:hget_int(uid, mdl, key) if is_nil(uid) or is_nil(mdl) or is_nil(key) then return end local val = self:hget(uid, mdl, key) if not is_nil(val) then return tonumber(val) end return 0 end -- 获取玩家信息 function root:hget_json(uid, mdl, key) if is_nil(uid) or is_nil(mdl) or is_nil(key) then return end local val = self:hget(uid, mdl, key) local info = nil if not is_empty(val) then info = cjson_decode(val) else info = {} end return info end function root:hincrby(uid, mdl, key, val) if is_nil(uid) or is_nil(mdl) or is_nil(key) then return end return lib_game_redis:hincrby(get_player_key(uid, mdl), key, val or 1) end function root:model_func(func, mdl, key, defaultValue) local resFunc = function(root, uid) if func then local val = func(root, uid, mdl, key) if is_empty(val) and not defaultValue then val = defaultValue end return val end end return resFunc end -- 常用数据方法 root.get_nickname = root:model_func(root.hget, "user", "nickname") root.get_channel = root:model_func(root.hget_int, "user", "channel") return root