go_dreamfactory/modules/caravan/api_buyorsell.go
2023-05-19 20:26:00 +08:00

155 lines
4.3 KiB
Go

package caravan
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
//参数校验
func (this *apiComp) BuyOrSellCheck(session comm.IUserSession, req *pb.CaravanBuyOrSellReq) (code pb.ErrorCode) {
if len(req.Items) == 0 || req.City == 0 {
code = pb.ErrorCode_ReqParameterError
}
return
}
func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.CaravanBuyOrSellReq) (code pb.ErrorCode, data *pb.ErrorData) {
var (
update map[string]interface{}
addScore int32 // 收益
cityInfo *pb.CityInfo
ok bool
upperLimit int32 // 单个物品堆叠上限
)
update = make(map[string]interface{})
if code = this.BuyOrSellCheck(session, req); code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
caravan, _ := this.module.modelCaravan.getCaravanList(session.GetUserId())
cityInfo, ok = caravan.City[req.City]
if !ok {
code = pb.ErrorCode_ConfigNoFound
return
}
if c := this.module.configure.GetCaravanLv(caravan.Lv); c != nil {
upperLimit = c.Bagtagnum // 获取单个格子堆叠数
}
// special 城市卖给玩家的商品
// exspecial 城市想要玩家卖给他的商品库
if !req.IsBuy { // 卖给npc
for k, v := range req.Items {
// 校验背包数据够不够
caravan.Items[k].Count -= v
if caravan.Items[k].Count < 0 {
code = pb.ErrorCode_TrollItemNoEnough // 道具数量不足
return
}
items := caravan.Items[k]
var price int32
price = items.Price
bFound := false
for _, v := range cityInfo.Special {
if v == k {
if cityConf := this.module.configure.GetCaravanCity(req.City); cityConf != nil {
price = cityConf.Specialnum * price / 1000
bFound = true
}
break
}
}
if !bFound {
if cityConf := this.module.configure.GetCaravanCity(req.City); cityConf != nil {
for _, v := range cityConf.Exspecial {
if v == k {
price = cityConf.Exspecialnum * price / 1000
}
}
}
}
addScore += price * v // 卖出收益
}
if this.module.ArrayBag(caravan, upperLimit) { // 背包满了
code = pb.ErrorCode_TrollMaxItemCount
return
}
// 统计 收益
var lvReward []*cfg.Gameatn
caravan.Profit += int64(addScore)
update["profit"] = caravan.Profit
curLv := this.module.CheckCaravavLvUp(caravan)
if curLv > caravan.Lv {
for i := caravan.Lv; i <= curLv-caravan.Lv; i++ {
if c := this.module.configure.GetCaravanLv(int32(i)); c != nil {
lvReward = append(lvReward, c.Reward...)
}
}
}
if len(lvReward) > 0 {
if reward := this.module.DispenseRes(session, lvReward, true); reward != pb.ErrorCode_Success {
this.module.Errorf("lv reward dispenseRes err:%v", lvReward)
}
}
} else { // 买入
for k, v := range req.Items {
if _, ok := caravan.Items[k]; !ok {
caravan.Items[k] = &pb.BagInfo{
Count: 0,
Price: 0,
}
}
bFound := false
// 计算均价
totla := caravan.Items[k].Count * caravan.Items[k].Price
var price int32
price = caravan.Goods[k].Price
for _, v := range cityInfo.Special {
if v == k {
bFound = true
break
}
}
if !bFound {
code = pb.ErrorCode_TrollCityUnSellItem // 城市不卖这个物品
return
}
caravan.Items[k].Count += v
totla += price * v
caravan.Items[k].Price = totla / caravan.Items[k].Count
// 同步更新该城市的 出售货物信息
cityInfo.Count[k] += v
if itemConf := this.configure.GetCaravanGoods(k); itemConf != nil { // 更新商店库存
if cityInfo.Count[k] > itemConf.Goodsnum {
code = pb.ErrorCode_TrollBuyMax // 商品数量不足
return
}
update["city"] = caravan.City
addScore -= price * v
}
}
if this.module.ArrayBag(caravan, upperLimit) { // 背包满了
code = pb.ErrorCode_TrollMaxItemCount
return
}
}
if code = this.module.DispenseRes(session, []*cfg.Gameatn{{
A: "attr",
T: "merchantmoney",
N: addScore,
}}, true); code != pb.ErrorCode_Success {
this.module.Errorf("获得虚拟币失败:%d", code)
}
update["items"] = caravan.Items
update["baglimit"] = caravan.Baglimit
update["useCount"] = caravan.UseCount // 更新背包使用数量
this.module.modelCaravan.modifyCaravanDataByObjId(session.GetUserId(), update)
session.SendMsg(string(this.module.GetType()), "buyorsell", &pb.CaravanBuyOrSellResp{
Data: caravan,
})
return
}