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) { // sz := make(map[int32]int32, 0) // sz[1] = -20 // sz[2] = -10 // this.BuyOrSell(session, &pb.TrollBuyOrSellReq{ // Items: sz, // }) 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 } // 测试代码===================== //this.module.TrollAI(session, trolltrain) //=========================== maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值 if maxCoefficient == 0 { code = pb.ErrorCode_ConfigNoFound return } // 跨天 则清除 每日交易次数 if !utils.IsToday(trolltrain.RefreshTime) { trolltrain.SellCount = 0 update["sellCount"] = trolltrain.SellCount 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) // 经过的周期数 if circleCount > 1 { trolltrain.Circle += circleCount n, _ := rand.Int(rand.Reader, big.NewInt(int64(trainNum))) n2, _ := rand.Int(rand.Reader, big.NewInt(int64(maxCoefficient))) trolltrain.SurpriseID[int32(n.Int64())+1] = int32(n2.Int64()) + 1 } leftTime = (int32(t) % circletime) // 循环次数 index += circleCount * trainNum // 计算火车的位置信息 for _, v := range sz { if leftTime <= v { trolltrain.RefreshTime = time.Now().Unix() + int64(leftTime-v) trolltrain.TarinPos += index trolltrain.RangeId += index trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1 trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1 update["refreshTime"] = trolltrain.RefreshTime update["tarinPos"] = trolltrain.TarinPos update["rangeId"] = trolltrain.RangeId break } index += 1 leftTime -= v } this.module.ModifyTrollData(session.GetUserId(), update) session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain}) return }