RoleCross.lua 4.2 KB

-- 跨越 agent 获取数据使用
local RoleCross = {}

--*********************************** agent 存在时调用  ****************************************--
RoleCross.bind = function (Role)
	-- 好友列表简约信息
	function Role:friendSInfo()
		local info = {
			name = self:getProperty("name"),
			level = self:getProperty("level"),
			headId = self:getProperty("headId"),
			ltime = self:getProperty("ltime"),
			battleV = self:getProperty("pvpTBVC") ~= 0 and self:getProperty("pvpTBVC") or self:getProperty("hangTBV")
		}
		return info
	end

	-- 好友详细队伍信息
	function Role:friendInfo()
		local info = self:friendSInfo()
		local heros = self:getProperty("pvpTBVC") ~= 0 and self:getProperty("pvpTSC") or self:getProperty("hangTS")
		info.heros = heros
		return info
	end

	-- 好友队伍战斗信息
	function Role:friendBattleInfo()
		return self:getProperty("pvpTBVC") ~= 0 and self:getProperty("pvpTBC") or self:getProperty("hangTB")
	end

	-- pvp 数据
	function Role:pvpCInfo()
		local info = {
			name = self:getProperty("name"),
			level = self:getProperty("level"),
			headId = self:getProperty("headId"),
			battleV = self:getProperty("pvpTBVC"),
			heros = self:getProperty("pvpTSC"),
			battleInfo = self:getProperty("pvpTBC")
		}
		return info
	end

end


--*********************************** agent 不存在时调用  ***************************************--
local CMD = {}
local SRole
local function unpackRoleField(field, value)
	if not SRole.schema[field] then
		return nil
	end
	local typ, def = table.unpack(SRole.schema[field])

	if typ == "number" then
    	value = tonumber(value or def)
	elseif typ == "table" then
		if type(value) == "string" then
            value = MsgPack.unpack(value)
        else
        	value = def
        end
    end
    return value
end

local function packRoleField(field, value)
	if not SRole.schema[field] then
		return nil
	end
	local typ, def = table.unpack(SRole.schema[field])

	if typ == "table" then
		if type(value) == "table" then
			value = MsgPack.pack(value)
		end
	end
	return value
end

local function getRoleKey(roleId)
	return string.format("role:%d", roleId)
end

function CMD.setProperty(roleId, field, value)
	local value = packRoleField(field, value) 
	if not value then return end
	return redisproxy:hset(getRoleKey(roleId), field, value)
end


function CMD.setProperties(roleId, fields)
	local result = {}
	for k,v in pairs(fields) do
		local value = packRoleField(k, v)
		if value then
			result[#result + 1] = k
			result[#result + 1] = value
		end
	end
	return redisproxy:hmset(getRoleKey(roleId), table.unpack(result))
end

function CMD.getProperty(roleId, field)
	return unpackRoleField(field ,redisproxy:hget(getRoleKey(roleId), field))
end

function CMD.getProperties(roleId, fields)
	local returnValue = redisproxy:hmget(getRoleKey(roleId), table.unpack(fields))
	local ret = {}
	for index, key in ipairs(fields) do
		ret[key] = unpackRoleField(key, returnValue[index])
	end
	return ret
end

function CMD.friendSInfo(roleId)
	local info = CMD.getProperties(roleId, {"name", "level", "headId", "ltime", "pvpTBVC", "hangTBV"})
	return {
		name = info.name,
		level = info.level,
		headId = info.headId,
		ltime = info.ltime,
		battleV = info.pvpTBVC ~= 0 and info.pvpTBVC or info.hangTBV,
	}
end

function CMD.friendInfo(roleId)
	local info = CMD.getProperties(roleId, {"name", "level", "headId", "ltime", "pvpTBVC", "hangTBV", "pvpTSC", "hangTS"})
	return {
		name = info.name,
		level = info.level,
		headId = info.headId,
		ltime = info.ltime,
		battleV = info.pvpTBVC ~= 0 and info.pvpTBVC or info.hangTBV,
		heros =  info.pvpTBVC ~= 0 and info.pvpTSC or info.hangTS
	}
end

function CMD.friendBattleInfo(roleId)
	local info = CMD.getProperties(roleId, {"pvpTBC", "hangTB"})
	return next(info.pvpTBC) and info.pvpTBC or info.hangTB
end

function CMD.pvpCInfo(roleId)
	local info = CMD.getProperties(roleId, {"name", "level", "headId", "pvpTBVC", "pvpTSC", "pvpTBC"})
	return {
		name = info.name,
		level = info.level,
		headId = info.headId,
		battleV = info.pvpTBVC,
		heros = info.pvpTSC,
		battleInfo = info.pvpTBC
	}
end

RoleCross.handle = function(cmd, roleId, ...)
	SRole = SRole or require("models.Role")
	if CMD[cmd] then
		return CMD[cmd](roleId, ...)
	end
end

return RoleCross