Example #1
0
        /// <summary>
        /// Starts the time period, ticking the time of day clock.
        /// </summary>
        /// <param name="startTime">The time of day this period begins at.</param>
        /// <param name="worldTimeFactor">The world time factor. This value can be used to adjust the interval between time of day updates.</param>
        /// <exception cref="System.ArgumentNullException">startTime can not be null.</exception>
        /// <exception cref="MudDesigner.MudEngine.Environment.InvalidTimeOfDayException">HoursPerDay can not be zero.</exception>
        public void Start(ITimeOfDay startTime, double worldTimeFactor)
        {
            if (startTime == null)
            {
                throw new ArgumentNullException(nameof(startTime), "startTime can not be null.");
            }

            if (startTime.HoursPerDay == 0)
            {
                throw new InvalidTimeOfDayException("HoursPerDay can not be zero.", startTime);
            }

            // Calculate how many seconds in real-world it takes to pass 1 minute in-game.
            double minuteInterval = 60 * worldTimeFactor;

            this.StateStartTime = startTime.Clone();
            this.Reset();

            // Update the state every in-game hour or minute based on the ratio we have
            if (minuteInterval < 0.4)
            {
                this.StartStateClock(TimeSpan.FromSeconds(minuteInterval).TotalMilliseconds, (timeOfDay) => timeOfDay.IncrementByHour(1));
            }
            else
            {
                this.StartStateClock(TimeSpan.FromSeconds(minuteInterval).TotalMilliseconds, (timeOfDay) => timeOfDay.IncrementByMinute(1));
            }

            this.Enable();
        }
Example #2
0
        /// <summary>
        /// Starts the time period, ticking the time of day clock.
        /// </summary>
        /// <param name="startTime">The time of day this period begins at.</param>
        /// <param name="worldTimeFactor">The world time factor. This value can be used to adjust the interval between time of day updates.</param>
        /// <exception cref="System.ArgumentNullException">startTime can not be null.</exception>
        /// <exception cref="MudDesigner.MudEngine.Environment.InvalidTimeOfDayException">HoursPerDay can not be zero.</exception>
        public void Start(ITimeOfDay startTime, double worldTimeFactor)
        {
            if (startTime == null)
            {
                throw new ArgumentNullException(nameof(startTime), "startTime can not be null.");
            }

            if (startTime.HoursPerDay == 0)
            {
                throw new InvalidTimeOfDayException("HoursPerDay can not be zero.", startTime);
            }

            // Calculate how many seconds in real-world it takes to pass 1 minute in-game.
            double minuteInterval = 60 * worldTimeFactor;

            this.StateStartTime = startTime.Clone();
            this.Reset();

            // Update the state every in-game hour or minute based on the ratio we have
            if (minuteInterval < 0.4)
            {
                this.StartStateClock(TimeSpan.FromSeconds(minuteInterval).TotalMilliseconds, (timeOfDay) => timeOfDay.IncrementByHour(1));
            }
            else
            {
                this.StartStateClock(TimeSpan.FromSeconds(minuteInterval).TotalMilliseconds, (timeOfDay) => timeOfDay.IncrementByMinute(1));
            }

            this.Enable();
        }
        /// <summary>
        /// Gets a state if there is one already in progress.
        /// </summary>
        /// <param name="currentTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the current time of day if an instance with a StartTime
        /// before the current world-time can be found
        /// </returns>
        ITimePeriod GetInProgressState(ITimeOfDay currentTime)
        {
            ITimePeriod inProgressState = null;

            foreach (ITimePeriod state in this.timeOfDayStates)
            {
                // If the state is already in progress, w
                if (state.StateStartTime.Hour <= currentTime.Hour ||
                    (state.StateStartTime.Hour <= currentTime.Hour &&
                     state.StateStartTime.Minute <= currentTime.Minute))
                {
                    if (inProgressState == null)
                    {
                        inProgressState = state;
                        continue;
                    }
                    else
                    {
                        if ((inProgressState.StateStartTime.Hour <= currentTime.Hour) ||
                            (inProgressState.StateStartTime.Hour == currentTime.Hour &&
                             inProgressState.StateStartTime.Minute <= currentTime.Minute))
                        {
                            inProgressState = state;
                        }
                    }
                }
            }

            return(inProgressState);
        }
        /// <summary>
        /// Gets the state that is up next.
        /// </summary>
        /// <param name="currentTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the up coming time of day if an instance with a StartTime
        /// after the current world-time can be found
        /// </returns>
        ITimePeriod GetNextState(ITimeOfDay currentTime)
        {
            ITimePeriod nextState = null;

            foreach (ITimePeriod state in this.timeOfDayStates)
            {
                // If this state is a future state, then preserve it as a possible next state.
                if (state.StateStartTime.Hour > currentTime.Hour ||
                    (state.StateStartTime.Hour >= currentTime.Hour &&
                     state.StateStartTime.Minute > currentTime.Minute))
                {
                    // If we do not have a next state, set it.
                    if (nextState == null)
                    {
                        nextState = state;
                        continue;
                    }
                    else
                    {
                        // We have a next state, so we must check which is sooner.
                        if (nextState.StateStartTime.Hour > state.StateStartTime.Hour &&
                            nextState.StateStartTime.Minute >= state.StateStartTime.Minute)
                        {
                            nextState = state;
                        }
                    }
                }
            }

            return(nextState);
        }
        /// <summary>
        /// Applies a time zone offset to the Realm.
        /// </summary>
        /// <param name="hour">The number of hours to offset by.</param>
        /// <param name="minute">The number of minutes to offset by.</param>
        /// <exception cref="System.InvalidTimeZoneException">The time zone offset can not be applied due to the realm not being owned by a world. Worlds manage the in-game time and time periods. A world must be assigned as owner on the realm in order for any time zone interactions to take place.</exception>
        /// <para>
        /// The Hour and Minute provided will cause the Realm's timezone to be offset from
        /// the Worlds standard time-zone.
        /// </para>
        public void ApplyTimeZoneOffset(int hour, int minute)
        {
            if (this.Owner == null)
            {
                throw new InvalidTimeZoneException("The time zone offset can not be applied due to the realm not being owned by a world. Worlds manage the in-game time and time periods. A world must be assigned as owner on the realm in order for any time zone interactions to take place.");
            }

            TimePeriodManager timeManager = this.Owner.TimePeriodManager;

            this.TimeZoneOffset = timeManager.CreateTimeOfDay(hour, minute, this.Owner.HoursPerDay);
        }
Example #6
0
        /// <summary>
        /// Creates an unintialized instance of a realm.
        /// All of the children zones will be initialized prior to being added to the realm.
        /// </summary>
        /// <param name="name">The name of the realm.</param>
        /// <param name="owner">The world that owns this realm.</param>
        /// <param name="timeZoneOffset">The time zone offset to apply to the realm.</param>
        /// <param name="zones">A collection of zones that will be initialized and added to the realm.</param>
        /// <returns>Returns an unintialized instance of IRealm</returns>
        public async Task<IRealm> CreateRealm(string name, IWorld owner, ITimeOfDay timeZoneOffset, IEnumerable<IZone> zones)
        {
            var realm = new MudRealm(this.zoneFactory, owner);

            if (timeZoneOffset != null)
            {
                realm.ApplyTimeZoneOffset(timeZoneOffset.Hour, timeZoneOffset.Minute);
            }

            realm.SetName(name);
            if (zones.Count() > 0)
            {
                await realm.AddZonesToRealm(zones);
            }

            return realm;
        }
        /// <summary>
        /// Creates an unintialized instance of a realm.
        /// All of the children zones will be initialized prior to being added to the realm.
        /// </summary>
        /// <param name="name">The name of the realm.</param>
        /// <param name="owner">The world that owns this realm.</param>
        /// <param name="timeZoneOffset">The time zone offset to apply to the realm.</param>
        /// <param name="zones">A collection of zones that will be initialized and added to the realm.</param>
        /// <returns>Returns an unintialized instance of IRealm</returns>
        public async Task <IRealm> CreateRealm(string name, IWorld owner, ITimeOfDay timeZoneOffset, IEnumerable <IZone> zones)
        {
            var realm = new MudRealm(this.zoneFactory, owner);

            if (timeZoneOffset != null)
            {
                realm.ApplyTimeZoneOffset(timeZoneOffset.Hour, timeZoneOffset.Minute);
            }

            realm.SetName(name);
            if (zones.Count() > 0)
            {
                await realm.AddZonesToRealm(zones);
            }

            return(realm);
        }
        /// <summary>
        /// Looks at a supplied time of day and figures out what TimeOfDayState needs to be returned that matches the time of day.
        /// </summary>
        /// <param name="currentGameTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the current time of day in the game.
        /// </returns>
        public ITimePeriod GetTimePeriodForDay(ITimeOfDay currentGameTime)
        {
            ITimePeriod inProgressState = null;
            ITimePeriod nextState       = null;

            inProgressState = this.GetInProgressState(currentGameTime);
            nextState       = this.GetNextState(currentGameTime);

            if (inProgressState != null)
            {
                return(inProgressState);
            }
            else if (nextState != null && nextState.StateStartTime.Hour <= currentGameTime.Hour && nextState.StateStartTime.Minute <= currentGameTime.Minute)
            {
                return(nextState);
            }

            return(null);
        }
Example #9
0
        /// <summary>
        /// Loads the component and any resources or dependencies it might have.
        /// Called during initialization of the component
        /// </summary>
        /// <returns></returns>
        protected override Task Load()
        {
            // Find a TimePeriod that matches to the current real-world time.
            // If none is found, we grab the first time period in the collection.
            ITimePeriod realWorldTimePeriod = this.timePeriods.FirstOrDefault(
                period => period.CurrentTime.Hour == DateTime.Now.Hour);

            if (realWorldTimePeriod == null)
            {
                realWorldTimePeriod = this.timePeriods.FirstOrDefault();
                if (realWorldTimePeriod == null)
                {
                    throw new InvalidOperationException("Unable to load a world without at least one time period.");
                }
            }

            ITimeOfDay currentTimeOfDay = realWorldTimePeriod.CurrentTime;

            this.CurrentTimeOfDay = this.TimePeriodManager.GetTimePeriodForDay(currentTimeOfDay);

            return(Task.FromResult(0));
        }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidTimeOfDayException"/> class.
 /// </summary>
 /// <param name="message">The message to apply to the excention.</param>
 /// <param name="timeOfDay">The time of day instance that was invalid, causing the exception.</param>
 public InvalidTimeOfDayException(string message, ITimeOfDay timeOfDay) : base(message)
 {
     this.Data.Add(timeOfDay.GetType(), timeOfDay);
 }
Example #11
0
 /// <summary>
 /// Creates an unintialized instance of a realm.
 /// </summary>
 /// <param name="name">The name of the realm.</param>
 /// <param name="owner">The world that owns this realm.</param>
 /// <param name="timeZoneOffset">The time zone offset to apply to the realm.</param>
 /// <returns>Returns an unintialized instance of IRealm</returns>
 public Task<IRealm> CreateRealm(string name, IWorld owner, ITimeOfDay timeZoneOffset)
             => this.CreateRealm(name, owner, timeZoneOffset, Enumerable.Empty<IZone>());
        /// <summary>
        /// Looks at a supplied time of day and figures out what TimeOfDayState needs to be returned that matches the time of day.
        /// </summary>
        /// <param name="currentTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the current time of day in the game.
        /// </returns>
        public ITimePeriod GetTimeOfDayState(DateTime?currentTime = null)
        {
            ITimeOfDay time = TimePeriodManager._factory(currentTime.Value.Hour, currentTime.Value.Minute, _hoursPerDay);

            return(this.GetTimePeriodForDay(time));
        }
Example #13
0
        /// <summary>
        /// Gets the state that is up next.
        /// </summary>
        /// <param name="currentTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the up coming time of day if an instance with a StartTime 
        /// after the current world-time can be found 
        /// </returns>
        ITimePeriod GetNextState(ITimeOfDay currentTime)
        {
            ITimePeriod nextState = null;
            foreach (ITimePeriod state in this.timeOfDayStates)
            {
                // If this state is a future state, then preserve it as a possible next state.
                if (state.StateStartTime.Hour > currentTime.Hour ||
                    (state.StateStartTime.Hour >= currentTime.Hour &&
                    state.StateStartTime.Minute > currentTime.Minute))
                {
                    // If we do not have a next state, set it.
                    if (nextState == null)
                    {
                        nextState = state;
                        continue;
                    }
                    else
                    {
                        // We have a next state, so we must check which is sooner.
                        if (nextState.StateStartTime.Hour > state.StateStartTime.Hour &&
                            nextState.StateStartTime.Minute >= state.StateStartTime.Minute)
                        {
                            nextState = state;
                        }
                    }
                }
            }

            return nextState;
        }
Example #14
0
        /// <summary>
        /// Gets a state if there is one already in progress.
        /// </summary>
        /// <param name="currentTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the current time of day if an instance with a StartTime 
        /// before the current world-time can be found 
        /// </returns>
        ITimePeriod GetInProgressState(ITimeOfDay currentTime)
        {
            ITimePeriod inProgressState = null;
            foreach (ITimePeriod state in this.timeOfDayStates)
            {
                // If the state is already in progress, w
                if (state.StateStartTime.Hour <= currentTime.Hour ||
                    (state.StateStartTime.Hour <= currentTime.Hour &&
                    state.StateStartTime.Minute <= currentTime.Minute))
                {
                    if (inProgressState == null)
                    {
                        inProgressState = state;
                        continue;
                    }
                    else
                    {
                        if ((inProgressState.StateStartTime.Hour <= currentTime.Hour) ||
                            (inProgressState.StateStartTime.Hour == currentTime.Hour &&
                            inProgressState.StateStartTime.Minute <= currentTime.Minute))
                        {
                            inProgressState = state;
                        }
                    }
                }
            }

            return inProgressState;
        }
 /// <summary>
 /// Creates an unintialized instance of a realm.
 /// </summary>
 /// <param name="name">The name of the realm.</param>
 /// <param name="owner">The world that owns this realm.</param>
 /// <param name="timeZoneOffset">The time zone offset to apply to the realm.</param>
 /// <returns>Returns an unintialized instance of IRealm</returns>
 public Task <IRealm> CreateRealm(string name, IWorld owner, ITimeOfDay timeZoneOffset)
 => this.CreateRealm(name, owner, timeZoneOffset, Enumerable.Empty <IZone>());
Example #16
0
 public Shop(ITimeOfDay timeOfDay)
 {
     _timeOfDay = timeOfDay;
     FillProducts();
 }
Example #17
0
        /// <summary>
        /// Looks at a supplied time of day and figures out what TimeOfDayState needs to be returned that matches the time of day.
        /// </summary>
        /// <param name="currentGameTime">The current time.</param>
        /// <returns>
        /// Returns an instance of ITimePeriod that represents the current time of day in the game.
        /// </returns>
        public ITimePeriod GetTimePeriodForDay(ITimeOfDay currentGameTime)
        {
            ITimePeriod inProgressState = null;
            ITimePeriod nextState = null;

            inProgressState = this.GetInProgressState(currentGameTime);
            nextState = this.GetNextState(currentGameTime);

            if (inProgressState != null)
            {
                return inProgressState;
            }
            else if (nextState != null && nextState.StateStartTime.Hour <= currentGameTime.Hour && nextState.StateStartTime.Minute <= currentGameTime.Minute)
            {
                return nextState;
            }

            return null;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidTimeOfDayException"/> class.
 /// </summary>
 /// <param name="message">The message to apply to the excention.</param>
 /// <param name="timeOfDay">The time of day instance that was invalid, causing the exception.</param>
 public InvalidTimeOfDayException(string message, ITimeOfDay timeOfDay) : base(message)
 {
     this.Data.Add(timeOfDay.GetType(), timeOfDay);
 }