From ae68f31d13acfede598c52a9fed96559c9893055 Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Thu, 3 Nov 2022 11:03:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/troll/api_buyorsell.go | 4 ++ modules/troll/api_getlist.go | 9 ++- modules/troll/module.go | 101 ++++++++++++++++++++++----------- 3 files changed, 79 insertions(+), 35 deletions(-) diff --git a/modules/troll/api_buyorsell.go b/modules/troll/api_buyorsell.go index efaca41f2..d8ef8ba63 100644 --- a/modules/troll/api_buyorsell.go +++ b/modules/troll/api_buyorsell.go @@ -39,6 +39,10 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell if v == 0 { // 过滤数量为0 的消息 continue } + if trolltrain.RangeId == 0 { + trolltrain.RangeId = 1 + update["rangeId"] = trolltrain.RangeId + } if v < 0 { if !bSell { bSell = true diff --git a/modules/troll/api_getlist.go b/modules/troll/api_getlist.go index ce4afe507..d956e4aea 100644 --- a/modules/troll/api_getlist.go +++ b/modules/troll/api_getlist.go @@ -41,7 +41,6 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq) aiCount := this.configure.GetTrollRule(comm.TrollAIBuyCount) if trolltrain.AiCount <= aiCount { // 小于离线挂机次数可执行 this.module.TrollAI(session, trolltrain, aiCount) - update["aiCount"] = trolltrain.AiCount } maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值 @@ -92,14 +91,18 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq) if leftTime <= v { trolltrain.RefreshTime = time.Now().Unix() + int64(leftTime-v) trolltrain.TarinPos += index - trolltrain.RangeId += index - trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1 + if trolltrain.RangeId != 0 { + trolltrain.RangeId += index + trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1 + } + trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1 break } index += 1 leftTime -= v } + update["aiCount"] = trolltrain.AiCount update["refreshTime"] = trolltrain.RefreshTime update["tarinPos"] = trolltrain.TarinPos update["rangeId"] = trolltrain.RangeId diff --git a/modules/troll/module.go b/modules/troll/module.go index 01920e33f..a4405a94d 100644 --- a/modules/troll/module.go +++ b/modules/troll/module.go @@ -10,6 +10,7 @@ import ( "go_dreamfactory/lego/core" "go_dreamfactory/modules" "go_dreamfactory/pb" + "math" "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) { var ( - t int64 // 上一次刷新的时间 sellPrice map[int32]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 } + update = make(map[string]interface{}) sellPrice = make(map[int32]int32) - t = troll.RefreshTime now := time.Now().Unix() trainNum := this.configure.GetTrollMaxTraintNum() maxCoefficient := this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值 @@ -75,49 +76,64 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain, ai } sz := this.configure.GetTrollAllTrain() if len(sz) == 0 { + this.Errorf("GetTrollAllTrain configure err") // 配置异常 打个日志 return } - iCount := this.configure.GetTrollRule(comm.TrollSurprise) - for { - pos++ - if pos > iCount { - break - } - index := int32(pos) % trainNum - if int32(len(sz)) <= index { - break - } - t += int64(sz[index]) - if now < t { - troll.RangeId++ + iCount := this.configure.GetTrollRule(comm.TrollAIBuyCount) + for index = 0; ; index++ { + + index := int32(index) % trainNum + troll.RefreshTime += int64(sz[index]) + if now >= troll.RefreshTime { troll.TarinPos++ + troll.RangeId++ troll.RangeId = (troll.RangeId % maxCoefficient) + 1 troll.TarinPos = (troll.TarinPos % trainNum) + 1 - coefficient := this.configure.GetTrollCoefficient(troll.RangeId) + coefficient := this.configure.GetTrollCoefficient(troll.RangeId) // 获取当前级别的涨幅数据 if coefficient == nil { return } if troll.Sell >= coefficient.Coefficient { // 可以出售 + var preGold int32 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) - troll.AiCount++ - if troll.AiCount > aiCount { //达到最大交易次数 - troll.RefreshTime = t // 设置上次刷新的时间 - break + // 出售之前算成本 + if len(troll.Items) > 0 { + sellGold := this.SellAllItem(troll, sellPrice) + totalGold += sellGold + // 计算本次出售赚的金币 + if sellGold-preGold > 0 { + troll.TotalEarn += int64(sellGold - preGold) + } + troll.AiCount++ + if troll.AiCount > aiCount { //达到最大交易次数 + break + } } } if troll.Buy >= coefficient.Coefficient { // 可以购买 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) // 买之前清除购买上限 totalGold += this.BuyAllItem(session.GetUserId(), troll, sellPrice) } } 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 } } @@ -134,6 +150,8 @@ func (this *Troll) SellAllItem(troll *pb.DBTrollTrain, price map[int32]int32) (g } delete(troll.Items, k) // 清除数据 } + troll.Price = make(map[int32]int32, 0) // 原来的价格也清除 + troll.GridNum = 0 // 清空格子 return } @@ -152,18 +170,29 @@ func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int3 box = make(map[int32]int32, 0) goods := this.configure.GetTrollAllGoods() for _, v := range goods { - if leftGirdNum > 0 && troll.Shop[v.Id] < v.Max { - leftGirdNum-- - troll.Items[v.Id] += maxgoods - troll.Shop[v.Id] += maxgoods - box[v.Id] += maxgoods // 加入篮子 + for { + if leftGirdNum > 0 && troll.Shop[v.Id] < v.Max { + leftGirdNum-- + //troll.Items[v.Id] += maxgoods + troll.Shop[v.Id] += maxgoods + box[v.Id] += maxgoods // 加入篮子 + } else { + break + } } + // 检查该位置的格子没有补满 full := (troll.Items[v.Id] + box[v.Id]) % maxgoods if full != 0 { 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) for k, v := range box { @@ -179,5 +208,13 @@ func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int3 } troll.Items = box gold = costGold + // 统计格子 + troll.GridNum = 0 + for _, v := range troll.Items { + if v > 0 { + troll.GridNum += int32(math.Ceil(float64(v) / float64(maxgoods))) + } + } + return }