package items import ( "go_dreamfactory/comm" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "time" ) //参数校验 func (this *apiComp) BuyPhysicalCheck(session comm.IUserSession, req *pb.ItemsBuyPhysicalReq) (code pb.ErrorCode) { if req.Amount <= 0 { code = pb.ErrorCode_ReqParameterError } return } //出售道具 func (this *apiComp) BuyPhysical(session comm.IUserSession, req *pb.ItemsBuyPhysicalReq) (code pb.ErrorCode, data *pb.ErrorData) { var ( err error user *pb.DBUserExpand needs []*cfg.Gameatn haveneeds []*cfg.Gameatn sale []*cfg.Gameatn ) if code = this.BuyPhysicalCheck(session, req); code != pb.ErrorCode_Success { return } if user, err = this.module.ModuleUser.GetUserExpand(session.GetUserId()); err != nil { code = pb.ErrorCode_ReqParameterError return } if needs = this.module.ModuleTools.GetGlobalConf().PsBuy; needs == nil && len(needs) == 0 { code = pb.ErrorCode_ConfigNoFound return } if time.Unix(user.PhysicalbuyLasttime, 0).Day() != time.Now().Day() { //不是同一天 重置购买次数 user.Physicalbuynum = 0 } if req.Amount+uint32(user.Physicalbuynum) > uint32(len(needs)) { code = pb.ErrorCode_ItemsBuyPsUpperLimit return } haveneeds = make([]*cfg.Gameatn, 0) for i := uint32(0); i < req.Amount; i++ { haveneeds = append(haveneeds, needs[int(int32(i)+user.Physicalbuynum)]) } if code = this.module.ConsumeRes(session, haveneeds, true); code != pb.ErrorCode_Success { return } sale = []*cfg.Gameatn{{A: comm.ItemType, T: comm.PhysicalMedicament, N: int32(req.Amount)}} if code = this.module.DispenseRes(session, sale, true); code != pb.ErrorCode_Success { return } user.Physicalbuynum += int32(req.Amount) user.PhysicalbuyLasttime = time.Now().Unix() this.module.ModuleUser.ChangeUserExpand(session.GetUserId(), map[string]interface{}{ "physicalbuynum": user.Physicalbuynum, "physicalbuyLasttime": user.PhysicalbuyLasttime, }) session.SendMsg(string(this.module.GetType()), "buyphysical", &pb.ItemsBuyPhysicalResp{Issucc: true, PhysicalBuyNum: user.Physicalbuynum, Asets: []*pb.UserAssets{{A: comm.ItemType, T: comm.PhysicalMedicament, N: int32(req.Amount)}}}) return }