Blame view

src/actions/NgxAction.lua 1.67 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  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