diff --git a/src/actions/GmAction.lua b/src/actions/GmAction.lua index 5712e47..50a6b13 100644 --- a/src/actions/GmAction.lua +++ b/src/actions/GmAction.lua @@ -16,7 +16,7 @@ function _M.clientRequest(agent, data) end function _M.testhotfix(role, pms) - return csvdb["itemCsv"][1]["name"] + return csvdb["itemCsv"][1]["name"] .. " -=- " .. csvdb["itemCsv"][2]["name"] .. " -=- " .. role:getItemCount(123) .. " -=- " .. table.pack(next(role.heros))[2]:getBattleValue() end function _M.hero(role, pms) diff --git a/src/actions/HttpAction.lua b/src/actions/HttpAction.lua index e634dc0..66affe5 100644 --- a/src/actions/HttpAction.lua +++ b/src/actions/HttpAction.lua @@ -5,6 +5,22 @@ local codecache = require "skynet.codecache" -- 清空缓存用 local _M = {} + +--[=[使用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("错误") +]=] + + -- 清空缓存 function _M.clearcache(query, body) skynet.error(string.format("clearcache time: %s", skynet.timex())) @@ -12,7 +28,7 @@ function _M.clearcache(query, body) return 'success' end ---重新加载 需要修改的csvdb +--重新加载 需要修改的csvdb -- 单字段修改 优先使用hotfix_csvdata --[=[ eg: body = """ csvdb["itemCsv"][1]["name"] = "测试一下" @@ -21,20 +37,49 @@ end function _M.reload_csvdata(query, body) if not body or body == "" then - return + return 'no body' end local ok = pcall(load, body) - if not ok then return end - - skynet.error(string.format("reload_csvdata time: %s, code: %s", skynet.timex(), body)) + if not ok then + return "code error" + end - local status = skynet.call('CSVDATA', "lua", "reload", body) + local ok, status = pcall(skynet.call, 'CSVDATA', "lua", "reload", body) if status == "ok" then + skynet.error(string.format("reload_csvdata time: %s, code: %s", skynet.timex(), body)) return 'success' + else + return 'error update' end end +--指定更新某个字段值 +--[=[ + 解码后 + 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 -- 热更新代码 -- 针对 agent 执行发送过来的代码 -- 代码要规范~ @@ -59,21 +104,53 @@ end HEHE = 123 """ -3. 修改 role 方法(待定) - +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的方法 body = """ - + 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 """ + + 3. 其他的类仿照上面的写法 ]=] -function _M.hotfix(query, body) +function _M.hotfix_code(query, body) if not body or body == "" then - return + return "no body" end local ok = pcall(load, body) - if not ok then return end + if not ok then + return "code error" + end - skynet.error(string.format("hotfix time: %s, code: %s", skynet.timex(), body)) + skynet.error(string.format("hotfix_code time: %s, code: %s", skynet.timex(), body)) pcall(skynet.call, 'WATCHDOG', "lua", "hotfix", body) return 'success' diff --git a/src/agent.lua b/src/agent.lua index e7d8350..bc0e15c 100644 --- a/src/agent.lua +++ b/src/agent.lua @@ -24,6 +24,7 @@ local agentInfo = {} -- { client_fd, role, gate_serv, open_timer} local agent_util, cs _hotfixActions = _hotfixActions or {} +_hotfixClass = _hotfixClass or {} --- {{{ 定时器相关 local function handle_timeout() @@ -286,10 +287,10 @@ end function CMD.hotfix(code) local ok, func = pcall(load, code) if ok then - ok = pcall(func) + ok = pcall(func, agentInfo.role) end if not ok then - skynet.error("hotfix error by code " .. code) + skynet.error("hotfix_code error by code " .. code) end end diff --git a/src/services/csvdatad.lua b/src/services/csvdatad.lua index 7016328..f65a431 100644 --- a/src/services/csvdatad.lua +++ b/src/services/csvdatad.lua @@ -61,8 +61,8 @@ end local CMD = {} + function CMD.reload(code) - -- 重新加载 csvdata csvdb = csvdb or {} local ok, func = pcall(load, code) @@ -78,6 +78,29 @@ function CMD.reload(code) return 'ok' end +--更新已经存在的 +function CMD.hotfix(fieldss) + csvdb = csvdb or {} + for _, fields in ipairs(fieldss) do + if #fields >= 3 and (type(fields[#fields]) == 'number' or type(fields[#fields]) == 'string') then + local temp = csvdb + local ok = false + for i = 1, #fields - 1 do + if type(temp) ~= 'table' then break end + if type(fields[i]) ~= 'number' and type(fields[i]) ~= 'string' then break end + if i == #fields - 1 then + if type(temp[fields[i]]) ~= 'number' and type(temp[fields[i]]) ~= 'string' then break end + temp[fields[i]] = fields[#fields] + else + temp = temp[fields[i]] + end + end + end + end + sharedata.update("csvdata", csvdb) + return 'ok' +end + skynet.start(function () travCsv("src/csvdata", file2timeMap) sharedata.new("csvdata", csvdb) diff --git a/src/shared/functions.lua b/src/shared/functions.lua index ad2a390..edabaa8 100644 --- a/src/shared/functions.lua +++ b/src/shared/functions.lua @@ -215,6 +215,17 @@ function class(classname, super) -- copy fields from class to native object for k,v in pairs(cls) do instance[k] = v end instance.class = cls + + -- 覆盖热更新的方法 + pcall(function() + if _hotfixClass and _hotfixClass[cls.__cname] then + for _, func in ipairs(_hotfixClass[cls.__cname]) do + func(instance) -- 绑定新的方法 + end + end + end) + + instance:ctor(...) return instance end @@ -235,6 +246,16 @@ function class(classname, super) function cls.new(...) local instance = setmetatable({}, cls) instance.class = cls + + -- 覆盖热更新的方法 + pcall(function() + if _hotfixClass and _hotfixClass[cls.__cname] then + for _, func in ipairs(_hotfixClass[cls.__cname]) do + func(instance) -- 绑定新的方法 + end + end + end) + instance:ctor(...) return instance end -- libgit2 0.21.2