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

165 lines
6.6 KiB
Lua
Raw 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 LevelTask = oo.class(task)
LevelTask.MaxShowItem = 4 --任务最大显示多少个
--检查一下玩家是否有新的任务
function LevelTask:LoginInitData( player )
if not player:IsUnlockSystem( dataType.UnlockSystem_LevelTask ) then
return
end
local cfgTask = skynet.server.gameConfig:GetPlayerAllCfg( player , "LevelTask") --任务配置
for k, v in pairs( cfgTask ) do
if not player.gameData.levelTask[ v.id ] then --and (44 == v.id or 46 == v.id) then
player.gameData.levelTask[ v.id ] = {}
player.gameData.levelTask[ v.id ].id = v.id
player.gameData.levelTask[ v.id ].type = v.taskType --任务类型
player.gameData.levelTask[ v.id ].target = v.taskTarget --任务目标
player.gameData.levelTask[ v.id ].progress =0 --当前进度
player.gameData.levelTask[ v.id ].status =self.TaskStatus_NoComplete --是否完成
player.gameData.levelTask[ v.id ].historyId = {}
end
end
end
--任务列表显示
function LevelTask:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SLevelTaskShow", 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 , 19 )
skynet.server.msgTips:Add( player , 19 , tipsCount)
skynet.server.msgTips:Reduce( player , 93 )
data.taskInfo = showTaskData
s2cData.cmd = pb.enum("MsgType","CMD_S2C_LevelTaskShow")
s2cData.data = assert(pb.encode("S2CLevelTaskShow", data))
end
--任务完成
function LevelTask:Accomplish( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SLevelTaskAccomplish", c2sData.data ))
local data = {}
local taskId = c2sData.data.taskId
for k, v in pairs( player.gameData.levelTask ) do
if taskId == k and self.TaskStatus_AlreadyComplete == v.status then
--只有状态为任务已完成才能领取
local cfgOne = skynet.server.gameConfig:GetPlayerCurCfg( player , "LevelTask" , v.id)
local eventId = pb.enum("EnumMoneyChangeEventID","EventID_77")
player:GiveReward( cfgOne.rewardId , eventId)
v.status = self.TaskStatus_AlreadyGet
player:AddExpCount( cfgOne.expNum , true )
skynet.server.msgTips:Reduce( player , 19 )
log.debug(string.format("玩家 %d 等级任务 任务完成 ID %d " , player.basicInfo.userID , taskId ))
break
end
end
data.taskInfo = self:GetShowTaskData( player )
s2cData.cmd = pb.enum("MsgType","CMD_S2C_LevelTaskAccomplish")
s2cData.data = assert(pb.encode("S2CLevelTaskAccomplish", data))
end
--修改任务
function LevelTask:Modify( player , type , count , taskTarge , goodsId )
if not player:IsUnlockSystem( dataType.UnlockSystem_LevelTask ) then
return
end
local level = player.gameData.level
taskTarge = taskTarge or 0
goodsId = goodsId or 0
log.debug(string.format("玩家 %d 等级任务 任务修改 类型 %d " , player.basicInfo.userID , type ))
local maxProgress = 0
for k1, v1 in pairs( player.gameData.levelTask ) do
local cfgOne = skynet.server.gameConfig:GetPlayerCurCfg( player , "LevelTask" , v1.id)
if cfgOne then
maxProgress = cfgOne.value
if type == v1.type and level >= cfgOne.taskLevel 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 , 19 )
end
log.debug(string.format("玩家 %d 等级任务 任务类型 %d 完成 " , player.basicInfo.userID , v1.type ))
else
log.debug(string.format("玩家 %d 等级任务 任务类型 %d 最新数量 %d " , player.basicInfo.userID , v1.type , v1.progress))
end
end
end
else
log.warning(string.format("玩家 %d 等级任务 任务修改 类型 %d 失败" , player.basicInfo.userID , type ))
end
end
end
--获取显示的任务数据
function LevelTask:GetShowTaskData( player )
local taskInfo = {}
local count = 0
local tipsCount = 0
for k, v in pairs( player.gameData.levelTask ) do
if count >= self.MaxShowItem then
break
end
--如果玩家已经完成了,并且可以显示就需要发送红点信息
if self.TaskStatus_AlreadyComplete == v.status then
tipsCount = tipsCount + 1
end
--已领取的就不用再显示了
--if self.TaskStatus_AlreadyGet ~= v.status and v.id == 26 or v.id == 27 then
if self.TaskStatus_AlreadyGet ~= v.status then
table.insert( taskInfo , { id = k , progress = v.progress , status = v.status })
count = count + 1
end
end
return taskInfo
end
--该任务是否显示中
function LevelTask: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.levelTask = LevelTask
return LevelTask