HttpAction.lua 5.13 KB

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("错误")
]=]


--[=[
	解码后
	body = {
		"src/csvdata/init.lua",
		"unitCsv",
		"story_cgCsv",
	}
]=]
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

	csvdb.hotfix(table.unpack(result))

	return 'success'
end

-- 热更新代码  -- 针对 agent 执行发送过来的代码 -- 代码要规范~ 

--[=[ eg:
1. *Action 
	body = """
		_hotfixActions = _hotfixActions or {}
		_hotfixActions["Gm.clientRequest"] = function(agent, data)
			bin = MsgPack.pack({ cmd = "testtest" })
			SendPacket(actionCodes.Gm_receiveResponse, bin)
		end
	"""

2. 修改 global 方法 和 变量 直接覆盖
	body = """
		function a()
			print(123)
		end

		globalCsv["asdasd"] = 12

		HEHE = 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的方法
	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_code(query, body)
	if not body or body == "" then
		return "no body"
	end
	local ok = pcall(load, body)
	if not ok then 
		return "code error"
	end

	skynet.error(string.format("hotfix_code time: %s, code: %s", skynet.timex(), body))
	
	pcall(skynet.call, '.watchdog', "lua", "hotfix", body)
	return 'success'
end

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

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
	-- 离线操作
	local role = require("models.Role").new({key = string.format("role:%d", query.id)})
	local ret = role:load()
	if not ret then
		return "角色不存在"
	end
	role:loadAll()
	role:startActionUcode()
	local status = gmFuncs[query.cmd](role, query)
	role:endActionUcode()
	
	return status
end

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
	local roleId = redisproxy:get(string.format("user:%s", string.upper(user)))
	if not roleId then return "not found" end
	return json.encode({roleId, user})
end

function _M.broadcast(query)
	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
		}
	}
	if not handle[query.cmd] then return "错误" end
	handle[query.cmd].exec()
	mcast_util.pub_world(handle[query.cmd].code, MsgPack.pack(msg))
	return "广播成功"
end

function _M.endless_season_check()
	mcast_util.pub_world(actionCodes.Sys_endlessSeason, "")
	return "success"
end

function _M.online(query)
	local count = datacenter.get("onlineCount") or 0
	return count
end

function _M.dbqlen(query)
	return skynet.call(redisd, "debug", "INFO")
end

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

return _M