package hero import ( "go_dreamfactory/comm" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/utils" "math" ) //参数校验 func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.HeroBuyReq) (errdata *pb.ErrorData) { if req.BuyType == 0 || req.BuyCount == 0 { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } } return } ///获取用户商品列表 func (this *apiComp) Buy(session comm.IUserSession, req *pb.HeroBuyReq) (errdata *pb.ErrorData) { var ( err error conf *cfg.GameShopitemData global *cfg.GameGlobalData need []*cfg.Gameatn give []*cfg.Gameatn totalCount int32 // 当前购买的总次数 update map[string]interface{} price []int32 // 购买所需的价钱 totalCost float64 // 购买打折系数 udata *pb.DBUser tasks []*pb.BuriedParam = make([]*pb.BuriedParam, 0) ) update = make(map[string]interface{}) if errdata = this.BuyCheck(session, req); errdata != nil { return } if udata = this.module.ModuleUser.GetUser(session.GetUserId()); udata == nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_UserNofound, // 没找到玩家数据 Title: pb.ErrorCode_UserNofound.ToString(), } return } if conf, err = this.module.configure.GetShopItemsConfigureByGroups(req.BuyType, udata); err != nil { // 找配置 errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.ToString(), Message: err.Error(), } return } record, err := this.module.modelRecord.GetHeroRecord(session.GetUserId()) if err != nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_HeroNoExist, // 没找到英雄扩展数据数据 Title: pb.ErrorCode_HeroNoExist.ToString(), } return } global = this.module.ModuleTools.GetGlobalConf() for _, v := range conf.Salelist { price = append(price, v) } if req.BuyType == global.DrawCardSupplement1 { totalCount = record.Onebuy + req.BuyCount update["onebuy"] = totalCount // 记录购买的数据 // 重新计算价格 } else if req.BuyType == global.DrawCardSupplement10 { totalCount = record.Tenbuy + req.BuyCount update["tenbuy"] = totalCount } else { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ReqParameterError, Title: pb.ErrorCode_ReqParameterError.ToString(), } return } if totalCount > conf.Buymaxnum { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ShopGoodsIsSoldOut, // 购买达到上限 Title: pb.ErrorCode_ShopGoodsIsSoldOut.ToString(), } return } for i := totalCount - req.BuyCount; i < totalCount; i++ { if i >= int32(len(price)) { totalCost += float64(price[len(price)-1]) / 1000 continue } totalCost += float64(price[i]) / 1000 } need = make([]*cfg.Gameatn, len(conf.Need)) for i, v := range conf.Need { need[i] = &cfg.Gameatn{ A: v.A, T: v.T, N: int32(math.Ceil(float64(v.N) * totalCost)), } } // 消耗 if errdata = this.module.ConsumeRes(session, need, true); errdata != nil { return } give = make([]*cfg.Gameatn, len(conf.Iteminfo)) for i, v := range conf.Iteminfo { give[i] = &cfg.Gameatn{ A: v.A, T: v.T, N: v.N * req.BuyCount, } } // 获得的道具 if errdata = this.module.DispenseRes(session, give, true); errdata != nil { return } this.module.modelRecord.ChangeHeroRecord(session.GetUserId(), update) //随机任务 // this.module.ModuleRtask.SendToRtask(session, comm.Rtype64, 1) tasks = append(tasks, comm.GetBuriedParam(comm.Rtype64, 1)) for _, v := range give { if v.A == comm.ItemType { // this.module.ModuleRtask.SendToRtask(session, comm.Rtype65, v.N, utils.ToInt32(v.T)) tasks = append(tasks, comm.GetBuriedParam(comm.Rtype65, v.N, utils.ToInt32(v.T))) } } go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), tasks...) session.SendMsg(string(this.module.GetType()), "buy", &pb.HeroBuyResp{ IsSucc: true, Onebuy: record.Onebuy + req.BuyCount, Tenbuy: record.Tenbuy + req.BuyCount, }) return }