HeroAction.lua 3.9 KB
local ipairs = ipairs
local table = table
local math = math
local next = next
local string = string
local redisproxy = redisproxy
local MsgPack = MsgPack
local getRandomName = getRandomName
local mcast_util = mcast_util
local string_format = string.format
local tonumber = tonumber
local require = require
local table_insert = table.insert
local tconcat = table.concat

local _M = {}
function _M.levelUpRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local hero = role.heros[msg.id]
	if not hero then return end

	if hero:getProperty("level") >= hero:getMaxLevel() then return end
	local curData = csvdb["unit_expCsv"][hero:getProperty("level")]
	local cost = {[ItemId.Exp] = curData.exp, [ItemId.Gold] = curData.gold}
	if not role:checkItemEnough(cost) then return end
	role:costItems(cost, {})
	hero:updateProperty({field = "level", delta = 1})
	
	SendPacket(actionCodes.Hero_levelUpRpc, '')
	return true
end

function _M.breakRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local hero = role.heros[msg.id]
	if not hero then return end

	if hero:getProperty("level") < hero:getMaxLevel() then return end
	if hero:getProperty("breakL") >= #csvdb["unit_breakCsv"] then return end
	local curData = csvdb["unit_breakCsv"][hero:getProperty("breakL")]
	local cost = {[ItemId.BreakCost] = curData.cost, [ItemId.Gold] = curData.gold}
	if not role:checkItemEnough(cost) then return end
	role:costItems(cost, {})
	hero:updateProperty({field = "breakL", delta = 1})
	
	SendPacket(actionCodes.Hero_breakRpc, '')
	return true
end

function _M.wakeRpc(agent, data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local hero = role.heros[msg.id]
	if not hero then return end
	if hero:getProperty("wakeL") >= #csvdb["unit_wakeCsv"] then return end
	local cost = {[hero:getProperty("type")] = csvdb["unit_wakeCsv"][hero:getProperty("wakeL")].cost}
	local isEnough, less = role:checkItemEnough(cost)
	if not isEnough then
		cost[ItemId.HeroFC[csvdb["unitCsv"][hero:getProperty("type")].rare]] = less[hero:getProperty("type")]
		if not role:checkItemEnough(cost) then return end
	end
	role:costItems(cost, {})
	hero:updateProperty({field = "wakeL", delta = 1})

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

function _M.skillUpRpc(agent, data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local index = msg.skillIdx -- 第几个技能 -- 1  2  3  
	local hero = role.heros[msg.id]
	if not hero then return end
	local curLevel = hero:getSkillLevel(index)
	if hero:getLSPoint() < 0 or curLevel >= #hero:getSkillData(index) then return end
	hero:updateProperty({field = "skillL", value = hero:getProperty("skillL"):setv(index, curLevel + 1)})

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

function _M.talentRpc(agent, data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local hero = role.heros[msg.id]
	if not hero then return end

	local index = msg.index -- 第几个天赋
	local need = {[1] = 1, [2] = 1, [3] = 1, [4] = 1}
	if not need[index] then return end

	local talent = hero:getProperty("talent")
	local curStage = talent:getv(0, 1)
	if curStage > csvdb["unit_breakCsv"][hero:getProperty("breakL")].talent then return end
	
	local curData = csvdb["unit_talentCsv"][curStage] 
	if not curData then return end

	local level = talent:getv(index, 0)
	if level >= #curData then return end

	local talentData = curData[level]
	if not talentData then return end
	local cost = talentData.cost:toNumMap()
	if not role:checkItemEnough(cost) then return end
	role:costItems(cost, {})
	talent = talent:incrv(index, 1)

	--是否进阶
	local max = true
	for i = 1, 4 do
		if talent:getv(i, 0) < #curData then
			max = false
			break
		end
	end
	if max then
		talent = talent:setv(0, curStage + 1)
		for i = 1, 4 do
			talent = talent:setv(i, 0)
		end
	end
	hero:updateProperty({field = "talent", value = talent})
	SendPacket(actionCodes.Hero_talentRpc, '')
	return true
end

return _M