go_dreamfactory/modules/plunder/model_land.go
2024-01-17 18:22:09 +08:00

105 lines
2.8 KiB
Go

package plunder
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"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
join map[string]struct{}
curUids []string // 过滤后的玩家
)
land = &pb.DBPlunderLand{
Id: primitive.NewObjectID().Hex(),
Ship: map[string]*pb.ShipData{},
Etime: utils.GetTodayZeroTime(configure.Now().Unix()) + 48*3600, // 临时处理 后面走配置
}
if user, err = this.module.ModuleUser.GetUser(uid); err != nil {
return
}
land.Uinfo = append(land.Uinfo, comm.GetUserBaseInfo(user))
limtSocre = user.Plunder - 100
if limtSocre < 0 {
limtSocre = 0
}
cur, err := this.DB.Find(core.SqlTable(comm.TableUser), bson.M{"gold": bson.M{"$gte": limtSocre}}, 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)
}
}
}
join = make(map[string]struct{})
// 批量查 是否有加入海岛的
info, _ = this.module.modelPlunder.getPlunderDataByUids(uids)
for _, v := range info {
if v.Landid != "" { // 过滤
join[v.Uid] = struct{}{}
}
}
for _, v := range uids { // 过滤已经加入海岛的玩家
if _, ok := join[v]; !ok {
curUids = append(curUids, v)
}
}
if users, err = this.module.ModuleUser.GetUsers(curUids); err == nil {
for _, v := range users {
land.Uinfo = append(land.Uinfo, comm.GetUserBaseInfo(v))
if len(land.Uinfo) > 20 {
break
}
}
}
err = this.AddList(comm.RDS_EMPTY, land.Id, land)
return
}