package db import ( "errors" cherryApp "f1-game/internal/cherry_app" dbFacade "f1-game/internal/component/db/facade" nameDB "f1-game/internal/name/db" dbLineup "f1-game/nodes/game/internal/db/data/lineup" clog "github.com/cherry-game/cherry/logger" "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" ) type ( // LineupTable 阵容表 LineupTable struct { PlayerID int64 `bson:"PlayerID,omitempty"` NodeID string `bson:"NodeID,omitempty"` dbLineup.Data `bson:"Data,omitempty"` dbFacade.TableBase `bson:",inline"` } ) func (p *LineupTable) TableName() string { return nameDB.Table_Lineup } func (p *LineupTable) PrimaryValue() any { return p.PlayerID } // func (p *LineupTable) Filter() bson.M { // return bson.M{ // nameDB.Field_NodeID: p.NodeID, // nameDB.Field_PlayerID: p.PlayerID, // } // } func (p *LineupTable) CheckField() { } func (p *LineupTable) Save2Queue() { save2Queue(p) } func GetLineupTable(playerID int64) *LineupTable { if val, ok := lineupTableCache.GetIfPresent(playerID); ok { return val.(*LineupTable) } lineupTable := GetLineupTableFromDB(playerID) lineupTableCache.Put(playerID, lineupTable) return lineupTable } func GetLineupTableFromDB(playerID int64) *LineupTable { table := newLineupTable(playerID) filter := bson.M{ nameDB.Field_NodeID: table.NodeID, nameDB.Field_PlayerID: table.PlayerID, } err := dbComponent.FindOne(table, filter) if err != nil { if errors.Is(err, mongo.ErrNoDocuments) { clog.Warnf("[playerID = %d] not found, create new one", playerID) return table } clog.Errorf("[playerID = %d] db error = %v", playerID, err) return table } table.CheckField() return table } func newLineupTable(playerID int64) *LineupTable { return &LineupTable{ PlayerID: playerID, NodeID: cherryApp.NodeID(), Data: dbLineup.NewData(), } }