150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
package troll
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"math"
|
|
"time"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
//参数校验
|
|
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 proto.Message) {
|
|
|
|
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
|
|
}
|
|
|
|
for k, v := range req.Items {
|
|
if v < 0 {
|
|
bSell = true
|
|
if bSell {
|
|
bSell = false
|
|
trolltrain.SellCount += 1 // 交易次数+1
|
|
if trolltrain.SellCount > 5 {
|
|
code = pb.ErrorCode_TrollMaxSellCount // 达到最大交易次数 直接返回
|
|
return
|
|
}
|
|
update["sellCount"] = trolltrain.SellCount
|
|
}
|
|
}
|
|
trolltrain.Items[k] += v
|
|
// 校验 是否大于买入最大限制
|
|
goods := this.configure.GetTrollGoods(k)
|
|
if goods == nil {
|
|
return
|
|
}
|
|
if goods.Max < trolltrain.Items[k] { // 判断是否有效交易
|
|
// 买入上限 直接返回
|
|
code = pb.ErrorCode_TrollBuyMax
|
|
return
|
|
} else if trolltrain.Items[k] < 0 { //卖出数量不足
|
|
code = pb.ErrorCode_TrollSellMax
|
|
return
|
|
}
|
|
|
|
// 第一次购买商品
|
|
if trolltrain.TarinPos == 0 {
|
|
trolltrain.Price[k] = goods.Goodsprice * goods.StarMoney / 1000
|
|
trolltrain.RefreshTime = time.Now().Unix()
|
|
//消耗的金币
|
|
gold = trolltrain.Price[k] * trolltrain.Items[k]
|
|
} else {
|
|
p := this.configure.GetTrollCoefficient(trolltrain.TarinPos)
|
|
if p == nil {
|
|
return
|
|
}
|
|
if v < 0 { // 卖出
|
|
// 卖出价格
|
|
var sellPrice int32
|
|
if trolltrain.TarinPos == 0 {
|
|
sellPrice = goods.Goodsprice * goods.StarMoney / 1000
|
|
} else {
|
|
|
|
sellPrice = int32(p.Coefficient) * goods.Goodsprice / 1000
|
|
}
|
|
if sellPrice > trolltrain.Price[k] { // 赚了
|
|
earn += (sellPrice - trolltrain.Price[k]) * trolltrain.Items[k]
|
|
}
|
|
gold += (sellPrice - trolltrain.Price[k]) * trolltrain.Items[k]
|
|
} else { // 买入 计算平均价格
|
|
totalGold := trolltrain.Items[k] * trolltrain.Price[k]
|
|
sellPrice := int32(p.Coefficient) * goods.Goodsprice / 1000
|
|
totalGold += v * goods.Goodsprice * sellPrice
|
|
trolltrain.Price[k] = totalGold / (trolltrain.Items[k] + v)
|
|
}
|
|
}
|
|
}
|
|
// 重新计算格子数量
|
|
grid := this.configure.GetTrollRule(comm.TrollItemCount)
|
|
for _, v := range trolltrain.Items {
|
|
if v > 0 {
|
|
gridNum += int32(math.Floor(float64(v / grid)))
|
|
}
|
|
}
|
|
// 获取背包格子上限配置
|
|
|
|
trolltrain.GridNum = gridNum
|
|
if gridNum > this.configure.GetTrollRule(comm.TrollGridCount) { // 背包格子上限
|
|
code = pb.ErrorCode_TrollMaxItemCount
|
|
return
|
|
}
|
|
if gold < 0 { // 买入的金币
|
|
if code = this.module.ConsumeRes(session, []*cfg.Gameatn{&cfg.Gameatn{
|
|
A: "attr",
|
|
T: "gold",
|
|
N: earn,
|
|
}}, true); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
} else {
|
|
if code = this.module.DispenseRes(session, []*cfg.Gameatn{&cfg.Gameatn{
|
|
A: "attr",
|
|
T: "gold",
|
|
N: earn,
|
|
}}, true); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
}
|
|
|
|
// check npc level
|
|
if confLv := this.configure.GetTrollLv(trolltrain.GetNpcLv() + 1); confLv != nil {
|
|
if earn >= confLv.Money {
|
|
trolltrain.NpcLv += 1 // npc levelUp
|
|
update["npcLv"] = trolltrain.NpcLv
|
|
}
|
|
}
|
|
|
|
trolltrain.TotalEarn += int64(earn) // 累计获得的金币
|
|
update["items"] = trolltrain.Items
|
|
update["price"] = trolltrain.Price
|
|
update["totalEarn"] = trolltrain.TotalEarn
|
|
|
|
update["gridNum"] = trolltrain.GridNum
|
|
this.module.ModifyTrollData(session.GetUserId(), update)
|
|
session.SendMsg(string(this.module.GetType()), TrollBuyOrSellResp, &pb.TrollBuyOrSellResp{Data: trolltrain})
|
|
return
|
|
}
|