go_dreamfactory/modules/plunder/model_land.go
2024-01-24 09:35:11 +08:00

116 lines
3.2 KiB
Go

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
info []*pb.DBPlunder
curUids []string // 过滤后的玩家
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.TableUser), bson.M{"plunder": bson.M{"$gte": limtSocre}, "name": bson.M{"$ne": ""}}, options.Find().SetSkip(int64(0)).SetLimit(int64(30)))
for cur.Next(context.TODO()) {
tmp := &pb.DBUser{}
if err = cur.Decode(tmp); err == nil {
if uid != tmp.Uid {
uids = append(uids, tmp.Uid)
}
}
}
if info, err = this.module.modelPlunder.queryPlunderInfos(uids, land.Id); err != nil {
return
}
for _, v := range info {
if v.Landid != "" { // 过滤
curUids = append(curUids, v.Uid)
}
}
if users, err = this.module.ModuleUser.GetUsers(curUids); err == nil {
for _, v := range users {
uInfos = append(uInfos, comm.GetUserBaseInfo(v))
if len(uInfos) >= 20 {
break
}
}
}
sz := utils.RandomNumbers(0, 20, len(uInfos))
for k, v := range uInfos {
land.Uinfo[int32(sz[k])] = v
}
err = this.AddList(comm.RDS_EMPTY, land.Id, land)
this.module.modelRecord.getPlunderRecordData(land.Id) // 创建一条记录数据
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)
}