chat_table.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package db
  2. import (
  3. "errors"
  4. cherryApp "f1-game/internal/cherry_app"
  5. dbFacade "f1-game/internal/component/db/facade"
  6. "f1-game/internal/enum"
  7. nameDB "f1-game/internal/name/db"
  8. dbChat "f1-game/nodes/game/internal/db/data/chat"
  9. clog "github.com/cherry-game/cherry/logger"
  10. "go.mongodb.org/mongo-driver/v2/bson"
  11. "go.mongodb.org/mongo-driver/v2/mongo"
  12. "go.mongodb.org/mongo-driver/v2/mongo/options"
  13. )
  14. type (
  15. // ChatTable 玩家聊天表
  16. ChatTable struct {
  17. PlayerID int64 `bson:"PlayerID,omitempty"`
  18. NodeID string `bson:"NodeID,omitempty"`
  19. dbChat.Data `bson:"Data,omitempty"`
  20. dbFacade.TableBase `bson:",inline"`
  21. }
  22. )
  23. func (p *ChatTable) TableName() string {
  24. return nameDB.Table_Chat
  25. }
  26. func (p *ChatTable) PrimaryValue() any {
  27. return p.PlayerID
  28. }
  29. func (p *ChatTable) Indexes() []mongo.IndexModel {
  30. var list []mongo.IndexModel
  31. list = append(list, mongo.IndexModel{
  32. Keys: bson.D{
  33. {
  34. Key: nameDB.Field_NodeID,
  35. Value: 1, // 升序
  36. },
  37. {
  38. Key: nameDB.Field_PlayerID,
  39. Value: 1, // 升序
  40. },
  41. },
  42. Options: options.Index().SetUnique(true),
  43. })
  44. return list
  45. }
  46. func (p *ChatTable) Save2Queue() {
  47. save2Queue(p)
  48. }
  49. func GetChatTable(playerID int64) *ChatTable {
  50. if val, ok := chatTableCache.GetIfPresent(playerID); ok {
  51. return val.(*ChatTable)
  52. }
  53. chatTable := GetChatTableFromDB(playerID)
  54. chatTableCache.Put(playerID, chatTable)
  55. return chatTable
  56. }
  57. func GetChatTableFromDB(playerID int64) *ChatTable {
  58. table := newChatTable(playerID)
  59. filter := bson.M{
  60. nameDB.Field_NodeID: table.NodeID,
  61. nameDB.Field_PlayerID: table.PlayerID,
  62. }
  63. err := dbComponent.FindOne(table, filter)
  64. if err != nil {
  65. if errors.Is(err, mongo.ErrNoDocuments) {
  66. clog.Warnf("[playerID = %d] not found, create new one", playerID)
  67. return table
  68. }
  69. clog.Errorf("[playerID = %d] db error = %v", playerID, err)
  70. return table
  71. }
  72. table.CheckField()
  73. return table
  74. }
  75. func newChatTable(playerID int64) *ChatTable {
  76. return &ChatTable{
  77. PlayerID: playerID,
  78. NodeID: cherryApp.NodeID(),
  79. Data: dbChat.NewData(),
  80. }
  81. }
  82. // 获取创建的群组数量
  83. func (p *ChatTable) GetCreateGroupCount() int {
  84. var count int
  85. for _, chatRoom := range p.ChatRoomList {
  86. if chatRoom.ChatType != enum.ChatType_Group {
  87. continue
  88. }
  89. if chatRoom.OwnerPlayerID == p.PlayerID {
  90. count++
  91. }
  92. }
  93. return count
  94. }
  95. // 判断是否是该群组的群主
  96. func (p *ChatTable) IsGroupRoomOwner(chatID int64) bool {
  97. info, found := p.GetChatRoom(enum.ChatType_Group, chatID)
  98. if !found {
  99. return false
  100. }
  101. return info.OwnerPlayerID == p.PlayerID
  102. }