Blame view

src/services/uniond.lua 3.6 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
  require "shared.init"
  require "utils.init"
  require "ProtocolCode"
  require "GlobalVar"
  require "RedisKeys"
  require "skynet.manager"
  
  local sharedata = require "sharedata"
  local redisproxy = require "shared.redisproxy"
  local datacenter = require "datacenter"
  local queue = require "skynet.queue"
  
  skynet = require "skynet"
  globalCsv = require "csvdata.GlobalDefine"
  
  local table_pack = table.pack
  local table_unpack = table.unpack
  local string_format = string.format
  local pairs = pairs
  local ipairs = ipairs
  local tonumber = tonumber
  
  -- 维护在线状态
  
  local unionInfo, CMD, cs = {}, {}
  
  skynet.register_protocol {
  	name = "role",
3c8a6b8a   zhouhaihai   get equip
29
  	id = 13,
314bc5df   zhengshouren   提交服务器初始代码
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  	pack = skynet.pack,
  	unpack = skynet.unpack,
  }
  
  local function handle_timeout()
  	local now = skynet.timex()
  	unionInfo:onTimer(now)
  	skynet.timeout(100, handle_timeout)
  end
  
  --[[
  getProperties({"field1", "field2", ...})
  return: {field1 = value1, field2 = value2, ...}
  -------
  setProperties({field1 = value1, field2 = value2, ...})
  ]]
  function rpcRole(roleId, funcName, ...)
  	if not unionInfo.members[roleId] then return end
  	local serv = unionInfo.members[roleId].serv
  	if serv then
  		if funcName == "getProperties" then
  			return skynet.call(serv, "role", funcName, table_unpack(...))
  		else
  			return skynet.call(serv, "role", funcName, ...)
  		end
  	else
  		local rediskey = string_format("role:%d", roleId)
  		if funcName == "getProperty" then
  			return redisproxy:hget(rediskey, ...)
  		elseif funcName == "setProperty" then
  			return redisproxy:hset(rediskey, ...)
  		elseif funcName == "getProperties" then
  			local sRole = require "models.Role"
  			local fields = table_pack(...)
  			local rets = redisproxy:hmget(rediskey, table_unpack(fields, 1, fields.n))
  			local result = {}
  			for i=1, fields.n do
  				local typ = sRole.schema[fields[i]][1]
  			    local def = sRole.schema[fields[i]][2]
  			    if typ == "number" then
  			    	result[fields[i]] = tonumber(rets[i] or def)
  				else
  			    	result[fields[i]] = rets[i]
  			    end
  			end
  			return result
  		elseif funcName == "setProperties" then
  			local fields = ...
  			local params = {}
  			for field, value in pairs(fields) do
  				params[#params+1] = field
  				params[#params+1] = value
  			end
  			return redisproxy:hmset(rediskey, table_unpack(params))
  		end
  	end
  end
  
  -- 加载联盟数据
  function CMD.load(unionId)
  	unionInfo = require("models.Union").new({key = UNION_KEY:format(unionId)})
  	unionInfo:load()
  	unionInfo:loadMembers()
  end
  
  -- 创建一个联盟
  function CMD.new(roleId)
  	local unionId = redisproxy:hincrby("autoincrement_set", "union", 1)
  	unionInfo = require("models.Union").new({
  		key = UNION_KEY:format(unionId),
  		master = roleId
  	})
  	unionInfo:create()
  	unionInfo:addMember(roleId, true)
  	redisproxy:sadd(UNION_SET, unionId)
  	return unionId
  end
  
  -- 登录/登出 需要向联盟服务报备
  function CMD.sign(cmd, roleId, serv)
      if not unionInfo.members[roleId] then return end
      if cmd == "login" then
          unionInfo.members[roleId].serv = serv
      elseif cmd == "logout" then
          unionInfo.members[roleId].serv = nil
      end
  end
  
  local function __init__()
  	skynet.dispatch("lua", function(_, _, command, ...)
  		cs(function (...)
  			if CMD[command] then
  				skynet.ret(skynet.pack(CMD[command](...)))
  				return
  			else
  				if unionInfo and unionInfo[submethod] then
  					if command == 'dismiss' then
  						unionInfo:dismiss(...)
  						return
  					end
  					local result = unionInfo[submethod](unionInfo, ...)
  					skynet.ret(skynet.pack(result))
  					return
  				end
  			end
  			skynet.error("uniond commond error cmd=", command)
  		end)
  	end)
  	skynet.register("UNIOND")
  	csvdb = sharedata.query("csvdata")
  	cs = queue()
  end
  
  skynet.start(__init__)