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 ModelCycle struct { modules.MCompModel module *Pagoda } func (this *ModelCycle) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = string(comm.TableCyclePagoda) 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 *ModelCycle) getPagodaCycleList(uid string) (result *pb.DBPagodaCycle, err error) { result = &pb.DBPagodaCycle{} if err = this.Get(uid, result); err != nil && err == mgo.MongodbNil { // 初始一条数据 result.Id = primitive.NewObjectID().Hex() result.Uid = uid result.Itype = 1 result.Data = make(map[int32]*pb.CycleData, 0) result.Rtime = configure.Now().Unix() if conf, e := this.module.configure.GetPagodaCirculateConf(1, 1); e == nil { result.Etime = configure.Now().Unix() + int64(conf.Time)*24*3600 } else { err = fmt.Errorf("conf not found: %v", e.Error()) return } err = this.Add(uid, result) return } return result, err } func (this *ModelCycle) ModifyPagodaCycleData(uid string, data map[string]interface{}) error { return this.Change(uid, data) } func (this *ModelCycle) getCrossPagodaCycleList(uid string) (result *pb.DBPagodaCycle, err error) { var ( model *db.DBModel ) if model, err = this.module.GetCrossDBModel(this.TableName); err != nil { return } result = &pb.DBPagodaCycle{} if err = model.Get(uid, result); err != nil && err == mgo.MongodbNil { // 初始一条数据 result.Id = primitive.NewObjectID().Hex() result.Uid = uid 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 *ModelCycle) ModifyCrossPagodaCycleData(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 *ModelCycle) addCrossPagodaCycle(uId string, result *pb.DBPagodaCycle) (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 *ModelCycle) 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 *ModelCycle) queryPlayers(uIds []string) (result []*pb.DBCyclePagodaRecord, err error) { var ( model *db.DBModel ) if model, err = this.module.GetCrossDBModel(this.TableName); err != nil { return } result = make([]*pb.DBCyclePagodaRecord, 0) if _, err = model.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil { //this.module.Errorln(err) return } return } // 六合塔记录 (存在跨服) func (this *ModelCycle) SetCyclePagodaRankList(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 } }