Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }