| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package seasonQuest
- import (
- capp "f1-game/internal/cherry_app"
- "f1-game/internal/data"
- "f1-game/nodes/map/internal/db"
- dbSeason "f1-game/nodes/map/internal/db/data/season"
- "slices"
- ctime "github.com/cherry-game/cherry/extend/time"
- cfacade "github.com/cherry-game/cherry/facade"
- cactor "github.com/cherry-game/cherry/net/actor"
- clog "github.com/cherry-game/cherry/logger"
- )
- var srv = &service{
- typ: newServiceType(),
- }
- func Service() *service {
- return srv
- }
- type service struct {
- typ serviceType
- }
- func (p *service) OnInit(seasonTable *db.SeasonTable) {
- for index, questRow := range data.SeasonQuest.List() {
- quest := dbSeason.NewQuest(questRow)
- if index == 0 {
- seasonTable.SeasonQuest.CurQuestID = questRow.ID
- quest.StartTime = seasonTable.StartTime
- quest.EndTime = quest.CalcEndTime()
- }
- seasonTable.SeasonQuest.Quests.Put(quest.QuestID, quest)
- }
- p.unlockNewFunc(seasonTable.SeasonQuest.CurQuestID)
- }
- // 定时检查是否能进入下一个任务
- func (p *service) OnCheckNextID(seasonTable *db.SeasonTable) {
- p.initNewQuests(seasonTable)
- quest, found := seasonTable.SeasonQuest.Quests.Get(seasonTable.SeasonQuest.CurQuestID)
- if !found {
- return
- }
- nowMillis := ctime.Now().ToMillisecond()
- if !quest.IsEnded(nowMillis) {
- return
- }
- clog.Infof("[OnCheckNextID] questID: %d ended, endTime: %d", quest.QuestID, quest.EndTime)
- p.tryAdvanceToNextQuest(seasonTable, quest)
- }
- func (p *service) initNewQuests(seasonTable *db.SeasonTable) {
- for _, questRow := range data.SeasonQuest.List() {
- if !seasonTable.SeasonQuest.Quests.Contains(questRow.ID) {
- seasonTable.SeasonQuest.Quests.Put(questRow.ID, dbSeason.NewQuest(questRow))
- seasonTable.SetChangeData()
- clog.Infof("[initNewQuests] new quest created: %d", questRow.ID)
- }
- }
- }
- // 检查是否能进入下一个霸业任务
- func (p *service) tryAdvanceToNextQuest(seasonTable *db.SeasonTable, curQuest *dbSeason.Quest) {
- questRow, found := data.SeasonQuest.GetByID(curQuest.QuestID)
- if !found || questRow.NextID <= 0 {
- return
- }
- if !data.SeasonQuest.ContainID(curQuest.QuestID) {
- return
- }
- nextQuest, found := seasonTable.SeasonQuest.Quests.Get(questRow.NextID)
- if !found {
- return
- }
- if nextQuest.StartTime > 0 || nextQuest.EndTime > 0 {
- clog.Warnf("[tryAdvanceToNextQuest] next questID: %d already started", questRow.NextID)
- return
- }
- seasonTable.SeasonQuest.CurQuestID = questRow.NextID
- nextQuest.StartTime = curQuest.EndTime
- nextQuest.EndTime = nextQuest.CalcEndTime()
- seasonTable.SetChangeData()
- clog.Infof("[tryAdvanceToNextQuest] on next questID: %d", questRow.NextID)
- p.unlockNewFunc(seasonTable.SeasonQuest.CurQuestID)
- }
- // TODO: 开启了新的霸业进度,通知处理解锁全服功能
- func (p *service) unlockNewFunc(questID int32) {
- }
- func (p *service) EventNames() []string {
- return p.typ.eventNames()
- }
- func (p *service) InitEvents(event cactor.IEvent) {
- event.Registers(p.typ.eventNames(), p.onEvent)
- }
- func (p *service) onEvent(e cfacade.IEventData) {
- seasonTable := db.GetSeasonTable(capp.NodeID())
- if seasonTable.StartTime <= 0 {
- return
- }
- var (
- nowMillis = ctime.Now().ToMillisecond()
- changeList []*dbSeason.Quest
- )
- for _, questEntity := range seasonTable.SeasonQuest.Quests.Values() {
- if questEntity.IsEnded(nowMillis) {
- continue
- }
- questRow, found := data.SeasonQuest.GetByID(questEntity.QuestID)
- if !found {
- clog.Warnf("[onEvent] quest config not found. questID = %d", questEntity.QuestID)
- continue
- }
- parser, found := p.typ.get(questRow.QuestType)
- if !found {
- continue
- }
- if !slices.Contains(parser.EventNames(), e.Name()) {
- continue
- }
- if parser.OnUpdate(questEntity, questRow, e) {
- changeList = append(changeList, questEntity)
- }
- }
- if len(changeList) > 0 {
- seasonTable.SetChangeData()
- }
- }
- // 获取目标进度
- func (p *service) GetTargetProgress(questID int32) int32 {
- row, found := data.SeasonQuest.GetByID(questID)
- if !found {
- return 0
- }
- parser, found := p.typ.get(row.QuestType)
- if !found {
- return 0
- }
- return parser.TargetProgress(row)
- }
|