package db import ( "errors" capp "f1-game/internal/cherry_app" dbFacade "f1-game/internal/component/db/facade" nameDB "f1-game/internal/name/db" dbChapter "f1-game/nodes/game/internal/db/data/chapter" "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" clog "github.com/cherry-game/cherry/logger" ) // ChapterTable 探险表 type ( ChapterTable struct { PlayerID int64 `bson:"PlayerID,omitempty"` NodeID string `bson:"NodeID,omitempty"` dbChapter.Data `bson:"Data,omitempty"` dbFacade.TableBase `bson:",inline"` } ) func (*ChapterTable) TableName() string { return nameDB.Table_Chapter } func (p *ChapterTable) PrimaryValue() any { return p.PlayerID } func (p *ChapterTable) CheckField() { } func (p *ChapterTable) Save2Queue() { save2Queue(p) } func GetChapterTable(playerID int64) *ChapterTable { if val, ok := chapterTableCache.GetIfPresent(playerID); ok { return val.(*ChapterTable) } chapterTable, found := GetChapterTableFromDB(playerID) if found { chapterTableCache.Put(playerID, chapterTable) } return chapterTable } func GetChapterTableFromDB(playerID int64) (*ChapterTable, bool) { chapterTable := newChapterTable(playerID) filter := bson.M{ nameDB.Field_NodeID: chapterTable.NodeID, nameDB.Field_PlayerID: chapterTable.PlayerID, } err := dbComponent.FindOne(chapterTable, filter) if err != nil { if errors.Is(err, mongo.ErrNoDocuments) { clog.Warnf("[playerID = %d] not found, create new one", playerID) chapterTable.CheckField() return chapterTable, true } clog.Errorf("[playerID = %d] db error = %v", playerID, err) return chapterTable, false } chapterTable.CheckField() return chapterTable, true } func newChapterTable(playerID int64) *ChapterTable { return &ChapterTable{ PlayerID: playerID, NodeID: capp.NodeID(), Data: dbChapter.NewData(), } }