service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package mainQuest
  2. import (
  3. "f1-game/internal/data"
  4. facade "f1-game/internal/facade"
  5. nameRoute "f1-game/internal/name/route"
  6. "f1-game/internal/pb"
  7. "f1-game/internal/sessions"
  8. "f1-game/nodes/map/internal/db"
  9. "f1-game/nodes/map/player/quest"
  10. clog "github.com/cherry-game/cherry/logger"
  11. cproto "github.com/cherry-game/cherry/net/proto"
  12. )
  13. func Service() *service {
  14. return &service{}
  15. }
  16. type service struct {
  17. facade.PlayerServiceBase[*db.MapPlayerTable]
  18. }
  19. func (p *service) OnInit(playerTable *db.MapPlayerTable) {
  20. // 初始化主线章节任务
  21. p.checkAndInitMainQuest(playerTable)
  22. }
  23. func (p *service) OnLogin(playerTable *db.MapPlayerTable, session *cproto.Session) {
  24. if session.Contains(sessions.IsNewPlayer) || playerTable.MainChapterID <= 0 {
  25. clog.Infof("map main quest init: PlayerID = %d", playerTable.PlayerID)
  26. // 初始化主线章节任务
  27. p.checkAndInitMainQuest(playerTable)
  28. }
  29. // 检测是否需要添加下一章节任务列表
  30. p.CheckAddNextMainChapterQuest(playerTable)
  31. }
  32. // 检测是否添加主线章节任务
  33. func (p *service) checkAndInitMainQuest(playerTable *db.MapPlayerTable) {
  34. if playerTable.MainChapterID > 0 {
  35. return
  36. }
  37. list := data.MapMainQuest.List()
  38. if len(list) < 1 {
  39. clog.Warnf("add main quest fail, config not exist. playerID:%d", playerTable.PlayerID)
  40. return
  41. }
  42. first := list[0]
  43. // 添加任务组
  44. if quest.Service().TriggerOpenGroup(playerTable.PlayerID, first.QuestGroupID) {
  45. playerTable.MainChapterID = first.ChapterID
  46. playerTable.IsMainReward = false
  47. playerTable.Save2Queue()
  48. }
  49. }
  50. // 检测是否需要添加下一章节任务
  51. func (*service) CheckAddNextMainChapterQuest(playerTable *db.MapPlayerTable) {
  52. if !playerTable.IsMainReward {
  53. return
  54. }
  55. var (
  56. playerID = playerTable.PlayerID
  57. mainChapterID = playerTable.MainChapterID
  58. )
  59. mainQuestRow, found := data.MapMainQuest.GetByChapterID(mainChapterID)
  60. if !found {
  61. clog.Warnf("[CheckAddNextMainChapterQuest] fail, config not exist. playerID:%d, chapterID:%d",
  62. playerID, mainChapterID)
  63. return
  64. }
  65. nextChapterID := mainQuestRow.NextChapterID
  66. if nextChapterID <= 0 {
  67. return
  68. }
  69. // 下一章节任务配置
  70. nextMainQuestRow, found := data.MapMainQuest.GetByChapterID(nextChapterID)
  71. if !found {
  72. clog.Warnf("[CheckAddNextMainChapterQuest] fail, next quest config not exist. playerID:%d, chapterID:%d",
  73. playerID, mainChapterID)
  74. return
  75. }
  76. if nextMainQuestRow.QuestGroupID <= 0 {
  77. clog.Warnf("[CheckAddNextMainChapterQuest] fail, groupId <= 0. playerID:%d, chapterID:%d, nextChapterID:%d",
  78. playerID, mainChapterID, nextChapterID)
  79. return
  80. }
  81. // 添加任务组任务
  82. success := quest.Service().TriggerOpenGroup(playerID, nextMainQuestRow.QuestGroupID)
  83. if !success {
  84. clog.Warnf("[CheckAddNextMainChapterQuest] fail, trigger open group fail. playerID:%d, nextChapterID:%d",
  85. playerID, mainQuestRow.NextChapterID)
  86. return
  87. }
  88. // 删除旧的任务组
  89. playerTable.Quests.DeleteQuestWithGroupID(mainQuestRow.QuestGroupID)
  90. // 添加成功后更新当前章节ID
  91. playerTable.MainChapterID = nextChapterID
  92. playerTable.IsMainReward = false
  93. playerTable.Save2Queue()
  94. }
  95. // Push 推送地图主线信息
  96. func (p *service) Push(mapPlayerTable *db.MapPlayerTable, session *cproto.Session) {
  97. // 推送地图主线列表
  98. resp := &pb.QuestChapterReward{
  99. ChapterID: mapPlayerTable.MainChapterID,
  100. IsMainReward: mapPlayerTable.IsMainReward,
  101. }
  102. sessions.PushWithSession(session, nameRoute.PushMapPlayer_MainQuest, resp)
  103. }