diff --git a/modules/troll/api_getlist.go b/modules/troll/api_getlist.go index d440eb6c4..687e7ed11 100644 --- a/modules/troll/api_getlist.go +++ b/modules/troll/api_getlist.go @@ -33,9 +33,7 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq) return } // 自动交易 - //if trolltrain.AiCount+trolltrain.SellCount < this.configure.GetTrollRule(comm.TrollAIBuyCount) { this.module.TrollAI(session, trolltrain) - //} maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值 if maxCoefficient == 0 { @@ -58,45 +56,40 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq) var ( circletime int32 // 循环一个周期的时间 circleCount int32 // 循环的次数 - leftTime int32 - index int32 + leftTime int32 // 离到达最后一站剩余的时间 + index int32 // 总共经过了多少次车站 ) for _, v := range sz { circletime += v } - t := int32(time.Now().Unix() - trolltrain.RefreshTime) // 经过的时间 - if t < sz[trolltrain.TarinPos-1] { + if int32(time.Now().Unix()-trolltrain.RefreshTime) < sz[trolltrain.TarinPos-1] { session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain}) return } trainNum := this.configure.GetTrollMaxTraintNum() trolltrain.Shop = make(map[int32]int32) // 清空商人的购买数据 update["shop"] = trolltrain.Shop - circleCount = (int32(t) / circletime) // 经过的周期数 + circleCount = (int32(time.Now().Unix()-trolltrain.Ctime) / circletime) // 经过的周期数 - c := int32((time.Now().Unix() - trolltrain.Ctime)) / circletime - if trolltrain.Circle != c { + if trolltrain.Circle != circleCount { trolltrain.SurpriseID = make(map[int32]int32, 0) n, _ := rand.Int(rand.Reader, big.NewInt(int64(trainNum))) goods := this.configure.GetTrollAllGoods() n2, _ := rand.Int(rand.Reader, big.NewInt(int64(len(goods)-1))) //算的是下标所以-1 trolltrain.SurpriseID[int32(n.Int64())+1] = int32(n2.Int64()) + 1 update["surpriseID"] = trolltrain.SurpriseID - trolltrain.Circle = c + trolltrain.Circle = circleCount update["circle"] = trolltrain.Circle } - leftTime = (int32(t) % circletime) - index += circleCount * trainNum // 计算火车的位置信息 + leftTime = (int32((time.Now().Unix() - trolltrain.Ctime)) % circletime) + + index = circleCount * trainNum // 计算火车的位置信息 for _, v := range sz { - if leftTime <= v { trolltrain.RefreshTime = time.Now().Unix() - trolltrain.TarinPos += index - trolltrain.RangeId += index - trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1 - - trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1 + trolltrain.RangeId = (index % maxCoefficient) + 1 + trolltrain.TarinPos = (index % trainNum) + 1 break } index += 1 diff --git a/modules/troll/module.go b/modules/troll/module.go index 172b888e1..c233d94da 100644 --- a/modules/troll/module.go +++ b/modules/troll/module.go @@ -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) { var ( - sellPrice map[int32]int32 // 出售货物价格 - totalGold int32 - index int32 - update map[string]interface{} + sellPrice map[int32]int32 // 出售货物价格 + totalGold int32 + index int32 + update map[string]interface{} + rangeId int32 // 增幅ID + tarinPos int32 // 火车位置 + refreshTime int64 // 刷新时间 ) if troll.Buy == 0 && troll.Sell == 0 { return @@ -76,6 +79,10 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c code = pb.ErrorCode_ConfigNoFound return } + rangeId = troll.RangeId + tarinPos = troll.TarinPos + refreshTime = troll.RefreshTime + goods := this.configure.GetTrollAllGoods() for _, v := range goods { 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) for index = 0; ; index++ { - if index < troll.TarinPos-1 { // 起始位置 + if index < tarinPos-1 { // 起始位置 continue } index := int32(index) % trainNum - troll.RefreshTime += int64(sz[index]) + refreshTime += int64(sz[index]) - if now >= troll.RefreshTime { - troll.RangeId = (troll.RangeId % maxCoefficient) + 1 - troll.TarinPos = (troll.TarinPos % trainNum) + 1 + if now >= refreshTime { + rangeId = (rangeId % maxCoefficient) + 1 + tarinPos = (tarinPos % trainNum) + 1 - coefficient := this.configure.GetTrollCoefficient(troll.RangeId) // 获取当前级别的涨幅数据 + coefficient := this.configure.GetTrollCoefficient(rangeId) // 获取当前级别的涨幅数据 if coefficient == nil { return } @@ -109,7 +116,7 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c } // 出售之前算成本 if len(troll.Items) > 0 { - sellGold := this.SellAllItem(session.GetUserId(), troll, sellPrice) + sellGold := this.SellAllItem(session.GetUserId(), troll, sellPrice, tarinPos) if sellGold != 0 { if code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, sellGold, true); code != pb.ErrorCode_Success { 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 } 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 code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, buyGold, true); code != pb.ErrorCode_Success { this.Errorf("玩家 uid:%s 金币不足,获得金币%d", session.GetUserId(), buyGold) @@ -144,7 +151,7 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c } } else { // 超过当前时间 - troll.RefreshTime -= int64(sz[index]) + refreshTime -= int64(sz[index]) break } @@ -152,10 +159,6 @@ func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (c break } } - - update["refreshTime"] = troll.RefreshTime - update["tarinPos"] = troll.TarinPos - update["rangeId"] = troll.RangeId update["shop"] = troll.Shop update["items"] = troll.Items 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 { if _, ok := price[k]; ok { gold += price[k] * v @@ -184,14 +187,14 @@ func (this *Troll) SellAllItem(uid string, troll *pb.DBTrollTrain, price map[int troll.GridNum = 0 // 清空格子 // 写统计 if gold > 0 { - this.record.AddTrollRecord(uid, gold, troll.TarinPos) + this.record.AddTrollRecord(uid, gold, tarinPos) } 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 ( box map[int32]int32 // 盒子 存放可购买的物品 leftGirdNum int32 // 剩余可购买格子数量 @@ -261,7 +264,7 @@ func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int3 } // 写统计 if gold != 0 { - this.record.AddTrollRecord(uid, gold, troll.TarinPos) + this.record.AddTrollRecord(uid, gold, tarinPos) } return }