HomeServer/Server/AllServer/GameServer/Task/NpcTask.lua
2024-11-20 15:41:37 +08:00

166 lines
6.2 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local skynet = require "skynet"
local oo = require "Class"
local task = require "Task"
local log = require "Log"
local dataType = require "DataType"
local pb = require "pb"
local NpcTask = oo.class(task)
NpcTask.MaxShowItem = 4 --任务最大显示多少个
--检查一下玩家是否有新的任务
function NpcTask:CheckNew( player )
if not player:IsUnlockSystem( dataType.UnlockSystem_NpcTask ) then
return
end
local cfgTask = skynet.server.gameConfig:GetPlayerAllCfg( player , "NPCTask") --任务配置
for k, v in pairs( cfgTask ) do
if not player.gameData.npcTask[ v.id ] then
player.gameData.npcTask[ v.id ] = {}
player.gameData.npcTask[ v.id ].id = v.id
player.gameData.npcTask[ v.id ].type = v.taskType --任务类型
player.gameData.npcTask[ v.id ].target = v.seedId --任务目标
player.gameData.npcTask[ v.id ].progress =0 --当前进度
player.gameData.npcTask[ v.id ].status =self.TaskStatus_NoComplete --是否完成
player.gameData.npcTask[ v.id ].historyId = {}
end
end
end
--任务列表显示
function NpcTask:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SNpcTaskShow", c2sData.data ))
local data = {}
local showTaskData = self:GetShowTaskData( player )
local tipsCount = 0 --获取最新的红点数量
for k, v in pairs( showTaskData ) do
if self.TaskStatus_AlreadyComplete == v.status then
tipsCount = tipsCount + 1
end
end
skynet.server.msgTips:Reset( player , 70 )
skynet.server.msgTips:Add( player , 70 , tipsCount)
skynet.server.msgTips:Reduce( player , 91 )
data.taskInfo = showTaskData
s2cData.cmd = pb.enum("MsgType","CMD_S2C_NpcTaskShow")
s2cData.data = assert(pb.encode("S2CNpcTaskShow", data))
end
--任务完成
function NpcTask:Accomplish( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SNpcTaskAccomplish", c2sData.data ))
local data = {}
local taskId = c2sData.data.taskId
for k, v in pairs( player.gameData.npcTask ) do
if taskId == k and self.TaskStatus_AlreadyComplete == v.status then
--只有状态为任务已完成才能领取
local cfgOne = skynet.server.gameConfig:GetPlayerCurCfg( player , "NPCTask" , v.id)
local eventId = pb.enum("EnumMoneyChangeEventID","EventID_78")
player:GiveReward( cfgOne.rewardId , eventId)
v.status = self.TaskStatus_AlreadyGet
player:AddExpCount( cfgOne.taskExp )
skynet.server.msgTips:Reduce( player , 70 )
log.debug(string.format("玩家 %d NPC任务 任务完成 ID %d " , player.basicInfo.userID , taskId ))
break
end
end
data.taskInfo = self:GetShowTaskData( player )
s2cData.cmd = pb.enum("MsgType","CMD_S2C_NpcTaskAccomplish")
s2cData.data = assert(pb.encode("S2CNpcTaskAccomplish", data))
end
--修改任务
function NpcTask:Modify( player , type , count , taskTarge , goodsId )
if not player:IsUnlockSystem( dataType.UnlockSystem_NpcTask )then
return
end
local level = player.gameData.level
taskTarge = taskTarge or 0
count = count or 1
goodsId = goodsId or 0
log.debug(string.format("玩家 %d NPC任务 任务修改 类型 %d " , player.basicInfo.userID , type ))
local maxProgress = 0
for k1, v1 in pairs( player.gameData.npcTask ) do
local cfgOne = skynet.server.gameConfig:GetPlayerCurCfg( player , "NPCTask" , v1.id)
if not cfgOne then
return
end
maxProgress = cfgOne.value
if type == v1.type and level >= cfgOne.beginCountLevel and self.TaskStatus_NoComplete == v1.status and taskTarge == v1.target then
local isAdd = true --是否能添加任务
if 0 ~= goodsId then
for k2, v2 in pairs( v1.historyId ) do
if goodsId == v2 then
isAdd = false
break
end
end
if isAdd then
--不存在商品ID 该任务保存商品ID
table.insert( v1.historyId , goodsId )
end
end
if isAdd then
v1.progress = v1.progress + count
--检查是否完成任务
if v1.progress >= maxProgress then
v1.status =self.TaskStatus_AlreadyComplete
v1.progress = maxProgress
if self:IsShow( player , k1 ) then
skynet.server.msgTips:Add( player , 70 )
end
log.debug(string.format("玩家 %d NPC任务 任务类型 %d 完成 " , player.basicInfo.userID , v1.type ))
end
end
end
end
end
--获取显示的任务数据
function NpcTask:GetShowTaskData( player )
local taskInfo = {}
local count = 0
local tipsCount = 0
local level = player.gameData.level
for k, v in pairs( player.gameData.npcTask ) do
if count >= self.MaxShowItem then
break
end
--如果玩家已经完成了,并且可以显示就需要发送红点信息
if self.TaskStatus_AlreadyComplete == v.status then
tipsCount = tipsCount + 1
end
--已领取的就不用再显示了
local cfgOneNPCTask = skynet.server.gameConfig:GetPlayerCurCfg( player , "NPCTask" , k )
if self.TaskStatus_AlreadyGet ~= v.status and level >= cfgOneNPCTask.taskLevel and 26 ~= v.id then --任务26暂时完不成就屏蔽了
table.insert( taskInfo , { id = k , progress = v.progress , status = v.status })
count = count + 1
end
end
return taskInfo
end
--该任务是否显示中
function NpcTask:IsShow( player , taskId )
local showTask = self:GetShowTaskData( player )
for k, v in pairs( showTask ) do
if taskId == v.id then
return true
end
end
return false
end
skynet.server.npcTask = NpcTask
return NpcTask