package parkour import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "math/rand" "sync" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/x/bsonx" ) // /竞速数据模块 type ModelParkourComp struct { modules.MCompModel module *Parkour refresh time.Time //上一次刷新时间 lock sync.RWMutex users []*Recommend //推荐用户信息 } // 组件初始化接口 func (this *ModelParkourComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.TableName = comm.TableParkour this.MCompModel.Init(service, module, comp, opt) this.module = module.(*Parkour) //创建uid索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } func (this *ModelParkourComp) Start() (err error) { err = this.MCompModel.Start() go this.refreshlist() return } // 获取推荐用户 func (this *ModelParkourComp) getrusers(uid string) (recommend []*pb.DBRaceMember, err error) { var ( users []*Recommend ondata bool ) this.lock.RLock() if len(this.users) == 0 { ondata = true } this.lock.RUnlock() if ondata { //没有数据 重新获取 this.refreshlist() } this.lock.RLock() users = make([]*Recommend, 0, len(this.users)) for _, v := range this.users { if v.parkour.User.Uid != uid { users = append(users, v) } } this.lock.RUnlock() var num int = 6 if len(users) < 6 { num = len(users) } recommend = make([]*pb.DBRaceMember, num) r := rand.New(rand.NewSource(configure.Now().Unix())) for i, v := range r.Perm(num) { if users[v].member == nil { if users[v].user, err = this.module.ModuleUser.GetUser(users[v].parkour.User.Uid); err != nil { continue } users[v].member = &pb.DBRaceMember{ User: users[v].member.User, } } recommend[i] = users[v].member } return } // 匹配机器人 func (this *ModelParkourComp) matcheAI(dan, num int32) (results []*pb.DBRaceMember, err error) { var ( robots []*cfg.GameRobotData dragon *pb.DBDragon ) results = make([]*pb.DBRaceMember, num) if robots, err = this.module.ModuleTools.RandRobotConfig(num); err != nil { return } for i, v := range robots { if dragon, err = this.module.dragon.CreateRobotDragon(v.Mtsid.T, v.Mtsid.N); err != nil { this.module.Errorln(err) return } results[i] = &pb.DBRaceMember{ User: comm.GetRobotBaseInfo(v), Dan: dan, Mount: dragon.Dragonid, Mlv: dragon.Lv, Property: dragon.Property, Currhp: dragon.Property[comm.Dhp], Isai: true, } } return } // 刷新推荐列表 func (this *ModelParkourComp) refreshlist() { var ( users []*Recommend c *mongo.Cursor err error ) if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{"captainid": ""}, options.Find().SetSort(bson.D{{"integral", -1}}).SetSkip(0).SetLimit(100)); err != nil { this.module.Errorln(err) return } else { result := make([]*pb.DBParkour, 0, c.RemainingBatchLength()) for c.Next(context.Background()) { tmp := &pb.DBParkour{} if err = c.Decode(tmp); err != nil { log.Errorln(err) } else { result = append(result, tmp) } } users = make([]*Recommend, len(result)) for i, v := range result { users[i] = &Recommend{ parkour: v, user: nil, } } this.lock.Lock() this.users = users this.lock.Unlock() } } func (this *ModelParkourComp) queryinfo(uid string) (result *pb.DBParkour, err error) { result = &pb.DBParkour{} if err = this.Get(uid, result); err != nil && err != mgo.MongodbNil { this.module.Errorln(err) return } if err == mgo.MongodbNil { var user *pb.DBUser if user, err = this.module.ModuleUser.GetUser(uid); err != nil { err = fmt.Errorf("no found udata:%s", uid) return } result = &pb.DBParkour{ Id: primitive.NewObjectID().Hex(), Uid: uid, User: comm.GetUserBaseInfo(user), State: pb.RaceTeamState_resting, Dan: 1, Property: make(map[string]int32), Weekreward: make(map[int32]bool), } if err = this.Add(uid, result); err != nil { this.module.Errorln(err) return } } err = nil return } func (this *ModelParkourComp) queryinfos(uids []string) (results []*pb.DBParkour, err error) { results = make([]*pb.DBParkour, 0) var ( onfound []string newdata map[string]interface{} = make(map[string]interface{}) ) if onfound, err = this.Gets(uids, &results); err != nil { this.module.Errorln(err) return } if len(onfound) > 0 { for _, v := range onfound { var user *pb.DBUser if user, err = this.module.ModuleUser.GetUser(v); err != nil { err = fmt.Errorf("no found udata:%s", v) return } temp := &pb.DBParkour{ Id: primitive.NewObjectID().Hex(), User: comm.GetUserBaseInfo(user), State: pb.RaceTeamState_resting, Weekreward: make(map[int32]bool), } newdata[v] = temp } if err = this.Adds(newdata); err != nil { this.module.Errorln(err) return } } return }