go_dreamfactory/modules/hunting/api_getlist.go
2022-12-23 18:19:00 +08:00

126 lines
3.1 KiB
Go

package hunting
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
"strconv"
"go.mongodb.org/mongo-driver/bson/primitive"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.HuntingGetListReq) (code pb.ErrorCode) {
return
}
func (this *apiComp) GetList(session comm.IUserSession, req *pb.HuntingGetListReq) (code pb.ErrorCode, data proto.Message) {
var (
mapData map[string]interface{}
curCount int32
)
mapData = make(map[string]interface{}, 0)
code = this.GetListCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
list, err := this.module.modelHunting.getHuntingList(session.GetUserId())
if mgo.MongodbNil == err {
list = &pb.DBHunting{
Id: primitive.NewObjectID().Hex(),
Uid: session.GetUserId(),
Boss: make(map[int32]int32),
BossTime: make(map[string]int32),
}
_cfg := this.module.configure.GetHuntingBossTypeConfigData()
for k := range _cfg {
list.Boss[k] = 1
str := strconv.Itoa(int(k)) + "_1"
list.BossTime[str] = 0
}
this.module.modelHunting.Add(session.GetUserId(), list)
conf := this.module.configure.GetGlobalConf()
if conf == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
iCont := conf.HuntingNum
atn := conf.HuntingCos
if iCont > 0 {
this.module.DispenseRes(session, []*cfg.Gameatn{&cfg.Gameatn{
A: atn.A,
T: atn.T,
N: iCont,
}}, true)
}
} else if err != nil {
code = pb.ErrorCode_DBError
return
}
// 校验 是不是当天
if !utils.IsToday(list.CTime) {
list.CTime = configure.Now().Unix()
list.BuyCount = 0
mapData["cTime"] = list.CTime
mapData["buyCount"] = list.BuyCount
}
// 检查恢复时间
conf := this.module.configure.GetGlobalConf()
if conf == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
costRes := conf.HuntingCos
if costRes == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
amount := int32(this.module.ModuleItems.QueryItemAmount(session.GetUserId(), costRes.T)) // 获取当前数量
curCount = amount
if amount < conf.HuntingNum {
if list.RecoveryTime == 0 {
list.RecoveryTime = configure.Now().Unix()
}
for { // 计算恢复时间
if list.RecoveryTime+int64(conf.HuntingRecovery*60) <= configure.Now().Unix() {
curCount++
list.RecoveryTime += int64(conf.HuntingRecovery * 60)
if curCount >= conf.HuntingNum {
list.RecoveryTime = 0
break
}
} else {
break
}
}
addCount := curCount - amount
if addCount > 0 {
res := &cfg.Gameatn{
A: "item",
T: costRes.T,
N: addCount,
}
if code = this.module.DispenseRes(session, []*cfg.Gameatn{res}, true); code != pb.ErrorCode_Success {
return
}
}
} else {
list.RecoveryTime = 0
}
mapData["recoveryTime"] = list.RecoveryTime
code = this.module.ModifyHuntingData(session.GetUserId(), mapData) //修改内存信息
session.SendMsg(string(this.module.GetType()), HuntingGetListResp, &pb.HuntingGetListResp{Data: list})
return
}