diff --git a/comm/imodule.go b/comm/imodule.go index aca286be1..c6708c469 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -494,5 +494,6 @@ type ( ICaravan interface { ITaskComplete + TestFunc(session IUserSession) } ) diff --git a/modules/caravan/api_buyorsell.go b/modules/caravan/api_buyorsell.go index 049c67148..cbe274cb4 100644 --- a/modules/caravan/api_buyorsell.go +++ b/modules/caravan/api_buyorsell.go @@ -47,16 +47,24 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe this.module.Errorf("背包道具数量不足%s,道具ID:%d,背包数量:%d", session.GetUserId(), k, v) continue } + bFound := false for _, k1 := range cityInfo.Special { if k == k1 { if v <= caravan.Items[k].Count { caravan.Items[k].Count -= v + bFound = true + break } else { code = pb.ErrorCode_TrollSellMax // 卖出数量不足 return } + } } + if !bFound { + code = pb.ErrorCode_TrollSellMax // 卖出数量不足 + return + } // 找到城市想要收购的物品 var price int32 price = items.Price @@ -93,25 +101,26 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe } } else { // 买入 for k, v := range req.Items { - items, ok := caravan.Items[k] - if !ok { - this.module.Errorf("背包道具数量不足%s,道具ID:%d,背包数量:%d", session.GetUserId(), k, v) - continue + if _, ok := caravan.Items[k]; !ok { + caravan.Items[k] = &pb.BagInfo{ + Count: 0, + Price: 0, + } } - if upperLimit > caravan.Items[k].Count+v { - code = pb.ErrorCode_TrollBuyMax // 达到购买上限直接退出 - return - } + // if upperLimit < caravan.Items[k].Count+v { + // code = pb.ErrorCode_TrollBuyMax // 达到购买上限直接退出 + // return + // } // 计算均价 totla := caravan.Items[k].Count * caravan.Items[k].Price var price int32 - price = items.Price + price = caravan.Goods[k].Price for _, v := range cityInfo.Special { if v == k { if cityConf := this.module.configure.GetCaravanCity(req.City); cityConf != nil { - price = cityConf.Specialnum * items.Price / 1000 + price = cityConf.Specialnum * price / 1000 } break } @@ -121,15 +130,16 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe totla += price * v caravan.Items[k].Price = totla / caravan.Items[k].Count // 同步更新该城市的 出售货物信息 - if _, ok := cityInfo.Count[req.City]; ok { - if cityInfo.Count[req.City] < v { + cityInfo.Count[req.City] += v + if itemConf := this.configure.GetCaravanGoods(k); itemConf != nil { // 更新商店库存 + if cityInfo.Count[req.City] > itemConf.Goodsnum { code = pb.ErrorCode_TrollBuyMax // 商品数量不足 return } - cityInfo.Count[req.City] -= v update["city"] = caravan.City + addScore -= price * v } - addScore -= price * v + } if this.module.ArrayBag(caravan, upperLimit) { // 背包满了 code = pb.ErrorCode_TrollMaxItemCount @@ -143,7 +153,7 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSe }}, true); code != pb.ErrorCode_Success { this.module.Errorf("获得虚拟币失败:%d", code) } - update["item"] = caravan.Items + update["items"] = caravan.Items update["baglimit"] = caravan.Baglimit update["useCount"] = caravan.UseCount // 更新背包使用数量 this.module.modelCaravan.modifyCaravanDataByObjId(session.GetUserId(), update) diff --git a/modules/caravan/module.go b/modules/caravan/module.go index 04ce15f49..98462bc08 100644 --- a/modules/caravan/module.go +++ b/modules/caravan/module.go @@ -71,9 +71,9 @@ func (this *Caravan) InitCaravanCityData(uid string, data *pb.DBCaravan) { } else { city.Special = append(city.Special, v.Special...) } - for _, v := range city.Special { - city.Count[v] = 40 // 配置暂无 后面走配置 - } + // for _, v := range city.Special { + // city.Count[v] = 40 // 配置暂无 后面走配置 + // } city.Exspecial = append(city.Special, v.Exspecial...) data.City[v.Id] = city @@ -134,11 +134,11 @@ func (this *Caravan) refreshCaravanCityInfo(uid string, data *pb.DBCaravan) { } else { v.Special = append(v.Special, c.Special...) } - for _, v1 := range v.Special { - if itemConf := this.configure.GetCaravanGoods(v1); itemConf != nil { // 更新商店库存 - v.Count[v1] = itemConf.Goodsnum - } - } + // for _, v1 := range v.Special { + // if itemConf := this.configure.GetCaravanGoods(v1); itemConf != nil { // 更新商店库存 + // v.Count[v1] = itemConf.Goodsnum + // } + // } v.Exspecial = append(v.Special, c.Exspecial...) bChange = true } @@ -305,10 +305,11 @@ func (this *Caravan) ArrayBag(data *pb.DBCaravan, limit int32) (bFull bool) { if v.Count == 0 { delete(data.Items, k) } else { + tmp := v.Count for { count++ - if v.Count > limit { - v.Count -= limit + if tmp > limit { + tmp -= limit } else { break } @@ -316,10 +317,10 @@ func (this *Caravan) ArrayBag(data *pb.DBCaravan, limit int32) (bFull bool) { } } if count > data.Baglimit { - return false + return true } data.UseCount = count - return true + return false } // 校验商队等级 @@ -338,3 +339,14 @@ func (this *Caravan) CheckCaravavLvUp(data *pb.DBCaravan) (curLv int32) { } return curLv } + +func (this *Caravan) TestFunc(session comm.IUserSession) { + this.modelCaravan.module.api.GetList(session, &pb.CaravanGetListReq{}) + this.modelCaravan.module.api.BuyOrSell(session, &pb.CaravanBuyOrSellReq{ + City: 101, + Items: map[int32]int32{ + 2: 20, + }, + IsBuy: false, + }) +} diff --git a/modules/gm/module.go b/modules/gm/module.go index 148b69f7c..6afd6c39f 100644 --- a/modules/gm/module.go +++ b/modules/gm/module.go @@ -449,6 +449,13 @@ func (this *GM) CreateCmd(session comm.IUserSession, cmd string) (code pb.ErrorC log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "0", Value: datas[0]}, ) + } else if len(datas) == 1 && (datas[0] == "test") { + module1, err := this.service.GetModule(comm.ModuleCaravan) + if err != nil { + return + } + + module1.(comm.ICaravan).TestFunc(session) } } } diff --git a/pb/caravan_db.pb.go b/pb/caravan_db.pb.go index e86dd6978..4450e207d 100644 --- a/pb/caravan_db.pb.go +++ b/pb/caravan_db.pb.go @@ -224,7 +224,7 @@ type DBCaravan struct { Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id" bson:"_id"` //ID Uid string `protobuf:"bytes,2,opt,name=uid,proto3" json:"uid" bson:"uid"` //用户ID - UseCount int32 `protobuf:"varint,3,opt,name=useCount,proto3" json:"useCount" bson:"usecount"` //当前背包使用的数量 + UseCount int32 `protobuf:"varint,3,opt,name=useCount,proto3" json:"useCount" bson:"useCount"` //当前背包使用的数量 Items map[int32]*BagInfo `protobuf:"bytes,4,rep,name=items,proto3" json:"items" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` // 背包数据 Goods map[int32]*Goods `protobuf:"bytes,5,rep,name=goods,proto3" json:"goods" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` // key 货物ID City map[int32]*CityInfo `protobuf:"bytes,6,rep,name=city,proto3" json:"city" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` // 城市信息