player_quest.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package mapPlayer
  2. import (
  3. capp "f1-game/internal/cherry_app"
  4. "f1-game/internal/code"
  5. "f1-game/internal/data"
  6. "f1-game/internal/enum"
  7. nameActor "f1-game/internal/name/actor"
  8. nameLocal "f1-game/internal/name/local"
  9. nameRemote "f1-game/internal/name/remote"
  10. "f1-game/internal/pb"
  11. "f1-game/internal/sessions"
  12. ao "f1-game/nodes/game/player/asset/origin"
  13. "f1-game/nodes/map/internal/db"
  14. dbMapPlayer "f1-game/nodes/map/internal/db/data/player"
  15. mapFacade "f1-game/nodes/map/internal/facade"
  16. mainQuest "f1-game/nodes/map/player/main_quest"
  17. "f1-game/nodes/map/player/quest"
  18. cfacade "github.com/cherry-game/cherry/facade"
  19. cproto "github.com/cherry-game/cherry/net/proto"
  20. )
  21. func (p *actorPlayer) initQuest() {
  22. p.Local().Register(nameLocal.MapPlayer_QuestReward, p.questReward)
  23. p.Local().Register(nameLocal.MapPlayer_QuestChapterReward, p.questChapterReward)
  24. p.Local().Register(nameLocal.MapPlayer_LeagueQuestReward, p.leagueQuestReward)
  25. p.Remote().Register(nameRemote.MapPlayer_QuestEvent, p.questEvent)
  26. //quest.Service().InitEvents(p.Event(), p.playerID)
  27. }
  28. // getReward 获取任务奖励 map.player.questReward
  29. func (p *actorPlayer) questReward(session *cproto.Session, req *pb.I32) {
  30. if req.Value < 1 {
  31. p.ResponseCode(session, code.IllegalArgument)
  32. return
  33. }
  34. var (
  35. questID = req.Value // quest id
  36. playerID = sessions.GetPlayerID(session) // player id
  37. playerTable = db.GetMapPlayerTable(playerID) // player table
  38. changeList []*dbMapPlayer.Quest // change list
  39. )
  40. questEntity, found := playerTable.Quests.GetQuest(questID)
  41. if !found {
  42. p.ResponseCode(session, code.QuestNotFound)
  43. return
  44. }
  45. // 任务已结束,不能领取
  46. if questEntity.Status == enum.QuestStatus_End {
  47. p.ResponseCode(session, code.QuestFinish)
  48. return
  49. }
  50. // 任务未完成,不能领取
  51. if questEntity.Status != enum.QuestStatus_Finish {
  52. p.ResponseCode(session, code.QuestNotFinished)
  53. return
  54. }
  55. // 任务配置未找到
  56. questRow, ok := data.Quest.GetByQuestGroupIDQuestID(questEntity.QuestGroupID, questID)
  57. if !ok {
  58. p.ResponseCode(session, code.ConfigNotFound_Quest)
  59. return
  60. }
  61. // 任务修改为已结束
  62. questEntity.Status = enum.QuestStatus_End
  63. if questRow.NextQuestID > 0 {
  64. // 如果有下一个任务,则添加
  65. nextQuestRow, _ := data.Quest.GetByQuestGroupIDQuestID(questRow.QuestGroupID, questRow.NextQuestID)
  66. nextQuestEntity, ok := quest.Service().AddQuest(playerTable, nextQuestRow)
  67. if ok {
  68. // 确保下一个任务添加成功后且需要删除上一个任务时 才删除上一个任务
  69. groupRow, ok := data.QuestGroup.GetByGroupID(questRow.QuestGroupID)
  70. if ok && groupRow.RemoveFinished {
  71. playerTable.Quests.DeleteQuest(questID)
  72. }
  73. changeList = append(changeList, nextQuestEntity)
  74. }
  75. }
  76. //save
  77. playerTable.Save2Queue()
  78. // change list
  79. changeList = append(changeList, questEntity)
  80. // 有奖励则发放奖励
  81. if len(questRow.Rewards) > 0 {
  82. _, errCode := mapFacade.Storage.AssetAdds(playerID, questRow.Rewards, ao.QuestReward)
  83. if code.IsFail(errCode) {
  84. p.Warn("[questReward] error. playerID = %d, questRow = %v", p.playerID, questRow)
  85. }
  86. }
  87. quest.Service().ChangePushWithSession(session, changeList)
  88. p.ResponseCode(session, code.OK)
  89. }
  90. // 领取主线章节奖励 // TODO 默认当前只有主线章节任务 如果后面有其他的类型再扩展
  91. func (p *actorPlayer) questChapterReward(session *cproto.Session, req *pb.I32) {
  92. mainChapterID := req.Value
  93. if mainChapterID < 1 {
  94. p.ResponseCode(session, code.RequestParamsError)
  95. return
  96. }
  97. mainChapterRow, found := data.MapMainQuest.GetByChapterID(mainChapterID)
  98. if !found {
  99. p.ResponseCode(session, code.MainChapterConfigNotFound)
  100. return
  101. }
  102. // 章节奖励
  103. if len(mainChapterRow.ChapterRewards) < 1 {
  104. p.ResponseCode(session, code.QuestGroupRewardNotExist)
  105. return
  106. }
  107. var (
  108. playerTable = db.GetMapPlayerTable(p.playerID)
  109. curMainChapterID = playerTable.MainChapterID
  110. isMainReward = playerTable.IsMainReward
  111. )
  112. // 章节奖励已领取
  113. if mainChapterID < curMainChapterID || isMainReward {
  114. p.ResponseCode(session, code.MainChapterRewardGot)
  115. return
  116. }
  117. // 分组任务未开启
  118. if mainChapterID > curMainChapterID {
  119. p.ResponseCode(session, code.MainChapterNotOpen)
  120. return
  121. }
  122. // 获取当前分组任务的任务ID列表 来判断是否所有任务都已完成
  123. questIDs, found := data.Quest.GetQuestIDsWithGroupID(mainChapterRow.QuestGroupID)
  124. if !found {
  125. p.ResponseCode(session, code.QuestNotFound)
  126. return
  127. }
  128. // 存在未完成/领取的任务,则不允许领取章节奖励
  129. if end := playerTable.Quests.CheckQuestsEnd(questIDs...); !end {
  130. p.ResponseCode(session, code.QuestNotFinished)
  131. return
  132. }
  133. // 发放章节奖励
  134. _, errCode := mapFacade.Storage.AssetAdds(p.playerID, mainChapterRow.ChapterRewards, ao.QuestChapterReward)
  135. if code.IsFail(errCode) {
  136. p.ResponseCode(session, errCode)
  137. return
  138. }
  139. // 设置当前章节奖励已领取
  140. playerTable.IsMainReward = true
  141. // 检测添加下一个任务组任务
  142. mainQuest.Service().CheckAddNextMainChapterQuest(playerTable)
  143. playerTable.Save2Queue()
  144. resp := &pb.QuestChapterReward{
  145. ChapterID: playerTable.MainChapterID,
  146. IsMainReward: playerTable.IsMainReward,
  147. }
  148. p.Response(session, resp)
  149. }
  150. // 联盟任务领取
  151. func (p *actorPlayer) leagueQuestReward(session *cproto.Session, req *pb.I32) {
  152. var (
  153. questID = req.Value
  154. playerTable = db.GetMapPlayerTable(p.playerID)
  155. leagueID = playerTable.LeagueID
  156. )
  157. questRow, found := data.Quest.GetByQuestID(questID)
  158. if !found {
  159. p.ResponseCode(session, code.QuestNotFound)
  160. return
  161. }
  162. if leagueID <= 0 {
  163. p.ResponseCode(session, code.LeagueNotJoin)
  164. return
  165. }
  166. // 任务已领取
  167. if got := playerTable.Quests.CheckLeagueQuestReward(questID); got {
  168. p.ResponseCode(session, code.QuestFinish)
  169. return
  170. }
  171. // 去联盟检测是否能领取奖励
  172. checkReq := &pb.I32{Value: questID}
  173. targetPath := cfacade.NewChildPath(capp.NodeID(), nameActor.Map_League, leagueID)
  174. errCode := p.CallWait(targetPath, nameRemote.MapLeague_QuestCheck, checkReq, nil)
  175. if code.IsFail(errCode) {
  176. p.ResponseCode(session, errCode)
  177. return
  178. }
  179. // 领取奖励
  180. _, errCode = mapFacade.Storage.AssetAdds(p.playerID, questRow.Rewards, ao.LeagueQuestReward)
  181. if code.IsFail(errCode) {
  182. p.Warn("[leagueQuestReward] error. playerID = %d, questRow = %v", p.playerID, questRow)
  183. return
  184. }
  185. playerTable.Quests.AddLeagueQuestID(questID)
  186. playerTable.Save2Queue()
  187. resp := &pb.I32{
  188. Value: questID,
  189. }
  190. p.Response(session, resp)
  191. }
  192. func (p *actorPlayer) questEvent(e cfacade.IEventData) {
  193. if e.UniqueID() != p.playerID {
  194. return
  195. }
  196. quest.Service().DoQuestEvent(p.playerID, e, p.isOnline)
  197. }