Exemplo n.º 1
0
        public static Assets Parse(SpeedrunComClient client, dynamic assetsElement)
        {
            var assets = new Assets();

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

            assets.Logo = ImageAsset.Parse(client, assetsElement.logo) as ImageAsset;
            assets.CoverTiny = ImageAsset.Parse(client, properties["cover-tiny"]) as ImageAsset;
            assets.CoverSmall = ImageAsset.Parse(client, properties["cover-small"]) as ImageAsset;
            assets.CoverMedium = ImageAsset.Parse(client, properties["cover-medium"]) as ImageAsset;
            assets.CoverLarge = ImageAsset.Parse(client, properties["cover-large"]) as ImageAsset;
            assets.Icon = ImageAsset.Parse(client, assetsElement.icon) as ImageAsset;
            assets.TrophyFirstPlace = ImageAsset.Parse(client, properties["trophy-1st"]) as ImageAsset;
            assets.TrophySecondPlace = ImageAsset.Parse(client, properties["trophy-2nd"]) as ImageAsset;
            assets.TrophyThirdPlace = ImageAsset.Parse(client, properties["trophy-3rd"]) as ImageAsset;
            assets.TrophyFourthPlace = ImageAsset.Parse(client, properties["trophy-4th"]) as ImageAsset;
            assets.BackgroundImage = ImageAsset.Parse(client, assetsElement.background) as ImageAsset;
            assets.ForegroundImage = ImageAsset.Parse(client, assetsElement.foreground) as ImageAsset;

            return assets;
        }
Exemplo n.º 2
0
        public static Game Parse(SpeedrunComClient client, dynamic gameElement)
        {
            var game = new Game();

            //Parse Attributes

            game.Header        = GameHeader.Parse(client, gameElement);
            game.YearOfRelease = gameElement.released;
            game.Ruleset       = Ruleset.Parse(client, gameElement.ruleset);

            game.IsRomHack = gameElement.romhack;

            var created = gameElement.created as string;

            if (!string.IsNullOrEmpty(created))
            {
                game.CreationDate = DateTime.Parse(created, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            }

            game.Assets = Assets.Parse(client, gameElement.assets);

            //Parse Embeds

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

            if (gameElement.moderators is DynamicJsonObject && gameElement.moderators.Properties.ContainsKey("data"))
            {
                Func <dynamic, User>      userParser = x => User.Parse(client, x) as User;
                ReadOnlyCollection <User> users      = client.ParseCollection(gameElement.moderators.data, userParser);
                game.moderatorUsers = new Lazy <ReadOnlyCollection <User> >(() => users);
            }
            else if (gameElement.moderators is DynamicJsonObject)
            {
                var moderatorsProperties = gameElement.moderators.Properties as IDictionary <string, dynamic>;
                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>(gameElement.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(gameElement.platforms.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>(gameElement.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
            {
                Func <dynamic, Region>      regionParser = x => Region.Parse(client, x) as Region;
                ReadOnlyCollection <Region> regions      = client.ParseCollection(gameElement.regions.data, regionParser);
                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(gameElement.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(gameElement.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(gameElement.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 as string;
                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);
        }
Exemplo n.º 3
0
        public static Series Parse(SpeedrunComClient client, dynamic seriesElement)
        {
            var series = new Series();

            //Parse Attributes

            series.ID           = seriesElement.id as string;
            series.Name         = seriesElement.names.international as string;
            series.JapaneseName = seriesElement.names.japanese as string;
            series.WebLink      = new Uri(seriesElement.weblink as string);
            series.Abbreviation = seriesElement.abbreviation as string;

            var created = seriesElement.created as string;

            if (!string.IsNullOrEmpty(created))
            {
                series.CreationDate = DateTime.Parse(created, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
            }

            series.Assets = Assets.Parse(client, seriesElement.assets);

            //Parse Embeds

            if (seriesElement.moderators is DynamicJsonObject && seriesElement.moderators.Properties.ContainsKey("data"))
            {
                Func <dynamic, User>      userParser = x => User.Parse(client, x) as User;
                ReadOnlyCollection <User> users      = client.ParseCollection(seriesElement.moderators.data, userParser);
                series.moderatorUsers = new Lazy <ReadOnlyCollection <User> >(() => users);
            }
            else if (seriesElement.moderators is DynamicJsonObject)
            {
                var moderatorsProperties = seriesElement.moderators.Properties as IDictionary <string, dynamic>;
                series.Moderators = moderatorsProperties.Select(x => Moderator.Parse(client, x)).ToList().AsReadOnly();

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

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

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

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

            //Parse Links

            series.Games = client.Series.GetGames(series.ID).Select(game =>
            {
                game.series = new Lazy <Series>(() => series);
                return(game);
            }).Cache();

            return(series);
        }