Ejemplo n.º 1
0
        public Game GetGame(string gameId, GameEmbeds embeds = default)
        {
            var parameters = new List <string>()
            {
                embeds.ToString()
            };
            var uri    = GetGamesUri(string.Format("/{0}{1}", Uri.EscapeDataString(gameId), parameters.ToParameters()));
            var result = baseClient.DoRequest(uri);

            return(Game.Parse(baseClient, result.data));
        }
Ejemplo n.º 2
0
        public IEnumerable <Game> GetGames(
            string seriesId,
            string name           = null, int?yearOfRelease   = null,
            string platformId     = null, string regionId     = null,
            string moderatorId    = null, int?elementsPerPage = null,
            GameEmbeds embeds     = default(GameEmbeds),
            GamesOrdering orderBy = default(GamesOrdering))
        {
            var parameters = new List <string>()
            {
                embeds.ToString()
            };

            parameters.AddRange(orderBy.ToParameters());

            if (!string.IsNullOrEmpty(name))
            {
                parameters.Add(string.Format("name={0}", Uri.EscapeDataString(name)));
            }

            if (yearOfRelease.HasValue)
            {
                parameters.Add(string.Format("released={0}", yearOfRelease.Value));
            }

            if (!string.IsNullOrEmpty(platformId))
            {
                parameters.Add(string.Format("platform={0}", Uri.EscapeDataString(platformId)));
            }

            if (!string.IsNullOrEmpty(regionId))
            {
                parameters.Add(string.Format("region={0}", Uri.EscapeDataString(regionId)));
            }

            if (!string.IsNullOrEmpty(moderatorId))
            {
                parameters.Add(string.Format("moderator={0}", Uri.EscapeDataString(moderatorId)));
            }

            if (elementsPerPage.HasValue)
            {
                parameters.Add(string.Format("max={0}", elementsPerPage.Value));
            }

            var uri = GetSeriesUri(string.Format("/{0}/games{1}",
                                                 Uri.EscapeDataString(seriesId),
                                                 parameters.ToParameters()));

            return(baseClient.DoPaginatedRequest(uri,
                                                 x => Game.Parse(baseClient, x) as Game));
        }
Ejemplo n.º 3
0
        public ReadOnlyCollection <Game> GetRomHacks(string gameId,
                                                     GameEmbeds embeds     = default(GameEmbeds),
                                                     GamesOrdering orderBy = default(GamesOrdering))
        {
            var parameters = new List <string>()
            {
                embeds.ToString()
            };

            parameters.AddRange(orderBy.ToParameters());

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

            return(baseClient.DoDataCollectionRequest(uri,
                                                      x => Game.Parse(baseClient, x) as Game));
        }
Ejemplo n.º 4
0
        internal static void Parse(Run run, SpeedrunComClient client, dynamic runElement)
        {
            //Parse Attributes
            IDictionary <string, dynamic> properties = runElement as IDictionary <string, dynamic>;

            run.ID      = properties["id"] as string;
            run.WebLink = new Uri(properties["weblink"] as string);
            run.Videos  = RunVideos.Parse(client, properties["videos"]) as RunVideos;
            run.Comment = properties["comment"] as string;
            run.Status  = RunStatus.Parse(client, properties["status"]) as RunStatus;

            Func <dynamic, Player> parsePlayer = x => Player.Parse(client, x) as Player;

            if (properties["players"] is IEnumerable <dynamic> )
            {
                run.Players = client.ParseCollection(properties["players"], parsePlayer);
            }
            else if (properties["players"] is System.Collections.ArrayList && properties["players"].Count == 0)
            {
                run.Players = new List <Player>().AsReadOnly();
            }
            else
            {
                run.Players = client.ParseCollection(properties["players"].data, parsePlayer);
            }

            if (properties.ContainsKey("date"))
            {
                if (properties["date"] != null)
                {
                    run.Date = DateTime.ParseExact(properties["date"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture);
                }
            }

            if (properties.ContainsKey("submitted"))
            {
                if (runElement.submitted != null)
                {
                    run.DateSubmitted = (DateTime)runElement.submitted;
                }
            }

            run.Times  = RunTimes.Parse(client, properties["times"]);
            run.System = RunSystem.Parse(client, properties["system"]);

            var splits = properties["splits"];

            if (splits != null)
            {
                run.SplitsUri = new Uri(splits.uri as string);
            }

            if (properties.ContainsKey("values"))
            {
                var valueProperties = properties["values"] as IDictionary <string, dynamic>;

                run.VariableValues = valueProperties.Select(x => VariableValue.ParseValueDescriptor(client, x) as VariableValue).ToList().AsReadOnly();
            }
            else
            {
                run.VariableValues = new List <VariableValue>().AsReadOnly();
            }

            //Parse Links

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

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

                if (category != null)
                {
                    run.CategoryID = category.ID;
                }
            }

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

                    run.level = new Lazy <Level>(() => level);

                    if (level != null)
                    {
                        run.LevelID = level.ID;
                    }
                }
                else
                {
                    run.level = new Lazy <Level>(() => null);
                }
            }

            if (properties.ContainsKey("platform"))
            {
                Platform platform = Platform.Parse(client, properties["platform"].data);

                run.System.platform = new Lazy <Platform>(() => platform);
            }

            if (properties.ContainsKey("region"))
            {
                Region region = Region.Parse(client, properties["region"].data);

                run.System.region = new Lazy <Region>(() => region);
            }

            if (!string.IsNullOrEmpty(run.Status.ExaminerUserID))
            {
                run.examiner = new Lazy <User>(() => client.Users.GetUser(run.Status.ExaminerUserID));
            }
            else
            {
                run.examiner = new Lazy <User>(() => null);
            }
        }
Ejemplo n.º 5
0
        internal static void Parse(Run run, SpeedrunComClient client, dynamic runElement)
        {
            //Parse Attributes

            run.ID      = runElement.id as string;
            run.WebLink = new Uri(runElement.weblink as string);
            run.Videos  = RunVideos.Parse(client, runElement.videos) as RunVideos;
            run.Comment = runElement.comment as string;
            run.Status  = RunStatus.Parse(client, runElement.status) as RunStatus;

            Func <dynamic, Player> parsePlayer = x => Player.Parse(client, x) as Player;

            if (runElement.players is IEnumerable <dynamic> )
            {
                run.Players = client.ParseCollection(runElement.players, parsePlayer);
            }
            else if (runElement.players is System.Collections.ArrayList && runElement.players.Count == 0)
            {
                run.Players = new List <Player>().AsReadOnly();
            }
            else
            {
                run.Players = client.ParseCollection(runElement.players.data, parsePlayer);
            }

            var runDate = runElement.date;

            if (!string.IsNullOrEmpty(runDate))
            {
                run.Date = DateTime.Parse(runDate, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            }

            var dateSubmitted = runElement.submitted;

            if (!string.IsNullOrEmpty(dateSubmitted))
            {
                run.DateSubmitted = DateTime.Parse(dateSubmitted, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            }

            run.Times  = RunTimes.Parse(client, runElement.times) as RunTimes;
            run.System = RunSystem.Parse(client, runElement.system) as RunSystem;

            var splits = runElement.splits;

            if (splits != null)
            {
                run.SplitsUri = new Uri(splits.uri as string);
            }

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

            //Parse Links

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

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

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

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

            if (properties.ContainsKey("platform"))
            {
                var platform = Platform.Parse(client, properties["platform"].data) as Platform;
                run.System.platform = new Lazy <Platform>(() => platform);
            }

            if (properties.ContainsKey("region"))
            {
                var region = Region.Parse(client, properties["region"].data) as Region;
                run.System.region = new Lazy <Region>(() => region);
            }

            if (!string.IsNullOrEmpty(run.Status.ExaminerUserID))
            {
                run.examiner = new Lazy <User>(() => client.Users.GetUser(run.Status.ExaminerUserID));
            }
            else
            {
                run.examiner = new Lazy <User>(() => null);
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }