88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package viking
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/redis/pipe"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
type ModelRank struct {
|
|
modules.MCompModel
|
|
moduleViking *Viking
|
|
}
|
|
|
|
func (this *ModelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableVikingRank
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.moduleViking = module.(*Viking)
|
|
//创建uid索引
|
|
this.DB.CreateIndex(core.SqlTable(comm.TableMail), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
func (this *ModelRank) AddRankList(uId string, id string, data *pb.DBVikingRank) (err error) {
|
|
if err = this.AddList(uId, id, data); err != nil {
|
|
return
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 获取排行榜数据
|
|
func (this *ModelRank) getVikingRankList(uid string) []*pb.DBVikingRank {
|
|
ranks := make([]*pb.DBVikingRank, 0)
|
|
err := this.GetList(uid, &ranks)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return ranks
|
|
}
|
|
|
|
func (this *ModelRank) getVikingRankListByBossType(uid string, bossType int32) *pb.DBVikingRank {
|
|
ranks := make([]*pb.DBVikingRank, 0)
|
|
err := this.GetList(uid, &ranks)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
for _, v := range ranks {
|
|
if v.Bosstype == bossType {
|
|
return v
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 排行数据写跨服
|
|
func (this *ModelRank) SetRankListData(tableName string, score int64, objId string) {
|
|
if !db.IsCross() {
|
|
if conn, err := db.Cross(); err == nil {
|
|
var (
|
|
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
|
|
menbers *redis.Z
|
|
)
|
|
|
|
menbers = &redis.Z{Score: float64(score), Member: objId}
|
|
|
|
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
|
|
|
|
dock, err1 := cmd.Result()
|
|
if err1 != nil {
|
|
this.moduleViking.Errorln(dock, err1)
|
|
}
|
|
}
|
|
if _, err := pipe.Exec(); err != nil {
|
|
this.moduleViking.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|