go_dreamfactory/modules/enchant/model_rank.go

216 lines
5.4 KiB
Go

package enchant
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"strconv"
"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 *Enchant
}
func (this *ModelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableEnchantRank // 挑战记录
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Enchant)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *ModelRank) rankKey(boosType int32) string {
return fmt.Sprintf("%s-%s-%d", this.module.service.GetTag(), this.TableName, boosType)
}
func (this *ModelRank) getEnchantRankList(uid string) []*pb.DBEnchantRank {
ranks := make([]*pb.DBEnchantRank, 0)
err := this.GetList(uid, &ranks)
if err != nil {
return nil
}
return ranks
}
// 获取排行榜前50的用户名单
func (this *ModelRank) queryRankUser(boos int32) (ranks []string, err error) {
var (
result []string
)
tableName := fmt.Sprintf("%s-%s", db.CrossTag(), this.TableName+strconv.Itoa(int(boos)))
if result, err = this.DBModel.Redis.ZRevRange(tableName, 0, comm.MaxRankList).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) updateRank(Score int32, uid string, boos int32) (err error) {
var (
pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO())
menbers *redis.Z
cmd *redis.IntCmd
)
menbers = &redis.Z{Score: float64(Score), Member: uid}
if cmd = pipe.ZAdd(this.rankKey(boos), menbers); err != nil {
this.module.Errorln(err)
}
if _, err = pipe.Exec(); err != nil {
this.module.Errorln(err)
return
}
if _, err = cmd.Result(); err != nil {
this.module.Errorln(err)
return
}
return
}
// 记录数据存在跨服
func (this *ModelRank) CheckRank(uid string, boosID int32, report *pb.BattleReport) {
conn_, err := db.Cross() // 获取跨服数据库对象
if err != nil {
return
}
user, err := this.module.ModuleUser.GetUser(uid)
if err != nil {
return
}
model := db.NewDBModelNoExpired(db.CrossTag(), comm.TableEnchantRank, conn_)
// 写入排行榜
record := &pb.DBEnchantRecord{
Data: map[int32]*pb.DBEnchantRank{},
}
if err = model.Get(uid, record); err == mgo.MongodbNil {
record.Id = primitive.NewObjectID().Hex()
record.Uid = uid
szLine := make([]*pb.LineUp, 0)
var Leadpos int32
if report != nil && len(report.Info.Redflist) > 0 {
Leadpos = report.Info.Redflist[0].Leadpos
for _, v := range report.Info.Redflist[0].Team {
if v != nil {
szLine = append(szLine, &pb.LineUp{
Cid: v.HeroID,
Star: v.Star,
Lv: v.Lv,
})
}
}
}
record.Data[boosID] = &pb.DBEnchantRank{
Uinfo: &pb.BaseUserInfo{
Uid: uid,
Sid: user.Sid,
Name: user.Name,
Gender: user.Gender,
Skin: user.CurSkin,
Aframe: user.Curaframe,
Title: user.Curtitle,
Lv: user.Lv,
},
Line: &pb.LineData{
Leadpos: Leadpos,
Line: szLine,
Bid: report.Info.Id, // 战报id
},
Score: report.Score,
Grade: report.Grade,
Gradegroup: report.Info.Scoregroup,
Battletime: report.Costtime,
}
model.Add(uid, record)
} else if err == nil {
szLine := make([]*pb.LineUp, 0)
var Leadpos int32
if report != nil && len(report.Info.Redflist) > 0 {
Leadpos = report.Info.Redflist[0].Leadpos
for _, v := range report.Info.Redflist[0].Team {
if v != nil {
szLine = append(szLine, &pb.LineUp{
Cid: v.HeroID,
Star: v.Star,
Lv: v.Lv,
})
}
}
}
record.Data[boosID] = &pb.DBEnchantRank{
Uinfo: &pb.BaseUserInfo{
Uid: uid,
Sid: user.Sid,
Name: user.Name,
Gender: user.Gender,
Skin: user.CurSkin,
Aframe: user.Curaframe,
Title: user.Curtitle,
Lv: user.Lv,
},
Line: &pb.LineData{
Leadpos: Leadpos,
Line: szLine,
Bid: report.Info.Id, // 战报id
},
Score: report.Score,
Grade: report.Grade,
Gradegroup: report.Info.Scoregroup,
Battletime: report.Costtime,
}
update := make(map[string]interface{}, 0)
update["data"] = record.Data
model.Change(uid, update)
}
var (
pipe *pipe.RedisPipe = conn_.Redis.RedisPipe(context.TODO())
menbers *redis.Z
tableName string
score int32
)
score = report.Score
tableName = fmt.Sprintf("%s-%s", db.CrossTag(), this.TableName+strconv.Itoa(int(boosID)))
menbers = &redis.Z{Score: float64(score), Member: uid}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
if _, err = cmd.Result(); err != nil {
this.module.Errorln(err)
}
}
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
return
}
}
func (this *ModelRank) queryPlayers(uIds []string) (result []*pb.DBEnchantRecord, err error) {
result = make([]*pb.DBEnchantRecord, 0)
if _, err = this.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
return
}