| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package db
- import (
- "errors"
- cherryApp "f1-game/internal/cherry_app"
- dbFacade "f1-game/internal/component/db/facade"
- "f1-game/internal/enum"
- nameDB "f1-game/internal/name/db"
- dbChat "f1-game/nodes/game/internal/db/data/chat"
- clog "github.com/cherry-game/cherry/logger"
- "go.mongodb.org/mongo-driver/v2/bson"
- "go.mongodb.org/mongo-driver/v2/mongo"
- "go.mongodb.org/mongo-driver/v2/mongo/options"
- )
- type (
- // ChatTable 玩家聊天表
- ChatTable struct {
- PlayerID int64 `bson:"PlayerID,omitempty"`
- NodeID string `bson:"NodeID,omitempty"`
- dbChat.Data `bson:"Data,omitempty"`
- dbFacade.TableBase `bson:",inline"`
- }
- )
- func (p *ChatTable) TableName() string {
- return nameDB.Table_Chat
- }
- func (p *ChatTable) PrimaryValue() any {
- return p.PlayerID
- }
- func (p *ChatTable) Indexes() []mongo.IndexModel {
- var list []mongo.IndexModel
- list = append(list, mongo.IndexModel{
- Keys: bson.D{
- {
- Key: nameDB.Field_NodeID,
- Value: 1, // 升序
- },
- {
- Key: nameDB.Field_PlayerID,
- Value: 1, // 升序
- },
- },
- Options: options.Index().SetUnique(true),
- })
- return list
- }
- func (p *ChatTable) Save2Queue() {
- save2Queue(p)
- }
- func GetChatTable(playerID int64) *ChatTable {
- if val, ok := chatTableCache.GetIfPresent(playerID); ok {
- return val.(*ChatTable)
- }
- chatTable := GetChatTableFromDB(playerID)
- chatTableCache.Put(playerID, chatTable)
- return chatTable
- }
- func GetChatTableFromDB(playerID int64) *ChatTable {
- table := newChatTable(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 newChatTable(playerID int64) *ChatTable {
- return &ChatTable{
- PlayerID: playerID,
- NodeID: cherryApp.NodeID(),
- Data: dbChat.NewData(),
- }
- }
- // 获取创建的群组数量
- func (p *ChatTable) GetCreateGroupCount() int {
- var count int
- for _, chatRoom := range p.ChatRoomList {
- if chatRoom.ChatType != enum.ChatType_Group {
- continue
- }
- if chatRoom.OwnerPlayerID == p.PlayerID {
- count++
- }
- }
- return count
- }
- // 判断是否是该群组的群主
- func (p *ChatTable) IsGroupRoomOwner(chatID int64) bool {
- info, found := p.GetChatRoom(enum.ChatType_Group, chatID)
- if !found {
- return false
- }
- return info.OwnerPlayerID == p.PlayerID
- }
|