go_dreamfactory/modules/pagoda/model_rank.go

165 lines
4.1 KiB
Go

package pagoda
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/sys/db"
//"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type ModelRank struct {
modules.MCompModel
module *Pagoda
}
func (this *ModelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TablePagodaRecord
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Pagoda)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(comm.TablePagodaRecord), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *ModelRank) getcrossPagodaRankList(uid string) *pb.DBPagodaRecord {
record := &pb.DBPagodaRecord{}
if db.IsCross() {
if err := this.Get(uid, record); err == mgo.MongodbNil {
result := &pb.DBPagodaRecord{
Data: make(map[int32]*pb.PagodaRecordData),
}
result.Id = primitive.NewObjectID().Hex()
result.Uid = uid
if err = this.Add(uid, result); err != nil {
this.module.Errorf("err:%v", err)
}
}
} else {
if conn, err := db.Cross(); err == nil {
dbModel := db.NewDBModel(db.CrossTag(), comm.TablePagodaRecord, conn)
if err = dbModel.Get(uid, record); err == mgo.MongodbNil {
result := &pb.DBPagodaRecord{
Data: make(map[int32]*pb.PagodaRecordData),
}
result.Id = primitive.NewObjectID().Hex()
result.Uid = uid
if err = dbModel.Add(uid, result); err != nil {
this.module.Errorf("err:%v", err)
}
}
}
}
return record
}
// 创建一个新的塔数据
func (this *ModelRank) addCrossPagodaRace(uId string, result *pb.DBPagodaRecord) (err error) {
var (
model *db.DBModel
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return
}
result.Id = primitive.NewObjectID().Hex()
result.Uid = uId
if err = model.Add(uId, result); err != nil {
this.module.Errorf("err:%v", err)
return
}
return nil
}
// 获取排行榜前50的用户名单 (跨服)
func (this *ModelRank) queryRankUser(key int32) (ranks []string, err error) {
var (
result []string
model *db.DBModel
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return
}
tableName := fmt.Sprintf("%s-%s-%d", db.CrossTag(), this.TableName, key)
if result, err = model.Redis.ZRevRange(tableName, 0, comm.MaxRankNum).Result(); err != nil {
//this.module.Errorln(err)
return
}
ranks = make([]string, 0)
for i := 0; i < len(result); i += 1 {
ranks = append(ranks, result[i])
}
return
}
func (this *ModelRank) queryPlayers(uIds []string) (result []*pb.DBPagodaRecord, err error) {
var (
model *db.DBModel
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return
}
result = make([]*pb.DBPagodaRecord, 0)
if _, err = model.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil {
//this.module.Errorln(err)
return
}
return
}
func (this *ModelRank) SetPagodaRankList(key int32, score int32, uid string) {
var (
pipe *pipe.RedisPipe
menbers *redis.Z
model *db.DBModel
err error
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return
}
tableName := fmt.Sprintf("%s-%s-%d", db.CrossTag(), this.TableName, key)
pipe = model.Redis.RedisPipe(context.TODO())
menbers = &redis.Z{Score: float64(score), Member: uid}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
dock, err1 := cmd.Result()
if err1 != nil {
this.module.Errorln(dock, err1)
}
}
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
return
}
}
func (this *ModelRank) ModifyCrossPagodaCycleData(uid string, data map[string]interface{}) error {
var (
model *db.DBModel
err error
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return err
}
return model.Change(uid, data)
}