package moonfantasy import ( "go_dreamfactory/comm" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "time" "google.golang.org/protobuf/proto" ) //参数校验 func (this *apiComp) AskCheck(session comm.IUserSession, req *pb.MoonfantasyAskReq) (code pb.ErrorCode) { return } ///询问怪物是否可以挑战 func (this *apiComp) Ask(session comm.IUserSession, req *pb.MoonfantasyAskReq) (code pb.ErrorCode, data proto.Message) { var ( globalconf *cfg.GameGlobalData mdata *pb.DBMoonFantasy lock *redis.RedisMutex umfantasy *pb.DBUserMFantasy user *pb.DBUser cd pb.ErrorCode err error ) defer func() { session.SendMsg(string(this.module.GetType()), "ask", &pb.MoonfantasyAskResp{Code: cd, Info: mdata}) }() if cd = this.AskCheck(session, req); cd != pb.ErrorCode_Success { return } //月之秘境需要加分布式锁 防止多人同时操作 if lock, err = this.module.modelDream.newDreamLock(req.Mid); err != nil { cd = pb.ErrorCode_DBError return } defer lock.Unlock() if err = lock.Lock(); err != nil { cd = pb.ErrorCode_DBError this.module.Errorln(err) return } if mdata, err = this.module.modelDream.querymfantasy(req.Mid); err != nil { cd = pb.ErrorCode_DBError return } if mdata == nil { cd = pb.ErrorCode_MoonfantasyHasExpired return } if len(mdata.Join) >= int(mdata.Numup) { cd = pb.ErrorCode_MoonfantasyJoinUp return } globalconf = this.module.configure.GetGlobalConf() if time.Now().Sub(time.Unix(mdata.Ctime, 0)).Seconds() >= float64(globalconf.DreamlandLimit) { this.module.modelDream.Del(mdata.Id) cd = pb.ErrorCode_MoonfantasyHasExpired return } if v, ok := mdata.Record[session.GetUserId()]; ok { if v >= mdata.Unitmup { cd = pb.ErrorCode_MoonfantasyDareUp return } } if umfantasy, err = this.module.modelUserMF.queryUsermfantasy(session.GetUserId()); err != nil { code = pb.ErrorCode_CacheReadError return } umfantasy.Mfantasys = append(umfantasy.Mfantasys, mdata.Id) this.module.modelUserMF.Change(session.GetUserId(), map[string]interface{}{ "mfantasys": umfantasy.Mfantasys, }) for _, v := range mdata.Join { if v.Uid == session.GetUserId() { return } } if user = this.module.ModuleUser.GetUser(session.GetUserId()); user == nil { this.module.Errorf("no found uer:%d", session.GetUserId()) code = pb.ErrorCode_DBError return } mdata.Join = append(mdata.Join, &pb.UserInfo{Uid: user.Uid, Name: user.Name, Avatar: user.Avatar, Lv: user.Lv}) this.module.modelDream.Change(mdata.Id, map[string]interface{}{ "join": mdata.Join, }) return }