DinerAction.lua 9.02 KB
local ipairs = ipairs
local table = table
local math = math
local redisproxy = redisproxy
local MsgPack = MsgPack

local _M = {}

function _M.addSellRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)

	local slot = msg.slot
	if math.illegalNum(slot, 1, role.dinerData:getMaxSlots()) then
		return 1
	end
	slot = tostring(slot)

	local dish = msg.dish
	local dishSet = csvdb["diner_dishCsv"][dish]
	if not dishSet then
		return 2
	end
	local dishLevel = role.dinerData:getProperty("dishTree"):getv(dish, 0)
	if dishLevel == 0 then
		return 3
	end
	local dishData = dishSet[dishLevel]
	if not dishData then
		return 4
	end

	local calSell = role.dinerData:updateSell(slot, true) or {
		deltaCount = 0,
		deltaTime = 0,
		lastCount = 0,
	}
	local count = msg.count
	local maxDishCount = role.dinerData:getMaxDishs()
	if math.illegalNum(count + calSell.lastCount, 1, maxDishCount) then
		return 5
	end

	local cost = dishData.material:toNumMap()
	for _, n in pairs(cost) do
		n = n * count
	end
	if not role:checkItemEnough(cost) then
		return 6
	end

	role:costItems(cost)
	role.dinerData:updateSell(slot)

	local dirty = false
	for type, value in pairs(cost) do
		if role.dishData:checkDinerTask(DinerTask.UseMaterial, value, type, nil, true) then
			dirty = true
		end
	end
	if role.dishData:checkDinerTask(DinerTask.AddDish, count, dish, nil, true) then
		dirty = true
	end
	if dirty then
		role.dishData:notifyUpdateProperty("order", role.dinerData:getProperty("order"))
	end

	local sells = json.decode(role.dinerData:getProperty("sells"))
	if not sells[slot] then
		sells[slot] = {
			dish = dish,
			level = dishLevel,
			reward = "",
			count = 0,
		}
	end
	local sell = sells[slot]
	sell.count = sell.count + count
	sell.time = skynet.timex() - calSell.deltaTime
	sell.hot = role.dinerData:getProperty("hot"):getv(sell.dish, 0)

	role.dinerData:updateProperty({field = "sells", value = json.encode(sells)})
	SendPacket(actionCodes.Diner_addSellRpc, "")
	
	return true
end

function _M.removeSellRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)

	local slot = msg.slot
	if math.illegalNum(slot, 1, role.dinerData:getMaxSlots()) then
		return 1
	end
	slot = tostring(slot)
	local sells = json.decode(role.dinerData:getProperty("sells"))
	local sell = sells[slot]
	if not sell then
		return 2
	end

	local dish = sell.dish
	local dishSet = csvdb["diner_dishCsv"][dish]
	if not dishSet then
		return 3
	end
	local dishLevel = role.dinerData:getProperty("dishTree"):getv(dish, 0)
	if dishLevel == 0 then
		return 4
	end
	local dishData = dishSet[dishLevel]
	if not dishData then
		return 5
	end

	local reward = {}
	local cost = dishData.material:toNumMap()
	for k, n in pairs(cost) do
		local sum = n*sell.count
		role:addItem({itemId = k,count = sum})
		reward[k] = sum
	end
	sells[slot] = nil
	role.dinerData:updateProperty({field = "sells", value = json.encode(sells)})
	SendPacket(actionCodes.Diner_removeSellRpc, MsgPack.pack({reward = reward}))
	return true
end

function _M.getSellRewardRpc( agent, data )
	local role = agent.role
	local dirty = false

	local reward = ""
	local sells = json.decode(role.dinerData:getProperty("sells"))
	for slot, sell in pairs(sells) do
		role.dinerData:updateSell(slot)
		local rewards = sell.reward:toNumMap()
		for k,v in pairs(rewards) do
			reward = reward:incrv(k, v)
		end
		sells[slot].reward = ""

		if rewards[ItemId.Gold] and rewards[ItemId.Gold] > 0 then
			if role.dishData:checkDinerTask(DinerTask.DishWithGold, rewards[ItemId.Gold], sell.dish, nil, true) then
				dirty = true
			end
		end
	end

	role.dinerData:updateProperty({field = "sells", value = json.encode(sells)})
	for k, v in pairs(reward:toNumMap()) do
		role:addItem({itemId = k,count = v})
	end
	if dirty then
		role.dishData:notifyUpdateProperty("order", role.dinerData:getProperty("order"))
	end

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

function _M.levelUpRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)

	local index = msg.index
	local buildingSet = csvdb["diner_buildingCsv"][index]
	if not buildingSet then
		return
	end

	local buildL = role.dinerData:getProperty("buildL")
	local curLevel = buildL:getv(index, 1)
	if curLevel >= #buildingSet then
		return
	end
	local buildingData = buildingSet[curLevel]
	if not buildingData then
		return
	end
	if buildingData.upLimit ~= "" then
		local id, level = buildingData.upLimit:match("(%d+)=(%d+)")
		if buildL:getv(tonumber(id), 1) < tonumber(level) then
			return
		end
	end

	local cost = buildingData.starCost:toNumMap()
	if not role:checkItemEnough(cost) then
		return 
	end

	role:costItems(cost)
	role.dinerData:updateProperty({field = "buildL", value = buildL:setv(index, curLevel + 1)})

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

function _M.talentUpRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)

	local dish = msg.dish
	local dishSet = csvdb["diner_dishCsv"][dish]
	if not dishSet then
		return
	end

	local dishTree = role.dinerData:getProperty("dishTree")
	local dishLevel = dishTree:getv(dish, 0)
	local cost = {}
	local treePoint = {}
	if dishLevel == 0 then
		local dishData = dishSet[1]
		if dishData.unlock_tree > 0 then
			if dishTree:getv(dishData.unlock_tree, 0) == 0 then
				return
			end
		end
		if dishData.unlock_carbon > 0 then
			local hangPass = role:getProperty("hangPass")
			if not hangPass[dishData.unlock_carbon] then
				return
			end
		end
		cost = globalCsv.diner_sell_dish_talent_unlock:toNumMap()
	else
		if dishLevel >= #dishSet then
			return
		end
		local dishData = dishSet[dishLevel]
		cost = dishData.tree_material:toNumMap()
		treePoint = dishData.tree_point:toNumMap()
	end
	if not role:checkItemEnough(cost) then
		return
	end

	role:costItems(cost)
	role.dinerData:updateProperty({field = "dishTree", value = dishTree:setv(dish, dishLevel + 1)})
	if next(treePoint) then
		role:award(treePoint)
	end

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

function _M.skillUpRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)

	local skill = msg.skill
	local skillSet = csvdb["diner_skillCsv"][skill]
	if not skillSet then
		return
	end

	local skillTree = role.dinerData:getProperty("skillTree")
	local skillLevel = skillTree:getv(skill, 1)
	local skillData = skillSet[skillLevel]
	if not skillData then
		return
	end
	if skillData.unlock ~= "" then
		local id, level = skillData.unlock:math("(%d+)=(%d+)")
		if skillTree:getv(tonumber(id), 1) < tonumber(level) then
			return
		end
	end
	local cost = skillData.cost:toNumMap()
	if not role:checkItemEnough(cost) then
		return
	end

	role:costItems(cost)
	role.dinerData:updateProperty({field = "skillTree", value = skillTree:setv(skill, skillLevel + 1)})

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

function _M.lockTaskRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local index = msg.index

	if math.illegalNum(index, 1, 7) then
		return 1
	end
	local orders = json.decode(role.dinerData:getProperty("order"))
	local order = orders[index]
	if not order then
		return 2
	end
	if order.lock ~= 0 then
		return 3
	end

	order.lock = 1
	role.dinerData:updateProperty({field = "order", value = json.encode(orders)})
	SendPacket(actionCodes.Diner_lockTaskRpc, '')
	return true
end

function _M.finishTaskRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local index = msg.index

	if math.illegalNum(index, 1, 7) then
		return 1
	end
	local orders = json.decode(role.dinerData:getProperty("order"))
	local order = orders[index]
	if not order then
		return 2
	end
	if order.lock == 2 then
		return 3
	end
	local taskSet = csvdb["diner_questCsv"][order.lv]
	if not taskSet then
		return 4
	end
	local taskData = taskSet[order.id]
	if not taskData then
		return 5
	end
	if order.n <= taskData.value then
		return 6
	end

	order.lock = 2
	role.dinerData:updateProperty({field = "order", value = json.encode(orders)})
	for type, count in pairs(taskData.reward:toNumMap()) do
		role:addItem({itemId = type, count = count})
	end

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

function _M.getSpecialTaskRpc( agent, data )
	local role = agent.role
	local msg = MsgPack.unpack(data)

	local cost = {[DinerSpTask] = 1}
	if not role:checkItemEnough(cost) then
		return 1
	end

	local orders = json.decode(role.dinerData:getProperty("order"))
	if #orders > 6 then
		return 2
	end

	local taskLevel = role.dinerData:getProperty("buildL"):getv(5, 1)
	local taskData = csvdb["diner_questCsv"][taskLevel]
	if not taskData then
		return 3
	end
	local taskPool = {}
	for _, data in pairs(taskData) do
		if data.rarity >= 3 then
			table.inser(taskPool, data)
		end
	end
	if #taskPool == 0 then
		return 4
	end

	role:costItems(cost)

	local index = math.randWeight(taskPool, "chance")
	local data = taskPool[index]

	table.insert(orders, {lv = taskLevel, id = data.id, n = 0, lock = 0})
	role.dinerData:updateProperty({field = "order", value = json.encode(orders)})
	
	SendPacket(actionCodes.Diner_getSpecialTaskRpc, '')	
	return true
end


return _M