170 lines
4.8 KiB
Go
170 lines
4.8 KiB
Go
package caravan
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"math"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) BuyOrSellCheck(session comm.IUserSession, req *pb.TrollBuyOrSellReq) (code pb.ErrorCode) {
|
|
if len(req.Items) == 0 {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSellReq) (code pb.ErrorCode, data *pb.ErrorData) {
|
|
var (
|
|
bSell bool // 是否有出售
|
|
gold int32 // 当次交易 获得的金币
|
|
earn int32 // 只统计赚的金币
|
|
update map[string]interface{}
|
|
gridNum int32 // 格子数量
|
|
)
|
|
update = make(map[string]interface{})
|
|
if code = this.BuyOrSellCheck(session, req); code != pb.ErrorCode_Success {
|
|
return // 参数校验失败直接返回
|
|
}
|
|
trolltrain, err := this.module.modelTroll.getTrollList(session.GetUserId())
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
dayMaxCount := this.configure.GetTrollRule(comm.TrollBuyCount)
|
|
aiMaxCount := this.configure.GetTrollRule(comm.TrollAIBuyCount)
|
|
for k, v := range req.Items {
|
|
|
|
if v == 0 { // 过滤数量为0 的消息
|
|
continue
|
|
}
|
|
if trolltrain.RangeId == 0 {
|
|
trolltrain.RangeId = 1
|
|
update["rangeId"] = trolltrain.RangeId
|
|
}
|
|
if v < 0 {
|
|
if !bSell {
|
|
bSell = true
|
|
trolltrain.SellCount += 1 // 交易次数+1
|
|
if trolltrain.SellCount > dayMaxCount || (trolltrain.SellCount+trolltrain.AiCount) > aiMaxCount {
|
|
code = pb.ErrorCode_TrollMaxSellCount // 达到最大交易次数 直接返回
|
|
return
|
|
}
|
|
update["sellCount"] = trolltrain.SellCount
|
|
}
|
|
}
|
|
|
|
if _, ok := trolltrain.Shop[k]; !ok {
|
|
if v > 0 {
|
|
trolltrain.Shop[k] = v // 限购
|
|
}
|
|
} else {
|
|
if v > 0 {
|
|
trolltrain.Shop[k] += v // 限购
|
|
}
|
|
}
|
|
// 校验 是否大于买入最大限制
|
|
goods := this.configure.GetTrollGoods(k)
|
|
if goods == nil {
|
|
return
|
|
}
|
|
if trolltrain.Shop[k] > goods.Max { // 判断是否有效交易
|
|
// 买入上限 直接返回
|
|
code = pb.ErrorCode_TrollBuyMax
|
|
return
|
|
} else if trolltrain.Items[k]+v < 0 { //卖出数量不足
|
|
code = pb.ErrorCode_TrollSellMax
|
|
return
|
|
}
|
|
|
|
// 第一次购买商品
|
|
if trolltrain.TarinPos == 0 {
|
|
if _, ok := trolltrain.Price[k]; !ok {
|
|
trolltrain.Price[k] = 0
|
|
}
|
|
trolltrain.Price[k] = goods.Goodsprice * goods.StarMoney / 1000
|
|
trolltrain.RefreshTime = configure.Now().Unix()
|
|
//消耗的金币
|
|
gold -= trolltrain.Price[k] * trolltrain.Items[k]
|
|
} else {
|
|
p := this.configure.GetTrollCoefficient(trolltrain.RangeId)
|
|
if p == nil {
|
|
return
|
|
}
|
|
var sellPrice int32 // 交易价格
|
|
if trolltrain.TarinPos == 0 {
|
|
sellPrice = goods.Goodsprice * goods.StarMoney / 1000
|
|
} else {
|
|
sellPrice = int32(p.Coefficient) * goods.Goodsprice / 1000
|
|
}
|
|
// 校验是不是惊喜价格
|
|
if d1, ok := trolltrain.SurpriseID[trolltrain.TarinPos-1]; ok {
|
|
if d1 == k {
|
|
sellPrice = this.configure.GetTrollRule(comm.TrollSurprise) / 1000
|
|
}
|
|
}
|
|
if v < 0 { // 卖出
|
|
trolltrain.Items[k] += v
|
|
if sellPrice > trolltrain.Price[k] { // 赚了
|
|
earn += (sellPrice - trolltrain.Price[k]) * v
|
|
}
|
|
gold -= sellPrice * v
|
|
} else { // 买入 计算平均价格
|
|
totalGold := trolltrain.Items[k] * trolltrain.Price[k]
|
|
totalGold += v * sellPrice
|
|
trolltrain.Items[k] += v
|
|
trolltrain.Price[k] = totalGold / trolltrain.Items[k]
|
|
gold -= v * sellPrice
|
|
}
|
|
}
|
|
}
|
|
// 重新计算格子数量
|
|
grid := this.configure.GetTrollRule(comm.TrollItemCount)
|
|
for _, v := range trolltrain.Items {
|
|
if v > 0 {
|
|
gridNum += int32(math.Ceil(float64(v) / float64(grid)))
|
|
}
|
|
}
|
|
|
|
trolltrain.GridNum = gridNum
|
|
if gridNum > this.configure.GetTrollRule(comm.TrollGridCount) { // 背包格子上限
|
|
code = pb.ErrorCode_TrollMaxItemCount
|
|
return
|
|
}
|
|
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, int32(gold), true)
|
|
if code != pb.ErrorCode_Success { // 金币不足
|
|
code = pb.ErrorCode_GoldNoEnough
|
|
return
|
|
}
|
|
|
|
// 清除数量为0 的
|
|
for k, v := range trolltrain.Items {
|
|
if v == 0 {
|
|
delete(trolltrain.Items, k)
|
|
if _, ok := trolltrain.Price[k]; ok {
|
|
delete(trolltrain.Price, k) // 清除价格
|
|
}
|
|
}
|
|
}
|
|
trolltrain.TotalEarn += -int64(earn) // 累计获得的金币
|
|
// check npc level
|
|
if confLv := this.configure.GetTrollLv(trolltrain.GetNpcLv() + 1); confLv != nil {
|
|
if trolltrain.TotalEarn >= int64(confLv.Money) {
|
|
trolltrain.NpcLv += 1 // npc levelUp
|
|
update["npcLv"] = trolltrain.NpcLv
|
|
}
|
|
}
|
|
update["items"] = trolltrain.Items
|
|
update["price"] = trolltrain.Price
|
|
update["totalEarn"] = trolltrain.TotalEarn
|
|
|
|
update["gridNum"] = trolltrain.GridNum
|
|
update["shop"] = trolltrain.Shop
|
|
this.module.ModifyTrollData(session.GetUserId(), update)
|
|
|
|
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype153, 1)
|
|
go this.module.ModuleRtask.TriggerTask(session.GetUserId(), comm.GettaskParam(comm.Rtype153, 1))
|
|
return
|
|
}
|