154 lines
3.4 KiB
Go
154 lines
3.4 KiB
Go
package guideTask
|
|
|
|
import (
|
|
"fmt"
|
|
"server/GoUtil"
|
|
GuideTaskCfg "server/conf/guideTask"
|
|
"server/game/mod/item"
|
|
"server/game/mod/quest"
|
|
"server/msg"
|
|
)
|
|
|
|
type GuideTaskMod struct {
|
|
Tasks map[int]*GuideTask
|
|
Active int
|
|
Reward []int // 任务奖励状态
|
|
UnlockTime int64 // 解锁时间
|
|
}
|
|
|
|
type GuideTask struct {
|
|
Items []*item.Item
|
|
Status int
|
|
Quest quest.QuestProgress
|
|
UnLock bool
|
|
}
|
|
|
|
func (gt *GuideTaskMod) InitData() {
|
|
if gt.Tasks == nil {
|
|
gt.Tasks = make(map[int]*GuideTask)
|
|
}
|
|
if len(gt.Tasks) == 0 {
|
|
for i := 1; ; i++ {
|
|
Str := GuideTaskCfg.GetTaskById(i)
|
|
if Str == "" {
|
|
break
|
|
}
|
|
Quest, _ := quest.ParseQuest(Str)
|
|
gt.Tasks[i] = &GuideTask{
|
|
Status: quest.QUEST_STATUS_UNFINISH,
|
|
Quest: Quest,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (gt *GuideTaskMod) Login() {
|
|
// 登录时触发的引导任务
|
|
if task, ok := gt.Tasks[1]; ok && !task.UnLock {
|
|
task.UnLock = true
|
|
task.Status = 1 // 设置为已解锁状态
|
|
}
|
|
}
|
|
|
|
func (gt *GuideTaskMod) Unlock(lv int) bool {
|
|
UnLockLv := GuideTaskCfg.GetUnlockLv()
|
|
if lv >= UnLockLv && gt.UnlockTime == 0 {
|
|
gt.UnlockTime = GoUtil.Now()
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (gt *GuideTaskMod) CheckOpen() bool {
|
|
// 检查是否可以打开引导任务
|
|
if gt.UnlockTime == 0 {
|
|
return false
|
|
}
|
|
return GoUtil.Now()-gt.UnlockTime <= 96*3600 // 96小时内可以打开
|
|
}
|
|
|
|
func (gt *GuideTaskMod) Trigger(Tr *quest.Trigger) bool {
|
|
if !gt.CheckOpen() {
|
|
return false
|
|
}
|
|
update := false
|
|
for k, v := range gt.Tasks {
|
|
if v.Status != quest.QUEST_STATUS_UNFINISH {
|
|
continue
|
|
}
|
|
up := quest.TriggerQuestProgress(&v.Quest, Tr)
|
|
if up {
|
|
update = true
|
|
}
|
|
if v.Quest.Status {
|
|
v.Status = quest.QUEST_STATUS_FINISH
|
|
if _, ok := gt.Tasks[k+1]; ok {
|
|
task := gt.Tasks[k+1]
|
|
task.UnLock = true
|
|
gt.Tasks[k+1] = task
|
|
}
|
|
}
|
|
gt.Tasks[k] = v
|
|
}
|
|
return update
|
|
}
|
|
|
|
func (gt *GuideTaskMod) GetTaskReward(Id int) ([]*item.Item, error) {
|
|
if task, ok := gt.Tasks[Id]; ok {
|
|
if task.Status == quest.QUEST_STATUS_FINISH {
|
|
task.Status = quest.QUEST_STATUS_REWARD
|
|
Active := GuideTaskCfg.GetTaskActive(Id)
|
|
gt.Active += Active
|
|
return GuideTaskCfg.GetTaskRewardById(Id), nil
|
|
}
|
|
// 登录任务特殊处理
|
|
if task.Quest.Label == "GuideLogin" {
|
|
Day := (GoUtil.Now() - gt.UnlockTime) / 86400
|
|
if task.Quest.Target-1 <= int(Day) {
|
|
task.Status = quest.QUEST_STATUS_REWARD
|
|
Active := GuideTaskCfg.GetTaskActive(Id)
|
|
gt.Active += Active
|
|
return GuideTaskCfg.GetTaskRewardById(Id), nil
|
|
}
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no task id %d", Id)
|
|
}
|
|
|
|
func (gt *GuideTaskMod) GetActiveReward(Id int) ([]*item.Item, error) {
|
|
for _, v := range gt.Reward {
|
|
if v == Id {
|
|
return nil, fmt.Errorf("active reward already got")
|
|
}
|
|
}
|
|
items, needActive := GuideTaskCfg.GetActiveReward(Id)
|
|
if items == nil {
|
|
return nil, fmt.Errorf("no active reward id %d", Id)
|
|
}
|
|
if gt.Active < needActive {
|
|
return nil, fmt.Errorf("active not enough")
|
|
}
|
|
gt.Reward = append(gt.Reward, Id)
|
|
return items, nil
|
|
}
|
|
|
|
func (gt *GuideTaskMod) BackData() *msg.ResGuideTask {
|
|
if !gt.CheckOpen() {
|
|
return nil
|
|
}
|
|
resTask := make(map[int32]*msg.GuideTask)
|
|
for k, v := range gt.Tasks {
|
|
resTask[int32(k)] = &msg.GuideTask{
|
|
Status: int32(v.Status),
|
|
Progress: quest.QuestProgressToMsg(&v.Quest),
|
|
}
|
|
}
|
|
|
|
return &msg.ResGuideTask{
|
|
Task: resTask,
|
|
Active: int32(gt.Active),
|
|
UnlockTime: int32(gt.UnlockTime),
|
|
ActiveReward: GoUtil.SliceIntToInt32(gt.Reward),
|
|
}
|
|
}
|