Exemplo n.º 1
0
        /// <summary>
        /// Starts a state, and returns a token that can be used to stop it again.
        /// </summary>
        /// <param name="stateData">Custom state data. This is useful in cases
        /// where all the active states needs to be examined. For example, this data
        /// can be used to identify states externally.</param>
        /// <param name="maxTime">The maximum amount of time this state should survive past the current time.</param>
        /// <param name="onTimeOut">The action to perform when timing out.</param>
        /// <returns>A token that wraps the custom state data and can be used to stop
        /// the state started with this method.</returns>
        /// <remarks>For a state to time out, it is necessary for the Update method to
        /// be called regularly (for example, in a MonoBehaviours Update method).</remarks>
        public IStateToken <TStateData> StartState(
            TStateData stateData,
            float maxTime,
            Action onTimeOut)
        {
            var token = tracker.StartState(stateData);
            var clock = new Clock();

            clock.OnClockExpired += () =>
            {
                StopState(token);

                if (onTimeOut != null)
                {
                    onTimeOut();
                }
            };

            clock.Reset(maxTime);

            if (!IsPaused)
            {
                clock.Unpause();
            }

            clocks[token] = clock;
            tokensCopy    = ActiveTokens.ToList();
            return(token);
        }
Exemplo n.º 2
0
        public static void Clean()
        {
            var colorBack = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("Starting Clean!");
            Console.ForegroundColor = colorBack;
            try
            {
                if (ActiveTokens.Count != 0)
                {
                    foreach (var item in ActiveTokens)
                    {
                        var now     = DateTime.Now;
                        var TimeNow = new TimeSpan(now.Hour, now.Minute, now.Second);
                        Console.WriteLine(item.ToString());
                        if (TimeNow.Subtract(item.TokenGeneratedDate) >= TokenInterval)
                        {
                            ActiveTokens.RemoveAll(Token => Token.TokenGuid == item.TokenGuid);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 3
0
 public static void Add(TokenSys token)
 {
     try
     {
         ActiveTokens.RemoveAll(item => item.Email == token.Email || token.TokenGuid == item.TokenGuid);
     }
     catch (NullReferenceException)
     {
         ActiveTokens = new List <TokenSys>();
     }
     ActiveTokens.Add(token);
 }
Exemplo n.º 4
0
        public static bool ThisTokenIsValid(Guid token)
        {
            var tested = ActiveTokens.FirstOrDefault(test => test.TokenGuid == token);

            if (tested == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Stops the state associated with the token. The token must be one that was
        /// returned when the state was started.
        /// </summary>
        /// <param name="token">The token of the state to stop.</param>
        /// <exception cref="ArgumentNullException">token</exception>
        /// <exception cref="InvalidOperationException">
        /// The given token is not from this state tracker
        /// or
        /// the given token is not active
        /// </exception>
        public void StopState(IStateToken <TStateData> token)
        {
            if (token == null)
            {
                throw new ArgumentNullException("token");
            }

            if (!clocks.ContainsKey(token))
            {
                throw new InvalidOperationException("The token was not obtain by this tracker or already has been stopped.");
            }

            clocks.Remove(token);
            tracker.StopState(token);
            tokensCopy = ActiveTokens.ToList();
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimedStateTracker{TStateData}"/> class.
 /// </summary>
 public TimedStateTracker()
 {
     tracker    = new StateTracker <TStateData>();
     clocks     = new Dictionary <IStateToken <TStateData>, Clock>();
     tokensCopy = ActiveTokens.ToList();
 }
Exemplo n.º 7
0
 public static TokenSys GetToken(Guid token)
 {
     return(ActiveTokens.FirstOrDefault(test => test.TokenGuid == token));
 }