go_dreamfactory/modules/troll/api_getlist.go

118 lines
3.7 KiB
Go

package troll
import (
"crypto/rand"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"math/big"
"time"
"google.golang.org/protobuf/proto"
)
//参数校验
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 proto.Message) {
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
}
//dayMaxCount := this.configure.GetTrollRule(comm.TrollBuyCount) // 8
aiCount := this.configure.GetTrollRule(comm.TrollAIBuyCount) // 10
if trolltrain.AiCount+trolltrain.SellCount < aiCount {
this.module.TrollAI(session, trolltrain)
}
maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
if maxCoefficient == 0 {
code = pb.ErrorCode_ConfigNoFound
return
}
// 跨天 则清除 每日交易次数
if !utils.IsToday(trolltrain.ResetTime) {
trolltrain.ResetTime = time.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)
}
/// 计算经过了多少个周期
sz := this.configure.GetTrollAllTrain()
var (
circletime int32 // 循环一个周期的时间
circleCount int32 // 循环的次数
leftTime int32
index int32
)
for _, v := range sz {
circletime += v
}
t := int32(time.Now().Unix() - trolltrain.RefreshTime) // 经过的时间
if t < 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) // 经过的周期数
c := int32((time.Now().Unix() - trolltrain.Ctime)) / circletime
if trolltrain.Circle != c {
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
update["circle"] = trolltrain.Circle
}
leftTime = (int32(t) % circletime)
// 循环次数
index += circleCount * trainNum // 计算火车的位置信息
for _, v := range sz {
if leftTime <= v {
trolltrain.RefreshTime = time.Now().Unix()
trolltrain.TarinPos += index
//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
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
}