service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package seasonQuest
  2. import (
  3. capp "f1-game/internal/cherry_app"
  4. "f1-game/internal/data"
  5. "f1-game/nodes/map/internal/db"
  6. dbSeason "f1-game/nodes/map/internal/db/data/season"
  7. "slices"
  8. ctime "github.com/cherry-game/cherry/extend/time"
  9. cfacade "github.com/cherry-game/cherry/facade"
  10. cactor "github.com/cherry-game/cherry/net/actor"
  11. clog "github.com/cherry-game/cherry/logger"
  12. )
  13. var srv = &service{
  14. typ: newServiceType(),
  15. }
  16. func Service() *service {
  17. return srv
  18. }
  19. type service struct {
  20. typ serviceType
  21. }
  22. func (p *service) OnInit(seasonTable *db.SeasonTable) {
  23. for index, questRow := range data.SeasonQuest.List() {
  24. quest := dbSeason.NewQuest(questRow)
  25. if index == 0 {
  26. seasonTable.SeasonQuest.CurQuestID = questRow.ID
  27. quest.StartTime = seasonTable.StartTime
  28. quest.EndTime = quest.CalcEndTime()
  29. }
  30. seasonTable.SeasonQuest.Quests.Put(quest.QuestID, quest)
  31. }
  32. p.unlockNewFunc(seasonTable.SeasonQuest.CurQuestID)
  33. }
  34. // 定时检查是否能进入下一个任务
  35. func (p *service) OnCheckNextID(seasonTable *db.SeasonTable) {
  36. p.initNewQuests(seasonTable)
  37. quest, found := seasonTable.SeasonQuest.Quests.Get(seasonTable.SeasonQuest.CurQuestID)
  38. if !found {
  39. return
  40. }
  41. nowMillis := ctime.Now().ToMillisecond()
  42. if !quest.IsEnded(nowMillis) {
  43. return
  44. }
  45. clog.Infof("[OnCheckNextID] questID: %d ended, endTime: %d", quest.QuestID, quest.EndTime)
  46. p.tryAdvanceToNextQuest(seasonTable, quest)
  47. }
  48. func (p *service) initNewQuests(seasonTable *db.SeasonTable) {
  49. for _, questRow := range data.SeasonQuest.List() {
  50. if !seasonTable.SeasonQuest.Quests.Contains(questRow.ID) {
  51. seasonTable.SeasonQuest.Quests.Put(questRow.ID, dbSeason.NewQuest(questRow))
  52. seasonTable.SetChangeData()
  53. clog.Infof("[initNewQuests] new quest created: %d", questRow.ID)
  54. }
  55. }
  56. }
  57. // 检查是否能进入下一个霸业任务
  58. func (p *service) tryAdvanceToNextQuest(seasonTable *db.SeasonTable, curQuest *dbSeason.Quest) {
  59. questRow, found := data.SeasonQuest.GetByID(curQuest.QuestID)
  60. if !found || questRow.NextID <= 0 {
  61. return
  62. }
  63. if !data.SeasonQuest.ContainID(curQuest.QuestID) {
  64. return
  65. }
  66. nextQuest, found := seasonTable.SeasonQuest.Quests.Get(questRow.NextID)
  67. if !found {
  68. return
  69. }
  70. if nextQuest.StartTime > 0 || nextQuest.EndTime > 0 {
  71. clog.Warnf("[tryAdvanceToNextQuest] next questID: %d already started", questRow.NextID)
  72. return
  73. }
  74. seasonTable.SeasonQuest.CurQuestID = questRow.NextID
  75. nextQuest.StartTime = curQuest.EndTime
  76. nextQuest.EndTime = nextQuest.CalcEndTime()
  77. seasonTable.SetChangeData()
  78. clog.Infof("[tryAdvanceToNextQuest] on next questID: %d", questRow.NextID)
  79. p.unlockNewFunc(seasonTable.SeasonQuest.CurQuestID)
  80. }
  81. // TODO: 开启了新的霸业进度,通知处理解锁全服功能
  82. func (p *service) unlockNewFunc(questID int32) {
  83. }
  84. func (p *service) EventNames() []string {
  85. return p.typ.eventNames()
  86. }
  87. func (p *service) InitEvents(event cactor.IEvent) {
  88. event.Registers(p.typ.eventNames(), p.onEvent)
  89. }
  90. func (p *service) onEvent(e cfacade.IEventData) {
  91. seasonTable := db.GetSeasonTable(capp.NodeID())
  92. if seasonTable.StartTime <= 0 {
  93. return
  94. }
  95. var (
  96. nowMillis = ctime.Now().ToMillisecond()
  97. changeList []*dbSeason.Quest
  98. )
  99. for _, questEntity := range seasonTable.SeasonQuest.Quests.Values() {
  100. if questEntity.IsEnded(nowMillis) {
  101. continue
  102. }
  103. questRow, found := data.SeasonQuest.GetByID(questEntity.QuestID)
  104. if !found {
  105. clog.Warnf("[onEvent] quest config not found. questID = %d", questEntity.QuestID)
  106. continue
  107. }
  108. parser, found := p.typ.get(questRow.QuestType)
  109. if !found {
  110. continue
  111. }
  112. if !slices.Contains(parser.EventNames(), e.Name()) {
  113. continue
  114. }
  115. if parser.OnUpdate(questEntity, questRow, e) {
  116. changeList = append(changeList, questEntity)
  117. }
  118. }
  119. if len(changeList) > 0 {
  120. seasonTable.SetChangeData()
  121. }
  122. }
  123. // 获取目标进度
  124. func (p *service) GetTargetProgress(questID int32) int32 {
  125. row, found := data.SeasonQuest.GetByID(questID)
  126. if !found {
  127. return 0
  128. }
  129. parser, found := p.typ.get(row.QuestType)
  130. if !found {
  131. return 0
  132. }
  133. return parser.TargetProgress(row)
  134. }