package plunder import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" "go_dreamfactory/utils" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" ) type modelLand struct { modules.MCompModel module *Plunder } func (this *modelLand) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { err = this.MCompModel.Init(service, module, comp, options) this.TableName = comm.TablePlunderLand this.module = module.(*Plunder) return } // 获取岛基本数据 func (this *modelLand) getPlunderLandData(id string) (info *pb.DBPlunderLand, err error) { info = &pb.DBPlunderLand{} if err = this.GetListObj(comm.RDS_EMPTY, id, info); err != nil && err != mgo.MongodbNil { this.module.Errorln(err) return } return } // 修改岛信息 func (this *modelLand) changePlunderLandData(id string, update map[string]interface{}) (err error) { err = this.ChangeList(comm.RDS_EMPTY, id, update) return } func (this *modelLand) createPlunderLandData(uid string) (land *pb.DBPlunderLand, err error) { var ( user *pb.DBUser limtSocre int32 uids []string users []*pb.DBUser uInfos []*pb.BaseUserInfo ) land = &pb.DBPlunderLand{ Id: primitive.NewObjectID().Hex(), Ship: map[string]*pb.ShipData{}, Uinfo: make(map[int32]*pb.BaseUserInfo, 0), Score: make(map[string]int32, 0), Etime: utils.GetTodayZeroTime(configure.Now().Unix()) + 48*3600, // 临时处理 后面走配置 } if user, err = this.module.ModuleUser.GetUser(uid); err != nil { return } limtSocre = user.Plunder - 100 if limtSocre < 0 { limtSocre = 0 } uids = append(uids, uid) // 优先加入自己 uInfos = append(uInfos, comm.GetUserBaseInfo(user)) cur, err := this.DB.Find(core.SqlTable(comm.TablePlunder), bson.M{"score": bson.M{"$gte": limtSocre}, "landid": bson.M{"$ne": ""}}, options.Find().SetSkip(int64(0)).SetLimit(int64(20))) for cur.Next(context.TODO()) { tmp := &pb.DBPlunder{} if err = cur.Decode(tmp); err == nil { if uid != tmp.Uid { uids = append(uids, tmp.Uid) } } } if users, err = this.module.ModuleUser.GetUsers(uids); err == nil { for _, v := range users { uInfos = append(uInfos, comm.GetUserBaseInfo(v)) } } sz := utils.RandomNumbers(0, 20, len(uInfos)) for k, v := range uInfos { land.Uinfo[int32(sz[k])] = v } if err = this.AddList(comm.RDS_EMPTY, land.Id, land); err != nil { return } if _, err = this.module.modelRecord.getPlunderRecordData(land.Id); err != nil { return } if _, err = this.DB.UpdateMany(core.SqlTable(comm.TablePlunder), bson.M{"uid": bson.M{"$in": uids}}, bson.M{"$set": bson.M{"landid": land.Id}}); err != nil { return } return } // 创建分布式锁 func (this *modelLand) landMutexLock(oid string) (result *redis.RedisMutex, err error) { return this.module.modelLand.NewRedisMutex(fmt.Sprintf("%s-%s_lock", this.TableName, oid), 5) }