go_dreamfactory/modules/hunting/api_buy.go
2022-08-30 17:58:24 +08:00

75 lines
1.8 KiB
Go

package hunting
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.HuntingBuyReq) (code pb.ErrorCode) {
if req.Count <= 0 {
code = pb.ErrorCode_ReqParameterError
return
}
return
}
func (this *apiComp) Buy(session comm.IUserSession, req *pb.HuntingBuyReq) (code pb.ErrorCode, data proto.Message) {
var (
curByCount int32
costRes []*cfg.Gameatn
mapData map[string]interface{}
)
code = this.BuyCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
list, err := this.module.modelHunting.getHuntingList(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...)
}
// 消耗校验
if code = this.module.CheckRes(session, costRes); code != pb.ErrorCode_Success {
return
}
//消耗
if code = this.module.ConsumeRes(session, costRes, true); code != pb.ErrorCode_Success {
return
}
list.BuyCount = curByCount
mapData["buyCount"] = curByCount
code = this.module.ModifyHuntingData(session.GetUserId(), mapData)
session.SendMsg(string(this.module.GetType()), HuntingBuyResp, &pb.HuntingBuyResp{Data: list})
return
}