Blame view

src/models/RoleLog.lua 6.58 KB
4c5d72ab   zhouhaihai   高级pvp
1
  
df883a11   zhouhaihai   日志处理
2
  -- logType
4c5d72ab   zhouhaihai   高级pvp
3
  local LogType = {
df883a11   zhouhaihai   日志处理
4
5
6
  	create = "common",
  	login = "common",
  	logout = "common",
3133cb76   zhouhaihai   日志
7
8
  	guide = "common",
  	
44c6e479   zhouhaihai   增加部分日志
9
10
11
12
13
14
15
16
17
18
19
20
  	in_item = "common",
  	out_item = "common",
  	in_diamond = "common",
  	out_diamond = "common",
  	in_hero = "common",
  	out_hero = "common",
  	in_equip = "common",
  	out_equip = "common",
  	in_rune = "common",
  	out_rune = "common",
  	player_exp = "common",
  	func_open = "common",
3133cb76   zhouhaihai   日志
21
22
23
24
25
  	in_adv = "common",
  	out_adv = "common",
  	in_artifact = "common",
  
  	mail_action = "common",
44c6e479   zhouhaihai   增加部分日志
26
  	role_action = "common",
3133cb76   zhouhaihai   日志
27
28
29
30
31
32
33
34
  	hang_action = "common",
  	hero_action = "common",
  	adv_action = "common",
  	rune_action = "common",
  	pvp_action = "common",
  	diner_action = "common",
  	tower_action = "common",
  	gm_action = "common",
d763fb14   zhouhaihai   签到 九宫格
35
  	act_action = "common",
4c5d72ab   zhouhaihai   高级pvp
36
37
  }
  
df883a11   zhouhaihai   日志处理
38
39
  -- 如要修改 要提前修改 _template mapping -- 对应 mapping 为 gamelog-*
  local Mapping = {
cbc6973f   zhouhaihai   天赋bug 日志调整
40
  	-- 预留一些数据格式  担心integer 范围不够用 将 通用的int* 全部换为long
df883a11   zhouhaihai   日志处理
41
42
43
44
45
46
47
48
49
  	common = {
  		desc = "keyword",--索引的短字符串
  		ucode = "keyword",--关联日志对应ucode
  		key1 = "keyword", --可索引的短字符串
  		key2 = "keyword", --可索引的短字符串
  		-- 几乎不用的长文本 
  		text1 = "text",  --长字符串不索引的类型
  		-- 五个不同类型的数字 基本上满足数量要求 尽量从低到高用
  		short1 = "short", 
cbc6973f   zhouhaihai   天赋bug 日志调整
50
51
  		int1 = "long",
  		int2 = "long",
df883a11   zhouhaihai   日志处理
52
53
  		long1 = "long",
  		float1 = "float",
44c6e479   zhouhaihai   增加部分日志
54
55
  
  		-- 底层使用的 一些参数
cbc6973f   zhouhaihai   天赋bug 日志调整
56
57
58
  		cint1 = "long",
  		cint2 = "long",
  		cint3 = "long",
df883a11   zhouhaihai   日志处理
59
60
  	}
  }
4c5d72ab   zhouhaihai   高级pvp
61
  
df883a11   zhouhaihai   日志处理
62
63
64
65
66
67
68
69
70
71
72
  -- 所有的日志都包括的部分 role 里面的信息  -- mapping 信息在 gamelog-role
  local commonRoleField = {
  	name = "keyword",
  	id = "integer",
  	uid = "keyword",
  	sid = "short",
  	device = "keyword",
  	ctime = "integer",
  	ltime = "integer",
  	level = "short",
  	rmbC = "integer",
4c5d72ab   zhouhaihai   高级pvp
73
74
  }
  
b216676d   zhouhaihai   log
75
76
77
78
  local function printError(info)
  	print(info)
  	print(debug.traceback())
  end
4c5d72ab   zhouhaihai   高级pvp
79
  
df883a11   zhouhaihai   日志处理
80
81
82
83
84
85
  local function checkType(logType, field, value, ctype)
  	local typecheckfunc = {
  		keyword = function()
  			--长度不超过256
  			if type(value) ~= "string" then
  				value = tostring(value)
b216676d   zhouhaihai   log
86
  				printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [keyword], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
87
88
  			else
  				if #value > 256 then
b216676d   zhouhaihai   log
89
  					printError(string.format("LOG ERROR: logType [%s] field [%s] [keyword] type to long. value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
90
91
92
93
94
95
96
  				end
  			end
  			return value
  		end,
  		text = function()
  			if type(value) ~= "string" then
  				value = tostring(value)
b216676d   zhouhaihai   log
97
  				printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [text], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
98
99
100
101
102
103
  			end
  			return value
  		end,
  		integer = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
b216676d   zhouhaihai   log
104
  				printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [integer], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
105
106
107
108
109
110
  			end
  			if value then
  				if math.type(value) ~= "integer" then
  					local oldValue = value
  					value = math.floor(value)
  					if value ~= oldValue then
b216676d   zhouhaihai   log
111
  						printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [integer], is float, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
112
113
114
  					end
  				end
  				if -2147483648 > value or value > 2147483647 then
b216676d   zhouhaihai   log
115
  					printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [integer], too big, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
116
  					value = nil
df883a11   zhouhaihai   日志处理
117
118
119
120
121
122
123
  				end
  			end 
  			return value
  		end,
  		short = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
b216676d   zhouhaihai   log
124
  				printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [short], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
125
126
127
128
129
130
  			end
  			if value then
  				if math.type(value) ~= "integer" then
  					local oldValue = value
  					value = math.floor(value)
  					if value ~= oldValue then
b216676d   zhouhaihai   log
131
  						printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [short], is float, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
132
133
134
135
  					end
  				end
  
  				if -32768 > value or value > 32768 then
b216676d   zhouhaihai   log
136
  					printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [short], too big, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
137
  					value = nil
df883a11   zhouhaihai   日志处理
138
139
140
141
142
143
144
  				end
  			end 
  			return value
  		end,
  		long = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
b216676d   zhouhaihai   log
145
  				printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [long], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
146
147
148
149
150
151
  			end
  			if value then
  				if math.type(value) ~= "integer" then
  					local oldValue = value
  					value = math.floor(value)
  					if type(value) ~= "integer" then
b216676d   zhouhaihai   log
152
  						printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [long], too big, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
153
  						value = nil
df883a11   zhouhaihai   日志处理
154
  					elseif value ~= oldValue then
b216676d   zhouhaihai   log
155
  						printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [long], is float, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
156
157
158
159
160
161
162
163
  					end
  				end
  			end 
  			return value
  		end,
  		float = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
b216676d   zhouhaihai   log
164
  				printError(string.format("LOG ERROR: logType [%s] field [%s] isn't [float], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
165
166
167
168
169
170
171
172
  			end
  			return value
  		end,
  	}
  
  	if typecheckfunc[ctype] then
  		return typecheckfunc[ctype]()
  	else
b216676d   zhouhaihai   log
173
  		printError(string.format("LOG ERROR: logType [%s] field [%s] have a new type [%s] need add check.", logType, field, ctype))
df883a11   zhouhaihai   日志处理
174
175
176
  		return nil
  	end
  end
4c5d72ab   zhouhaihai   高级pvp
177
  
df883a11   zhouhaihai   日志处理
178
179
  local RoleLog = {}
  function RoleLog.bind(Role)
4c5d72ab   zhouhaihai   高级pvp
180
  	function Role:log(logType, contents)
4c5d72ab   zhouhaihai   高级pvp
181
  		contents = contents or {}
df883a11   zhouhaihai   日志处理
182
183
  		local _logType = LogType[logType]
  		if not _logType then 
b216676d   zhouhaihai   log
184
  			printError(string.format("LOG ERROR: new logType [%s] need Add Maping.", logType))
df883a11   zhouhaihai   日志处理
185
186
  			return 
  		end
4c5d72ab   zhouhaihai   高级pvp
187
  		local doc = {}
df883a11   zhouhaihai   日志处理
188
189
  		for field, ctype in pairs(commonRoleField) do
  			if contents[field] then
b216676d   zhouhaihai   log
190
  				printError(string.format("LOG ERROR: logType [%s] had field [%s] overwrite default.", logType, field))
df883a11   zhouhaihai   日志处理
191
192
  			end
  			doc[field] = checkType("commonRoleField", field, self:getProperty(field), ctype)
4c5d72ab   zhouhaihai   高级pvp
193
  		end
df883a11   zhouhaihai   日志处理
194
195
  
  		local mapping = Mapping[_logType]
44c6e479   zhouhaihai   增加部分日志
196
197
198
  		if mapping["ucode"] and not contents["ucode"] then
  			contents["ucode"] = self:getActionUcode()
  		end
df883a11   zhouhaihai   日志处理
199
  
4c5d72ab   zhouhaihai   高级pvp
200
  		for field, value in pairs(contents) do
df883a11   zhouhaihai   日志处理
201
202
203
  			local ftype = mapping[field]
  			if ftype then
  				doc[field] = checkType(logType, field, value, ftype)
4c5d72ab   zhouhaihai   高级pvp
204
  			else
b216676d   zhouhaihai   log
205
  				printError(string.format("LOG ERROR: logType [%s] have new field [%s] no type in mapping.", logType, field))
4c5d72ab   zhouhaihai   高级pvp
206
207
  			end
  		end
df883a11   zhouhaihai   日志处理
208
209
  		if not logd then return end
  		pcall(skynet.send, logd, "lua", "log", logType, doc, _logType)
4c5d72ab   zhouhaihai   高级pvp
210
  	end
44c6e479   zhouhaihai   增加部分日志
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  
  	function Role:startActionUcode()
  		if not self._uniqueCount then
  			self._uniqueCount = 0
  		end
  		local action = {self:getProperty("id"), skynet.timex(), self._uniqueCount}
  		self._uniqueCount = self._uniqueCount + 1
  
  		self._actionUcode = table.concat(action, "_")
  	end
  
  	function Role:endActionUcode()
  		self._actionUcode = nil
  	end
  
  	function Role:getActionUcode()
  		return self._actionUcode
  	end
  
4c5d72ab   zhouhaihai   高级pvp
230
  end
4c5d72ab   zhouhaihai   高级pvp
231
  return RoleLog