package moonfantasy import ( "go_dreamfactory/comm" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/pb" "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 ( mdata *pb.DBMoonFantasy lock *redis.RedisMutex // umfantasy *pb.DBUserMFantasy user *pb.DBUser cd pb.ErrorCode isjson bool 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 this.module.modelDream.checkMFantasyExpiration(mdata) { //已过期 cd = pb.ErrorCode_MoonfantasyHasExpired return } for _, v := range mdata.Join { if v.Uid == session.GetUserId() { isjson = true break } } if !isjson { if len(mdata.Join) >= int(mdata.Numup) { cd = pb.ErrorCode_MoonfantasyJoinUp 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, // }) 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.ChangeList("", mdata.Id, map[string]interface{}{ "join": mdata.Join, }) } return }