This commit is contained in:
liwei1dao 2022-11-09 15:24:39 +08:00
commit 4845be43dc
2 changed files with 43 additions and 43 deletions

View File

@ -33,9 +33,7 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
return return
} }
// 自动交易 // 自动交易
//if trolltrain.AiCount+trolltrain.SellCount < this.configure.GetTrollRule(comm.TrollAIBuyCount) {
this.module.TrollAI(session, trolltrain) this.module.TrollAI(session, trolltrain)
//}
maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值 maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
if maxCoefficient == 0 { if maxCoefficient == 0 {
@ -54,49 +52,48 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
} }
/// 计算经过了多少个周期 /// 计算经过了多少个周期
sz := this.configure.GetTrollAllTrain() szTrain := this.configure.GetTrollAllTrain()
var ( var (
circletime int32 // 循环一个周期的时间 circletime int32 // 循环一个周期的时间
circleCount int32 // 循环的次数 circleCount int32 // 循环的次数
leftTime int32 leftTime int32 // 离到达最后一站剩余的时间
index int32 index int32 // 总共经过了多少次车站
) )
for _, v := range sz { for _, v := range szTrain {
circletime += v circletime += v
} }
t := int32(configure.Now().Unix() - trolltrain.RefreshTime) // 经过的时间 if int32(len(szTrain)) < trolltrain.TarinPos {
if t < sz[trolltrain.TarinPos-1] { this.module.Errorf("TarinPos error: TarinPos:%d,maxLen:%d", trolltrain.TarinPos, len(szTrain))
code = pb.ErrorCode_ConfigNoFound
return
}
if int32(configure.Now().Unix()-trolltrain.RefreshTime) < szTrain[trolltrain.TarinPos-1] {
session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain}) session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain})
return return
} }
trainNum := this.configure.GetTrollMaxTraintNum() trainNum := this.configure.GetTrollMaxTraintNum()
trolltrain.Shop = make(map[int32]int32) // 清空商人的购买数据 trolltrain.Shop = make(map[int32]int32) // 清空商人的购买数据
update["shop"] = trolltrain.Shop update["shop"] = trolltrain.Shop
circleCount = (int32(t) / circletime) // 经过的周期数 t := int32(configure.Now().Unix() - trolltrain.Ctime)
circleCount = t / circletime // 经过的周期数
c := int32((configure.Now().Unix() - trolltrain.Ctime)) / circletime leftTime = t % circletime
if trolltrain.Circle != c { if trolltrain.Circle != circleCount {
trolltrain.SurpriseID = make(map[int32]int32, 0) trolltrain.SurpriseID = make(map[int32]int32, 0)
n, _ := rand.Int(rand.Reader, big.NewInt(int64(trainNum))) n, _ := rand.Int(rand.Reader, big.NewInt(int64(trainNum)))
goods := this.configure.GetTrollAllGoods() goods := this.configure.GetTrollAllGoods()
n2, _ := rand.Int(rand.Reader, big.NewInt(int64(len(goods)-1))) //算的是下标所以-1 n2, _ := rand.Int(rand.Reader, big.NewInt(int64(len(goods)-1))) //算的是下标所以-1
trolltrain.SurpriseID[int32(n.Int64())+1] = int32(n2.Int64()) + 1 trolltrain.SurpriseID[int32(n.Int64())+1] = int32(n2.Int64()) + 1
update["surpriseID"] = trolltrain.SurpriseID update["surpriseID"] = trolltrain.SurpriseID
trolltrain.Circle = c trolltrain.Circle = circleCount
update["circle"] = trolltrain.Circle update["circle"] = trolltrain.Circle
} }
leftTime = (int32(t) % circletime)
index += circleCount * trainNum // 计算火车的位置信息
for _, v := range sz {
index = circleCount * trainNum // 计算火车的位置信息
for _, v := range szTrain {
if leftTime <= v { if leftTime <= v {
trolltrain.RefreshTime = configure.Now().Unix() trolltrain.RefreshTime = configure.Now().Unix()
trolltrain.TarinPos += index trolltrain.RangeId = (index % maxCoefficient) + 1
trolltrain.RangeId += index trolltrain.TarinPos = (index % trainNum) + 1
trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1
trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1
break break
} }
index += 1 index += 1

View File

@ -59,10 +59,13 @@ func (this *Troll) ModifyTrollData(uid string, data map[string]interface{}) (cod
func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (code pb.ErrorCode) { func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (code pb.ErrorCode) {
var ( var (
sellPrice map[int32]int32 // 出售货物价格 sellPrice map[int32]int32 // 出售货物价格
totalGold int32 totalGold int32
index int32 index int32
update map[string]interface{} update map[string]interface{}
rangeId int32 // 增幅ID
tarinPos int32 // 火车位置
refreshTime int64 // 刷新时间
) )
if troll.Buy == 0 && troll.Sell == 0 { if troll.Buy == 0 && troll.Sell == 0 {
return return
@ -76,6 +79,10 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
code = pb.ErrorCode_ConfigNoFound code = pb.ErrorCode_ConfigNoFound
return return
} }
rangeId = troll.RangeId
tarinPos = troll.TarinPos
refreshTime = troll.RefreshTime
goods := this.configure.GetTrollAllGoods() goods := this.configure.GetTrollAllGoods()
for _, v := range goods { for _, v := range goods {
sellPrice[v.Id] = v.Goodsprice sellPrice[v.Id] = v.Goodsprice
@ -87,17 +94,17 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
} }
iCount := this.configure.GetTrollRule(comm.TrollAIBuyCount) iCount := this.configure.GetTrollRule(comm.TrollAIBuyCount)
for index = 0; ; index++ { for index = 0; ; index++ {
if index < troll.TarinPos-1 { // 起始位置 if index < tarinPos-1 { // 起始位置
continue continue
} }
index := int32(index) % trainNum index := int32(index) % trainNum
troll.RefreshTime += int64(sz[index]) refreshTime += int64(sz[index])
if now >= troll.RefreshTime { if now >= refreshTime {
troll.RangeId = (troll.RangeId % maxCoefficient) + 1 rangeId = (rangeId % maxCoefficient) + 1
troll.TarinPos = (troll.TarinPos % trainNum) + 1 tarinPos = (tarinPos % trainNum) + 1
coefficient := this.configure.GetTrollCoefficient(troll.RangeId) // 获取当前级别的涨幅数据 coefficient := this.configure.GetTrollCoefficient(rangeId) // 获取当前级别的涨幅数据
if coefficient == nil { if coefficient == nil {
return return
} }
@ -109,7 +116,7 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
} }
// 出售之前算成本 // 出售之前算成本
if len(troll.Items) > 0 { if len(troll.Items) > 0 {
sellGold := this.SellAllItem(session.GetUserId(), troll, sellPrice) sellGold := this.SellAllItem(session.GetUserId(), troll, sellPrice, tarinPos)
if sellGold != 0 { if sellGold != 0 {
if code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, sellGold, true); code != pb.ErrorCode_Success { if code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, sellGold, true); code != pb.ErrorCode_Success {
this.Errorf("玩家 uid:%s 金币不足,获得金币%d", session.GetUserId(), sellGold) this.Errorf("玩家 uid:%s 金币不足,获得金币%d", session.GetUserId(), sellGold)
@ -133,7 +140,7 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient / 1000 sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient / 1000
} }
troll.Shop = make(map[int32]int32) // 买之前清除购买上限 troll.Shop = make(map[int32]int32) // 买之前清除购买上限
buyGold := this.BuyAllItem(session.GetUserId(), troll, sellPrice) buyGold := this.BuyAllItem(session.GetUserId(), troll, sellPrice, tarinPos)
if buyGold != 0 { if buyGold != 0 {
if code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, buyGold, true); code != pb.ErrorCode_Success { if code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, buyGold, true); code != pb.ErrorCode_Success {
this.Errorf("玩家 uid:%s 金币不足,获得金币%d", session.GetUserId(), buyGold) this.Errorf("玩家 uid:%s 金币不足,获得金币%d", session.GetUserId(), buyGold)
@ -144,7 +151,7 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
} }
} else { // 超过当前时间 } else { // 超过当前时间
troll.RefreshTime -= int64(sz[index]) refreshTime -= int64(sz[index])
break break
} }
@ -152,10 +159,6 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
break break
} }
} }
update["refreshTime"] = troll.RefreshTime
update["tarinPos"] = troll.TarinPos
update["rangeId"] = troll.RangeId
update["shop"] = troll.Shop update["shop"] = troll.Shop
update["items"] = troll.Items update["items"] = troll.Items
update["price"] = troll.Price update["price"] = troll.Price
@ -173,7 +176,7 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c
} }
// 出售所有货物 // 出售所有货物
func (this *Troll) SellAllItem(uid string, troll *pb.DBTrollTrain, price map[int32]int32) (gold int32) { func (this *Troll) SellAllItem(uid string, troll *pb.DBTrollTrain, price map[int32]int32, tarinPos int32) (gold int32) {
for k, v := range troll.Items { for k, v := range troll.Items {
if _, ok := price[k]; ok { if _, ok := price[k]; ok {
gold += price[k] * v gold += price[k] * v
@ -184,14 +187,14 @@ func (this *Troll) SellAllItem(uid string, troll *pb.DBTrollTrain, price map[int
troll.GridNum = 0 // 清空格子 troll.GridNum = 0 // 清空格子
// 写统计 // 写统计
if gold > 0 { if gold > 0 {
this.record.AddTrollRecord(uid, gold, troll.TarinPos) this.record.AddTrollRecord(uid, gold, tarinPos)
} }
return return
} }
// 可以购买商品 // 可以购买商品
func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int32]int32) (gold int32) { func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int32]int32, tarinPos int32) (gold int32) {
var ( var (
box map[int32]int32 // 盒子 存放可购买的物品 box map[int32]int32 // 盒子 存放可购买的物品
leftGirdNum int32 // 剩余可购买格子数量 leftGirdNum int32 // 剩余可购买格子数量
@ -261,7 +264,7 @@ func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int3
} }
// 写统计 // 写统计
if gold != 0 { if gold != 0 {
this.record.AddTrollRecord(uid, gold, troll.TarinPos) this.record.AddTrollRecord(uid, gold, tarinPos)
} }
return return
} }