Blame view

src/models/Spark.lua 1.89 KB
3d8468b2   liuzujun   火花系统
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  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