TowerAction.lua 4.43 KB
local ipairs = ipairs
local table = table
local math = math
local redisproxy = redisproxy
local MsgPack = MsgPack


local _M = {}


function _M.roleFormatRpc(agent , data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local towerTeam = role:getProperty("towerF")
	for slot, heroId in pairs(msg.heros) do
		if not role.heros[heroId] then
			return
		end
	end
	local supports = {}
	for slot, support in pairs(msg.supports) do
		if slot ~= 1 and slot ~= 2 then return end
		local level = role.dinerData:getProperty("dishTree"):getv(support, 0)
		if level <= 0 then return end
		supports[slot] = support
	end
	table.clear(towerTeam)
	towerTeam.heros = {}
	for slot, heroId in pairs(msg.heros) do
		towerTeam.heros[slot] = heroId
	end
	towerTeam.leader = msg.leader
	towerTeam.supports = supports


	role:updateProperty({field = "towerF", value = towerTeam}) 
	SendPacket(actionCodes.Tower_roleFormatRpc, '')
	return true
end


local function getUpdateTime(lastCount, lastTime)
	local nextCount, nextTime = lastCount, skynet.timex()
	if lastTime then
		local addCount = math.floor((skynet.timex() - lastTime) / (globalCsv.tower_count_up * 60))
		nextCount = math.min(lastCount + addCount, globalCsv.tower_count_limit)
		nextTime = lastTime + addCount * (globalCsv.tower_count_up * 60)
	end
	return nextCount, nextTime
end

function _M.startBattleRpc(agent, data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local id = msg.id
	
	if not role:isFuncUnlock(FuncUnlock.Tower) then return end

	local towerInfo = role:getProperty("towerInfo")

	if towerInfo.l ~= id then return end  -- 层数不对

	if not csvdb["tower_battleCsv"][id] then return end
	local curCount, nextTime = getUpdateTime(towerInfo.c, towerInfo.t)
	if curCount < 1 then return end -- 没有次数返回

	--搞起来
	towerInfo.c = curCount - 1
	if curCount == globalCsv.tower_count_limit then --满的情况下的消耗了 恢复时间从当下开始
		nextTime = skynet.timex()
	end
	towerInfo.t = nextTime
	local key = tostring(math.random())
	towerInfo.k = key

	role:updateProperty({field = "towerInfo", value = towerInfo})
	role:checkTaskEnter("TowerBattle", {level = towerInfo.l})

	SendPacket(actionCodes.Tower_startBattleRpc, '')
	return true
end

function _M.endBattleRpc(agent, data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local id = msg.id
	local key = msg.key
	local passTime = msg.passTime

	local towerInfo = role:getProperty("towerInfo")
	if towerInfo.l ~= id or not towerInfo.k or towerInfo.k ~= key then 
		SendPacket(actionCodes.Tower_endBattleRpc, MsgPack.pack({errorCode = 1}))
		return true
	end
	local curTower = csvdb["tower_battleCsv"][id] 
	if not curTower then return 2 end

	local curCount, nextTime = getUpdateTime(towerInfo.c, towerInfo.t)


	local reward, change
	if msg.starNum and msg.starNum > 0 then --win
		curCount = math.min(curCount + 1, globalCsv.tower_count_limit) -- 返还次数
		--排行榜
		role:setTowerRank(towerInfo.l)

		towerInfo.l = towerInfo.l + 1
		reward, change = role:award(curTower.reward, {log = {desc = "towerBattle", int1 = id}})
		role:checkTaskEnter("TowerPass", {level = towerInfo.l - 1})
	end

	towerInfo.c = curCount
	towerInfo.t = nextTime
	towerInfo.k = nil
	role:updateProperty({field = "towerInfo", value = towerInfo})

	SendPacket(actionCodes.Tower_endBattleRpc, MsgPack.pack({reward = reward, change = change}))
	return true
end

function _M.bugCountRpc(agent, data)
	local role = agent.role

	local towerInfo = role:getProperty("towerInfo")
	local curCount, nextTime = getUpdateTime(towerInfo.c, towerInfo.t)
	local needCount = globalCsv.tower_count_limit - curCount
	if needCount > 0 then -- 补充
		local cost = globalCsv.tower_chance_item:toNumMap()
		for k , v in pairs(cost) do
			cost[k] = v * needCount
		end
		if not role:checkItemEnough(cost) then return end
		role:costItems(cost, {log = {desc = "towerCount"}})
		curCount = globalCsv.tower_count_limit
	end
	towerInfo.c = curCount
	towerInfo.t = nextTime
	role:updateProperty({field = "towerInfo", value = towerInfo})

	SendPacket(actionCodes.Tower_bugCountRpc, '')
	return true
end

function _M.rankRpc(agent , data)
	local role = agent.role
	SendPacket(actionCodes.Tower_rankRpc, MsgPack.pack(role:getTowerRank()))
	return true
end

function _M.rankInfoRpc(agent , data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local roleId = msg.roleId
	SendPacket(actionCodes.Tower_rankInfoRpc, MsgPack.pack({format = role:getTowerRankOneInfo(roleId)}))
	return true
end

return _M