pvpd.lua 3.04 KB
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__)