优化
This commit is contained in:
parent
f641886c6e
commit
ae68f31d13
@ -39,6 +39,10 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell
|
|||||||
if v == 0 { // 过滤数量为0 的消息
|
if v == 0 { // 过滤数量为0 的消息
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if trolltrain.RangeId == 0 {
|
||||||
|
trolltrain.RangeId = 1
|
||||||
|
update["rangeId"] = trolltrain.RangeId
|
||||||
|
}
|
||||||
if v < 0 {
|
if v < 0 {
|
||||||
if !bSell {
|
if !bSell {
|
||||||
bSell = true
|
bSell = true
|
||||||
|
@ -41,7 +41,6 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
|
|||||||
aiCount := this.configure.GetTrollRule(comm.TrollAIBuyCount)
|
aiCount := this.configure.GetTrollRule(comm.TrollAIBuyCount)
|
||||||
if trolltrain.AiCount <= aiCount { // 小于离线挂机次数可执行
|
if trolltrain.AiCount <= aiCount { // 小于离线挂机次数可执行
|
||||||
this.module.TrollAI(session, trolltrain, aiCount)
|
this.module.TrollAI(session, trolltrain, aiCount)
|
||||||
update["aiCount"] = trolltrain.AiCount
|
|
||||||
}
|
}
|
||||||
|
|
||||||
maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
|
maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
|
||||||
@ -92,14 +91,18 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
|
|||||||
if leftTime <= v {
|
if leftTime <= v {
|
||||||
trolltrain.RefreshTime = time.Now().Unix() + int64(leftTime-v)
|
trolltrain.RefreshTime = time.Now().Unix() + int64(leftTime-v)
|
||||||
trolltrain.TarinPos += index
|
trolltrain.TarinPos += index
|
||||||
trolltrain.RangeId += index
|
if trolltrain.RangeId != 0 {
|
||||||
trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1
|
trolltrain.RangeId += index
|
||||||
|
trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1
|
||||||
|
}
|
||||||
|
|
||||||
trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1
|
trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
index += 1
|
index += 1
|
||||||
leftTime -= v
|
leftTime -= v
|
||||||
}
|
}
|
||||||
|
update["aiCount"] = trolltrain.AiCount
|
||||||
update["refreshTime"] = trolltrain.RefreshTime
|
update["refreshTime"] = trolltrain.RefreshTime
|
||||||
update["tarinPos"] = trolltrain.TarinPos
|
update["tarinPos"] = trolltrain.TarinPos
|
||||||
update["rangeId"] = trolltrain.RangeId
|
update["rangeId"] = trolltrain.RangeId
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"go_dreamfactory/lego/core"
|
"go_dreamfactory/lego/core"
|
||||||
"go_dreamfactory/modules"
|
"go_dreamfactory/modules"
|
||||||
"go_dreamfactory/pb"
|
"go_dreamfactory/pb"
|
||||||
|
"math"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -52,16 +53,16 @@ func (this *Troll) ModifyTrollData(uid string, data map[string]interface{}) (cod
|
|||||||
|
|
||||||
func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain, aiCount int32) (code pb.ErrorCode) {
|
func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain, aiCount int32) (code pb.ErrorCode) {
|
||||||
var (
|
var (
|
||||||
t int64 // 上一次刷新的时间
|
|
||||||
sellPrice map[int32]int32 // 出售货物价格
|
sellPrice map[int32]int32 // 出售货物价格
|
||||||
totalGold int32
|
totalGold int32
|
||||||
pos int32
|
index int32
|
||||||
|
update map[string]interface{}
|
||||||
)
|
)
|
||||||
if troll.Buy != 0 && troll.Sell != 0 {
|
if troll.Buy == 0 && troll.Sell == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
update = make(map[string]interface{})
|
||||||
sellPrice = make(map[int32]int32)
|
sellPrice = make(map[int32]int32)
|
||||||
t = troll.RefreshTime
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
trainNum := this.configure.GetTrollMaxTraintNum()
|
trainNum := this.configure.GetTrollMaxTraintNum()
|
||||||
maxCoefficient := this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
|
maxCoefficient := this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
|
||||||
@ -75,49 +76,64 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain, ai
|
|||||||
}
|
}
|
||||||
sz := this.configure.GetTrollAllTrain()
|
sz := this.configure.GetTrollAllTrain()
|
||||||
if len(sz) == 0 {
|
if len(sz) == 0 {
|
||||||
|
this.Errorf("GetTrollAllTrain configure err") // 配置异常 打个日志
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
iCount := this.configure.GetTrollRule(comm.TrollSurprise)
|
iCount := this.configure.GetTrollRule(comm.TrollAIBuyCount)
|
||||||
for {
|
for index = 0; ; index++ {
|
||||||
pos++
|
|
||||||
if pos > iCount {
|
index := int32(index) % trainNum
|
||||||
break
|
troll.RefreshTime += int64(sz[index])
|
||||||
}
|
if now >= troll.RefreshTime {
|
||||||
index := int32(pos) % trainNum
|
|
||||||
if int32(len(sz)) <= index {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
t += int64(sz[index])
|
|
||||||
if now < t {
|
|
||||||
troll.RangeId++
|
|
||||||
troll.TarinPos++
|
troll.TarinPos++
|
||||||
|
troll.RangeId++
|
||||||
troll.RangeId = (troll.RangeId % maxCoefficient) + 1
|
troll.RangeId = (troll.RangeId % maxCoefficient) + 1
|
||||||
troll.TarinPos = (troll.TarinPos % trainNum) + 1
|
troll.TarinPos = (troll.TarinPos % trainNum) + 1
|
||||||
coefficient := this.configure.GetTrollCoefficient(troll.RangeId)
|
coefficient := this.configure.GetTrollCoefficient(troll.RangeId) // 获取当前级别的涨幅数据
|
||||||
if coefficient == nil {
|
if coefficient == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if troll.Sell >= coefficient.Coefficient { // 可以出售
|
if troll.Sell >= coefficient.Coefficient { // 可以出售
|
||||||
|
var preGold int32
|
||||||
for _, v := range goods {
|
for _, v := range goods {
|
||||||
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient
|
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient / 1000
|
||||||
|
preGold += troll.Price[v.Id] * troll.Items[v.Id]
|
||||||
}
|
}
|
||||||
|
// 出售之前算成本
|
||||||
totalGold += this.SellAllItem(troll, sellPrice)
|
if len(troll.Items) > 0 {
|
||||||
troll.AiCount++
|
sellGold := this.SellAllItem(troll, sellPrice)
|
||||||
if troll.AiCount > aiCount { //达到最大交易次数
|
totalGold += sellGold
|
||||||
troll.RefreshTime = t // 设置上次刷新的时间
|
// 计算本次出售赚的金币
|
||||||
break
|
if sellGold-preGold > 0 {
|
||||||
|
troll.TotalEarn += int64(sellGold - preGold)
|
||||||
|
}
|
||||||
|
troll.AiCount++
|
||||||
|
if troll.AiCount > aiCount { //达到最大交易次数
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if troll.Buy >= coefficient.Coefficient { // 可以购买
|
if troll.Buy >= coefficient.Coefficient { // 可以购买
|
||||||
for _, v := range goods {
|
for _, v := range goods {
|
||||||
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient
|
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient / 1000
|
||||||
}
|
}
|
||||||
troll.Shop = make(map[int32]int32) // 买之前清除购买上限
|
troll.Shop = make(map[int32]int32) // 买之前清除购买上限
|
||||||
totalGold += this.BuyAllItem(session.GetUserId(), troll, sellPrice)
|
totalGold += this.BuyAllItem(session.GetUserId(), troll, sellPrice)
|
||||||
}
|
}
|
||||||
} else { // 超过当前时间
|
} else { // 超过当前时间
|
||||||
troll.RefreshTime = t
|
troll.RefreshTime -= int64(sz[index])
|
||||||
|
update["refreshTime"] = troll.RefreshTime
|
||||||
|
update["tarinPos"] = troll.TarinPos
|
||||||
|
update["rangeId"] = troll.RangeId
|
||||||
|
update["shop"] = troll.Shop
|
||||||
|
update["items"] = troll.Items
|
||||||
|
update["price"] = troll.Price
|
||||||
|
update["aiCount"] = troll.AiCount
|
||||||
|
update["gridNum"] = troll.GridNum
|
||||||
|
this.ModifyTrollData(session.GetUserId(), update)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if index > iCount*maxCoefficient { // ai挂机最大限制
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,6 +150,8 @@ func (this *Troll) SellAllItem(troll *pb.DBTrollTrain, price map[int32]int32) (g
|
|||||||
}
|
}
|
||||||
delete(troll.Items, k) // 清除数据
|
delete(troll.Items, k) // 清除数据
|
||||||
}
|
}
|
||||||
|
troll.Price = make(map[int32]int32, 0) // 原来的价格也清除
|
||||||
|
troll.GridNum = 0 // 清空格子
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,18 +170,29 @@ func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int3
|
|||||||
box = make(map[int32]int32, 0)
|
box = make(map[int32]int32, 0)
|
||||||
goods := this.configure.GetTrollAllGoods()
|
goods := this.configure.GetTrollAllGoods()
|
||||||
for _, v := range goods {
|
for _, v := range goods {
|
||||||
if leftGirdNum > 0 && troll.Shop[v.Id] < v.Max {
|
for {
|
||||||
leftGirdNum--
|
if leftGirdNum > 0 && troll.Shop[v.Id] < v.Max {
|
||||||
troll.Items[v.Id] += maxgoods
|
leftGirdNum--
|
||||||
troll.Shop[v.Id] += maxgoods
|
//troll.Items[v.Id] += maxgoods
|
||||||
box[v.Id] += maxgoods // 加入篮子
|
troll.Shop[v.Id] += maxgoods
|
||||||
|
box[v.Id] += maxgoods // 加入篮子
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查该位置的格子没有补满
|
// 检查该位置的格子没有补满
|
||||||
full := (troll.Items[v.Id] + box[v.Id]) % maxgoods
|
full := (troll.Items[v.Id] + box[v.Id]) % maxgoods
|
||||||
if full != 0 {
|
if full != 0 {
|
||||||
box[v.Id] += (maxgoods - full) // 格子补满
|
box[v.Id] += (maxgoods - full) // 格子补满
|
||||||
}
|
}
|
||||||
|
g := troll.Items[v.Id] * troll.Price[v.Id]
|
||||||
|
g += box[v.Id] * price[v.Id]
|
||||||
|
if (troll.Items[v.Id] + box[v.Id]) != 0 {
|
||||||
|
troll.Price[v.Id] = g / (troll.Items[v.Id] + box[v.Id])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通过金币来校验哪些物品可以买
|
// 通过金币来校验哪些物品可以买
|
||||||
curGold := this.ModuleUser.QueryAttributeValue(uid, comm.ResGold)
|
curGold := this.ModuleUser.QueryAttributeValue(uid, comm.ResGold)
|
||||||
for k, v := range box {
|
for k, v := range box {
|
||||||
@ -179,5 +208,13 @@ func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int3
|
|||||||
}
|
}
|
||||||
troll.Items = box
|
troll.Items = box
|
||||||
gold = costGold
|
gold = costGold
|
||||||
|
// 统计格子
|
||||||
|
troll.GridNum = 0
|
||||||
|
for _, v := range troll.Items {
|
||||||
|
if v > 0 {
|
||||||
|
troll.GridNum += int32(math.Ceil(float64(v) / float64(maxgoods)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user