Spark.lua 1.89 KB
local Spark = class("Spark", require("shared.ModelBase"))
Spark.schema = {
	id 		    = {"number"},      		-- 唯一自增id
	cfg_id 		= {"number"},
	level		= {"number", 0},		-- 等级
	attrs       = {"table", {}}         -- 基础属性值 id=value
}

function Spark:ctor( properties )
    Spark.super.ctor(self, properties)
end

function Spark:notifyUpdateProperty(field, newValue, oldValue)
	local datas = {
		id = self:getProperty("id"),
		datas = {
			{
				key = field,
				newValue = newValue,
				oldValue = oldValue,
			}
		}
	}
	self:notifyUpdateProperties(datas)
end

function Spark:mylog(contents)
	contents = contents or {}
	if contents["cint1"] or contents["cint2"] then
		print("sparkLog error log have cint1 or cint2  ", debug.traceback())
	end
	contents["cint1"] = self:getProperty("id")
	contents["cint2"] = self:getProperty("cfg_id")

	self.owner:mylog("spark_action", contents)
end

function Spark:notifyUpdateProperties(params)
	local updateData = {
		id = self:getProperty("id"),
		datas = params
	}
	SendPacket(actionCodes.Role_updateSpark, MsgPack.pack(updateData))
end

function Spark:updateProperty(params)
	if not params.field or (not params.delta and not params.value) then
		return
	end
	if params.delta then
		self:incrProperty(params.field, params.delta)
	elseif params.value then
		self:setProperty(params.field, params.value)
	end
	local datas = {}
	table.insert(datas, {key = params.field, newValue = self:getProperty(params.field)})

	self:notifyUpdateProperties(datas)
end

function Spark:addAttr(attrs)
    local curAttrs = clone(self:getProperty("attrs"))
    for k, v in pairs(attrs) do
        curAttrs[k] = (curAttrs[k] or 0) + v
    end
	self:updateProperty({field = "attrs", value = curAttrs})
end

function Spark:data()
	return {
		id = self:getProperty("id"),
		cfg_id = self:getProperty("cfg_id"),
		level = self:getProperty("level"),
		attrs = self:getProperty("attrs"),
	}
end

return Spark