go_dreamfactory/modules/caravan/api_getlist.go
2023-04-24 20:35:03 +08:00

112 lines
3.7 KiB
Go

package caravan
import (
"crypto/rand"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/utils"
"math/big"
)
//参数校验
func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.TrollGetListReq) (code pb.ErrorCode) {
return
}
func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq) (code pb.ErrorCode, data *pb.ErrorData) {
var (
update map[string]interface{}
maxCoefficient int32
)
update = make(map[string]interface{})
if code = this.GetListCheck(session, req); code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
trolltrain, err := this.module.modelTroll.getTrollList(session.GetUserId())
if err != nil {
code = pb.ErrorCode_DBError
return
}
// 自动交易
this.module.TrollAI(session, trolltrain)
maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
if maxCoefficient == 0 {
code = pb.ErrorCode_ConfigNoFound
return
}
// 跨天 则清除 每日交易次数
if !utils.IsToday(trolltrain.ResetTime) {
trolltrain.ResetTime = configure.Now().Unix()
update["resetTime"] = trolltrain.ResetTime
trolltrain.SellCount = 0
update["sellCount"] = trolltrain.SellCount // 重置每日交易次数
trolltrain.AiCount = 0
update["aiCount"] = trolltrain.AiCount // 重置ai 交易次数
this.module.ModifyTrollData(session.GetUserId(), update)
}
/// 计算经过了多少个周期
szTrain := this.configure.GetTrollAllTrain()
var (
circletime int32 // 循环一个周期的时间
circleCount int32 // 循环的次数
leftTime int32 // 离到达最后一站剩余的时间
index int32 // 总共经过了多少次车站
)
for _, v := range szTrain {
circletime += v
}
if int32(len(szTrain)) < trolltrain.TarinPos {
this.module.Errorf("TarinPos error: TarinPos:%d,maxLen:%d", trolltrain.TarinPos, len(szTrain))
code = pb.ErrorCode_ConfigNoFound
return
}
if configure.Now().Unix()-trolltrain.RefreshTime < int64(szTrain[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
t := int32(configure.Now().Unix() - trolltrain.Ctime)
circleCount = t / circletime // 经过的周期数
leftTime = t % circletime
if trolltrain.Circle != circleCount {
trolltrain.SurpriseID = make(map[int32]int32, 0)
n, _ := rand.Int(rand.Reader, big.NewInt(int64(trainNum)))
// 只算当前商人所属的货物
g := this.configure.GetTrollGoodsFor(int32(n.Int64()) + 1)
n2, _ := rand.Int(rand.Reader, big.NewInt(int64(len(g))))
trolltrain.SurpriseID[int32(n.Int64())+1] = g[int32(n2.Int64())]
update["surpriseID"] = trolltrain.SurpriseID
trolltrain.Circle = circleCount
update["circle"] = trolltrain.Circle
}
index = circleCount * trainNum // 计算火车的位置信息
for _, v := range szTrain {
if leftTime <= v {
trolltrain.RefreshTime = configure.Now().Unix() - int64(leftTime) //trolltrain.Ctime + int64(circleCount*circletime+leftTime)
trolltrain.RangeId = (index % maxCoefficient) + 1
trolltrain.TarinPos = (index % trainNum) + 1
break
}
index += 1
leftTime -= v
}
update["aiCount"] = trolltrain.AiCount
update["refreshTime"] = trolltrain.RefreshTime
update["tarinPos"] = trolltrain.TarinPos
update["rangeId"] = trolltrain.RangeId
update["shop"] = trolltrain.Shop
update["items"] = trolltrain.Items
update["price"] = trolltrain.Price
this.module.ModifyTrollData(session.GetUserId(), update)
session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain})
return
}