Blame view

src/actions/HttpAction.lua 6.66 KB
ae3753b1   zhangqijia   feat: 增加 role_by_...
1
  require "utils.MysqlUtil"
222a7d5f   zhouhaihai   httpGm
2
  
3fe4471e   zhouhaihai   热更新 demo
3
4
  local _M = {}
  
2d392ede   zhouhaihai   热更新 最终版
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  
  --[=[使用python 调用的话eg:
  
  	import requests
  
  	@params 指处理方法中的 query
  	@data 指处理方法中的 body
  	@return 'success' 成功
  
  	status = requests.post("http://127.0.0.1:8001/clearcache", params = {"key": "zhaolu1234dangge"}, data = "", timeout = 300).text
  
  	if status != 'success' :
  		print("错误")
  ]=]
  
  
2d392ede   zhouhaihai   热更新 最终版
21
22
23
  --[=[
  	解码后
  	body = {
a5486ede   zhouhaihai   csvdata 修改为 share...
24
25
26
  		"src/csvdata/init.lua",
  		"unitCsv",
  		"story_cgCsv",
2d392ede   zhouhaihai   热更新 最终版
27
28
29
30
31
32
33
34
35
36
37
  	}
  ]=]
  function _M.hotfix_csvdata(query, body)
  	if not body or body == "" then
  		return 'no body'
  	end
  
  	local ok, result = pcall(json.decode, body)
  	if not ok or type(result) ~= 'table' then
  		return "decode error"
  	end
2d392ede   zhouhaihai   热更新 最终版
38
  
a5486ede   zhouhaihai   csvdata 修改为 share...
39
40
41
  	csvdb.hotfix(table.unpack(result))
  
  	return 'success'
2d392ede   zhouhaihai   热更新 最终版
42
  end
3fe4471e   zhouhaihai   热更新 demo
43
44
45
46
  
  -- 热更新代码  -- 针对 agent 执行发送过来的代码 -- 代码要规范~ 
  
  --[=[ eg:
5e5d7680   zhouhaihai   热更新 优化
47
48
49
50
51
52
53
54
  1. *Action 
  	body = """
  		_hotfixActions = _hotfixActions or {}
  		_hotfixActions["Gm.clientRequest"] = function(agent, data)
  			bin = MsgPack.pack({ cmd = "testtest" })
  			SendPacket(actionCodes.Gm_receiveResponse, bin)
  		end
  	"""
3fe4471e   zhouhaihai   热更新 demo
55
56
57
58
59
60
  
  2. 修改 global 方法  变量 直接覆盖
  	body = """
  		function a()
  			print(123)
  		end
5e5d7680   zhouhaihai   热更新 优化
61
62
63
64
  
  		globalCsv["asdasd"] = 12
  
  		HEHE = 123
3fe4471e   zhouhaihai   热更新 demo
65
66
  	"""
  
2d392ede   zhouhaihai   热更新 最终版
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  3. 修改 class  方法
  	1. 修改role的方法
  	body = """
  		local role = ...
  		_hotfixClass = _hotfixClass or {}
  		_hotfixClass["Role"] = _hotfixClass["Role"] or {}
  		local hotfixFunc = function(hotfixclass)
  			function hotfixclass:getItemCount(itemId)
  				return self:getProperty("id") .. "__" .. self:getProperty("uid") .. "__" .. itemId
  			end
  		end
  		table.insert(_hotfixClass["Role"], hotfixFunc)
  		if role then
  			hotfixFunc(role)
  		end
  	"""
  	2. 修改hero的方法
3fe4471e   zhouhaihai   热更新 demo
84
  	body = """
2d392ede   zhouhaihai   热更新 最终版
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  		local role = ...
  		_hotfixClass = _hotfixClass or {}
  		_hotfixClass["Hero"] = _hotfixClass["Hero"] or {}
  		local hotfixFunc = function(hotfixclass)
  			function hotfixclass:getBattleValue()
  				return self:getProperty("id") + "98700000000"
  			end
  		end
  		table.insert(_hotfixClass["Hero"], hotfixFunc)
  		if role and role.heros then
  			for _, hero in pairs(role.heros) do
  				hotfixFunc(hero)
  			end
  		end
3fe4471e   zhouhaihai   热更新 demo
99
  	"""
2d392ede   zhouhaihai   热更新 最终版
100
101
  
  	3. 其他的类仿照上面的写法
3fe4471e   zhouhaihai   热更新 demo
102
103
  ]=]
  
2d392ede   zhouhaihai   热更新 最终版
104
  function _M.hotfix_code(query, body)
3fe4471e   zhouhaihai   热更新 demo
105
  	if not body or body == "" then
2d392ede   zhouhaihai   热更新 最终版
106
  		return "no body"
3fe4471e   zhouhaihai   热更新 demo
107
  	end
3fe4471e   zhouhaihai   热更新 demo
108
  	local ok = pcall(load, body)
2d392ede   zhouhaihai   热更新 最终版
109
110
111
  	if not ok then 
  		return "code error"
  	end
5e5d7680   zhouhaihai   热更新 优化
112
  
2d392ede   zhouhaihai   热更新 最终版
113
  	skynet.error(string.format("hotfix_code time: %s, code: %s", skynet.timex(), body))
5e5d7680   zhouhaihai   热更新 优化
114
  	
a5486ede   zhouhaihai   csvdata 修改为 share...
115
  	pcall(skynet.call, '.watchdog', "lua", "hotfix", body)
3fe4471e   zhouhaihai   热更新 demo
116
117
118
  	return 'success'
  end
  
222a7d5f   zhouhaihai   httpGm
119
120
121
122
123
124
125
126
  local function proc_online(cmd, roleId, pms)
  	local agent = datacenter.get("agent", roleId)
  	if agent then
  		local ok, result = pcall(skynet.call, agent.serv, "lua", cmd, pms)
  		return ok and result or "指令在线失败"
  	end
  	return "not_online"
  end
3fe4471e   zhouhaihai   热更新 demo
127
  
222a7d5f   zhouhaihai   httpGm
128
129
130
131
132
133
134
135
136
  function _M.gm_action(query)
  	local gmFuncs = require "actions.GmAction"
  	if not query.cmd or not query.id or not gmFuncs[query.cmd] then return "指令不存在" end
  	-- 在线操作
  	query.id = tonumber(query.id)
  	local isOn = proc_online(query.cmd, query.id, query)
  	if isOn ~= "not_online" then
  		return isOn
  	end
222a7d5f   zhouhaihai   httpGm
137
  	-- 离线操作
0de80321   liuzujun   创建游戏数据库,role对应mys...
138
  	local role = require("models.Role").new({key = string.format("%d", query.id)})
222a7d5f   zhouhaihai   httpGm
139
140
141
142
  	local ret = role:load()
  	if not ret then
  		return "角色不存在"
  	end
913e070e   liuzujun   添加订单表,全局id定时回写数据库
143
144
145
146
  	role:loadAll()
  	role:startActionUcode()
  	local status = gmFuncs[query.cmd](role, query)
  	role:endActionUcode()
5c8c3b70   liuzujun   离线角色强制保存
147
      role:saveRoleData()
913e070e   liuzujun   添加订单表,全局id定时回写数据库
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  	
  	return status
  end
  
  function _M.pay_action(query)
  	local gmFuncs = require "actions.GmAction"
  	if not query.cmd or not query.order or not gmFuncs[query.cmd] then return "指令不存在" end
  	local mysqlproxy = require "shared.mysqlproxy"
  	local res = mysqlproxy:query(string.format("SELECT roleid FROM `Order` WHERE `id` = %s", query.order))
  	if res[1] then 
  		query.id = res[1]["roleid"]
  	else
  		return "订单非法"
  	end
  	-- 在线操作
  	query.id = tonumber(query.id)
  	local isOn = proc_online(query.cmd, query.id, query)
  	if isOn ~= "not_online" then
  		return isOn
  	end
  	-- 离线操作
  	local role = require("models.Role").new({key = string.format("%d", query.id)})
  	local ret = role:load()
  	if not ret then
  		return "角色不存在"
  	end
222a7d5f   zhouhaihai   httpGm
174
  	role:loadAll()
3133cb76   zhouhaihai   日志
175
176
177
  	role:startActionUcode()
  	local status = gmFuncs[query.cmd](role, query)
  	role:endActionUcode()
5c8c3b70   liuzujun   离线角色强制保存
178
      role:saveRoleData()
3133cb76   zhouhaihai   日志
179
180
  	
  	return status
222a7d5f   zhouhaihai   httpGm
181
  end
3fe4471e   zhouhaihai   热更新 demo
182
  
b23cd820   zhouhaihai   查询角色
183
184
185
186
  function _M.query_role(query)
  	if not query.uid then return "not found" end
  	local user = redisproxy:get(string.format("uid:%s", query.uid))
  	if not user then return "not found" end
3fdfef30   zhouhaihai   httpweb
187
  	local roleId = redisproxy:get(string.format("user:%s", string.upper(user)))
b23cd820   zhouhaihai   查询角色
188
189
190
191
  	if not roleId then return "not found" end
  	return json.encode({roleId, user})
  end
  
c763e563   zhouhaihai   删除NGX
192
  function _M.broadcast(query)
5a7e4d88   zhouhaihai   自定义维护公告
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  	local msg = {}
  	local handle = {
  		["common"] = {
  			code = actionCodes.Sys_commonNotice,
  			exec = function()
  				msg["body"] = query.content
  			end
  		},
  		["maintain"] = {
  			code = actionCodes.Sys_maintainNotice,
  			exec = function()
  				msg["body"] = query.content
  			end
  		},
  		["custom"] = {
  			code = actionCodes.Sys_customNotice,
  			exec = function()
  				msg["title"] = query.title
  				msg["body"] = query.content
  				msg["logout"] = query.logout
  			end
e84a1beb   liuzujun   gm后台查询日志,公告相关协议
214
215
216
217
218
219
220
221
  		},
  		["game"] = {
  			code = actionCodes.Sys_gameNotice,
  			exec = function()
  				msg["new"] = query.status
  				msg["channels"] = query.channels
  			end
  		},
c763e563   zhouhaihai   删除NGX
222
  	}
5a7e4d88   zhouhaihai   自定义维护公告
223
224
225
  	if not handle[query.cmd] then return "错误" end
  	handle[query.cmd].exec()
  	mcast_util.pub_world(handle[query.cmd].code, MsgPack.pack(msg))
c763e563   zhouhaihai   删除NGX
226
227
228
  	return "广播成功"
  end
  
59acc11e   zhouhaihai   赛季更新
229
230
231
232
233
  function _M.endless_season_check()
  	mcast_util.pub_world(actionCodes.Sys_endlessSeason, "")
  	return "success"
  end
  
c763e563   zhouhaihai   删除NGX
234
235
236
237
238
  function _M.online(query)
  	local count = datacenter.get("onlineCount") or 0
  	return count
  end
  
3b903aa0   zhouhaihai   队列长度
239
240
241
  function _M.dbqlen(query)
  	return skynet.call(redisd, "debug", "INFO")
  end
c763e563   zhouhaihai   删除NGX
242
  
c7ecb87f   zhouhaihai   添加 测试账号 方法
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  function _M.account_init(query, body)
  	if not query.id  or not body or body == "" then return "指令不存在" end
  
  	local ok, result = pcall(json.decode, body)
  	if not ok or type(result) ~= 'table' then
  		return "decode body error"
  	end
  	query.id = tonumber(query.id)
  	local agent = datacenter.get("agent", query.id)
  	if agent and agent.serv then
  		-- local ok, result = pcall(skynet.call, agent.serv, "role", "accountInit", result)
  		-- return ok and result or "指令在线失败"
  		skynet.call(agent.serv, "role", "accountInit", result)
  		return "成功"
  	else
  		return "角色不在线"
  	end
  end
3fe4471e   zhouhaihai   热更新 demo
261
  
54bd82e3   zhangqijia   fix: 修复 查询抽奖ssr记录...
262
263
264
265
266
267
  function _M.hero_draw_pro(query)
  	local gmFuncs = require "actions.GmAction"
  	if not query.cmd or not gmFuncs[query.cmd] then return "指令不存在" end
  	return gmFuncs[query.cmd]()
  end
  
ae3753b1   zhangqijia   feat: 增加 role_by_...
268
  function _M.role_by_uid(query)
9ad83c89   zhangqijia   fix: role_by_uid ...
269
      local ret, res= roleByUid(query.uid)
ae3753b1   zhangqijia   feat: 增加 role_by_...
270
271
  	local rsp = {}
      rsp.code = 0
9ad83c89   zhangqijia   fix: role_by_uid ...
272
  	if not ret then
ae3753b1   zhangqijia   feat: 增加 role_by_...
273
274
275
276
277
278
279
280
281
282
283
  		rsp.code = -1
          return json.encode(rsp)
  	end
  
  	rsp.id = res["id"]
  	rsp.name = res["name"]
  	rsp.level = res["level"]
  	rsp.code = 0
      return json.encode(rsp)
  end
  
3fe4471e   zhouhaihai   热更新 demo
284
  return _M