126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package viking
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/utils"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.VikingBuyReq) (code pb.ErrorCode) {
|
|
if req.Count <= 0 {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) Buy(session comm.IUserSession, req *pb.VikingBuyReq) (code pb.ErrorCode, data proto.Message) {
|
|
var (
|
|
curByCount int32
|
|
costRes []*cfg.Gameatn
|
|
mapData map[string]interface{}
|
|
)
|
|
mapData = make(map[string]interface{}, 0)
|
|
code = this.BuyCheck(session, req)
|
|
if code != pb.ErrorCode_Success {
|
|
return // 参数校验失败直接返回
|
|
}
|
|
list, err := this.module.modelViking.getVikingList(session.GetUserId())
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
|
|
// 校验是不是今天
|
|
if !utils.IsToday(list.CTime) {
|
|
list.CTime = configure.Now().Unix()
|
|
list.BuyCount = 0
|
|
list.ChallengeCount = 0
|
|
|
|
mapData["cTime"] = list.CTime
|
|
mapData["buyCount"] = list.BuyCount
|
|
mapData["challengeCount"] = list.ChallengeCount
|
|
} else {
|
|
curByCount = list.BuyCount
|
|
}
|
|
conf := this.module.configure.GetGlobalConf()
|
|
if conf == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
if list.LeftCount != conf.VikingNum {
|
|
if list.RecoveryTime == 0 {
|
|
list.RecoveryTime = configure.Now().Unix()
|
|
}
|
|
count := (configure.Now().Unix() - list.RecoveryTime) / int64(conf.VikingExpeditionRecoveryTime*60)
|
|
if count > 1 {
|
|
list.LeftCount += int32(count)
|
|
}
|
|
if list.LeftCount >= conf.VikingNum {
|
|
list.LeftCount = conf.VikingNum
|
|
list.RecoveryTime = 0
|
|
} else {
|
|
list.RecoveryTime += count * int64(conf.VikingExpeditionRecoveryTime*60) // 更新刷新时间
|
|
}
|
|
} else {
|
|
list.RecoveryTime = 0
|
|
}
|
|
curByCount += req.Count // 当前需要购买的数量
|
|
|
|
if this.configure.GetMaxBuyChallengeCount() < curByCount {
|
|
code = pb.ErrorCode_VikingBuyMaxCount
|
|
return
|
|
}
|
|
list.LeftCount += req.Count
|
|
mapData["leftCount"] = list.LeftCount
|
|
if list.LeftCount > conf.VikingNum {
|
|
code = pb.ErrorCode_VikingBuyMaxCount
|
|
return
|
|
} else if list.LeftCount == conf.VikingNum {
|
|
list.RecoveryTime = 0
|
|
mapData["recoveryTime"] = list.RecoveryTime // 更新刷新时间
|
|
}
|
|
|
|
// 消耗资源整合
|
|
for i := list.BuyCount + 1; i <= curByCount; i++ {
|
|
_cfg := this.configure.GetBuyChallengeCount(i)
|
|
if _cfg == nil {
|
|
code = pb.ErrorCode_VikingBuyMaxCount
|
|
return
|
|
}
|
|
costRes = append(costRes, _cfg.Need...)
|
|
}
|
|
sz := make([]*cfg.Gameatn, 0)
|
|
for _, v := range costRes {
|
|
bFound := false
|
|
for _, v1 := range sz {
|
|
if v.A == v1.A && v.T == v1.T {
|
|
v1.N += v.N
|
|
bFound = true
|
|
}
|
|
}
|
|
if !bFound {
|
|
sz = append(sz, v)
|
|
}
|
|
}
|
|
// 消耗校验
|
|
if code = this.module.CheckRes(session, sz); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
|
|
//消耗
|
|
if code = this.module.ConsumeRes(session, sz, true); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
list.BuyCount = curByCount
|
|
mapData["buyCount"] = curByCount
|
|
code = this.module.ModifyVikingData(session.GetUserId(), mapData)
|
|
session.SendMsg(string(this.module.GetType()), VikingBuyResp, &pb.VikingBuyResp{Data: list})
|
|
return
|
|
}
|