Blame view

src/actions/HttpAction.lua 3.52 KB
3fe4471e   zhouhaihai   热更新 demo
1
2
3
4
5
6
7
  
  local skynet = require "skynet"
  local sharedata = require "skynet.sharedata"
  local codecache = require "skynet.codecache"  -- 清空缓存用
  
  local _M = {}
  
2d392ede   zhouhaihai   热更新 最终版
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  
  --[=[使用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("错误")
  ]=]
  
  
5e5d7680   zhouhaihai   热更新 优化
24
  -- 清空缓存
3fe4471e   zhouhaihai   热更新 demo
25
26
27
28
29
30
  function _M.clearcache(query, body)
  	skynet.error(string.format("clearcache time: %s", skynet.timex()))
  	codecache.clear()
  	return 'success'
  end
  
2d392ede   zhouhaihai   热更新 最终版
31
  --重新加载 需要修改的csvdb  -- 单字段修改 优先使用hotfix_csvdata
5e5d7680   zhouhaihai   热更新 优化
32
33
34
35
36
37
  --[=[ eg:
  	body = """
  		csvdb["itemCsv"][1]["name"] = "测试一下"
  	"""
  ]=]
  
3fe4471e   zhouhaihai   热更新 demo
38
  function _M.reload_csvdata(query, body)
5e5d7680   zhouhaihai   热更新 优化
39
  	if not body or body == "" then
2d392ede   zhouhaihai   热更新 最终版
40
  		return 'no body'
3fe4471e   zhouhaihai   热更新 demo
41
  	end
3fe4471e   zhouhaihai   热更新 demo
42
  
5e5d7680   zhouhaihai   热更新 优化
43
  	local ok = pcall(load, body)
2d392ede   zhouhaihai   热更新 最终版
44
45
46
  	if not ok then 
  		return "code error"
  	end
5e5d7680   zhouhaihai   热更新 优化
47
  
2d392ede   zhouhaihai   热更新 最终版
48
  	local ok, status = pcall(skynet.call, 'CSVDATA', "lua", "reload", body)
5e5d7680   zhouhaihai   热更新 优化
49
  	if status == "ok" then	
2d392ede   zhouhaihai   热更新 最终版
50
  		skynet.error(string.format("reload_csvdata time: %s, code: %s", skynet.timex(), body))
5e5d7680   zhouhaihai   热更新 优化
51
  		return 'success'
2d392ede   zhouhaihai   热更新 最终版
52
53
  	else
  		return 'error update'
5e5d7680   zhouhaihai   热更新 优化
54
  	end
3fe4471e   zhouhaihai   热更新 demo
55
56
  end
  
2d392ede   zhouhaihai   热更新 最终版
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  --指定更新某个字段值
  --[=[
  	解码后
  	body = {
  		{"itemCsv", 1, "name", "测试一下"},
  		{"itemCsv", 2, "name", "测试一下"}
  	}
  ]=]
  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
  	local ok, status = pcall(skynet.call, 'CSVDATA', "lua", "hotfix", result)
  
  	if status == "ok" then	
  		skynet.error(string.format("hotfix_csvdata time: %s, code: %s", skynet.timex(), body))
  		return 'success'
  	else
  		return 'error update'
  	end
  end
3fe4471e   zhouhaihai   热更新 demo
83
84
85
86
  
  -- 热更新代码  -- 针对 agent 执行发送过来的代码 -- 代码要规范~ 
  
  --[=[ eg:
5e5d7680   zhouhaihai   热更新 优化
87
88
89
90
91
92
93
94
  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
95
96
97
98
99
100
  
  2. 修改 global 方法  变量 直接覆盖
  	body = """
  		function a()
  			print(123)
  		end
5e5d7680   zhouhaihai   热更新 优化
101
102
103
104
  
  		globalCsv["asdasd"] = 12
  
  		HEHE = 123
3fe4471e   zhouhaihai   热更新 demo
105
106
  	"""
  
2d392ede   zhouhaihai   热更新 最终版
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  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
124
  	body = """
2d392ede   zhouhaihai   热更新 最终版
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  		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
139
  	"""
2d392ede   zhouhaihai   热更新 最终版
140
141
  
  	3. 其他的类仿照上面的写法
3fe4471e   zhouhaihai   热更新 demo
142
143
  ]=]
  
2d392ede   zhouhaihai   热更新 最终版
144
  function _M.hotfix_code(query, body)
3fe4471e   zhouhaihai   热更新 demo
145
  	if not body or body == "" then
2d392ede   zhouhaihai   热更新 最终版
146
  		return "no body"
3fe4471e   zhouhaihai   热更新 demo
147
  	end
3fe4471e   zhouhaihai   热更新 demo
148
  	local ok = pcall(load, body)
2d392ede   zhouhaihai   热更新 最终版
149
150
151
  	if not ok then 
  		return "code error"
  	end
5e5d7680   zhouhaihai   热更新 优化
152
  
2d392ede   zhouhaihai   热更新 最终版
153
  	skynet.error(string.format("hotfix_code time: %s, code: %s", skynet.timex(), body))
5e5d7680   zhouhaihai   热更新 优化
154
  	
3fe4471e   zhouhaihai   热更新 demo
155
156
157
158
159
160
161
162
  	pcall(skynet.call, 'WATCHDOG', "lua", "hotfix", body)
  	return 'success'
  end
  
  
  
  
  return _M