diff --git a/modules/caravan/api_buyorsell.go b/modules/caravan/api_buyorsell.go index 5fb6104f0..cfe15cb48 100644 --- a/modules/caravan/api_buyorsell.go +++ b/modules/caravan/api_buyorsell.go @@ -51,8 +51,13 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe continue } for _, k1 := range cityInfo.Like { - if k == k1 && v < caravan.Items[k].Count { - caravan.Items[k].Count -= v + if k == k1 { + if v < caravan.Items[k].Count { + caravan.Items[k].Count -= v + } else { + code = pb.ErrorCode_TrollSellMax // 卖出数量不足 + return + } } } // 找到城市想要收购的物品 @@ -94,6 +99,10 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe } } caravan.Items[k].Count += v + if this.module.ArrayBag(caravan, upperLimit) { // 背包满了 + code = pb.ErrorCode_TrollMaxItemCount + return + } totla += price * v caravan.Items[k].Price = totla / caravan.Items[k].Count // 同步更新该城市的 出售货物信息 @@ -116,6 +125,7 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe this.module.Errorf("获得虚拟币失败:%d", code) } update["item"] = caravan.Items + update["baglimit"] = caravan.Baglimit this.module.modelCaravan.modifyCaravanDataByObjId(session.GetUserId(), update) session.SendMsg(string(this.module.GetType()), "buyorsell", &pb.CaravanBuyOrSellResp{ Data: caravan, diff --git a/modules/caravan/module.go b/modules/caravan/module.go index 896d18fdb..c0a239fa3 100644 --- a/modules/caravan/module.go +++ b/modules/caravan/module.go @@ -299,3 +299,26 @@ func (this *Caravan) TaskComplete(session comm.IUserSession, taskid int32) { } } } + +// 整理背包 (true 表示背包装不下) +func (this *Caravan) ArrayBag(data *pb.DBCaravan, limit int32) (bFull bool) { + var count int32 + for k, v := range data.Items { + if v.Count == 0 { + delete(data.Items, k) + } else { + for { + count++ + if v.Count > limit { + v.Count -= limit + } else { + break + } + } + } + } + if count > data.Baglimit { + return false + } + return true +}