| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package player
- import (
- "f1-game/internal/code"
- "f1-game/internal/data"
- "f1-game/internal/enum"
- nameLocal "f1-game/internal/name/local"
- "f1-game/internal/pb"
- "f1-game/internal/sessions"
- dbQuest "f1-game/nodes/game/internal/db/data/quest"
- "f1-game/nodes/game/player/asset"
- ao "f1-game/nodes/game/player/asset/origin"
- mainQuest "f1-game/nodes/game/player/main_quest"
- "f1-game/nodes/game/player/quest"
- cproto "github.com/cherry-game/cherry/net/proto"
- )
- func (p *actorPlayer) initQuest() {
- p.Local().Register(nameLocal.Player_QuestReward, p.questReward)
- p.Local().Register(nameLocal.Player_QuestChapterReward, p.questChapterReward)
- quest.Service().InitEvents(p.Event(), p.playerID)
- }
- // getReward 获取任务奖励 game.player.questReward
- func (p *actorPlayer) questReward(session *cproto.Session, req *pb.I32) {
- if req.Value < 1 {
- p.ResponseCode(session, code.IllegalArgument)
- return
- }
- var (
- questID = req.Value // quest id
- playerID = sessions.GetPlayerID(session) // player id
- questTable = p.QuestTable() // quest table
- changeList []*dbQuest.Quest // change list
- )
- questEntity, found := questTable.GetQuest(questID)
- if !found {
- p.ResponseCode(session, code.QuestNotFound)
- return
- }
- // 任务已结束,不能领取
- if questEntity.Status == enum.QuestStatus_End {
- p.ResponseCode(session, code.QuestFinish)
- return
- }
- // 任务未完成,不能领取
- if questEntity.Status != enum.QuestStatus_Finish {
- p.ResponseCode(session, code.QuestNotFinished)
- return
- }
- // 任务配置未找到
- questRow, ok := data.Quest.GetByQuestGroupIDQuestID(questEntity.QuestGroupID, questID)
- if !ok {
- p.ResponseCode(session, code.ConfigNotFound_Quest)
- return
- }
- // 任务修改为已结束
- questEntity.Status = enum.QuestStatus_End
- if questRow.NextQuestID > 0 {
- // 如果有下一个任务,则添加
- nextQuestRow, _ := data.Quest.GetByQuestGroupIDQuestID(questRow.QuestGroupID, questRow.NextQuestID)
- nextQuestEntity, ok := quest.Service().AddQuest(questTable, nextQuestRow)
- if ok {
- // 确保下一个任务添加成功后且需要删除上一个任务时 才删除上一个任务
- groupRow, ok := data.QuestGroup.GetByGroupID(questRow.QuestGroupID)
- if ok && groupRow.RemoveFinished {
- questTable.DeleteQuest(questID)
- }
- changeList = append(changeList, nextQuestEntity)
- }
- }
- //save
- questTable.Save2Queue()
- // change list
- changeList = append(changeList, questEntity)
- // 有奖励则发放奖励
- if len(questRow.Rewards) > 0 {
- _, errCode := asset.Service().Adds(playerID, questRow.Rewards, ao.QuestReward, true)
- if code.IsFail(errCode) {
- p.Warn("[questReward] error. playerID = %d, questRow = %v", p.playerID, questRow)
- }
- }
- quest.Service().ChangePushWithSession(session, changeList)
- p.ResponseCode(session, code.OK)
- }
- // 领取主线章节奖励 // TODO 默认当前只有主线章节任务 如果后面有其他的类型再扩展
- func (p *actorPlayer) questChapterReward(session *cproto.Session, req *pb.I32) {
- mainChapterID := req.Value
- if mainChapterID < 1 {
- p.ResponseCode(session, code.RequestParamsError)
- return
- }
- mainChapterRow, found := data.GameMainQuest.GetByChapterID(mainChapterID)
- if !found {
- p.ResponseCode(session, code.MainChapterConfigNotFound)
- return
- }
- // 章节奖励
- if len(mainChapterRow.ChapterRewards) < 1 {
- p.ResponseCode(session, code.QuestGroupRewardNotExist)
- return
- }
- var (
- questTable = p.QuestTable()
- playerTable = p.PlayerTable()
- curMainChapterID = playerTable.MainQuest.MainChapterID
- isMainReward = playerTable.MainQuest.IsMainReward
- )
- // 章节奖励已领取
- if mainChapterID < curMainChapterID || isMainReward {
- p.ResponseCode(session, code.MainChapterRewardGot)
- return
- }
- // 分组任务未开启
- if mainChapterID > curMainChapterID {
- p.ResponseCode(session, code.MainChapterNotOpen)
- return
- }
- // 获取当前分组任务的任务ID列表 来判断是否所有任务都已完成
- questIDs, found := data.Quest.GetQuestIDsWithGroupID(mainChapterRow.QuestGroupID)
- if !found {
- p.ResponseCode(session, code.QuestNotFound)
- return
- }
- // 存在未完成/领取的任务,则不允许领取章节奖励
- if end := questTable.CheckQuestsEnd(questIDs...); !end {
- p.ResponseCode(session, code.QuestNotFinished)
- return
- }
- // 发放章节奖励
- _, errCode := asset.Service().Adds(p.playerID, mainChapterRow.ChapterRewards, ao.QuestChapterReward, true)
- if code.IsFail(errCode) {
- p.ResponseCode(session, errCode)
- return
- }
- // 设置当前章节奖励已领取
- playerTable.MainQuest.IsMainReward = true
- // 检测添加下一个任务组任务
- mainQuest.Service().CheckAddNextMainChapterQuest(playerTable)
- playerTable.Save2Queue()
- questTable.Save2Queue()
- resp := &pb.QuestChapterReward{
- ChapterID: playerTable.MainQuest.MainChapterID,
- IsMainReward: playerTable.MainQuest.IsMainReward,
- }
- p.Response(session, resp)
- }
|