212 lines
5.2 KiB
Go
212 lines
5.2 KiB
Go
package plunder
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/utils"
|
|
"reflect"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.PlunderGetListReq) (errdata *pb.ErrorData) {
|
|
return
|
|
}
|
|
|
|
// 获取基本信息
|
|
func (this *apiComp) GetList(session comm.IUserSession, req *pb.PlunderGetListReq) (errdata *pb.ErrorData) {
|
|
var (
|
|
err error
|
|
list *pb.DBPlunder
|
|
land *pb.DBPlunderLand
|
|
update map[string]interface{}
|
|
)
|
|
|
|
if errdata = this.GetListCheck(session, req); errdata != nil {
|
|
return
|
|
}
|
|
update = make(map[string]interface{})
|
|
if list, err = this.module.modelPlunder.getPlunderData(session); err != nil {
|
|
var customErr = new(comm.CustomError)
|
|
if errors.As(err, &customErr) {
|
|
code := customErr.Code
|
|
errdata = &pb.ErrorData{
|
|
Code: code,
|
|
Title: code.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
} else {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if list.Landid == "" {
|
|
if land, err = this.module.modelLand.createPlunderLandData(list); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
list.Landid = land.Id
|
|
update["landid"] = land.Id
|
|
} else {
|
|
if land, err = this.module.modelLand.getPlunderLandData(list.Landid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
var uids []string
|
|
// 清理岛数据
|
|
if land.Etime < configure.Now().Unix() {
|
|
// 清理之前先发奖
|
|
this.sendRankReward(land.Score)
|
|
for _, v := range land.Uinfo { // 重置成员信息
|
|
uids = append(uids, v.Uid)
|
|
}
|
|
if _, err = this.module.modelPlunder.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
|
|
}
|
|
|
|
if land, err = this.module.modelLand.createPlunderLandData(list); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
list.Landid = land.Id
|
|
update["landid"] = land.Id
|
|
list.Etime = land.Etime
|
|
update["etime"] = land.Etime
|
|
}
|
|
|
|
}
|
|
if len(list.Source) == 0 {
|
|
list.Source, err = this.module.modelPlunder.refreshGoodsInfo()
|
|
list.Setout = []int32{}
|
|
update["setout"] = list.Setout
|
|
update["source"] = list.Source
|
|
|
|
}
|
|
// 校验解锁的是否到期
|
|
for _, v := range list.Line {
|
|
if v.Closetime > 0 && v.Closetime < configure.Now().Unix() {
|
|
v.Closetime = -1
|
|
update["line"] = list.Line
|
|
}
|
|
}
|
|
// 每日重置次数
|
|
if !utils.IsToday(list.Ctime) { //不是同一天 可以重置数据
|
|
list.Ctime = configure.Now().Unix()
|
|
list.Pvpcount = 0
|
|
list.Count = 0
|
|
list.Freecount = 0
|
|
update["pvpcount"] = list.Pvpcount
|
|
update["count"] = list.Count
|
|
update["ctime"] = list.Ctime
|
|
update["freecount"] = list.Freecount
|
|
}
|
|
if user, err := this.module.ModuleUser.GetUser(session.GetUserId()); err == nil {
|
|
uinfo := comm.GetUserBaseInfo(user)
|
|
if !reflect.DeepEqual(uinfo, list.Uinfo) {
|
|
list.Uinfo = uinfo
|
|
update["uinfo"] = uinfo
|
|
// 同步岛屿上的信息
|
|
var lock *redis.RedisMutex
|
|
lock, err = this.module.modelLand.landMutexLock(list.Landid)
|
|
err = lock.Lock()
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Title: pb.ErrorCode_DBError.ToString(),
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
defer lock.Unlock()
|
|
|
|
for pos, v := range land.Uinfo {
|
|
if v.Uid == uinfo.Uid {
|
|
land.Uinfo[pos] = uinfo
|
|
this.module.modelLand.changePlunderLandData(list.Landid, map[string]interface{}{
|
|
"uinfo": land.Uinfo,
|
|
})
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
if len(update) > 0 {
|
|
this.module.modelPlunder.changePlunderData(session.GetUserId(), update)
|
|
}
|
|
session.SendMsg(string(this.module.GetType()), "getlist", &pb.PlunderGetListResp{
|
|
List: list,
|
|
Land: land,
|
|
})
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) sendRankReward(data map[string]int32) (err error) {
|
|
var (
|
|
list []Pair
|
|
rewardConf []*cfg.GamePlunderRankData
|
|
uid []string //
|
|
reward []*pb.UserAssets
|
|
)
|
|
|
|
list = sortMap(data)
|
|
for _, v := range list {
|
|
if v.Value > 0 { // 只算 分数大于0 的玩家
|
|
uid = append(uid, v.Key)
|
|
}
|
|
}
|
|
if rewardConf, err = this.module.configure.getPlunderRandConf(); err != nil {
|
|
return
|
|
}
|
|
for _, v := range rewardConf {
|
|
var sz []string
|
|
for i := v.ScoreLow; i <= v.ScoreUp; i++ {
|
|
if int32(len(uid)) > i-1 { // 越界校验
|
|
sz = append(sz, uid[i-1])
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
if len(sz) == 0 { // 过滤
|
|
break
|
|
}
|
|
for _, v := range v.Reward {
|
|
reward = append(reward, &pb.UserAssets{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: v.N,
|
|
})
|
|
}
|
|
this.module.mail.SendNewMail(&pb.DBMailData{
|
|
Cid: "PlunderRankingReward",
|
|
Param: []string{fmt.Sprintf("%d-%d", v.ScoreLow, v.ScoreUp)}, // 参数 分数下线和分数上限之间
|
|
CreateTime: uint64(configure.Now().Unix()),
|
|
Items: reward,
|
|
}, sz...)
|
|
}
|
|
return
|
|
}
|