/* 模块名:viking 描述:维京远征 开发:梅雄风 */ package viking import ( "context" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis/pipe" "go_dreamfactory/utils" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" "go_dreamfactory/sys/db" "math" "strconv" "github.com/go-redis/redis/v8" "go.mongodb.org/mongo-driver/bson/primitive" ) type Viking struct { modules.ModuleBase modelViking *modelViking api *apiComp configure *configureComp modulerank *ModelRank battle comm.IBattle service base.IRPCXService } const ( Cycle int32 = 14 // 持续14天 Continued int32 = 30 ) func NewModule() core.IModule { return &Viking{} } func (this *Viking) GetType() core.M_Modules { return comm.ModuleViking } func (this *Viking) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { if err = this.ModuleBase.Init(service, module, options); err != nil { return } this.service = service.(base.IRPCXService) return } func (this *Viking) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.modelViking = this.RegisterComp(new(modelViking)).(*modelViking) this.modulerank = this.RegisterComp(new(ModelRank)).(*ModelRank) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } // 接口信息 func (this *Viking) ModifyVikingData(uid string, data map[string]interface{}) (errdata *pb.ErrorData) { err := this.modelViking.modifyVikingDataByObjId(uid, data) if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } } return } func (this *Viking) Start() (err error) { if err = this.ModuleBase.Start(); err != nil { return } var module core.IModule if module, err = this.service.GetModule(comm.ModuleBattle); err != nil { return } this.battle = module.(comm.IBattle) this.CheckCurSeasonData() return } func (this *Viking) CheckUserBaseVikingInfo(uid string) (data []*pb.DBVikingRank) { list, err := this.modelViking.getVikingList(uid) if err == nil { for k := range list.Boss { if k <= 3 { _d := this.modulerank.getVikingRankListByBossType(uid, k) if _d != nil { data = append(data, _d) } } } } return } // 记录数据存在跨服 func (this *Viking) CheckRank(uid string, boosID int32, difficulty int32, report *pb.BattleReport) { conn_, err := db.Cross() // 获取跨服数据库对象 if err != nil { return } userinfo, err := this.ModuleUser.GetUser(uid) if err != nil { return } model := db.NewDBModelByExpired(comm.TableVikingRank, conn_) costTime := report.Costtime szLine := make([]*pb.LineUp, len(report.Info.Redflist[0].Team)) Leadpos := 0 if report != nil && report.Info != nil && len(report.Info.Redflist) > 0 { costTime = report.Costtime Leadpos = int(report.Info.Redflist[0].Leadpos) for i, v := range report.Info.Redflist[0].Team { if v != nil { szLine[i] = &pb.LineUp{ Cid: v.HeroID, Star: v.Star, Lv: v.Lv, } } } } // 写入排行榜 objID := "" bFind := false ranks := make([]*pb.DBVikingRank, 0) model.GetList(uid, &ranks) for _, v := range ranks { if v.Bosstype == boosID { mapRankData := make(map[string]interface{}, 0) mapRankData["difficulty"] = difficulty mapRankData["bosstype"] = boosID mapRankData["Leadpos"] = Leadpos mapRankData["line"] = szLine mapRankData["costTime"] = costTime model.ChangeList(uid, v.Id, mapRankData) objID = v.Id bFind = true break } } if !bFind { new := &pb.DBVikingRank{ Id: primitive.NewObjectID().Hex(), Uid: uid, Difficulty: difficulty, Bosstype: boosID, Nickname: userinfo.Name, Skin: userinfo.CurSkin, Lv: userinfo.Lv, Leadpos: int32(Leadpos), Line: szLine, CostTime: costTime, Sex: userinfo.Gender, Title: userinfo.Curtitle, } objID = new.Id model.AddList(uid, new.Id, new) } var ( pipe *pipe.RedisPipe = conn_.Redis.RedisPipe(context.TODO()) menbers *redis.Z tableName string score int64 ) score = int64(difficulty)<<31 + int64(math.MaxInt32-costTime) tableName = "vikingRank" + strconv.Itoa(int(boosID)) strKey := "vikingrank:" + uid + "-" + objID // 自定义key menbers = &redis.Z{Score: float64(score), Member: strKey} if cmd := pipe.ZAdd(tableName, menbers); cmd != nil { dock, err1 := cmd.Result() if err1 != nil { this.Errorln(dock, err1) } } if _, err := pipe.Exec(); err != nil { this.Errorln(err) return } } //红点查询 func (this *Viking) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]*pb.ReddotItem) { reddot = make(map[comm.ReddotType]*pb.ReddotItem) for _, v := range rid { switch v { case comm.Reddot13102: reddot[comm.Reddot13102] = &pb.ReddotItem{ Rid: int32(comm.Reddot13102), Activated: this.modelViking.checkReddot31(session), } break } } return } // 解锁远征所有难度 func (this *Viking) CompleteAllLevel(session comm.IUserSession) (errdata *pb.ErrorData) { list, err := this.modelViking.getVikingList(session.GetUserId()) if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_DBError, Title: pb.ErrorCode_DBError.ToString(), Message: err.Error(), } return } list.Boss = make(map[int32]int32) list.BossTime = make(map[string]int32) conf := this.configure.GetVikingBossAllData() for k, v := range conf { list.Boss[k] = v } mapData := make(map[string]interface{}, 0) mapData["boss"] = list.Boss mapData["bossTime"] = list.BossTime errdata = this.ModifyVikingData(session.GetUserId(), mapData) session.SendMsg(string(this.GetType()), VikingGetListResp, &pb.VikingGetListResp{Data: list}) return } // 检查当前赛季是在本服还是在跨服 func (this *Viking) CheckCurSeasonData() (bLocal bool, endSeasonTime int64) { var subTime int64 var oneSeason int64 openTime := this.service.GetOpentime().Unix() this.Debugf("%d", openTime) // 获取第一个赛季结束的时间 oneSeason = utils.GetTodayZeroTime(openTime) //+ int64((6-d)*3600*24) this.Debugf("%d", oneSeason) 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 *Viking) CheckPreSeasonData() (bLocal bool) { openTime := this.service.GetOpentime().Unix() this.Debugf("%d", openTime) // 获取第一个赛季结束的时间 endSeasonTime := utils.GetTodayZeroTime(openTime) //+ int64((6-d)*3600*24) this.Debugf("%d", endSeasonTime) 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 *Viking) CheckSeasonRank(uid string, boosID int32, difficulty int32, leadpos int32, szLine []*pb.LineUp, huihe int32) { var ( conn_ *db.DBConn err error bcross bool ) bcross, _ = this.CheckCurSeasonData() if bcross { conn_, err = db.Local() // 获取跨服数据库对象 if err != nil { return } } else { //数据记录在本服 conn_, err = db.Cross() if err != nil { return } } userinfo, err := this.ModuleUser.GetUser(uid) if err != nil { return } model := db.NewDBModelByExpired(comm.TableVikingSRank, conn_) // 写入排行榜 objID := "" bFind := false ranks := make([]*pb.DBVSeasonRank, 0) model.GetList(uid, &ranks) for _, v := range ranks { if v.Bosstype == boosID && v.Difficulty <= difficulty { if v.Difficulty == difficulty { // 难度相等 则回合数少则更新 if v.Huihe != 0 && v.Huihe <= huihe { break } } mapRankData := make(map[string]interface{}, 0) mapRankData["difficulty"] = difficulty mapRankData["bosstype"] = boosID mapRankData["Leadpos"] = leadpos mapRankData["line"] = szLine mapRankData["huihe"] = huihe model.ChangeList(uid, v.Id, mapRankData) objID = v.Id bFind = true break } } if !bFind { new := &pb.DBVSeasonRank{ Id: primitive.NewObjectID().Hex(), Uid: uid, Difficulty: difficulty, Bosstype: boosID, Nickname: userinfo.Name, Skin: userinfo.CurSkin, Lv: userinfo.Lv, Leadpos: leadpos, Line: szLine, Huihe: huihe, Sid: userinfo.Sid, Sex: userinfo.Gender, Title: userinfo.Curtitle, } objID = new.Id model.AddList(uid, new.Id, new) } var ( pipe *pipe.RedisPipe = conn_.Redis.RedisPipe(context.TODO()) menbers *redis.Z tableName string score int32 ) score = difficulty<<16 + huihe tableName = "vSeasonRank" + strconv.Itoa(int(boosID)) strKey := "vikingsrank:" + uid + "-" + objID // 自定义key menbers = &redis.Z{Score: float64(score), Member: strKey} if cmd := pipe.ZAdd(tableName, menbers); cmd != nil { dock, err1 := cmd.Result() if err1 != nil { this.Errorln(dock, err1) } } if _, err := pipe.Exec(); err != nil { this.Errorln(err) return } // 以下是测试 // var ( // szRank []*pb.DBVSeasonRank // rd *redis.StringSliceCmd // ) // //dbModel := db.NewDBModel(comm.TableVikingSRank, 0, conn_) // pipe = conn_.Redis.RedisPipe(context.TODO()) // rd = pipe.ZRevRange("vSeasonRank"+strconv.Itoa(int(boosID)), 0, comm.MaxRankList) // if _, err := pipe.Exec(); err != nil { // this.Errorln(err) // return // } // _dataList := rd.Val() // for _, v := range _dataList { // result := &pb.DBVSeasonRank{} // if err := model.Redis.HGetAll(v, result); err == nil { // szRank = append(szRank, result) // } // } }