Exemple #1
0
        public static Variable Parse(SpeedrunComClient client, dynamic variableElement)
        {
            var variable = new Variable();

            variable.client = client;
            var properties = variableElement as IDictionary <string, dynamic>;
            var links      = properties["links"] as IEnumerable <dynamic>;

            //Parse Attributes

            variable.ID                      = variableElement.id as string;
            variable.Name                    = variableElement.name as string;
            variable.Scope                   = VariableScope.Parse(client, variableElement.scope) as VariableScope;
            variable.IsMandatory             = (bool)(variableElement.mandatory ?? false);
            variable.IsUserDefined           = (bool)(properties["user-defined"] ?? false);
            variable.IsUsedForObsoletingRuns = (bool)variableElement.obsoletes;
            if (!(variableElement.values.choices is ArrayList))
            {
                var choiceElements = variableElement.values.choices as IDictionary <string, dynamic>;
                variable.Values = choiceElements.Select(x => VariableValue.ParseIDPair(client, variable, x) as VariableValue).ToList().AsReadOnly();
            }
            else
            {
                variable.Values = new ReadOnlyCollection <VariableValue>(new VariableValue[0]);
            }
            var valuesProperties = variableElement.values as IDictionary <string, dynamic>;
            var defaultValue     = valuesProperties["default"] as string;

            if (!string.IsNullOrEmpty(defaultValue))
            {
                variable.DefaultValue = variable.Values.FirstOrDefault(x => x.ID == defaultValue);
            }
            variable.IsSubcategory = (bool)(properties["is-subcategory"] ?? false);

            //Parse Links

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

            if (gameLink != null)
            {
                var gameUri = gameLink.uri as string;
                variable.GameID = gameUri.Substring(gameUri.LastIndexOf("/") + 1);
                variable.game   = new Lazy <Game>(() => client.Games.GetGame(variable.GameID));
            }
            else
            {
                variable.game = new Lazy <Game>(() => null);
            }
            variable.CategoryID = variableElement.category as string;
            if (!string.IsNullOrEmpty(variable.CategoryID))
            {
                variable.category = new Lazy <Category>(() => client.Categories.GetCategory(variable.CategoryID));
            }
            else
            {
                variable.category = new Lazy <Category>(() => null);
            }
            if (!string.IsNullOrEmpty(variable.Scope.LevelID))
            {
                variable.level = new Lazy <Level>(() => client.Levels.GetLevel(variable.Scope.LevelID));
            }
            else
            {
                variable.level = new Lazy <Level>(() => null);
            }
            return(variable);
        }