This commit is contained in:
meixiongfeng 2022-11-02 17:21:52 +08:00
parent b149c733ab
commit 8dbfc3ac6f

View File

@ -84,24 +84,21 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell
if p == nil { if p == nil {
return return
} }
if v < 0 { // 卖出 var sellPrice int32 // 交易价格
// 卖出价格
var sellPrice int32
if trolltrain.TarinPos == 0 { if trolltrain.TarinPos == 0 {
sellPrice = goods.Goodsprice * goods.StarMoney / 1000 sellPrice = goods.Goodsprice * goods.StarMoney / 1000
} else { } else {
sellPrice = int32(p.Coefficient) * goods.Goodsprice / 1000 sellPrice = int32(p.Coefficient) * goods.Goodsprice / 1000
} }
if v < 0 { // 卖出
if sellPrice > trolltrain.Price[k] { // 赚了 if sellPrice > trolltrain.Price[k] { // 赚了
earn += (sellPrice - trolltrain.Price[k]) * trolltrain.Items[k] earn += (sellPrice - trolltrain.Price[k]) * v
} }
gold += (sellPrice - trolltrain.Price[k]) * trolltrain.Items[k] gold += sellPrice * v
} else { // 买入 计算平均价格 } else { // 买入 计算平均价格
totalGold := trolltrain.Items[k] * trolltrain.Price[k] totalGold := (trolltrain.Items[k] - v) * trolltrain.Price[k]
sellPrice := int32(p.Coefficient) * goods.Goodsprice / 1000
totalGold += v * sellPrice totalGold += v * sellPrice
trolltrain.Price[k] = totalGold / (trolltrain.Items[k] + v) trolltrain.Price[k] = totalGold / (trolltrain.Items[k])
gold -= v * sellPrice gold -= v * sellPrice
} }
} }
@ -110,10 +107,9 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell
grid := this.configure.GetTrollRule(comm.TrollItemCount) grid := this.configure.GetTrollRule(comm.TrollItemCount)
for _, v := range trolltrain.Items { for _, v := range trolltrain.Items {
if v > 0 { if v > 0 {
gridNum += int32(math.Ceil(float64(v / grid))) gridNum += int32(math.Ceil(float64(v) / float64(grid)))
} }
} }
// 获取背包格子上限配置
trolltrain.GridNum = gridNum trolltrain.GridNum = gridNum
if gridNum > this.configure.GetTrollRule(comm.TrollGridCount) { // 背包格子上限 if gridNum > this.configure.GetTrollRule(comm.TrollGridCount) { // 背包格子上限
@ -132,8 +128,13 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell
update["npcLv"] = trolltrain.NpcLv update["npcLv"] = trolltrain.NpcLv
} }
} }
// 清除数量为0 的
trolltrain.TotalEarn += int64(earn) // 累计获得的金币 for k, v := range trolltrain.Items {
if v == 0 {
delete(trolltrain.Items, k)
}
}
trolltrain.TotalEarn += -int64(earn) // 累计获得的金币
update["items"] = trolltrain.Items update["items"] = trolltrain.Items
update["price"] = trolltrain.Price update["price"] = trolltrain.Price
update["totalEarn"] = trolltrain.TotalEarn update["totalEarn"] = trolltrain.TotalEarn