Esempio n. 1
0
    void GenerateRenewable(RenewableTypes newRenewableType)
    {
        if (newRenewableType == RenewableTypes.None)
        {
            return;
        }
        Type newRenewable = GetRenewableType(newRenewableType);

        equippedRenewable = gameObject.AddComponent(newRenewable) as IRenewable;
    }
Esempio n. 2
0
        public void TestBasicAcquireReleaseAndRenew()
        {
            var mockBuildStrategy = new Mock <IBuildStrategy <IRenewable> >();
            var renewStrategyMock = new Mock <IRenewStrategy>();
            var renewableMock     = new Mock <IRenewable>();

            bool shouldRenew = false;

            mockBuildStrategy.Setup(x => x.Build()).Returns(() => renewableMock.Object);
            renewStrategyMock
            .Setup(x => x.ShallRenew(It.IsAny <IRenewable>()))
            .Returns((IRenewable x) => shouldRenew);

            var        leasable = new AutoRenewLeasable <IRenewable>(mockBuildStrategy.Object, renewStrategyMock.Object);
            IRenewable ret      = null;

            // acquire and release, this should trigger build
            ret = leasable.Acquire();
            Assert.AreSame(renewableMock.Object, ret);
            mockBuildStrategy.Verify(x => x.Build(), Times.Once);
            Assert.IsTrue(leasable.LeaseCount == 1);
            leasable.Release();
            renewStrategyMock.Verify(x => x.ShallRenew(It.Is((IRenewable y) => y == renewableMock.Object)), Times.Once);

            // mark object as should renew, and acquire/release again. Last release should destroy the object.
            shouldRenew = true;
            ret         = leasable.Acquire();
            Assert.AreSame(renewableMock.Object, ret);
            mockBuildStrategy.Verify(x => x.Build(), Times.Once);
            Assert.IsTrue(leasable.LeaseCount == 1);
            leasable.Release();
            renewStrategyMock.Verify(x => x.ShallRenew(It.Is((IRenewable y) => y == renewableMock.Object)), Times.Exactly(2));

            // Now renew should happen and Build is called again
            ret = leasable.Acquire();
            mockBuildStrategy.Verify(x => x.Build(), Times.Exactly(2));
            Assert.IsTrue(leasable.LeaseCount == 1);

            // An embeded acquire/release doesn't trigger renew since there is active lease
            var ret2 = leasable.Acquire();

            mockBuildStrategy.Verify(x => x.Build(), Times.Exactly(2));
            Assert.IsTrue(leasable.LeaseCount == 2);
            leasable.Release();
            Assert.IsTrue(leasable.LeaseCount == 1);
            renewStrategyMock.Verify(x => x.ShallRenew(It.Is((IRenewable y) => y == renewableMock.Object)), Times.Exactly(2));

            // outer release triggers check
            leasable.Release();
            renewStrategyMock.Verify(x => x.ShallRenew(It.Is((IRenewable y) => y == renewableMock.Object)), Times.Exactly(3));
        }
Esempio n. 3
0
 public bool RenewBook(int bookCatalogNum)
 {
     for (int i = 0; i < bookArray.Length; i++)
     {
         if (bookArray[i] != null && bookArray[i].CatalogNumber == bookCatalogNum)
         {
             IRenewable r = bookArray[i] as IRenewable;
             if (r != null)
             {
                 return(r.Renew());
             }
         }
     }
     return(false);
 }
    void UpdateRenewable(ref IRenewable activeRenewable)
    {
        RenewableTypes activeRenewableType     = activeRenewable.Type;
        Sprite         activeHardwareBubSprite = DataAssociations.GetRenewableTypeBubImage(activeRenewableType);

        renewableBubImage.sprite = activeHardwareBubSprite;

        activeRenewable.DurationUpdater  = null;
        activeRenewable.DurationUpdater += GeneratePercentDurationUpdater();

        activeRenewable.CooldownPercentUpdater  = null;
        activeRenewable.CooldownPercentUpdater += GenerateCooldownPercentUpdater();

        activeRenewable.CooldownDurationUpdater  = null;
        activeRenewable.CooldownDurationUpdater += GenerateCooldownDurationUpdater();
    }
Esempio n. 5
0
        public bool RenewBook(int bookCatalogNum)
        {
            bool renew = false;

            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] != null && bookArray[i] is IRenewable)
                {
                    if (bookArray[i].CatalogNumber == bookCatalogNum)
                    {
                        IRenewable text = (IRenewable)bookArray[i];
                        renew = text.renew();
                        break;
                    }
                }
            }

            return(renew);
        }
        /// <summary>
        /// Should the renewable be renewed
        /// </summary>
        /// <returns>true if yes</returns>
        public virtual bool ShallRenew(IRenewable renewable)
        {
            if (this.predicateChain.Count == 0)
            {
                // Default to eager renew - whenever asked
                return(true);
            }

            foreach (var predicate in this.predicateChain)
            {
                // If any predicate says yes, we need to renew
                if (predicate(renewable))
                {
                    return(true);
                }
            }

            return(false);
        }