lineup_table.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package db
  2. import (
  3. "errors"
  4. cherryApp "f1-game/internal/cherry_app"
  5. dbFacade "f1-game/internal/component/db/facade"
  6. nameDB "f1-game/internal/name/db"
  7. dbLineup "f1-game/nodes/game/internal/db/data/lineup"
  8. clog "github.com/cherry-game/cherry/logger"
  9. "go.mongodb.org/mongo-driver/v2/bson"
  10. "go.mongodb.org/mongo-driver/v2/mongo"
  11. )
  12. type (
  13. // LineupTable 阵容表
  14. LineupTable struct {
  15. PlayerID int64 `bson:"PlayerID,omitempty"`
  16. NodeID string `bson:"NodeID,omitempty"`
  17. dbLineup.Data `bson:"Data,omitempty"`
  18. dbFacade.TableBase `bson:",inline"`
  19. }
  20. )
  21. func (p *LineupTable) TableName() string {
  22. return nameDB.Table_Lineup
  23. }
  24. func (p *LineupTable) PrimaryValue() any {
  25. return p.PlayerID
  26. }
  27. // func (p *LineupTable) Filter() bson.M {
  28. // return bson.M{
  29. // nameDB.Field_NodeID: p.NodeID,
  30. // nameDB.Field_PlayerID: p.PlayerID,
  31. // }
  32. // }
  33. func (p *LineupTable) CheckField() {
  34. }
  35. func (p *LineupTable) Save2Queue() {
  36. save2Queue(p)
  37. }
  38. func GetLineupTable(playerID int64) *LineupTable {
  39. if val, ok := lineupTableCache.GetIfPresent(playerID); ok {
  40. return val.(*LineupTable)
  41. }
  42. lineupTable := GetLineupTableFromDB(playerID)
  43. lineupTableCache.Put(playerID, lineupTable)
  44. return lineupTable
  45. }
  46. func GetLineupTableFromDB(playerID int64) *LineupTable {
  47. table := newLineupTable(playerID)
  48. filter := bson.M{
  49. nameDB.Field_NodeID: table.NodeID,
  50. nameDB.Field_PlayerID: table.PlayerID,
  51. }
  52. err := dbComponent.FindOne(table, filter)
  53. if err != nil {
  54. if errors.Is(err, mongo.ErrNoDocuments) {
  55. clog.Warnf("[playerID = %d] not found, create new one", playerID)
  56. return table
  57. }
  58. clog.Errorf("[playerID = %d] db error = %v", playerID, err)
  59. return table
  60. }
  61. table.CheckField()
  62. return table
  63. }
  64. func newLineupTable(playerID int64) *LineupTable {
  65. return &LineupTable{
  66. PlayerID: playerID,
  67. NodeID: cherryApp.NodeID(),
  68. Data: dbLineup.NewData(),
  69. }
  70. }