player_quest.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package player
  2. import (
  3. "f1-game/internal/code"
  4. "f1-game/internal/data"
  5. "f1-game/internal/enum"
  6. nameLocal "f1-game/internal/name/local"
  7. "f1-game/internal/pb"
  8. "f1-game/internal/sessions"
  9. dbQuest "f1-game/nodes/game/internal/db/data/quest"
  10. "f1-game/nodes/game/player/asset"
  11. ao "f1-game/nodes/game/player/asset/origin"
  12. mainQuest "f1-game/nodes/game/player/main_quest"
  13. "f1-game/nodes/game/player/quest"
  14. cproto "github.com/cherry-game/cherry/net/proto"
  15. )
  16. func (p *actorPlayer) initQuest() {
  17. p.Local().Register(nameLocal.Player_QuestReward, p.questReward)
  18. p.Local().Register(nameLocal.Player_QuestChapterReward, p.questChapterReward)
  19. quest.Service().InitEvents(p.Event(), p.playerID)
  20. }
  21. // getReward 获取任务奖励 game.player.questReward
  22. func (p *actorPlayer) questReward(session *cproto.Session, req *pb.I32) {
  23. if req.Value < 1 {
  24. p.ResponseCode(session, code.IllegalArgument)
  25. return
  26. }
  27. var (
  28. questID = req.Value // quest id
  29. playerID = sessions.GetPlayerID(session) // player id
  30. questTable = p.QuestTable() // quest table
  31. changeList []*dbQuest.Quest // change list
  32. )
  33. questEntity, found := questTable.GetQuest(questID)
  34. if !found {
  35. p.ResponseCode(session, code.QuestNotFound)
  36. return
  37. }
  38. // 任务已结束,不能领取
  39. if questEntity.Status == enum.QuestStatus_End {
  40. p.ResponseCode(session, code.QuestFinish)
  41. return
  42. }
  43. // 任务未完成,不能领取
  44. if questEntity.Status != enum.QuestStatus_Finish {
  45. p.ResponseCode(session, code.QuestNotFinished)
  46. return
  47. }
  48. // 任务配置未找到
  49. questRow, ok := data.Quest.GetByQuestGroupIDQuestID(questEntity.QuestGroupID, questID)
  50. if !ok {
  51. p.ResponseCode(session, code.ConfigNotFound_Quest)
  52. return
  53. }
  54. // 任务修改为已结束
  55. questEntity.Status = enum.QuestStatus_End
  56. if questRow.NextQuestID > 0 {
  57. // 如果有下一个任务,则添加
  58. nextQuestRow, _ := data.Quest.GetByQuestGroupIDQuestID(questRow.QuestGroupID, questRow.NextQuestID)
  59. nextQuestEntity, ok := quest.Service().AddQuest(questTable, nextQuestRow)
  60. if ok {
  61. // 确保下一个任务添加成功后且需要删除上一个任务时 才删除上一个任务
  62. groupRow, ok := data.QuestGroup.GetByGroupID(questRow.QuestGroupID)
  63. if ok && groupRow.RemoveFinished {
  64. questTable.DeleteQuest(questID)
  65. }
  66. changeList = append(changeList, nextQuestEntity)
  67. }
  68. }
  69. //save
  70. questTable.Save2Queue()
  71. // change list
  72. changeList = append(changeList, questEntity)
  73. // 有奖励则发放奖励
  74. if len(questRow.Rewards) > 0 {
  75. _, errCode := asset.Service().Adds(playerID, questRow.Rewards, ao.QuestReward, true)
  76. if code.IsFail(errCode) {
  77. p.Warn("[questReward] error. playerID = %d, questRow = %v", p.playerID, questRow)
  78. }
  79. }
  80. quest.Service().ChangePushWithSession(session, changeList)
  81. p.ResponseCode(session, code.OK)
  82. }
  83. // 领取主线章节奖励 // TODO 默认当前只有主线章节任务 如果后面有其他的类型再扩展
  84. func (p *actorPlayer) questChapterReward(session *cproto.Session, req *pb.I32) {
  85. mainChapterID := req.Value
  86. if mainChapterID < 1 {
  87. p.ResponseCode(session, code.RequestParamsError)
  88. return
  89. }
  90. mainChapterRow, found := data.GameMainQuest.GetByChapterID(mainChapterID)
  91. if !found {
  92. p.ResponseCode(session, code.MainChapterConfigNotFound)
  93. return
  94. }
  95. // 章节奖励
  96. if len(mainChapterRow.ChapterRewards) < 1 {
  97. p.ResponseCode(session, code.QuestGroupRewardNotExist)
  98. return
  99. }
  100. var (
  101. questTable = p.QuestTable()
  102. playerTable = p.PlayerTable()
  103. curMainChapterID = playerTable.MainQuest.MainChapterID
  104. isMainReward = playerTable.MainQuest.IsMainReward
  105. )
  106. // 章节奖励已领取
  107. if mainChapterID < curMainChapterID || isMainReward {
  108. p.ResponseCode(session, code.MainChapterRewardGot)
  109. return
  110. }
  111. // 分组任务未开启
  112. if mainChapterID > curMainChapterID {
  113. p.ResponseCode(session, code.MainChapterNotOpen)
  114. return
  115. }
  116. // 获取当前分组任务的任务ID列表 来判断是否所有任务都已完成
  117. questIDs, found := data.Quest.GetQuestIDsWithGroupID(mainChapterRow.QuestGroupID)
  118. if !found {
  119. p.ResponseCode(session, code.QuestNotFound)
  120. return
  121. }
  122. // 存在未完成/领取的任务,则不允许领取章节奖励
  123. if end := questTable.CheckQuestsEnd(questIDs...); !end {
  124. p.ResponseCode(session, code.QuestNotFinished)
  125. return
  126. }
  127. // 发放章节奖励
  128. _, errCode := asset.Service().Adds(p.playerID, mainChapterRow.ChapterRewards, ao.QuestChapterReward, true)
  129. if code.IsFail(errCode) {
  130. p.ResponseCode(session, errCode)
  131. return
  132. }
  133. // 设置当前章节奖励已领取
  134. playerTable.MainQuest.IsMainReward = true
  135. // 检测添加下一个任务组任务
  136. mainQuest.Service().CheckAddNextMainChapterQuest(playerTable)
  137. playerTable.Save2Queue()
  138. questTable.Save2Queue()
  139. resp := &pb.QuestChapterReward{
  140. ChapterID: playerTable.MainQuest.MainChapterID,
  141. IsMainReward: playerTable.MainQuest.IsMainReward,
  142. }
  143. p.Response(session, resp)
  144. }