go_dreamfactory/modules/hunting/model_rank.go
2024-01-05 11:33:06 +08:00

188 lines
4.7 KiB
Go

package hunting
import (
"context"
"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
moduleHunting *Hunting
}
func (this *ModelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableHuntingRank
err = this.MCompModel.Init(service, module, comp, options)
this.moduleHunting = module.(*Hunting)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(comm.TableHuntingRank), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取排行榜数据
func (this *ModelRank) getHuntingRank(uid string) *pb.DBHuntingRecord {
data := &pb.DBHuntingRecord{}
if !db.IsCross() {
if conn_, err := db.Cross(); err == nil {
model := db.NewDBModelByExpired(db.CrossTag(), comm.TableHuntingRank, conn_)
if err := model.Get(uid, data); err != nil {
return data
}
}
}
return data
}
// 获取排行榜前50的用户名单
func (this *ModelRank) querySRankUser(bossid int) (ranks []string, err error) {
var (
result []string
)
tableName := this.TableName + strconv.Itoa(int(bossid))
if result, err = this.Redis.ZRevRange(tableName, 0, comm.MinRankList).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.DBHuntingRecord, err error) {
result = make([]*pb.DBHuntingRecord, 0)
if _, err = this.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil {
//this.module.Errorln(err)
return
}
return
}
// 记录数据存在跨服
func (this *ModelRank) CheckRank(uid string, boosID int32, difficulty int32, line *pb.LineData, costTime int32) {
conn_, err := db.Cross() // 获取跨服数据库对象
if err != nil {
return
}
user, err := this.moduleHunting.ModuleUser.GetUser(uid)
if err != nil {
return
}
model := db.NewDBModelByExpired(db.CrossTag(), comm.TableHuntingRank, conn_)
// 写入排行榜
record := &pb.DBHuntingRecord{
Data: map[int32]*pb.HuntingData{},
}
if err = model.Get(uid, record); err == mgo.MongodbNil {
record.Id = primitive.NewObjectID().Hex()
record.Data = make(map[int32]*pb.HuntingData, 0)
record.Uid = uid
record.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,
}
mpLine := make(map[int32]*pb.LineData, 0)
mpLine[difficulty] = line
tmp := make(map[int32]int32, 0)
tmp[difficulty] = costTime
if _, ok := record.Data[boosID]; !ok {
record.Data[boosID] = &pb.HuntingData{
Costime: tmp,
Maxnandu: difficulty,
Line: mpLine,
}
} else {
if record.Data[boosID].Maxnandu < difficulty {
record.Data[boosID].Maxnandu = difficulty
}
}
model.Add(uid, record)
} else if err == nil {
record.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,
}
update := make(map[string]interface{}, 0)
update["uinfo"] = record.Uinfo
if _, ok := record.Data[boosID]; !ok {
tmp := make(map[int32]int32, 0)
tmp[difficulty] = costTime
mpLine := make(map[int32]*pb.LineData, 0)
mpLine[difficulty] = line
record.Data[boosID] = &pb.HuntingData{
Costime: tmp,
Maxnandu: difficulty,
Line: mpLine,
}
} else {
if record.Data[boosID].Maxnandu < difficulty {
record.Data[boosID].Maxnandu = difficulty
} else {
if record.Data[boosID].Costime[difficulty] > costTime { // 不是新记录不写
return
}
}
}
record.Data[boosID].Costime[difficulty] = costTime
record.Data[boosID].Line[difficulty] = line
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 = difficulty*10000 + (10000 - costTime)
tableName = 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.moduleHunting.Errorln(err)
}
}
if _, err := pipe.Exec(); err != nil {
this.moduleHunting.Errorln(err)
return
}
}