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(user *pb.DBPlunder) (land *pb.DBPlunderLand, err error) { var ( uids []string other []string users []*pb.DBPlunder socre int32 eTime int64 ) eTime = this.module.modelLand.GetEndTime() 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: eTime, } socre = user.Score - 100 if socre < 0 { socre = 0 } users = append(users, user) // 优先加入自己 cur, err := this.DB.Find(core.SqlTable(comm.TablePlunder), bson.M{"score": bson.M{"$gte": socre}, "uid": bson.M{"$ne": user.Uid}, "landid": ""}, options.Find().SetSkip(int64(0)).SetLimit(int64(19))) for cur.Next(context.TODO()) { tmp := &pb.DBPlunder{} if err = cur.Decode(tmp); err == nil { users = append(users, tmp) other = append(other, tmp.Uid) } } sz := utils.RandomNumbers(0, 20, len(users)) for k, v := range users { land.Uinfo[int32(sz[k])] = v.Uinfo } if err = this.AddList(comm.RDS_EMPTY, land.Id, land); err != nil { return } if _, err = this.module.modelRecord.getPlunderRecordData(land.Id); err != nil { return } for _, v := range users { uids = append(uids, v.Uid) } if err = this.module.modelPlunder.RedisDels(other); 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 { fmt.Printf("err:%v", err) 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) } // 获取结束时间 func (this *modelLand) GetEndTime() (newTime int64) { opentime := this.module.service.GetOpentime().Unix() // 开服时间 subTime := (configure.Now().Unix() - opentime) % int64(this.module.ModuleTools.GetGlobalConf().PlunderPvpSeasontime) st := 24 - this.module.ModuleTools.GetGlobalConf().PlunderPvpEndtime newTime = utils.GetZeroTime(configure.Now().Unix()) + subTime*24*3600 - int64(st*3600) return }