ecs/servers/game/logic/player_task.go

239 lines
6.6 KiB
Go
Raw Permalink Normal View History

2025-06-04 18:17:39 +08:00
package logic
2025-06-21 16:17:24 +08:00
import (
"ecs/proto/pb"
"ecs/servers/game/data"
)
type PlayerTask struct {
Id uint32 `bson:"id" key:"1"`
Status uint32 `bson:"status"` //任务状态见pb.AbleStatus - 0.不可提交(未完成)1.可提交(已完成)2.已提交(奖励已领取)
Okayed []bool `bson:"okayed"` //点赞
Process []uint32 `bson:"process"` //进度
}
func (this *PlayerTask) BuildMsgTask() *pb.Task {
return &pb.Task{Id: this.Id, Process: this.Process, Status: pb.AbleStatus(this.Status)}
}
func (this *Player) checkTaskCompetedAhead(task *PlayerTask, taskTable *data.Task) {
if pb.TaskType(taskTable.Type) != pb.TaskType_TaskMain && pb.TaskType(taskTable.Type) != pb.TaskType_TaskMainChapter {
return
}
var changed = false
var completed = true
for i := range taskTable.TaskArgs1 {
var arg1 = taskTable.TaskArgs1[i]
switch pb.TaskArgs(arg1) {
case pb.TaskArgs_TaskArgsId:
changed = this.processTask(task, taskTable, i, this.GetCounterTotal(pb.CounterType_TaskCompleted, uint64(taskTable.TaskArgs2[i]))) || changed
case pb.TaskArgs_TaskArgsType:
case pb.TaskArgs_TaskSignIn:
case pb.TaskArgs_TaskHeroBook:
case pb.TaskArgs_TaskHeroLevel:
case pb.TaskArgs_TaskHeroBreak:
case pb.TaskArgs_TaskEquipLevel:
case pb.TaskArgs_TaskEquipRefine:
case pb.TaskArgs_TaskTreasureLevel:
case pb.TaskArgs_TaskTreasureRefine:
case pb.TaskArgs_TaskCopyChapter:
case pb.TaskArgs_TaskCopyLevel:
default:
continue
}
completed = completed && task.Okayed[i]
}
if completed {
task.Status = uint32(pb.AbleStatus_Able)
}
}
func (this *Player) addTask(taskTable *data.Task) *PlayerTask {
var task = &PlayerTask{
Id: uint32(taskTable.Id),
Status: uint32(pb.AbleStatus_Unable),
Okayed: make([]bool, len(taskTable.TaskArgs1)),
Process: make([]uint32, len(taskTable.TaskArgs1)),
}
this.Task[task.Id] = task
this.checkTaskCompetedAhead(task, taskTable)
this.SaveModel(task)
//this.manager.eventManager.PlayerTaskLog(this, LogTypeTaskAdd, uint32(taskTable.Id), taskTable.Section1, taskTable.Section2, taskTable.Section3)
return task
}
func (this *Player) AddTask(taskIds ...int) {
if len(taskIds) == 0 {
return
}
var changeList []*pb.TaskChange
for _, taskId := range taskIds {
var task = this.Task[uint32(taskId)]
if task != nil {
continue
}
var taskTable = this.manager.tables.Task.Find1(taskId)
if taskTable == nil {
continue
}
for i := range taskTable.TaskArgs1 {
if pb.TaskArgs(taskTable.TaskArgs1[i]) == pb.TaskArgs_TaskArgsId {
var subTaskTable = this.manager.tables.Task.Find1(taskTable.TaskArgs1[i])
if subTaskTable != nil {
var subTask = this.addTask(taskTable)
changeList = append(changeList, &pb.TaskChange{ChangeType: pb.ChangeType_Add, Task: subTask.BuildMsgTask()})
}
}
}
task = this.addTask(taskTable)
changeList = append(changeList, &pb.TaskChange{ChangeType: pb.ChangeType_Add, Task: task.BuildMsgTask()})
}
if len(changeList) > 0 {
_ = this.Send(pb.ModId_ModuleTask, pb.MsgId_ModTaskChange, &pb.TaskChangeListAck{ChangeList: changeList})
}
}
func (this *Player) checkTaskDeprecated() (changed bool) {
for id, task := range this.Task {
if taskTable := this.manager.tables.Task.Find1(int(id)); taskTable == nil {
delete(this.Task, id)
this.WipeModel(task)
changed = true
}
}
return
}
func (this *Player) checkTaskReset(taskType pb.TaskType) (changed bool) {
var changeList []*pb.TaskChange
var taskTables = this.manager.tables.Task.Find2(int(taskType))
for _, taskTable := range taskTables {
var task = this.Task[uint32(taskTable.Id)]
if task != nil {
task.Okayed = make([]bool, len(taskTable.TaskArgs1))
task.Process = make([]uint32, len(taskTable.TaskArgs1))
task.Status = uint32(pb.AbleStatus_Unable)
changeList = append(changeList, &pb.TaskChange{ChangeType: pb.ChangeType_Changed, Task: task.BuildMsgTask()})
} else {
task = this.addTask(taskTable)
changeList = append(changeList, &pb.TaskChange{ChangeType: pb.ChangeType_Add, Task: task.BuildMsgTask()})
}
this.SaveModel(task)
this.ResetCounterDaily(pb.CounterType_TaskCompleted, uint64(task.Id))
changed = true
}
_ = this.Send(pb.ModId_ModuleTask, pb.MsgId_ModTaskChange, &pb.TaskChangeListAck{ChangeList: changeList})
return
}
func (this *Player) CheckTask(arg1 pb.TaskArgs, arg2 int, value uint32) bool {
var changeList []*pb.TaskChange
for _, task := range this.Task {
var taskTable = this.manager.tables.Task.Find1(int(task.Id))
if taskTable == nil {
continue
}
if pb.AbleStatus(task.Status) >= pb.AbleStatus_Able {
continue
}
var changed = false
if len(task.Process) != len(taskTable.TaskArgs1) {
var process = task.Process
var okay = task.Okayed
task.Process = make([]uint32, len(taskTable.TaskArgs1))
task.Okayed = make([]bool, len(taskTable.TaskArgs1))
copy(task.Process, process)
copy(task.Okayed, okay)
changed = true
}
var completed = true
for i := range taskTable.TaskArgs1 {
if task.Okayed[i] {
continue
}
if pb.TaskArgs(taskTable.TaskArgs1[i]) != arg1 {
completed = false
continue
}
if len(taskTable.TaskArgs2) > i && taskTable.TaskArgs2[i] > 0 {
if arg2 != taskTable.TaskArgs2[i] {
completed = false
continue
}
}
changed = this.processTask(task, taskTable, i, value) || changed
completed = completed && task.Okayed[i]
}
if changed {
if completed {
task.Status = uint32(pb.AbleStatus_Able)
}
this.SaveModel(task)
changeList = append(changeList, &pb.TaskChange{ChangeType: pb.ChangeType_Changed, Task: task.BuildMsgTask()})
}
}
if len(changeList) == 0 {
return false
}
_ = this.Send(pb.ModId_ModuleTask, pb.MsgId_ModTaskChange, &pb.TaskChangeListAck{ChangeList: changeList})
return true
}
func (this *Player) processTask(task *PlayerTask, taskTable *data.Task, i int, value uint32) bool {
switch pb.TaskArgs(taskTable.TaskArgs1[i]) {
case pb.TaskArgs_TaskArgsType:
if task.Process[i] >= uint32(taskTable.TaskArgs3[i]) {
return false
}
task.Process[i] += value
if task.Process[i] >= uint32(taskTable.TaskArgs3[i]) {
task.Process[i] = uint32(taskTable.TaskArgs3[i])
task.Okayed[i] = true
}
case pb.TaskArgs_TaskArgsId:
if task.Process[i] >= uint32(taskTable.TaskArgs3[i]) {
return false
}
task.Process[i] = value
if task.Process[i] >= uint32(taskTable.TaskArgs3[i]) {
task.Process[i] = uint32(taskTable.TaskArgs3[i])
task.Okayed[i] = true
}
default:
return false
}
return true
}
func (this *Player) BuildMsgTaskListAck() *pb.MsgTaskListAck {
var list []*pb.Task
for _, task := range this.Task {
list = append(list, task.BuildMsgTask())
}
return &pb.MsgTaskListAck{TaskList: list}
}