package hunting 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.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 // 门票atn 类型 只取T mapData map[string]interface{} szCostRes []*cfg.Gameatn // 购买累计消耗 curCount int32 // 当前门票数量 addCount int32 //获得数量 ) mapData = make(map[string]interface{}, 0) 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 = configure.Now().Unix() list.BuyCount = 0 mapData["cTime"] = list.CTime mapData["buyCount"] = list.BuyCount } else { curByCount = list.BuyCount } curByCount += req.Count // 当前需要购买的数量 if this.configure.GetMaxBuyChallengeCount() < curByCount { code = pb.ErrorCode_HuntingBuyMaxCount return } 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 } } if curCount-amount > 0 { addCount = curCount - amount } } else { list.RecoveryTime = 0 } addCount += req.Count if amount+addCount > conf.VikingNum { code = pb.ErrorCode_VikingBuyMaxCount return } mapData["recoveryTime"] = list.RecoveryTime for i := list.BuyCount + 1; i <= curByCount; i++ { _cfg := this.configure.GetBuyChallengeCount(i) if _cfg == nil { code = pb.ErrorCode_HuntingBuyMaxCount return } szCostRes = append(szCostRes, _cfg.Need...) } sz := make([]*cfg.Gameatn, 0) for _, v := range szCostRes { bFound := false for _, v1 := range sz { if v.A == v1.A && v.T == v1.T { v1.N += v.N bFound = true break } } if !bFound { sz = append(sz, v) } } //消耗 if code = this.module.ConsumeRes(session, sz, true); code != pb.ErrorCode_Success { return } 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 } 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 }