package hero import ( "go_dreamfactory/comm" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/utils" "math" "google.golang.org/protobuf/proto" ) //参数校验 func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.HeroBuyReq) (code pb.ErrorCode) { if req.BuyType == 0 || req.BuyCount == 0 { code = pb.ErrorCode_ReqParameterError } return } ///获取用户商品列表 func (this *apiComp) Buy(session comm.IUserSession, req *pb.HeroBuyReq) (code pb.ErrorCode, data proto.Message) { 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 []*comm.TaskParam = make([]*comm.TaskParam, 0) ) update = make(map[string]interface{}) if code = this.BuyCheck(session, req); code != pb.ErrorCode_Success { return } if udata = this.module.ModuleUser.GetUser(session.GetUserId()); udata == nil { code = pb.ErrorCode_SystemError return } if conf, err = this.module.configure.GetShopItemsConfigureByGroups(req.BuyType, udata); err != nil { // 找配置 code = pb.ErrorCode_ConfigNoFound return } record, err := this.module.modelRecord.GetHeroRecord(session.GetUserId()) if err != nil { code = pb.ErrorCode_SystemError return } global = this.module.configure.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 { code = pb.ErrorCode_ReqParameterError return } if totalCount > conf.Buymaxnum { // 购买达到上限 code = pb.ErrorCode_ShopGoodsIsSoldOut 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 code = this.module.ConsumeRes(session, need, true); code != pb.ErrorCode_Success { 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 code = this.module.DispenseRes(session, give, true); code != pb.ErrorCode_Success { return } this.module.modelRecord.ChangeHeroRecord(session.GetUserId(), update) //随机任务 // this.module.ModuleRtask.SendToRtask(session, comm.Rtype64, 1) tasks = append(tasks, comm.GettaskParam(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.GettaskParam(comm.Rtype65, v.N, utils.ToInt32(v.T))) } } for _, v := range need { if v.A == comm.AttrType && (v.T == comm.ResGold || v.T == comm.ResDiamond) { // this.module.ModuleRtask.SendToRtask(session, comm.Rtype67, v.N, utils.ToInt32(v.T)) tasks = append(tasks, comm.GettaskParam(comm.Rtype67, v.N, utils.ToInt32(v.T))) } } go this.module.ModuleRtask.TriggerTask(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 }