AdvTask.lua 7.68 KB
local AdvTask = {}


local advTaskChange = {}
function AdvTask.bind(Adv)
	Adv.TaskType = {
		Arrive 		= 1, --到达N层
		Kill		= 2, --消灭指定怪物N个
		Item		= 3, --获得指定道具N个
		Choose		= 4, --完成指定抉择点N次
		Shop		= 5, --和指定商人交易N次
		Build		= 6, --和指定建筑交互N次
		KillAll		= 7, --消灭本层所有怪物
	}

	-- 检查任务状态 在新领取任务的时候回进行检查
	local function checkTaskStatus(self, taskData, status, count, cond)
		count = count or 0
		if status == -1 or not taskData then
			return
		end
		local function checkCondValue()
			if taskData.value2 ~= "" then
				local conds = taskData.value2:toArray(true, "=")
				for _, one in pairs(conds) do
					if one == cond then
						return true
					end
				end
			end
			return true
		end	

		local checkTaskFunc = {}
		checkTaskFunc[Adv.TaskType.Arrive] = function()
			if self.level > status then
				if self.level >= taskData.value1 then
					return -1
				else
					return self.level
				end
			end
		end
		checkTaskFunc[Adv.TaskType.KillAll] = function()
			if  #self.battle.player:getTeam(2, nil, nil, true) == 0 then
				return -1
			end
		end

		if checkTaskFunc[taskData.condition] then
			status = checkTaskFunc[taskData.condition]()
		else
			if count == 0 or not checkCondValue() then return end --没有变化
			status = status + count
			if status >= taskData.value1 then
				status = -1
			end
		end
		return status
	end 

	function Adv:initLayerTask()
		self.advTask = {}
		advTaskChange.t = true
		advTaskChange.m = true
		if not next(self.advMTask) then
			self.advMTask = {
				id = 1,
				status = 0,
				lock = 1,
			}
		end
		self.advMTask.id = self.advMTask.id or 1
		local mainTaskData = csvdb["adv_questCsv"][self.advMTask.id]
		if mainTaskData and self.advMTask.lock then 
			if self.chapterId == mainTaskData.chapter and self.level >= mainTaskData.level then --到达指定关卡解锁当前任务
				self.advMTask.lock = nil
				self.advMTask.status = 0
				local ts = checkTaskStatus(self, mainTaskData, self.advMTask.status, 0)
				if ts then
					self.advMTask.status = ts
				end
			end
		end
	end

	function Adv:checkTask(taskType, count, cond)
		local chapter = self.chapterId
		for taskId, status in pairs(self.advTask) do
			local taskData = csvdb["event_questCsv"][taskId]
			if taskData and taskData.levelchapter == chapter and taskType == taskData.condition then
				local ts = checkTaskStatus(self, taskData, status, count, cond)
				if ts then
					self.advTask[taskId] = ts
					advTaskChange.t = true
				end
			end
		end

		local mainTaskData = csvdb["adv_questCsv"][self.advMTask.id]
		if not self.advMTask.lock and mainTaskData and mainTaskData.chapter == chapter and taskType == mainTaskData.condition then
			local ts = checkTaskStatus(self, mainTaskData, self.advMTask.status, count, cond)
			if ts then
				self.advMTask.status = ts
				advTaskChange.m = true
			end
		end
	end

	-- 点击任务地块领取任务
	function Adv:addTask(taskId)
		local taskData = csvdb["event_questCsv"][taskId]
		if taskData then
			self.advTask[taskId] = 0
			local ts = checkTaskStatus(self, taskData, self.advTask[taskId], 0)
			if ts then
				self.advTask[taskId] = ts
			end

			advTaskChange.t = true
			return true
		end
	end

	--完成层任务
	function Adv:finishTask(taskId)
		local ok, reward
		if self.advTask[taskId] and self.advTask[taskId] == -1 then
			local taskData = csvdb["event_questCsv"][taskId]
			if not taskData then return end
			if taskData.reward == 1 then
				reward = self.owner:award(taskData.rewardValue)
			end
			self.advTask[taskId] = nil
			ok = true
			advTaskChange.t = true
		end
		return ok, reward
	end

	-- 完成主线任务
	function Adv:finishMTask()
		local ok, reward
		if self.advMTask.status == -1 then  --已完成带领取
			local mainTaskData = csvdb["adv_questCsv"][self.advMTask.id]
			if not mainTaskData then return end
			if mainTaskData.reward == 1 then
				reward = self.owner:award(mainTaskData.rewardValue)
			end
			self.advMTask.id = self.advMTask.id + 1
			self.advMTask.status = 0
			local nextTaskData = csvdb["adv_questCsv"][self.advMTask.id]
			if not nextTaskData or self.chapterId ~= nextTaskData.chapter or self.level < nextTaskData.level then
				self.advMTask.lock = true
			else
				local ts = checkTaskStatus(self, nextTaskData, self.advMTask.status, 0)
				if ts then
					self.advMTask.status = ts
				end
			end
			ok = true
			advTaskChange.m = true
		end
		return ok, reward
	end

	function Adv:updateTask(notNotify)
		local properties = {}
		if advTaskChange.t then
			properties.advTask = self.advTask
		end
		if advTaskChange.m then
			properties.advMTask = self.advMTask
		end
		if next(properties) then
			self.owner:updateProperties(properties, notNotify)
		end
		advTaskChange = {}
	end



	------ 冒险成就 ------------
	Adv.AchievType = {
		StartBattle 		= 1, --累计挑战N次
		OverWin				= 2, --通关N次M层
		TaskLayer			= 3, --完成每层任务N次
		UseItem				= 4, --使用道具N次
		GetItem				= 5, --获得道具N个
		GetMWeapon			= 6, --获得神器N个 -todo
		Build				= 7, --完成建筑N个
		Choose				= 8, --完成事件N个
		Shop				= 9, --完成商店N次
		LinkChoose			= 10, --完成连锁事件N次
		Trap				= 11, --触发陷阱N次
		Kill				= 12, --消灭怪物N个
		EnterILayer			= 13, --进入夹层N次
		KillByBuff			= 14, --使用BUFF消灭敌人N个
	}



	local advAchievChange = {}

	local function insertChange(chapterId, taskId, value)
		table.insert(advAchievChange, {type = "advAchiev", field = {chapterId, taskId}, value = value})
	end

	function Adv:checkAchievement(taskType, count, cond)
		local advAchiev = self.owner:getProperty("advAchiev")[self.chapterId] or {}
		for taskId , data in pairs(csvdb["adv_achievementCsv"][self.chapterId] or {}) do
			local oldStatus = advAchiev[taskId] or 0
			if oldStatus ~= -1 and data.type == taskType then
				local status
				local checkTaskFunc = {}
				-- checkTaskFunc[Adv.AchievType.] = function()

				-- end
				if checkTaskFunc[taskType] then
					status = checkTaskFunc[taskType]()
				else
					if count ~= 0 and (data.value2 == 0 or data.value2 == cond) then 
						status = oldStatus + count
					end
				end
				if (status or -1) >= data.value1 then
					status = -1
				end
				if status ~= oldStatus then
					insertChange(self.chapterId, taskId, status)
					if status == -1 then
						local ptcount = (self.owner:getProperty("advAchiev")[self.chapterId] or {})[-1] or 0
						ptcount = ptcount + data.pt
						insertChange(self.chapterId, -1, ptcount)
					end
				end

			end
		end
	end

	-- --说不用领取 注释掉
	-- function Adv:finishAchievement(chapterId, taskId)
	-- 	local achievData = (csvdb["adv_achievementCsv"][chapterId] or {})[taskId]
	-- 	local status = (self.owner:getProperty("advAchiev")[chapterId] or {})[taskId] or -1

	-- 	if status >= achievData.value1 then
	-- 		insertChange(chapterId, taskId, -1)
	-- 		local count = (self.owner:getProperty("advAchiev")[chapterId] or {})[-1] or 0
	-- 		count = count + achievData.pt
	-- 		insertChange(chapterId, -1, count)
	-- 		return true
	-- 	end
	-- end

	function Adv:getAchievementReward(chapterId, taskId)
		local count = (self.owner:getProperty("advAchiev")[chapterId] or {})[-1] or 0
		local achievData = (csvdb["adv_achievement_rewardCsv"][chapterId] or {})[taskId]
		local status = ((self.owner:getProperty("advAchiev")[chapterId] or {})["pts"] or {})[taskId] or 0
		if status == -1 or count < achievData.pt then return end

		local reward = self.owner:award(achievData.reward)
		insertChange(chapterId, taskId, -1)
		return true, reward
	end

	function Adv:updateAchievement(notNotify)
		if not next(advAchievChange) then return end
		self.owner:changeUpdates(advAchievChange, notNotify)
	end

end


return AdvTask