155 lines
4.5 KiB
Go
155 lines
4.5 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 {
|
|
// 校验背包数据够不够
|
|
items, ok := caravan.Items[k]
|
|
if !ok {
|
|
this.module.Errorf("背包道具数量不足%s,道具ID:%d,背包数量:%d", session.GetUserId(), k, v)
|
|
continue
|
|
}
|
|
for _, k1 := range cityInfo.Special {
|
|
if k == k1 {
|
|
if v <= caravan.Items[k].Count {
|
|
caravan.Items[k].Count -= v
|
|
} else {
|
|
code = pb.ErrorCode_TrollSellMax // 卖出数量不足
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// 找到城市想要收购的物品
|
|
var price int32
|
|
price = items.Price
|
|
for _, v := range cityInfo.Exspecial {
|
|
if v == k {
|
|
if cityConf := this.module.configure.GetCaravanCity(req.City); cityConf != nil {
|
|
price = cityConf.Exspecialnum * items.Price / 1000
|
|
}
|
|
break
|
|
}
|
|
}
|
|
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 {
|
|
items, ok := caravan.Items[k]
|
|
if !ok {
|
|
this.module.Errorf("背包道具数量不足%s,道具ID:%d,背包数量:%d", session.GetUserId(), k, v)
|
|
continue
|
|
}
|
|
|
|
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
|
|
for _, v := range cityInfo.Special {
|
|
if v == k {
|
|
if cityConf := this.module.configure.GetCaravanCity(req.City); cityConf != nil {
|
|
price = cityConf.Specialnum * items.Price / 1000
|
|
}
|
|
break
|
|
}
|
|
}
|
|
caravan.Items[k].Count += v
|
|
|
|
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 {
|
|
code = pb.ErrorCode_TrollBuyMax // 商品数量不足
|
|
return
|
|
}
|
|
cityInfo.Count[req.City] -= v
|
|
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["item"] = 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
|
|
}
|