void IAnimalEventHandler.Apply(AnimalEvent e, IAnimalEventContext context)
 {
     if (e is TEvent)
     {
         Apply(e as TEvent, context);
     }
 }
        bool IAnimalEventHandler.Validate(AnimalEvent e, IAnimalEventContext context)
        {
            if (e is TEvent)
            {
                return(Validate(e as TEvent, context));
            }

            return(true);
        }
        public override void Apply(AnimalRulesetChangeEvent e, IAnimalEventContext context)
        {
            Animal animal = context.Animal;

            context.ActiveRuleset = context.GetRulesetAsync(e.NewVersionId).GetAwaiter().GetResult();

            AnimalType animalType    = context.ActiveRuleset.AnimalTypes[animal.TypeId];
            var        newAttributes = animalType.Attributes.Keys.Except(animal.Attributes.Keys);

            foreach (string newAttributeId in newAttributes)
            {
                animal.Attributes.Add(newAttributeId, animalType.Attributes[newAttributeId].InitialValue);
            }
        }
Example #4
0
        public override void Apply(AnimalActionEvent e, IAnimalEventContext context)
        {
            Animal       animal     = context.Animal;
            AnimalAction action     = context.ActiveRuleset.AnimalActions[e.AnimalActionId];
            AnimalType   animalType = context.ActiveRuleset.AnimalTypes[animal.TypeId];

            foreach (string attributeId in action.AttributeEffects.Keys)
            {
                AnimalTypeAttribute animalTypeAttribute = animalType.Attributes[attributeId];
                decimal             minValue            = animalTypeAttribute.MinValue;
                decimal             maxValue            = animalTypeAttribute.MaxValue;
                decimal             newValue            = animal.Attributes[attributeId] + action.AttributeEffects[attributeId];
                newValue = Math.Max(minValue, Math.Min(maxValue, newValue));
                animal.Attributes[attributeId] = newValue;
            }
        }
        public override void Apply(CreateAnimalEvent e, IAnimalEventContext context)
        {
            AnimalType animalType = context.ActiveRuleset.AnimalTypes[e.AnimalTypeId];

            var animal = new Animal
            {
                Id             = e.AnimalId,
                Name           = e.Name,
                UserId         = e.OwnerUserId,
                TypeId         = e.AnimalTypeId,
                Attributes     = animalType.Attributes.ToDictionary(a => a.Key, a => a.Value.InitialValue),
                LastCalculated = e.Time,
                Created        = e.Time
            };

            context.Animal = animal;
        }
 public abstract void Apply(TEvent e, IAnimalEventContext context);
 public abstract bool Validate(TEvent e, IAnimalEventContext context);
 public override bool Validate(CreateAnimalEvent e, IAnimalEventContext context)
 {
     return(true);
 }
Example #9
0
 public override bool Validate(AnimalActionEvent e, IAnimalEventContext context)
 {
     return(true);
 }
 public override bool Validate(AnimalRulesetChangeEvent e, IAnimalEventContext context)
 {
     return(true);
 }