Blame view

src/services/pvpd.lua 3.04 KB
c779be0c   zhouhaihai   pvp 中心服务
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
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
  local skynet = require "skynet"
  local json = require("shared.json")
  local redisproxy = require("shared.redisproxy")
  local cluster = require "skynet.cluster"
  
  require "shared.init"
  require "utils.init"
  require "RedisKeys"
  require "skynet.manager"
  globalCsv = require "csvdata/GlobalDefine"
  require "GlobalVar"
  
  
  
  
  
  function rpcRole(roleId, funcName, ...)
  	local fields = ...
  	local agent = datacenter.get("agent", roleId)
  	if agent and agent.serv then
  		if funcName == "getProperties" then
  			return skynet.call(agent.serv, "role", funcName, fields)
  		else
  			return skynet.call(agent.serv, "role", funcName, ...)
  		end
  	else
  		local roleCross = require("models.RoleCross")
  		if funcName == "getProperties" then
  			return roleCross.handle(funcName, roleId, fields)
  		else
  			return roleCross.handle(funcName, roleId, ...)
  		end
  	end
  end
  
  local function upLoadTeams()
  	local chatd = cluster.query("center", "chatd")
  	if not chatd then
  		return
  	end
  	return pcall(cluster.call, "center", chatd, "test", {serverId = 123, hehe = "asd"})
  end
  
  
  
  
  local function update()
  	local now = skynet.timex()
  	local resetData = csvdb["time_resetCsv"][TimeReset.PvpCross]
  	
  	local oldRound = tonum(redisproxy:hget("pvp_cross", "round"), -100)
  
  	local startTime = START_RESET_TIME + curRound  * resetData.interval + resetData.start
  	local endTime = startTime + (resetData.duration == 0 and resetData.interval or math.min(resetData.interval, resetData.duration))
  	local nextStartTime = startTime + resetData.interval
  
  	local updateTime  = math.max((nextStartTime - now) / 2, CHECK_PVP_STATUS_INTERVAL)
  	-- 已经 上传过阵容了
  	if curRound ~= oldRound then
  		-- 跨服竞技场已经结束了
  		if now >= endTime then
  			redisproxy:hset("pvp_cross", "round", curRound)
  		else
  			-- 上传信息
  			local status, back = upLoadTeams()
  			if status and back == "success" then --上传成功
  				redisproxy:hset("pvp_cross", "round", curRound)
  			else
  				updateTime = CHECK_PVP_STATUS_INTERVAL
  			end
  		end
  	end
  	return updateTime
  end
  
  
  local CMD = {}
  
  function CMD.start()
  	-- check_pvp_update()
  end
  
  local function getDBKey()
  	local resetData = csvdb["time_resetCsv"][TimeReset.PvpCross]
  	local curRound = math.floor((skynet.timex() - START_RESET_TIME - resetData.start) / resetData.interval)
  	local idx = 1
  	if curRound % 2 == 1 then
  		idx = 2 
  	end
  	return RANK_PVP_HIGHT_KEY[idx]
  end
  
  function CMD.loadRoleInfo(roleIds)
  	roleIds = roleIds or {}
  	local infos = {}
  	for _, roleId in ipairs(roleIds) do
  		infos[roleId] = rpcRole(roleId, "pvpHInfo")
  	end
  	return infos
  end
  
  function CMD.loadTeams()
  	local dbKey = getDBKey()
  	local redret = redisproxy:zrevrange(dbKey, 0, 15)
  	local roleIds = {}
  	for _, roleId in ipairs(redret) do
  		table.insert(roleIds, tonumber(roleId))
  	end
  
  	local infos = CMD.loadRoleInfo(roleIds)
  
  	return {roleIds = roleIds, infos = infos}
  end
  
  local function __init__()
  	skynet.dispatch("lua", function(_, _, command, ...)
  		local f = CMD[command]
  		if f then
  			skynet.ret(skynet.pack(f(...)))
  		end
  	end)
  	redisd = skynet.localname(".REDIS")
  	skynet.register(".PVPCROSS")
  end
  
  skynet.start(__init__)