Exemplo n.º 1
0
        public Song(BeatSaverSong s, IEnumerable <ScoreSaberDifficulty> scoreSaberDifficulties = null)
            : this()
        {
            SongId      = s._id;
            Key         = s.key.ToLower();
            Name        = s.name;
            Description = s.description;
            DeletedAt   = s.deletedAt;
            Hash        = s.hash.ToUpper();
            Uploaded    = s.uploaded;
            Converted   = s.converted;
            DownloadUrl = s.downloadURL;
            CoverUrl    = s.coverURL;

            SongName        = s.metadata.songName;
            SongSubName     = s.metadata.songSubName;
            SongAuthorName  = s.metadata.songAuthorName;
            LevelAuthorName = s.metadata.levelAuthorName;
            BeatsPerMinute  = s.metadata.bpm;

            Downloads = s.stats.downloads;
            Plays     = s.stats.plays;
            DownVotes = s.stats.downVotes;
            UpVotes   = s.stats.upVotes;
            Heat      = s.stats.heat;
            Rating    = s.stats.rating;

            ScrapedAt = s.ScrapedAt;

            //SongDifficulties = Difficulty.DictionaryToDifficulties(s.metadata.difficulties).
            //    Select(d => new SongDifficulty() { Difficulty = d, Song = this, SongId = s._id }).ToList();
            BeatmapCharacteristics = BeatmapCharacteristic.ConvertFrom(s.metadata.characteristics, this);
            UploaderRefId          = s.uploader.id;
            Uploader = new Uploader()
            {
                UploaderId = UploaderRefId, UploaderName = s.uploader.username
            };
            if (scoreSaberDifficulties != null)
            {
                ScoreSaberDifficulties = scoreSaberDifficulties.ToList();
            }
            else
            {
                ScoreSaberDifficulties = null; // new List<ScoreSaberDifficulty>();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new Song from a Beat Saver song JSON token.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown when the passed JToken doesn't contain the nested metadata, stats, or uploader objects.</exception>
        /// <param name="jSong"></param>
        public Song(JToken jSong)
        {
            JToken jMetadata        = jSong["metadata"];
            JToken jCharacteristics = jMetadata["characteristics"];
            JToken jStats           = jSong["stats"];
            JToken jUploader        = jSong["uploader"];

            if (jMetadata == null || jStats == null || jUploader == null)
            {
                var nullTokens = new string[] {
                    jMetadata == null ? "metadata" : string.Empty,
                    jStats == null ? "stats" : string.Empty,
                    jUploader == null ? "uploader" : string.Empty
                };
                throw new ArgumentException($"Unable to parse Song from jSong: {(string.Join(", ", nullTokens.Where(s => !string.IsNullOrEmpty(s))))} cannot be null.");
            }


            // Root
            SongId      = jSong["_id"]?.Value <string>();
            Key         = jSong["key"]?.Value <string>();
            Name        = jSong["name"]?.Value <string>();
            Description = jSong["description"]?.Value <string>();
            DeletedAt   = jSong["deletedAt"]?.Value <DateTime?>();
            Hash        = jSong["hash"]?.Value <string>().ToUpper();
            Uploaded    = jSong["uploaded"]?.Value <DateTime?>() ?? DateTime.MinValue;
            Converted   = jSong["converted"]?.Value <string>();
            DownloadUrl = jSong["downloadURL"]?.Value <string>();
            CoverUrl    = jSong["coverURL"]?.Value <string>();

            // Metadata
            //var characteristics = JsonConvert.DeserializeObject<List<string>>(jMetadata["characteristics"]?.ToString());// Change
            var diffs = JsonConvert.DeserializeObject <Dictionary <string, bool> >(jMetadata["difficulties"]?.ToString());

            //SongDifficulties = Difficulty.DictionaryToDifficulties(diffs)
            //        .Select(d => new SongDifficulty() { Difficulty = d, Song = this, SongId = SongId }).ToList();

            BeatmapCharacteristics = BeatmapCharacteristic.ConvertFrom(jCharacteristics, SongId);

            SongName        = jMetadata["songName"]?.Value <string>();
            SongSubName     = jMetadata["songSubName"]?.Value <string>();
            SongAuthorName  = jMetadata["songAuthorName"]?.Value <string>();
            LevelAuthorName = jMetadata["levelAuthorName"]?.Value <string>();
            BeatsPerMinute  = jMetadata["bpm"]?.Value <float>() ?? default(float);
            //Stats
            Downloads = jStats["downloads"]?.Value <int>() ?? 0;
            Plays     = jStats["plays"]?.Value <int>() ?? 0;
            DownVotes = jStats["downVotes"]?.Value <int>() ?? 0;
            UpVotes   = jStats["upVotes"]?.Value <int>() ?? 0;
            Heat      = jStats["heat"]?.Value <double>() ?? 0;
            Rating    = jStats["rating"]?.Value <double>() ?? 0;
            // Uploader
            UploaderRefId = jUploader["_id"]?.Value <string>().ToLower();
            Uploader      = new Uploader()
            {
                UploaderId = UploaderRefId, UploaderName = jUploader["username"]?.Value <string>()
            };

            ScrapedAt = jSong["ScrapedAt"]?.Value <DateTime>() ?? DateTime.MinValue;

            //ScoreSaberDifficulties = s.ScoreSaberInfo.Values.Select(d => new ScoreSaberDifficulty(d)).ToList(),
        }