Esempio n. 1
0
        public static VariableValue ParseIDPair(SpeedrunComClient client, Variable variable, KeyValuePair<string, dynamic> valueElement)
        {
            var value = new VariableValue();

            value.VariableID = variable.ID;
            value.ID = valueElement.Key as string;

            //Parse Links

            value.variable = new Lazy<Variable>(() => variable);

            var valueName = valueElement.Value as string;
            value.value = new Lazy<string>(() => valueName);

            return value;
        }
Esempio n. 2
0
        public static Variable Parse(SpeedrunComClient client, dynamic variableElement)
        {
            var variable = new Variable();

            var properties = variableElement.Properties 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.Properties as IDictionary<string, dynamic>;
                variable.Choices = choiceElements.Select(x => VariableValue.ParseIDPair(client, variable, x) as VariableValue).ToList().AsReadOnly();
            }
            else
            {
                variable.Choices = new ReadOnlyCollection<VariableValue>(new VariableValue[0]);
            }

            var valuesProperties = variableElement.values.Properties as IDictionary<string, dynamic>;
            var defaultChoice = valuesProperties["default"] as string;
            if (!string.IsNullOrEmpty(defaultChoice))
                variable.DefaultChoice = variable.Choices.First(x => x.ID == defaultChoice);

            //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;
        }