NgxAction.lua 1.67 KB
local string_format = string.format
local mcast_util = mcast_util
local gmFuncs = require "actions.GmAction"

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

local _M = {}
local __const = {["id"]=1,["pm1"]=1,["pm2"]=1,["pm3"]=1}

local _T = setmetatable({}, {__index = function(_, k)
	return function (pms)
		for k, v in pairs(pms) do
			-- tonum(v, v) 能tonumber 则 转换,不能则返回原来的值
			pms[k] = __const[k] and tonum(v, v) or v
		end
		-- 群体操作
		if not pms.id or pms.id == 0 then
			return _M[k] and _M[k](pms) or "指令不存在"
		end
		-- 个体操作
		if not gmFuncs[k] then return "指令不存在" end
		-- 在线操作
		local isOn = proc_online(k, pms.id, pms)
		if isOn ~= "not_online" then
			return isOn
		end
		-- 如果_M有直接操作,跳过load角色
		if _M[k] then return _M[k](pms) end
		-- 离线操作
		local role = require("models.Role").new({key = string_format("role:%d", pms.id)})
		local ret = role:load()
		if not ret then
			return "角色不存在"
		end
		role:loadAll()
		return gmFuncs[k](role, pms)
	end
end})

-- 在线广播
function _M.broadcast(pms)
	local bin = MsgPack.pack({body = pms.pm2})
	local codes = {
		["common"] = actionCodes.Sys_commonNotice,
		["maintain"] = actionCodes.Sys_maintainNotice,
	}
	if not codes[pms.pm1] then return "错误" end

	mcast_util.pub_world(codes[pms.pm1], bin)
	return "广播成功"
end

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

return _T