go_dreamfactory/modules/plunder/model_land.go

127 lines
3.5 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
join map[string]struct{}
curUids []string // 过滤后的玩家
uInfos []*pb.BaseUserInfo
)
land = &pb.DBPlunderLand{
Id: primitive.NewObjectID().Hex(),
Ship: map[string]*pb.ShipData{},
Uinfo: make(map[int32]*pb.BaseUserInfo, 0),
Etime: utils.GetTodayZeroTime(configure.Now().Unix()) + 48*3600, // 临时处理 后面走配置
}
if user, err = this.module.ModuleUser.GetUser(uid); err != nil {
return
}
uInfos = append(uInfos, 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}, "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)
}
}
}
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 {
uInfos = append(uInfos, comm.GetUserBaseInfo(v))
if len(uInfos) >= 20 {
break
}
if list, err := this.module.modelPlunder.getPlunderData(v.Uid); err == nil {
if list.Landid == "" {
list.Landid = land.Id
this.module.modelPlunder.changePlunderData(v.Uid, map[string]interface{}{
"landid": land.Id,
})
}
}
}
}
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)
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)
}