go_dreamfactory/modules/viking/api_buy.go
2022-09-07 12:09:35 +08:00

89 lines
2.1 KiB
Go

package viking
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
"time"
"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 = time.Now().Unix()
list.BuyCount = 0
list.ChallengeCount = 0
mapData["cTime"] = list.CTime
mapData["buyCount"] = list.BuyCount
mapData["challengeCount"] = list.ChallengeCount
} else {
curByCount = list.BuyCount
}
curByCount += req.Count // 当前需要购买的数量
// 消耗资源整合
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
}