service.go 2.9 KB

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