go_dreamfactory/modules/pagoda/model_race.go
2024-01-12 10:27:24 +08:00

181 lines
4.5 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/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"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 ModelRace struct {
modules.MCompModel
module *Pagoda
}
func (this *ModelRace) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = string(comm.TableRacePagoda)
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Pagoda)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 获取爬塔信息
func (this *ModelRace) getPagodaRaceList(uid string) (result *pb.DBPagodaRace, err error) {
result = &pb.DBPagodaRace{
Data: map[int32]*pb.RaceData{},
}
if err = this.Get(uid, result); err != nil && err == mgo.MongodbNil { // 初始一条数据
result.Id = primitive.NewObjectID().Hex()
result.Uid = uid
result.Data = make(map[int32]*pb.RaceData)
result.Reward = make(map[int32]int32)
result.Rtime = configure.Now().Unix()
err = this.Add(uid, result)
return
}
return result, err
}
func (this *ModelRace) ModifyPagodaRaceData(uid string, data map[string]interface{}) error {
return this.Change(uid, data)
}
func (this *ModelRace) getCrossPagodaRaceList(uid string) (result *pb.DBPagodaRace, err error) {
var (
model *db.DBModel
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return
}
result = &pb.DBPagodaRace{
Data: map[int32]*pb.RaceData{},
}
if err = model.Get(uid, result); err != nil && err == mgo.MongodbNil { // 初始一条数据
result.Id = primitive.NewObjectID().Hex()
result.Uid = uid
result.Data = make(map[int32]*pb.RaceData)
result.Rtime = configure.Now().Unix()
if err = model.Add(uid, result); err != nil {
this.module.Errorf("err:%v", err)
return
}
return
}
return result, err
}
// 修改爬塔数据信息
func (this *ModelRace) ModifyCrossPagodaRaceData(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)
}
// 创建一个新的塔数据
func (this *ModelRace) addCrossPagodaRace(uId string, result *pb.DBPagodaRace) (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 *ModelRace) queryRankUser() (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", db.CrossTag(), this.TableName)
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 *ModelRace) queryPlayers(uIds []string) (result []*pb.DBRacePagodaRecord, err error) {
var (
model *db.DBModel
)
if model, err = this.module.GetCrossDBModel(this.TableName); err != nil {
return
}
result = make([]*pb.DBRacePagodaRecord, 0)
if _, err = model.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil {
//this.module.Errorln(err)
return
}
return
}
// 六合塔记录 (存在跨服)
func (this *ModelRace) SetRacePagodaRankList(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", db.CrossTag(), this.TableName)
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
}
}