From b101d46b823b7622c008d6fb8bb8b761fb82f32a Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Fri, 22 Dec 2023 13:43:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego/sys/mgo/core.go | 5 ++++ lego/sys/mgo/mgo.go | 4 +++- modules/entertainment/modelrecord.go | 23 +++++++++++------- modules/entertainment/room.go | 22 ++++++++--------- modules/modeluserlog.go | 4 +--- pb/entertain_db.pb.go | 35 ++++++++++++++-------------- 6 files changed, 52 insertions(+), 41 deletions(-) diff --git a/lego/sys/mgo/core.go b/lego/sys/mgo/core.go index eb0fc742d..604855773 100644 --- a/lego/sys/mgo/core.go +++ b/lego/sys/mgo/core.go @@ -16,6 +16,7 @@ type ( ListCollectionNames(filter interface{}, opts ...*options.ListCollectionsOptions) ([]string, error) Collection(sqltable core.SqlTable) *mongo.Collection CreateIndex(sqltable core.SqlTable, model mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error) + CreateMany(sqltable core.SqlTable, model []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) DeleteIndex(sqltable core.SqlTable, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) UseSession(fn func(sessionContext mongo.SessionContext) error) error CountDocuments(sqltable core.SqlTable, filter interface{}, opts ...*options.CountOptions) (int64, error) @@ -73,6 +74,10 @@ func Collection(sqltable core.SqlTable) *mongo.Collection { func CreateIndex(sqltable core.SqlTable, model mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error) { return defsys.CreateIndex(sqltable, model, opts...) } + +func CreateMany(sqltable core.SqlTable, model []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) { + return defsys.CreateMany(sqltable, model, opts...) +} func DeleteIndex(sqltable core.SqlTable, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) { return defsys.DeleteIndex(sqltable, name, opts...) } diff --git a/lego/sys/mgo/mgo.go b/lego/sys/mgo/mgo.go index 47bd1b5b6..6e863d5bb 100644 --- a/lego/sys/mgo/mgo.go +++ b/lego/sys/mgo/mgo.go @@ -72,7 +72,9 @@ func (this *Mongodb) getContext() (ctx context.Context) { func (this *Mongodb) CreateIndex(sqltable core.SqlTable, model mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error) { return this.Collection(sqltable).Indexes().CreateOne(this.getContext(), model, opts...) } - +func (this *Mongodb) CreateMany(sqltable core.SqlTable, model []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) { + return this.Collection(sqltable).Indexes().CreateMany(this.getContext(), model, opts...) +} func (this *Mongodb) DeleteIndex(sqltable core.SqlTable, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) { return this.Collection(sqltable).Indexes().DropOne(this.getContext(), name, opts...) } diff --git a/modules/entertainment/modelrecord.go b/modules/entertainment/modelrecord.go index 13deeb1f7..7b48f47ec 100644 --- a/modules/entertainment/modelrecord.go +++ b/modules/entertainment/modelrecord.go @@ -10,6 +10,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + mgooptions "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/x/bsonx" ) @@ -23,16 +24,20 @@ func (this *modelRecode) Init(service core.IService, module core.IModule, comp c this.TableName = comm.TableEntertainRecode this.MCompModel.Init(service, module, comp, opt) this.module = module.(*Entertainment) + // 通过uid创建索引 - this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ - Keys: bsonx.Doc{{Key: "p1", Value: bsonx.Int32(1)}}, - }) - this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ - Keys: bsonx.Doc{{Key: "p2", Value: bsonx.Int32(1)}}, - }) - this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ - Keys: bsonx.Doc{{Key: "ctime", Value: bsonx.Int32(1)}}, - }) + if _, err = this.DB.CreateMany(core.SqlTable(this.TableName), []mongo.IndexModel{ + { + Keys: bsonx.Doc{{Key: "p1", Value: bsonx.Int32(1)}, + {Key: "p2", Value: bsonx.Int32(1)}}, + }, { + Keys: bson.M{"createtime": -1}, + Options: mgooptions.Index().SetExpireAfterSeconds(7 * 24 * 3600), + }, + }); err != nil { + return + } + return } diff --git a/modules/entertainment/room.go b/modules/entertainment/room.go index b16643983..e3ab534d4 100644 --- a/modules/entertainment/room.go +++ b/modules/entertainment/room.go @@ -619,17 +619,17 @@ func (this *Room) GameOver(winner *pb.PlayerData) (errdata *pb.ErrorData) { this.module.modelRank.updateXxlRank(pl...) // 写记录 recode1 := &pb.DBXxlRecord{ - Id: primitive.NewObjectID().Hex(), - P1: this.player1.Userinfo.Uid, - P2: this.player2.Userinfo.Uid, - Uinfo1: this.player1.Userinfo, - Uinfo2: this.player2.Userinfo, - P1Score: this.player1.Score, - P2Score: this.player2.Score, - Win: winner.Userinfo.Uid, - Winscore: winScore, - Lostscore: lostScore, - Ctime: configure.Now().Unix(), + Id: primitive.NewObjectID().Hex(), + P1: this.player1.Userinfo.Uid, + P2: this.player2.Userinfo.Uid, + Uinfo1: this.player1.Userinfo, + Uinfo2: this.player2.Userinfo, + P1Score: this.player1.Score, + P2Score: this.player2.Score, + Win: winner.Userinfo.Uid, + Winscore: winScore, + Lostscore: lostScore, + Createtime: configure.Now().Unix(), } this.module.modelRecode.updateXxlRecord(recode1) diff --git a/modules/modeluserlog.go b/modules/modeluserlog.go index fcc283d69..8f1586313 100644 --- a/modules/modeluserlog.go +++ b/modules/modeluserlog.go @@ -42,7 +42,6 @@ func (this *modelUserLog) Init(service core.IService, module core.IModule, comp // 用户操作明细数据记录 func (this *modelUserLog) AddUserLog(uid string, req interface{}, itype int32, tag string, data interface{}) { var ( - dbModel *db.DBModel err error jsonStr []byte logType string @@ -77,8 +76,7 @@ func (this *modelUserLog) AddUserLog(uid string, req interface{}, itype int32, t if db.IsCross() { //如果是跨服 直接找到对应的本服 if tag, _, b := utils.UIdSplit(uid); b { if conn, err := db.ServerDBConn(tag); err == nil { - dbModel = db.NewDBModel(comm.TableEntertainm, conn) - if _, err = dbModel.DB.InsertOne(core.SqlTable(this.TableName), &pb.DBUserLog{ + if _, err = conn.Mgo.InsertOne(core.SqlTable(this.TableName), &pb.DBUserLog{ Id: primitive.NewObjectID().Hex(), Uid: uid, ExpireAt: time.Now().Add(time.Hour * 24 * 8).Unix(), diff --git a/pb/entertain_db.pb.go b/pb/entertain_db.pb.go index 7f56c36fe..7cd49e882 100644 --- a/pb/entertain_db.pb.go +++ b/pb/entertain_db.pb.go @@ -768,17 +768,17 @@ type DBXxlRecord struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //ID - P1 string `protobuf:"bytes,2,opt,name=p1,proto3" json:"p1"` // 服务端索引用 - P2 string `protobuf:"bytes,3,opt,name=p2,proto3" json:"p2"` // 服务端索引用 - Uinfo1 *BaseUserInfo `protobuf:"bytes,4,opt,name=uinfo1,proto3" json:"uinfo1"` //p1基本信息 - Uinfo2 *BaseUserInfo `protobuf:"bytes,5,opt,name=uinfo2,proto3" json:"uinfo2"` //p2基本信息 - P1Score int32 `protobuf:"varint,6,opt,name=p1score,proto3" json:"p1score"` // p1 得分 - P2Score int32 `protobuf:"varint,7,opt,name=p2score,proto3" json:"p2score"` // p2 得分 - Win string `protobuf:"bytes,8,opt,name=win,proto3" json:"win"` // uid 胜利 - Winscore int32 `protobuf:"varint,9,opt,name=winscore,proto3" json:"winscore"` - Lostscore int32 `protobuf:"varint,10,opt,name=lostscore,proto3" json:"lostscore"` - Ctime int64 `protobuf:"varint,11,opt,name=ctime,proto3" json:"ctime"` // 创建时间 + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //ID + P1 string `protobuf:"bytes,2,opt,name=p1,proto3" json:"p1"` // 服务端索引用 + P2 string `protobuf:"bytes,3,opt,name=p2,proto3" json:"p2"` // 服务端索引用 + Uinfo1 *BaseUserInfo `protobuf:"bytes,4,opt,name=uinfo1,proto3" json:"uinfo1"` //p1基本信息 + Uinfo2 *BaseUserInfo `protobuf:"bytes,5,opt,name=uinfo2,proto3" json:"uinfo2"` //p2基本信息 + P1Score int32 `protobuf:"varint,6,opt,name=p1score,proto3" json:"p1score"` // p1 得分 + P2Score int32 `protobuf:"varint,7,opt,name=p2score,proto3" json:"p2score"` // p2 得分 + Win string `protobuf:"bytes,8,opt,name=win,proto3" json:"win"` // uid 胜利 + Winscore int32 `protobuf:"varint,9,opt,name=winscore,proto3" json:"winscore"` + Lostscore int32 `protobuf:"varint,10,opt,name=lostscore,proto3" json:"lostscore"` + Createtime int64 `protobuf:"varint,11,opt,name=createtime,proto3" json:"createtime" bson:"createtime"` // } func (x *DBXxlRecord) Reset() { @@ -883,9 +883,9 @@ func (x *DBXxlRecord) GetLostscore() int32 { return 0 } -func (x *DBXxlRecord) GetCtime() int64 { +func (x *DBXxlRecord) GetCreatetime() int64 { if x != nil { - return x.Ctime + return x.Createtime } return 0 } @@ -1027,7 +1027,7 @@ var file_entertain_entertain_db_proto_rawDesc = []byte{ 0x12, 0x1e, 0x0a, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x75, 0x6d, 0x65, 0x65, 0x78, 0x70, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x63, 0x6f, 0x6e, 0x73, 0x75, 0x6d, 0x65, 0x65, 0x78, 0x70, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x61, 0x78, 0x73, 0x6f, 0x63, 0x72, 0x65, 0x18, 0x05, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x08, 0x6d, 0x61, 0x78, 0x73, 0x6f, 0x63, 0x72, 0x65, 0x22, 0xa1, 0x02, 0x0a, + 0x28, 0x05, 0x52, 0x08, 0x6d, 0x61, 0x78, 0x73, 0x6f, 0x63, 0x72, 0x65, 0x22, 0xab, 0x02, 0x0a, 0x0b, 0x44, 0x42, 0x58, 0x78, 0x6c, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x70, 0x31, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x70, 0x31, 0x12, 0x0e, 0x0a, 0x02, @@ -1044,9 +1044,10 @@ var file_entertain_entertain_db_proto_rawDesc = []byte{ 0x12, 0x1a, 0x0a, 0x08, 0x77, 0x69, 0x6e, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x77, 0x69, 0x6e, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x09, 0x6c, 0x6f, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x74, - 0x69, 0x6d, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x63, 0x74, 0x69, 0x6d, 0x65, - 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x09, 0x6c, 0x6f, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x63, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, + 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x74, 0x69, 0x6d, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, + 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var (