Example #1
0
        public Variable GetVariable(string variableId)
        {
            var uri = GetVariablesUri(string.Format("/{0}",
                                                    Uri.EscapeDataString(variableId)));

            var result = baseClient.DoRequest(uri);

            return(Variable.Parse(baseClient, result.data));
        }
Example #2
0
        public ReadOnlyCollection <Variable> GetVariables(string gameId, VariablesOrdering orderBy = default)
        {
            var parameters = new List <string>(orderBy.ToParameters());

            var uri = GetGamesUri(string.Format("/{0}/variables{1}",
                                                Uri.EscapeDataString(gameId),
                                                parameters.ToParameters()));

            return(baseClient.DoDataCollectionRequest(uri,
                                                      x => Variable.Parse(baseClient, x) as Variable));
        }
Example #3
0
        public static Level Parse(SpeedrunComClient client, dynamic levelElement)
        {
            if (levelElement is ArrayList)
            {
                return(null);
            }

            var level = new Level();

            //Parse Attributes

            level.ID      = levelElement.id as string;
            level.Name    = levelElement.name as string;
            level.WebLink = new Uri(levelElement.weblink as string);
            level.Rules   = levelElement.rules;

            //Parse Links

            var properties = levelElement.Properties as IDictionary <string, dynamic>;
            var links      = properties["links"] as IEnumerable <dynamic>;

            var gameUri = links.First(x => x.rel == "game").uri as string;

            level.GameID = gameUri.Substring(gameUri.LastIndexOf('/') + 1);
            level.game   = new Lazy <Game>(() => client.Games.GetGame(level.GameID));

            if (properties.ContainsKey("categories"))
            {
                Func <dynamic, Category>      categoryParser = x => Category.Parse(client, x) as Category;
                ReadOnlyCollection <Category> categories     = client.ParseCollection(levelElement.categories.data, categoryParser);
                level.categories = new Lazy <ReadOnlyCollection <Category> >(() => categories);
            }
            else
            {
                level.categories = new Lazy <ReadOnlyCollection <Category> >(() => client.Levels.GetCategories(level.ID));
            }

            if (properties.ContainsKey("variables"))
            {
                Func <dynamic, Variable>      variableParser = x => Variable.Parse(client, x) as Variable;
                ReadOnlyCollection <Variable> variables      = client.ParseCollection(levelElement.variables.data, variableParser);
                level.variables = new Lazy <ReadOnlyCollection <Variable> >(() => variables);
            }
            else
            {
                level.variables = new Lazy <ReadOnlyCollection <Variable> >(() => client.Levels.GetVariables(level.ID));
            }

            level.Runs = client.Runs.GetRuns(levelId: level.ID);

            return(level);
        }
Example #4
0
        public static Leaderboard Parse(SpeedrunComClient client, dynamic leaderboardElement)
        {
            var leaderboard = new Leaderboard();

            var properties = leaderboardElement.Properties as IDictionary <string, dynamic>;

            //Parse Attributes

            leaderboard.WebLink = new Uri(leaderboardElement.weblink as string);

            var emulators = leaderboardElement.emulators as string;

            if (emulators == "true")
            {
                leaderboard.EmulatorFilter = EmulatorsFilter.OnlyEmulators;
            }
            else if (emulators == "false")
            {
                leaderboard.EmulatorFilter = EmulatorsFilter.NoEmulators;
            }
            else
            {
                leaderboard.EmulatorFilter = EmulatorsFilter.NotSet;
            }

            leaderboard.AreRunsWithoutVideoFilteredOut = properties["video-only"];

            //TODO Not actually optional
            if (leaderboardElement.timing != null)
            {
                leaderboard.OrderedBy = TimingMethodHelpers.FromString(leaderboardElement.timing as string);
            }

            if (leaderboardElement.values is DynamicJsonObject)
            {
                var valueProperties = leaderboardElement.values.Properties as IDictionary <string, dynamic>;
                leaderboard.VariableFilters = valueProperties.Select(x => VariableValue.ParseValueDescriptor(client, x) as VariableValue).ToList().AsReadOnly();
            }
            else
            {
                leaderboard.VariableFilters = new List <VariableValue>().AsReadOnly();
            }

            Func <dynamic, Record> recordParser = x => Record.Parse(client, x) as Record;

            leaderboard.Records = client.ParseCollection(leaderboardElement.runs, recordParser);

            //Parse Links

            if (properties["game"] is string)
            {
                leaderboard.GameID = leaderboardElement.game as string;
                leaderboard.game   = new Lazy <Game>(() => client.Games.GetGame(leaderboard.GameID));
            }
            else
            {
                var game = Game.Parse(client, properties["game"].data) as Game;
                leaderboard.game   = new Lazy <Game>(() => game);
                leaderboard.GameID = game.ID;
            }

            if (properties["category"] is string)
            {
                leaderboard.CategoryID = leaderboardElement.category as string;
                leaderboard.category   = new Lazy <Category>(() => client.Categories.GetCategory(leaderboard.CategoryID));
            }
            else
            {
                var category = Category.Parse(client, properties["category"].data) as Category;
                leaderboard.category = new Lazy <Category>(() => category);
                if (category != null)
                {
                    leaderboard.CategoryID = category.ID;
                }
            }

            if (properties["level"] == null)
            {
                leaderboard.level = new Lazy <Level>(() => null);
            }
            else if (properties["level"] is string)
            {
                leaderboard.LevelID = leaderboardElement.level as string;
                leaderboard.level   = new Lazy <Level>(() => client.Levels.GetLevel(leaderboard.LevelID));
            }
            else
            {
                var level = Level.Parse(client, properties["level"].data) as Level;
                leaderboard.level = new Lazy <Level>(() => level);
                if (level != null)
                {
                    leaderboard.LevelID = level.ID;
                }
            }

            if (properties["platform"] == null)
            {
                leaderboard.platformFilter = new Lazy <Platform>(() => null);
            }
            else if (properties["platform"] is string)
            {
                leaderboard.PlatformIDOfFilter = properties["platform"] as string;
                leaderboard.platformFilter     = new Lazy <Platform>(() => client.Platforms.GetPlatform(leaderboard.PlatformIDOfFilter));
            }
            else
            {
                var platform = Platform.Parse(client, properties["platform"].data) as Platform;
                leaderboard.platformFilter = new Lazy <Platform>(() => platform);
                if (platform != null)
                {
                    leaderboard.PlatformIDOfFilter = platform.ID;
                }
            }

            if (properties["region"] == null)
            {
                leaderboard.regionFilter = new Lazy <Region>(() => null);
            }
            else if (properties["region"] is string)
            {
                leaderboard.RegionIDOfFilter = properties["region"] as string;
                leaderboard.regionFilter     = new Lazy <Region>(() => client.Regions.GetRegion(leaderboard.RegionIDOfFilter));
            }
            else
            {
                var region = Region.Parse(client, properties["region"].data) as Region;
                leaderboard.regionFilter = new Lazy <Region>(() => region);
                if (region != null)
                {
                    leaderboard.RegionIDOfFilter = region.ID;
                }
            }

            //Parse Embeds

            if (properties.ContainsKey("players"))
            {
                Func <dynamic, Player> playerParser = x => Player.Parse(client, x) as Player;
                var players = client.ParseCollection(leaderboardElement.players.data, playerParser) as ReadOnlyCollection <Player>;

                foreach (var record in leaderboard.Records)
                {
                    record.Players = record.Players.Select(x => players.FirstOrDefault(y => x.Equals(y))).ToList().AsReadOnly();
                }

                leaderboard.players = new Lazy <ReadOnlyCollection <Player> >(() => players);
            }
            else
            {
                leaderboard.players = new Lazy <ReadOnlyCollection <Player> >(() => leaderboard.Records.SelectMany(x => x.Players).ToList().Distinct().ToList().AsReadOnly());
            }

            if (properties.ContainsKey("regions"))
            {
                Func <dynamic, Region> regionParser = x => Region.Parse(client, x) as Region;
                var regions = client.ParseCollection(leaderboardElement.regions.data, regionParser) as ReadOnlyCollection <Region>;

                foreach (var record in leaderboard.Records)
                {
                    record.System.region = new Lazy <Region>(() => regions.FirstOrDefault(x => x.ID == record.System.RegionID));
                }

                leaderboard.usedRegions = new Lazy <ReadOnlyCollection <Region> >(() => regions);
            }
            else
            {
                leaderboard.usedRegions = new Lazy <ReadOnlyCollection <Region> >(() => leaderboard.Records.Select(x => x.Region).Distinct().Where(x => x != null).ToList().AsReadOnly());
            }

            if (properties.ContainsKey("platforms"))
            {
                Func <dynamic, Platform> platformParser = x => Platform.Parse(client, x) as Platform;
                var platforms = client.ParseCollection(leaderboardElement.platforms.data, platformParser) as ReadOnlyCollection <Platform>;

                foreach (var record in leaderboard.Records)
                {
                    record.System.platform = new Lazy <Platform>(() => platforms.FirstOrDefault(x => x.ID == record.System.PlatformID));
                }

                leaderboard.usedPlatforms = new Lazy <ReadOnlyCollection <Platform> >(() => platforms);
            }
            else
            {
                leaderboard.usedPlatforms = new Lazy <ReadOnlyCollection <Platform> >(() => leaderboard.Records.Select(x => x.Platform).Distinct().Where(x => x != null).ToList().AsReadOnly());
            }

            Action <ReadOnlyCollection <Variable> > patchVariablesOfRecords = variables =>
            {
                foreach (var record in leaderboard.Records)
                {
                    foreach (var value in record.VariableValues)
                    {
                        value.variable = new Lazy <Variable>(() => variables.FirstOrDefault(x => x.ID == value.VariableID));
                    }
                }
            };

            if (properties.ContainsKey("variables"))
            {
                Func <dynamic, Variable> variableParser = x => Variable.Parse(client, x) as Variable;
                var variables = client.ParseCollection(leaderboardElement.variables.data, variableParser) as ReadOnlyCollection <Variable>;

                patchVariablesOfRecords(variables);

                leaderboard.applicableVariables = new Lazy <ReadOnlyCollection <Variable> >(() => variables);
            }
            else if (string.IsNullOrEmpty(leaderboard.LevelID))
            {
                leaderboard.applicableVariables = new Lazy <ReadOnlyCollection <Variable> >(() =>
                {
                    var variables = leaderboard.Category.Variables;

                    patchVariablesOfRecords(variables);

                    return(variables);
                });
            }
            else
            {
                leaderboard.applicableVariables = new Lazy <ReadOnlyCollection <Variable> >(() =>
                {
                    var variables = leaderboard.Category.Variables.Concat(leaderboard.Level.Variables).ToList().Distinct().ToList().AsReadOnly();

                    patchVariablesOfRecords(variables);

                    return(variables);
                });
            }

            return(leaderboard);
        }
Example #5
0
        public static Category Parse(SpeedrunComClient client, dynamic categoryElement)
        {
            if (categoryElement is ArrayList)
            {
                return(null);
            }

            var category = new Category();

            //Parse Attributes

            category.ID              = categoryElement.id as string;
            category.Name            = categoryElement.name as string;
            category.WebLink         = new Uri(categoryElement.weblink as string);
            category.Type            = categoryElement.type == "per-game" ? CategoryType.PerGame : CategoryType.PerLevel;
            category.Rules           = categoryElement.rules as string;
            category.Players         = Players.Parse(client, categoryElement.players);
            category.IsMiscellaneous = categoryElement.miscellaneous;

            //Parse Links

            var properties = categoryElement.Properties as IDictionary <string, dynamic>;
            var links      = properties["links"] as IEnumerable <dynamic>;

            var gameUri = links.First(x => x.rel == "game").uri as string;

            category.GameID = gameUri.Substring(gameUri.LastIndexOf('/') + 1);

            if (properties.ContainsKey("game"))
            {
                var gameElement = properties["game"].data;
                var game        = Game.Parse(client, gameElement) as Game;
                category.game = new Lazy <Game>(() => game);
            }
            else
            {
                category.game = new Lazy <Game>(() => client.Games.GetGame(category.GameID));
            }

            if (properties.ContainsKey("variables"))
            {
                Func <dynamic, Variable> parser = x => Variable.Parse(client, x) as Variable;
                var variables = client.ParseCollection(properties["variables"].data, parser);
                category.variables = new Lazy <ReadOnlyCollection <Variable> >(() => variables);
            }
            else
            {
                category.variables = new Lazy <ReadOnlyCollection <Variable> >(() => client.Categories.GetVariables(category.ID));
            }

            category.Runs = client.Runs.GetRuns(categoryId: category.ID);

            if (category.Type == CategoryType.PerGame)
            {
                category.leaderboard = new Lazy <Leaderboard>(() =>
                {
                    var leaderboard = client.Leaderboards
                                      .GetLeaderboardForFullGameCategory(category.GameID, category.ID);

                    leaderboard.game     = new Lazy <Game>(() => category.Game);
                    leaderboard.category = new Lazy <Category>(() => category);

                    foreach (var record in leaderboard.Records)
                    {
                        record.game     = leaderboard.game;
                        record.category = leaderboard.category;
                    }

                    return(leaderboard);
                });
                category.worldRecord = new Lazy <Record>(() =>
                {
                    if (category.leaderboard.IsValueCreated)
                    {
                        return(category.Leaderboard.Records.FirstOrDefault());
                    }

                    var leaderboard = client.Leaderboards
                                      .GetLeaderboardForFullGameCategory(category.GameID, category.ID, top: 1);

                    leaderboard.game     = new Lazy <Game>(() => category.Game);
                    leaderboard.category = new Lazy <Category>(() => category);

                    foreach (var record in leaderboard.Records)
                    {
                        record.game     = leaderboard.game;
                        record.category = leaderboard.category;
                    }

                    return(leaderboard.Records.FirstOrDefault());
                });
            }
            else
            {
                category.leaderboard = new Lazy <Leaderboard>(() => null);
                category.worldRecord = new Lazy <Record>(() => null);
            }

            return(category);
        }
Example #6
0
        public static Game Parse(SpeedrunComClient client, dynamic gameElement)
        {
            var game = new Game();

            //Parse Attributes
            var properties = gameElement as IDictionary <string, dynamic>;

            game.Header        = GameHeader.Parse(client, gameElement);
            game.YearOfRelease = (int)properties["released"];
            game.Ruleset       = Ruleset.Parse(client, properties["ruleset"]);
            game.IsRomHack     = properties["romhack"];
            game.CreationDate  = properties["created"];
            game.Assets        = Assets.Parse(client, properties["assets"]);

            //Parse Embeds

            if (properties.ContainsKey("moderators"))
            {
                IDictionary <string, dynamic> moderatorsProperties = properties["moderators"] as IDictionary <string, dynamic>;

                if (moderatorsProperties.ContainsKey("data"))
                {
                    ReadOnlyCollection <User> users = client.ParseCollection(moderatorsProperties["data"], new Func <dynamic, User>(x => User.Parse(client, x) as User));
                    game.moderatorUsers = new Lazy <ReadOnlyCollection <User> >(() => users);
                }
                else
                {
                    game.Moderators = moderatorsProperties.Select(x => Moderator.Parse(client, x)).ToList().AsReadOnly();

                    game.moderatorUsers = new Lazy <ReadOnlyCollection <User> >(
                        () =>
                    {
                        ReadOnlyCollection <User> users;

                        if (game.Moderators.Count(x => !x.user.IsValueCreated) > 1)
                        {
                            users = client.Games.GetGame(game.ID, embeds: new GameEmbeds(embedModerators: true)).ModeratorUsers;

                            foreach (var user in users)
                            {
                                var moderator = game.Moderators.FirstOrDefault(x => x.UserID == user.ID);
                                if (moderator != null)
                                {
                                    moderator.user = new Lazy <User>(() => user);
                                }
                            }
                        }
                        else
                        {
                            users = game.Moderators.Select(x => x.User).ToList().AsReadOnly();
                        }

                        return(users);
                    });
                }
            }
            else
            {
                game.Moderators     = new ReadOnlyCollection <Moderator>(new Moderator[0]);
                game.moderatorUsers = new Lazy <ReadOnlyCollection <User> >(() => new List <User>().AsReadOnly());
            }

            if (properties["platforms"] is IList)
            {
                game.PlatformIDs = client.ParseCollection <string>(properties["platforms"]);

                if (game.PlatformIDs.Count > 1)
                {
                    game.platforms = new Lazy <ReadOnlyCollection <Platform> >(
                        () => client.Games.GetGame(game.ID, embeds: new GameEmbeds(embedPlatforms: true)).Platforms);
                }
                else
                {
                    game.platforms = new Lazy <ReadOnlyCollection <Platform> >(
                        () => game.PlatformIDs.Select(x => client.Platforms.GetPlatform(x)).ToList().AsReadOnly());
                }
            }
            else
            {
                Func <dynamic, Platform>      platformParser = x => Platform.Parse(client, x) as Platform;
                ReadOnlyCollection <Platform> platforms      = client.ParseCollection(properties["regions"]["data"], platformParser);
                game.platforms   = new Lazy <ReadOnlyCollection <Platform> >(() => platforms);
                game.PlatformIDs = platforms.Select(x => x.ID).ToList().AsReadOnly();
            }

            if (properties["regions"] is IList)
            {
                game.RegionIDs = client.ParseCollection <string>(properties["regions"]);

                if (game.RegionIDs.Count > 1)
                {
                    game.regions = new Lazy <ReadOnlyCollection <Region> >(
                        () => client.Games.GetGame(game.ID, embeds: new GameEmbeds(embedRegions: true)).Regions);
                }
                else
                {
                    game.regions = new Lazy <ReadOnlyCollection <Region> >(
                        () => game.RegionIDs.Select(x => client.Regions.GetRegion(x)).ToList().AsReadOnly());
                }
            }
            else
            {
                ReadOnlyCollection <Region> regions = client.ParseCollection(properties["regions"]["data"], new Func <dynamic, Region>(x => Region.Parse(client, x) as Region));
                game.regions   = new Lazy <ReadOnlyCollection <Region> >(() => regions);
                game.RegionIDs = regions.Select(x => x.ID).ToList().AsReadOnly();
            }

            //Parse Links

            game.Runs = client.Runs.GetRuns(gameId: game.ID);

            if (properties.ContainsKey("levels"))
            {
                Func <dynamic, Level>      levelParser = x => Level.Parse(client, x) as Level;
                ReadOnlyCollection <Level> levels      = client.ParseCollection(properties["levels"]["data"], levelParser);
                game.levels = new Lazy <ReadOnlyCollection <Level> >(() => levels);
            }
            else
            {
                game.levels = new Lazy <ReadOnlyCollection <Level> >(() => client.Games.GetLevels(game.ID));
            }

            if (properties.ContainsKey("categories"))
            {
                Func <dynamic, Category>      categoryParser = x => Category.Parse(client, x) as Category;
                ReadOnlyCollection <Category> categories     = client.ParseCollection(properties["categories"]["data"], categoryParser);


                foreach (var category in categories)
                {
                    category.game = new Lazy <Game>(() => game);
                }

                game.categories = new Lazy <ReadOnlyCollection <Category> >(() => categories);
            }
            else
            {
                game.categories = new Lazy <ReadOnlyCollection <Category> >(() =>
                {
                    var categories = client.Games.GetCategories(game.ID);

                    foreach (var category in categories)
                    {
                        category.game = new Lazy <Game>(() => game);
                    }

                    return(categories);
                });
            }

            if (properties.ContainsKey("variables"))
            {
                Func <dynamic, Variable>      variableParser = x => Variable.Parse(client, x) as Variable;
                ReadOnlyCollection <Variable> variables      = client.ParseCollection(properties["variables"]["data"], variableParser);
                game.variables = new Lazy <ReadOnlyCollection <Variable> >(() => variables);
            }
            else
            {
                game.variables = new Lazy <ReadOnlyCollection <Variable> >(() => client.Games.GetVariables(game.ID));
            }

            var links      = properties["links"] as IEnumerable <dynamic>;
            var seriesLink = links.FirstOrDefault(x => x.rel == "series");

            if (seriesLink != null)
            {
                var parentUri = seriesLink.uri.ToString();
                game.SeriesID = parentUri.Substring(parentUri.LastIndexOf('/') + 1);
                game.series   = new Lazy <Series>(() => client.Series.GetSingleSeries(game.SeriesID));
            }
            else
            {
                game.series = new Lazy <Series>(() => null);
            }

            var originalGameLink = links.FirstOrDefault(x => x.rel == "game");

            if (originalGameLink != null)
            {
                var originalGameUri = originalGameLink.uri as string;
                game.OriginalGameID = originalGameUri.Substring(originalGameUri.LastIndexOf('/') + 1);
                game.originalGame   = new Lazy <Game>(() => client.Games.GetGame(game.OriginalGameID));
            }
            else
            {
                game.originalGame = new Lazy <Game>(() => null);
            }

            game.romHacks = new Lazy <ReadOnlyCollection <Game> >(() =>
            {
                var romHacks = client.Games.GetRomHacks(game.ID);

                if (romHacks != null)
                {
                    foreach (var romHack in romHacks)
                    {
                        romHack.originalGame = new Lazy <Game>(() => game);
                    }
                }

                return(romHacks);
            });

            return(game);
        }