public void AddSettingOutsideLimits(XElement element, SettingLimits settingLimits) { string errorString = String.Format("The values generated by '{0}' must be within the range(s): {1}.", element.Name.LocalName, settingLimits.ToString()); Add(errorString, element); }
public override bool IsValueRangeWithinLimits(SettingLimits settingLimits) { foreach (WeightedSelection selection in _selections) { if (selection.ValueGenerator != null && !selection.ValueGenerator.IsValueRangeWithinLimits(settingLimits)) { return(false); } } return(true); }
public override bool IsValueRangeWithinLimits(SettingLimits settingLimits) { return(settingLimits.IsInRange(_min, _max)); }
void ParseMaximumWeaponCountElement(XElement maximumWeaponCountElement, WeaponSettingGuarantee weaponSettingGuarantee, SettingLimits weaponCountSettingLimits) { weaponSettingGuarantee.MaximumWeaponCount = CreateValueGenerator(maximumWeaponCountElement, weaponCountSettingLimits); //Parse optional attributes. IEnumerable <XAttribute> attributes = maximumWeaponCountElement.Attributes(); foreach (XAttribute attribute in attributes) { if (attribute.Name == "forceExcessWeaponsToValue") { int intValue; if (TryParseIntegerAttribute(maximumWeaponCountElement, attribute, out intValue)) { weaponSettingGuarantee.MaximumWeaponCountForcedValue = intValue; } } else { _errorCollection.AddInvalidAttribute(maximumWeaponCountElement, attribute); } } }
WeaponSettingGuarantee CreateWeaponSettingGuarantee(XElement weaponSettingGuaranteeElement) { //Get setting XAttribute settingAttribute = weaponSettingGuaranteeElement.Attribute("setting"); if (settingAttribute == null) { _errorCollection.AddAttributeNotFound("setting", weaponSettingGuaranteeElement); return(null); } WeaponSettings weaponSetting = WeaponSettings.Count; Enum.TryParse <WeaponSettings>(settingAttribute.Value, out weaponSetting); if (weaponSetting == WeaponSettings.Count) { _errorCollection.AddAttributeValueInvalidValue(weaponSettingGuaranteeElement, settingAttribute); return(null); } WeaponSettingGuarantee weaponSettingGuarantee = new WeaponSettingGuarantee(weaponSetting); SettingLimits weaponCountSettingLimits = new SettingLimits(0, 64); SettingLimits settingValueSettingLimits = new SettingLimits(0, 255); //Iterate through all elements. FoundElements foundElements = new FoundElements(); IEnumerable <XElement> elements = weaponSettingGuaranteeElement.Elements(); foreach (XElement element in elements) { //Handle minimum weapons element. if (element.Name.LocalName == ElementTypes.MinimumWeaponCount.ToString()) { if (!foundElements.Contains(ElementTypes.MinimumWeaponCount)) { foundElements.Add(ElementTypes.MinimumWeaponCount, element); weaponSettingGuarantee.MinimumWeaponCount = CreateValueGenerator(element, weaponCountSettingLimits); } else { _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MinimumWeaponCount)); } } //Handle maximum weapons element. else if (element.Name.LocalName == ElementTypes.MaximumWeaponCount.ToString()) { if (!foundElements.Contains(ElementTypes.MaximumWeaponCount)) { foundElements.Add(ElementTypes.MaximumWeaponCount, element); ParseMaximumWeaponCountElement(element, weaponSettingGuarantee, weaponCountSettingLimits); } else { _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MaximumWeaponCount)); } } //Handle minimum value element. else if (element.Name.LocalName == ElementTypes.MinimumSettingValue.ToString()) { if (!foundElements.Contains(ElementTypes.MinimumSettingValue)) { foundElements.Add(ElementTypes.MinimumSettingValue, element); weaponSettingGuarantee.MinimumSettingValue = CreateValueGenerator(element, settingValueSettingLimits); } else { _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MinimumSettingValue)); } } //Handle maximum weapons element. else if (element.Name.LocalName == ElementTypes.MaximumSettingValue.ToString()) { if (!foundElements.Contains(ElementTypes.MaximumSettingValue)) { foundElements.Add(ElementTypes.MaximumSettingValue, element); weaponSettingGuarantee.MaximumSettingValue = CreateValueGenerator(element, settingValueSettingLimits); } else { _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MaximumSettingValue)); } } //Handle inclusion filter element. else if (element.Name.LocalName == ElementTypes.InclusionFilter.ToString()) { if (!foundElements.Contains(ElementTypes.InclusionFilter)) { foundElements.Add(ElementTypes.InclusionFilter, element); ParseWeaponFilterElement(element, ref weaponSettingGuarantee.GetCategoryInclusionFilterRef(), ref weaponSettingGuarantee.GetInclusionMatchTypeRef()); } else { _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.InclusionFilter)); } } //Handle inclusion filter element. else if (element.Name.LocalName == ElementTypes.ExclusionFilter.ToString()) { if (!foundElements.Contains(ElementTypes.ExclusionFilter)) { foundElements.Add(ElementTypes.ExclusionFilter, element); ParseWeaponFilterElement(element, ref weaponSettingGuarantee.GetCategoryExclusionFilterRef(), ref weaponSettingGuarantee.GetExclusionMatchTypeRef()); } else { _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.ExclusionFilter)); } } //Invalid element. else { _errorCollection.AddInvalidElement(element); } } return(weaponSettingGuarantee); }
WeightedSelectorValueGenerator CreateWeightedSelectorValueGenerator(XElement weightedSelectorValueElement, SettingLimits settingLimits) { WeightedSelectorValueGenerator weightedSelectorValueGenerator = new WeightedSelectorValueGenerator(); foreach (XElement element in weightedSelectorValueElement.Elements()) { if (element.Name.LocalName == "Selection") { double weight = 1.0; //Optionally get weight XAttribute weightAttribute = element.Attribute("weight"); if (weightAttribute != null) { TryParseDoubleAttribute(element, weightAttribute, out weight); } ValueGenerator valueGenerator = CreateValueGenerator(element, settingLimits); if (valueGenerator != null) { weightedSelectorValueGenerator.AddSelection(valueGenerator, weight); } } //Invalid element. else { _errorCollection.AddInvalidElement(element); } } return(weightedSelectorValueGenerator); }
ValueGenerator CreateValueGenerator(XElement settingElement, SettingLimits settingLimits = null) { ValueGenerator outValueGenerator = null; bool hasCreatedValueGenerator = false; //Iterate through all elements. IEnumerable <XElement> elements = settingElement.Elements(); foreach (XElement element in elements) { //Warn about more than one value element. if (hasCreatedValueGenerator) { _errorCollection.AddInvalidElement(element); continue; } //Handle value element. ValueGeneratorTypes valueGeneratorType; if (Enum.TryParse <ValueGeneratorTypes>(element.Name.LocalName, out valueGeneratorType)) { switch (valueGeneratorType) { case ValueGeneratorTypes.Null: outValueGenerator = null; break; case ValueGeneratorTypes.Constant: outValueGenerator = CreateConstantValueGenerator(element); break; case ValueGeneratorTypes.Range: outValueGenerator = CreateRangeValueGenerator(element); break; case ValueGeneratorTypes.Boolean: outValueGenerator = CreateBooleanValueGenerator(element); break; case ValueGeneratorTypes.CoinFlip: outValueGenerator = CreateCoinFlipValueGenerator(element); break; case ValueGeneratorTypes.WeightedSelector: outValueGenerator = CreateWeightedSelectorValueGenerator(element, settingLimits); break; default: _errorCollection.AddInvalidElement(element); break; } hasCreatedValueGenerator = outValueGenerator != null || valueGeneratorType == ValueGeneratorTypes.Null; if (outValueGenerator != null && settingLimits != null && !outValueGenerator.IsValueRangeWithinLimits(settingLimits)) { _errorCollection.AddSettingOutsideLimits(element, settingLimits); } } //Invalid element. else { _errorCollection.AddInvalidElement(element); } } return(outValueGenerator); }
public override bool IsValueRangeWithinLimits(SettingLimits settingLimits) { return(settingLimits.IsInRange(_headsValue) && settingLimits.IsInRange(_tailsValue)); }
/// <summary> /// When overridden in a deriving type, returns if the value generator's output range is within the given limits. /// </summary> public abstract bool IsValueRangeWithinLimits(SettingLimits settingLimits);