Role.lua 4.32 KB
local Role = class("Role", require("shared.ModelBase"))

local RolePlugin = import(".RolePlugin")
local RoleTask = import(".RoleTask")
local RoleAdv = import(".RoleAdv")
local RoleActivity = import(".RoleActivity")
local RoleChangeStruct = import(".RoleChangeStruct")
RolePlugin.bind(Role)
RoleTask.bind(Role)
RoleAdv.bind(Role)
RoleActivity.bind(Role)
RoleChangeStruct.bind(Role)

function Role:ctor( properties )
	Role.super.ctor(self, properties)
	self.ignoreHeartbeat = false
	self.heros = {}
end

Role.schema = {
	id 					= {"number"},
	uid 				= {"string", ""},
	name 				= {"string", ""},
	sid 				= {"number", 0},
	device 				= {"string", ""},
	banTime 			= {"number", 0},
	banType 			= {"number", 0},
	ltime 				= {"number", 0}, -- 最后登录时间
	ctime 				= {"number", skynet.timex()},  -- 创建时间
	ignoreMt			= {"number", 0},  -- 忽略维护拦截
	sversion			= {"number", globalCsv.StructVersion or 0}, -- 重整数据版本

	-- roleInfo
	level 				= {"number", 0},
	diamond				= {"number", 0},
	reDiamond			= {"number", 0},
	items				= {"string", ""},
	loveStatus			= {"string", ""}, --统计角色的最高 好感度等级 类型相关 -- type=loveL type=loveL

	--冒险相关
	advPass				= {"string", ""}, -- 通关记录
	advInfo				= {"table", {}}, -- 其他信息

}


function Role:notifyUpdateProperty(field, newValue, oldValue, extraValue)
	local updateData = {
		{
			key = field,
			newValue = newValue,
			oldValue = oldValue or "",
			extraValue = extraValue,
		}
	}

	SendPacket(actionCodes.Role_updateProperty, MsgPack.pack(updateData))
end

function Role:updateProperty(params)
	params = params or {}
	if not self.schema[params.field] then return end
	local oldValue = self:getProperty(params.field)
	local ret = {key = params.field, oldValue = oldValue}
	if params.value then
		ret.newValue = params.value
		self:setProperty(params.field, params.value)
	elseif params.delta then
		self:incrProperty(params.field, params.delta)
		ret.newValue = self:getProperty(params.field)
	else
		return
	end
	if not params.notNotify then
		SendPacket(actionCodes.Role_updateProperty, MsgPack.pack({ret}))
	end
end

function Role:updateProperties(params, notNotify)
	for field, value in pairs(params) do
		self:setProperty(field, value)
	end
	if not notNotify then
		SendPacket(actionCodes.Role_updateProperties, MsgPack.pack(params))
	end
end

function Role:notifyUpdateProperties(params)
	SendPacket(actionCodes.Role_updateProperties, MsgPack.pack(params))
end

-- 某些字段 更新改变量 改变量的定义由字段自身决定 {{type = ""}, }
function Role:changeUpdates(params, notNotify)
	local changeUpdateFunc = {
		["loveStatus"] = function(info)
			self:setProperty("loveStatus", self:getProperty("loveStatus"):setv(info["field"], info["value"]))
			return {type = "loveStatus", field = info["field"], value = info["value"]}
		end,
		--table 类型通用更新
		["tableCommon"] = function(fieldType, info)
			if self.class.schema[fieldType][1] ~= "table" then
				error("[ERROR:] need handler for changeUpdate, field : " .. fieldType)
				return
			end
			--支持多深度单字段 
			local curValue = self:getProperty(fieldType)
			if type(info["field"]) == "table" then
				for _idx, _field in ipairs(info["field"]) do
					if _idx < #info["field"] then
						curValue[_field] = curValue[_field] or {}
						curValue = curValue[_field]
					else
						curValue[_field] = info["value"]
					end
				end
			else
				curValue[info["field"]] = info["value"]
			end
			self:setProperty(fieldType)
			return {type = fieldType, field = info["field"], value = info["value"]}
		end,
	}

	local updates = {}
	for _, one in ipairs(params) do
		if changeUpdateFunc[one["type"]] then
			table.insert(updates, changeUpdateFunc[one["type"]](one))
		else
			table.insert(updates, changeUpdateFunc["tableCommon"](one["type"], one))
		end
	end
	if not notNotify and next(updates) then
		SendPacket(actionCodes.Role_changeUpdate, MsgPack.pack(updates))
	end
end

function Role:data()
	return {
		id = self:getProperty("id"),
		name = self:getProperty("name"),
		level = self:getProperty("level"),
		diamond = self:getProperty("diamond"),
		reDiamond = self:getProperty("reDiamond"),
		items = self:getProperty("items"):toNumMap(),
		loveStatus = self:getProperty("loveStatus"):toNumMap(),
		advPass = self:getProperty("advPass"),
		advInfo = self:getProperty("advInfo"),
	}
end

return Role