package troll import ( "crypto/rand" "go_dreamfactory/comm" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" "math/big" ) //参数校验 func (this *apiComp) SurpriseIdCheck(session comm.IUserSession, req *pb.TrollSurpriseIdReq) (code pb.ErrorCode) { return } func (this *apiComp) SurpriseId(session comm.IUserSession, req *pb.TrollSurpriseIdReq) (code pb.ErrorCode, data *pb.ErrorData) { var ( circletime int32 // 循环一个周期的时间 circleCount int32 // 循环的次数 update map[string]interface{} ) update = make(map[string]interface{}) trolltrain, err := this.module.modelTroll.getTrollList(session.GetUserId()) if err != nil { code = pb.ErrorCode_DBError return } /// 计算经过了多少个周期 szTrain := this.configure.GetTrollAllTrain() 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 int32(configure.Now().Unix()-trolltrain.RefreshTime) >= szTrain[trolltrain.TarinPos-1] { trainNum := this.configure.GetTrollMaxTraintNum() t := int32(configure.Now().Unix() - trolltrain.Ctime) circleCount = 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 this.module.ModifyTrollData(session.GetUserId(), update) } return } session.SendMsg(string(this.module.GetType()), TrollSurpriseIdResp, &pb.TrollSurpriseIdResp{ SurpriseID: trolltrain.SurpriseID, }) return }