package viking 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" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/sys/db" "go_dreamfactory/utils" "strconv" "github.com/go-redis/redis/v8" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) type ModelSRank struct { modules.MCompModel moduleViking *Viking } func (this *ModelSRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = comm.TableVikingSRank err = this.MCompModel.Init(service, module, comp, options) this.moduleViking = module.(*Viking) //创建uid索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } // 批量查询 func (this *ModelSRank) queryPlayers(uIds []string) (result []*pb.DBVSeasonRecord, err error) { var conn *db.DBConn conn, err = db.Cross() if err != nil { return } result = make([]*pb.DBVSeasonRecord, 0) model := db.NewDBModelByExpired(db.CrossTag(), comm.TableVikingSRank, conn) if _, err = model.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil { //this.module.Errorln(err) return } return } func (this *ModelSRank) CheckCurSeasonData() (bLocal bool, endSeasonTime int64) { var subTime int64 var oneSeason int64 openTime := this.moduleViking.service.GetOpentime().Unix() // 获取第一个赛季结束的时间 oneSeason = utils.GetTodayZeroTime(openTime) //+ int64((6-d)*3600*24) var c int32 if Continued%Cycle == 0 { c = Continued / Cycle } else { c = Continued/Cycle + 1 } if configure.Now().Unix() > oneSeason { subTime = configure.Now().Unix() - oneSeason subTime = subTime/(14*3600*24) + 1 } // 只需判断当前时间是否大于第c个赛季即可 endSeasonTime = oneSeason endSeasonTime += 14 * 3600 * 24 * int64(c) if endSeasonTime <= configure.Now().Unix() { endSeasonTime = oneSeason + subTime*14*3600*24 return false, endSeasonTime } endSeasonTime = oneSeason + subTime*14*3600*24 return true, endSeasonTime } // 检查上一个赛季实在本服还是在跨服 func (this *ModelSRank) CheckPreSeasonData() (bLocal bool) { openTime := this.moduleViking.service.GetOpentime().Unix() // 获取第一个赛季结束的时间 endSeasonTime := utils.GetTodayZeroTime(openTime) //+ int64((6-d)*3600*24) var c int32 if Continued%Cycle == 0 { c = Continued / Cycle } else { c = Continued/Cycle + 1 } // 只需判断当前时间是否大于第c个赛季即可 endSeasonTime += 3600 * 24 * int64(Cycle*c) if endSeasonTime <= configure.Now().Unix()-int64(Cycle*3600*24) { return false } return true } // 记录数据存在跨服 func (this *ModelSRank) CheckSeasonRank(uid string, boosID int32, difficulty int32, line *pb.LineData, huihe int32) { var ( conn_ *db.DBConn err error record *pb.DBVSeasonRecord update map[string]interface{} ) update = make(map[string]interface{}, 0) conn_, err = db.Cross() if err != nil { return } user, err := this.moduleViking.ModuleUser.GetUser(uid) if err != nil { return } model := db.NewDBModelByExpired(db.CrossTag(), comm.TableVikingSRank, conn_) // 写入排行榜 record = &pb.DBVSeasonRecord{ Id: "", Uinfo: &pb.BaseUserInfo{}, Data: make(map[int32]*pb.HuiheData, 0), } if err = model.Get(uid, record); err == mgo.MongodbNil { record.Id = primitive.NewObjectID().Hex() record.Uid = uid record.Data = make(map[int32]*pb.HuiheData, 0) 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] = huihe if _, ok := record.Data[boosID]; !ok { record.Data[boosID] = &pb.HuiheData{ Huihe: 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["uinfo"] = record.Uinfo if _, ok := record.Data[boosID]; !ok { mpLine := make(map[int32]*pb.LineData, 0) mpLine[difficulty] = line tmp := make(map[int32]int32, 0) tmp[difficulty] = huihe record.Data[boosID] = &pb.HuiheData{ Huihe: tmp, Maxnandu: difficulty, Line: mpLine, } } else { if record.Data[boosID].Maxnandu < difficulty { record.Data[boosID].Maxnandu = difficulty } else { if record.Data[boosID].Huihe[difficulty] > huihe { // 不是新记录不写 return } } } record.Data[boosID].Huihe[difficulty] = huihe 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 - huihe) 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.moduleViking.Errorln(err) } } if _, err := pipe.Exec(); err != nil { this.moduleViking.Errorln(err) return } } // 获取排行榜前50的用户名单 func (this *ModelSRank) querySRankUser(bossid int) (ranks []string, err error) { var ( result []string conn *db.DBConn ) conn, err = db.Cross() if err != nil { return } tableName := fmt.Sprintf("%s-%s", db.CrossTag(), this.TableName+strconv.Itoa(int(bossid))) if result, err = conn.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 *ModelSRank) raceSettlement() { var ( result []string Items []*pb.UserAssets err error szReward []*cfg.GameVikingRewardData mailCid string ) for iBossType := 1; iBossType <= 3; iBossType++ { mailCid = fmt.Sprintf("SeasonViking%dReward", iBossType) tableName := this.TableName + strconv.Itoa(int(iBossType)) szReward = this.moduleViking.configure.GetVikingRewardConf() if result, err = this.DBModel.Redis.ZRevRange(fmt.Sprintf("%s-%s", this.DBModel, tableName), 0, comm.MaxRankList).Result(); err != nil { this.moduleViking.Errorln(err) return } if len(result) == 0 { continue } szReward = this.moduleViking.configure.GetVikingRewardConf() for _, v := range szReward { ids := []string{} // 玩家发奖 var iEndIndex int32 if v.RankUp >= int32(len(result)) { iEndIndex = int32(len(result)) } else { iEndIndex = v.RankUp } if v.RankLow > int32(len(result)) { // 没这么多人直接返回 break } ids = append(result[v.RankLow-1 : iEndIndex]) this.moduleViking.mail.SendNewMail(&pb.DBMailData{ // //发邮件 Cid: mailCid, Param: []string{fmt.Sprintf("%d-%d", v.RankLow, v.RankUp)}, CreateTime: uint64(configure.Now().Unix()), Items: Items, }, ids...) } } ctx := context.Background() this.DBModel.Redis.DelAllPrefixkey(ctx, fmt.Sprintf("%s-%s", this.DBModel.ServiceId, "vikingsrank")) this.DBModel.DB.DeleteMany(core.SqlTable(this.TableName), bson.M{}) return }