go_dreamfactory/modules/pagoda/model_rank.go
2023-01-30 18:11:30 +08:00

165 lines
4.2 KiB
Go

package pagoda
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"strconv"
"time"
//"go_dreamfactory/lego/sys/redis"
"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"
)
var floorRankKey = "pagoda:floor"
type ModelRank struct {
modules.MCompModel
modulePagoda *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.modulePagoda = module.(*Pagoda)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(comm.TableMail), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// func (this *ModelRank) GetRankData() (data []*pb.DBPagodaRecord, err error) {
// data = make([]*pb.DBPagodaRecord, 0)
// if conn, err := db.Cross(); err == nil {
// err = conn.Redis.LRange(comm.TableSeasonRankList, 0, -1, &data)
// }
// //err = this.Redis.LRange(comm.TablePagodaRankList, 0, -1, &data)
// return
// }
// 写记录
func (this *ModelRank) AddPagodaRecord(uid string, costTime, pagodaId, pagodaType int32) (err error) {
userinfo := this.modulePagoda.ModuleUser.GetUser(uid)
newData := &pb.DBPagodaRecord{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
PagodaId: pagodaId,
Type: pagodaType,
Nickname: userinfo.Name,
Icon: "", // icon 暂无
Lv: userinfo.Lv,
CostTime: costTime,
}
err = this.AddList(uid, newData.Id, newData)
return
}
func (this *ModelRank) getPagodaRankList(uid string) []*pb.DBPagodaRecord {
pagodaRank := make([]*pb.DBPagodaRecord, 0)
err := this.GetList(uid, &pagodaRank)
if err != nil {
return nil
}
return pagodaRank
}
func (this *ModelRank) getPagodaRankListByFloorid(uid string, floorid int32) *pb.DBPagodaRecord {
pagodaRank := make([]*pb.DBPagodaRecord, 0)
err := this.GetList(uid, &pagodaRank)
if err != nil {
return nil
}
for _, v := range pagodaRank {
if v.PagodaId == floorid {
return v
}
}
return nil
}
func (this *ModelRank) SetNormalPagodaRankList(tableName string, score int32, uid string) {
var (
pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO())
menbers *redis.Z
)
menbers = &redis.Z{Score: float64(score), Member: uid}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
dock, err1 := cmd.Result()
if err1 != nil {
this.modulePagoda.Errorln(dock, err1)
}
}
if _, err := pipe.Exec(); err != nil {
this.modulePagoda.Errorln(err)
return
}
}
func (this *ModelRank) seasonSettlement() {
rankReward := this.modulePagoda.configure.GetPagodaSeasonReward()
if rankReward == nil {
return
}
if !db.IsCross() {
if conn, err := db.Cross(); err == nil {
var (
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
Items []*pb.UserAssets
)
rd := pipe.ZRevRange("pagodaSeasonRank", 0, comm.MaxRankList)
_dataList := rd.Val()
model1 := db.NewDBModel(comm.TableSeasonPagoda, time.Hour, conn)
for index, v := range _dataList {
result := &pb.DBPagodaRecord{}
//result := &pb.DBVikingRank{}
if err := model1.Get(v, result); err == nil {
if userinfo := this.modulePagoda.ModuleUser.GetUser(result.Uid); userinfo != nil {
Items = make([]*pb.UserAssets, 0) //TO 排名配置
for _, v := range rankReward {
if len(v.Ranking) != 2 {
continue
}
if index >= int(v.Ranking[0]) && index <= int(v.Ranking[1]) {
for _, v1 := range v.Reward {
Items = append(Items, &pb.UserAssets{
A: v1.A,
T: v1.T,
N: v1.N,
})
}
this.modulePagoda.mail.SendNewMail(&pb.DBMailData{
CreateTime: uint64(configure.Now().Unix()),
Items: Items,
Cid: "SeasonPagodaReward",
Param: []string{strconv.Itoa(index + 1)}, // 名次
}, result.Uid)
break
}
}
}
}
}
}
}
}