public Account(bool isVerified, bool isOpen, bool isFrozen, decimal initialAmount, string holder, Action onUnFreeze)
        {
            if (isVerified)
            {
                _state |= AccountState.Verfied;
            }
            if (isOpen)
            {
                _state |= AccountState.Open;
            }
            else
            {
                _state |= AccountState.Close;
                _state &= ~AccountState.Open;
            }

            if (isFrozen)
            {
                this.Freezable = new Frozen(onUnFreeze);
            }
            else
            {
                this.Freezable = new UnFrozen();
            }

            _amount         = initialAmount;
            _holder         = holder;
            this.onUnFreeze = onUnFreeze;
        }
Пример #2
0
 private void GoToTempCommands()
 {
     if (command.Length > 3)
     {
         throw new ArgumentException("Wrong command");
     }
     else if ("down" == command[2].ToLower())
     {
         if (deviceList[command[0]] is IFreezable)
         {
             IFreezable device = (IFreezable)deviceList[command[0]];
             device.FreezDown();
         }
         else
         {
             throw new ArgumentException("Wrong device");
         }
     }
     else if ("up" == command[2].ToLower())
     {
         if (deviceList[command[0]] is IFreezable)
         {
             IFreezable device = (IFreezable)deviceList[command[0]];
             device.FreezUp();
         }
         else
         {
             throw new ArgumentException("Wrong device");
         }
     }
     else
     {
         throw new ArgumentException("Wrong command");
     }
 }
Пример #3
0
 public static void ThrowIfFrozen(IFreezable freezable)
 {
     if (freezable.IsFrozen)
     {
         throw new InvalidOperationException("Cannot mutate frozen " + freezable.GetType().Name);
     }
 }
Пример #4
0
 public static void Freeze(object item)
 {
     if (item is IFreezable)
     {
         IFreezable f = item as IFreezable;
         f.Freeze();
     }
 }
Пример #5
0
        // we do not need it anymore, because changing the state become the interface responsibility
        // private Action ManageUnfreezing { get; set; }

        public Account4(Action onUnfreeze)
        {
            // Account class will not be responsible for it any more.
            //this.OnUnfreeze = onUnfreeze;

            // we do not need it anymore, because changing the state become the interface responsibility
            // this.ManageUnfreezing = this.StayUnfrozen;
            this.Freezable = new Active(onUnfreeze);
        }
Пример #6
0
 public void Deposit(decimal amount)
 {
     if (IsClosed)
     {
         return;
     }
     Freezable = Freezable.Deposit();
     Balance  += amount;
 }
Пример #7
0
        public static void Freeze(object item)
        {
            IFreezable f = item as IFreezable;

            if (f != null)
            {
                f.Freeze();
            }
        }
Пример #8
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        IFreezable freezable = collision.gameObject.GetComponent <IFreezable>();

        if (freezable != null)
        {
            Freeze(freezable);
            //Debug.Log("Freezing " + collision.gameObject.name);
        }
    }
        public void Intercept(IInvocation invocation)
        {
            IFreezable obj = (IFreezable)invocation.InvocationTarget;

            if (obj.IsFrozen && invocation.Method.Name.StartsWith("set_", StringComparison.OrdinalIgnoreCase))
            {
                throw new NotSupportedException("Target is frozen");
            }
            invocation.Proceed();
        }
Пример #10
0
 public static bool SetRefProperty <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value)
     where TProperty : class
 {
     if (ReferenceEquals(thisProperty, value))
     {
         return(false);
     }
     that.ThrowIfFrozen();
     thisProperty = value;
     return(true);
 }
Пример #11
0
 public static bool SetValueProperty <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value)
     where TProperty : struct
 {
     if (thisProperty.Equals(value))
     {
         return(false);
     }
     that.ThrowIfFrozen();
     thisProperty = value;
     return(true);
 }
Пример #12
0
 private void Freeze(IFreezable freezable)
 {
     if (freezable != null && !collisions.Contains(freezable))
     {
         if (isActive)
         {
             freezable.Freeze();
         }
         collisions.Add(freezable);
     }
 }
Пример #13
0
        public Exception ValidateValue(object value, string locationName, LocationKind locationKind)
        {
            IFreezable freezable = value as IFreezable;

            if (freezable != null)
            {
                freezable.Freeze();
            }

            return(null);
        }
Пример #14
0
 public void Freeze()
 {
     if (!IsVerified)
     {
         return;
     }
     if (IsClosed)
     {
         return;
     }
     Freezable = Freezable.Freeeze();
 }
Пример #15
0
    protected override void OnCollisionEnter2D(Collision2D collision)
    {
        IFreezable  freezable  = collision.gameObject.GetComponent <IFreezable>();
        IDamageable damageable = collision.gameObject.GetComponent <IDamageable>();

        if (freezable != null)
        {
            damageable.TakeDamage(player.bow.dmg);
            freezable.Freze(duration);
        }
        Destroy(gameObject);
    }
Пример #16
0
    private void OnTriggerExit2D(Collider2D collision)
    {
        IFreezable freezable = collision.gameObject.GetComponent <IFreezable>();

        if (freezable != null)
        {
            if (isActive)
            {
                freezable.UnFreeze();
            }
            collisions.Remove(freezable);
        }
    }
Пример #17
0
    private void OnParticleCollision(GameObject other)
    {
        IFreezable fre = other.GetComponent <IFreezable>();

        rand = Random.Range(1, 100);
        if (fre != null)
        {
            fre.SlownDown(5);
            if (rand == 50)
            {
                fre.Freze(3);
            }
        }
    }
Пример #18
0
 public static bool SetRefPropertyOnce <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value, [CallerMemberName] string callerMemberName = null)
     where TProperty : class
 {
     if (ReferenceEquals(thisProperty, value))
     {
         return(false);
     }
     if ((object)thisProperty != null)
     {
         throw new System.ArgumentException($"{that.GetType().Name}.{callerMemberName} is already set.");
     }
     thisProperty = value;
     return(true);
 }
Пример #19
0
 public void Withdraw(decimal amount)
 {
     if (!IsVerified)
     {
         return;
     }
     if (IsClosed)
     {
         return;
     }
     Freezable = Freezable.Withdraw();
     //Withdraw money
     Balance -= amount;
 }
Пример #20
0
 public static bool SetValueProperty <TProperty>(this IFreezable that, ref Nullable <TProperty> thisProperty, Nullable <TProperty> value)
     where TProperty : struct
 {
     if (thisProperty.HasValue && value.HasValue && thisProperty.Value.Equals(value.Value))
     {
         return(false);
     }
     if (!thisProperty.HasValue && !value.HasValue)
     {
         return(false);
     }
     that.ThrowIfFrozen();
     thisProperty = value;
     return(true);
 }
 //[System.Diagnostics.DebuggerStepThrough]
 //[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
 public static bool SetStringProperty(this IFreezable that, ref string thisProperty, string value, string nameOfProperty = null)
 {
     //if (value == string.Empty) { value = null; }
     if (ReferenceEquals(thisProperty, value))
     {
         return(false);
     }
     if (string.Equals(thisProperty, value, System.StringComparison.Ordinal))
     {
         return(false);
     }
     that.ThrowIfFrozen(nameOfProperty);
     thisProperty = value;
     return(true);
 }
        public static TProperty CreateOrGetCachedObject <TProperty, TArg0>(this IFreezable that, ref TProperty thisProperty, TArg0 arg0, Func <TArg0, TProperty> generator)
            where TProperty : class
        {
            var result = thisProperty;

            if (result is null)
            {
                result = generator(arg0);
                if (@that.IsFrozen())
                {
                    thisProperty = result;
                }
            }
            return(result);
        }
Пример #23
0
        public void Freeze()
        {
            if (this.IsClosed)
            {
                return;
            }
            if (!this.IsVerified)
            {
                return;
            }

            // we do not need it anymore, because changing the state become the interface responsibility
            //this.ManageUnfreezing = this.Unfreeze;

            this.Freezable = this.Freezable.Freeze();
        }
Пример #24
0
        public void Deposit(decimal amount)
        {
            if (this.IsClosed)
            {
                return;
            }

            // we do not need it anymore, because changing the state become the interface responsibility
            //this.ManageUnfreezing();

            // state change is now operated by Freezable.Deposit()
            // it will change the state and return the result for us.
            this.Freezable = this.Freezable.Deposit();

            this.Balance += amount;
        }
Пример #25
0
 public static void ThrowIfNotFrozen(this IFreezable that, string name = null)
 {
     if ((object)that != null)
     {
         if (!(that.IsFrozen()))
         {
             if (name is null)
             {
                 throw new InvalidOperationException($"{that.GetType().FullName} is NOT frozen.");
             }
             else
             {
                 throw new InvalidOperationException($"{name} is NOT frozen.");
             }
         }
     }
 }
Пример #26
0
        public static void TestFreeze(IFreezable sut)
        {
            var properties =
                sut.GetType().GetRuntimeProperties()
                .Where(x =>
                       x.CanWrite &&
                       x.CanRead &&
                       x.Name != "IsFrozen");

            foreach (var property in properties)
            {
                var value =
                    property.PropertyType.IsValueType ?
                    Activator.CreateInstance(property.PropertyType) :
                    null;

                property.SetValue(sut, value);

                var result = property.GetValue(sut);

                Assert.Equal(value, result);
            }

            sut.Freeze();

            foreach (var property in properties)
            {
                var value =
                    property.PropertyType.IsValueType ?
                    Activator.CreateInstance(property.PropertyType) :
                    null;

                Assert.Throws <InvalidOperationException>(() =>
                {
                    try
                    {
                        property.SetValue(sut, value);
                    }
                    catch (Exception ex)
                    {
                        throw ex.InnerException;
                    }
                });
            }
        }
Пример #27
0
 public static void SetOrThrowIfFrozen <T>(this IFreezable that, ref T target, T value, string name = null)
 {
     if ((object)that != null)
     {
         if (that.IsFrozen())
         {
             if (name is null)
             {
                 throw new InvalidOperationException($"{that.GetType().FullName} is frozen.");
             }
             else
             {
                 throw new InvalidOperationException($"{name} is frozen.");
             }
         }
     }
     target = value;
 }
Пример #28
0
    public override void Attack(GameObject source, Vector3 contactPoint, float AttackArg1, float AttackArg2, float AttackArg3, float AttackArg4, Collider collider, GameObject effect, GameObject effect2)
    {
        //float Damage, float speedDecreasePercent, float time

        Collider[] colliders = Physics.OverlapSphere(contactPoint, AttackArg4);

        int hitNum = 0;

        try{
            foreach (Collider hit in colliders)
            {
                IFreezable freezableObject = hit.GetComponent <IFreezable>();

                if (freezableObject != null)
                {
                    freezableObject.TakeIce(AttackArg1, AttackArg2, AttackArg3, contactPoint, effect);
                    hitNum++;
                }
                else if (string.Compare(hit.tag, "Enemy") == 0)
                {
                    IFreezable freezableObject2 = hit.transform.parent.parent.parent.parent.GetComponent <IFreezable>();
                    if (freezableObject2 != null)
                    {
                        freezableObject2.TakeIce(AttackArg1, AttackArg2, AttackArg3, contactPoint, effect);
                    }
                    hitNum++;
                }
            }
        }catch (NullReferenceException e) {}


        Destroy(Instantiate(effect.gameObject, source.transform.position + 0.2f * Vector3.up, Quaternion.identity) as GameObject, 3f);
        if (hitNum > 0)
        {
            if (effect2 != null)
            {
                Destroy(Instantiate(effect2.gameObject, source.transform.position + 0.2f * Vector3.up, Quaternion.identity) as GameObject, 3f);
            }
        }
        //Destroy(source);
        source.SetActive(false);
    }
Пример #29
0
    protected override void OnCollisionEnter2D(Collision2D collision)
    {
        IFreezable  freezable  = collision.gameObject.GetComponent <IFreezable>();
        IDamageable damageable = collision.gameObject.GetComponent <IDamageable>();

        if (freezable != null)
        {
            damageable.TakeDamage(player.bow.dmg);
            int rand = Random.Range(1, 100);
            if (rand < slowChance)
            {
                freezable.SlownDown(slowDuration);
            }
            if (rand < frezeChance)
            {
                freezable.Freze(frezeDuration);
            }
        }
        Destroy(gameObject);
    }
        public void Deposit(decimal amount)
        {
            if (_state.HasFlag(AccountState.Close))
            {
                Console.WriteLine("La cuenta se encuentra cerrada");
                throw new AccountClosedException();
            }

            if (_state.HasFlag(AccountState.Open) && !_state.HasFlag(AccountState.Verfied))
            {
                Console.WriteLine("La cuenta no se encuentra verificada");
                throw new AccountNotVerifiedException();
            }

            if (_state.HasFlag(AccountState.Open) && _state.HasFlag(AccountState.Verfied))
            {
                deposit(amount);
            }

            this.Freezable = this.Freezable.Deposit();
        }