1. Hi,

    I would like to have your opinion on the algorithm I use to detect the time it has in the real day.

    I would have liked to know what you thought about the performance and if it could be improved?

    Code:
            private void OnTick()
            {
                if (!_raidShedules) return;
                if (IsTimeForRaid() && !_timeForRaid)
                {
                    BroadcastMessageHandler("starthour");
                    _timeForRaid = true;
                }
                else if (!IsTimeForRaid() && _timeForRaid)
                {
                    BroadcastMessageHandler("endhour");
                    _timeForRaid = false;
                }
            }        private bool IsTimeForRaid()
            {
                var time = DateTime.Now.TimeOfDay;
                var dayOfWeek = DateTime.Now.DayOfWeek;
                var tmpStart = "00:00:00";
                var tmpEnd = "24:00:00";
                switch (dayOfWeek)
                {
                    case DayOfWeek.Monday:
                        tmpStart = _configData.Shedules.Monday.Start;
                        tmpEnd = _configData.Shedules.Monday.End;
                        break;
                    case DayOfWeek.Tuesday:
                        tmpStart = _configData.Shedules.Tuesday.Start;
                        tmpEnd = _configData.Shedules.Tuesday.End;
                        break;
                    case DayOfWeek.Wednesday:
                        tmpStart = _configData.Shedules.Wednesday.Start;
                        tmpEnd = _configData.Shedules.Wednesday.End;
                        break;
                    case DayOfWeek.Thursday:
                        tmpStart = _configData.Shedules.Thursday.Start;
                        tmpEnd = _configData.Shedules.Thursday.End;
                        break;
                    case DayOfWeek.Friday:
                        tmpStart = _configData.Shedules.Friday.Start;
                        tmpEnd = _configData.Shedules.Friday.End;
                        break;
                    case DayOfWeek.Saturday:
                        tmpStart = _configData.Shedules.Saturday.Start;
                        tmpEnd = _configData.Shedules.Saturday.End;
                        break;
                    case DayOfWeek.Sunday:
                        tmpStart = _configData.Shedules.Sunday.Start;
                        tmpEnd = _configData.Shedules.Sunday.End;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
                var start = ToTimeSpan(tmpStart);
                var end = ToTimeSpan(tmpEnd);
                return start < end ? time < start || time >= end : time < start && time >= end;
            }
    
    Thanks to those who will have an opinion on the question.
     
  2. Maybe try something like this note that you need to implement something that reschedules the tasks at the start of each week depending on when your server restarts. This is probably not the best way it just shows that you don't need to check every tick.

    Code:
    void OnServerInitialized()
            {
                ScheduleRaidTimes();
            }        private void ScheduleRaidTimes()
            {
                //Initialize _timeForRaid
                switch (DateTime.Now.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Monday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Monday.End.Ticks;
                        break;
                    case DayOfWeek.Tuesday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Tuesday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Tuesday.End.Ticks;
                        break;
                    case DayOfWeek.Wednesday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Wednesday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Wednesday.End.Ticks;
                        break;
                    case DayOfWeek.Thursday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Thursday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Thursday.End.Ticks;
                        break;
                    case DayOfWeek.Friday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Friday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Friday.End.Ticks;
                        break;
                    case DayOfWeek.Saturday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Saturday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Saturday.End.Ticks;
                        break;
                    case DayOfWeek.Sunday:
                        _timeForRaid = DateTime.Now.Ticks > _configData.Shedules.Sunday.Start.Ticks && DateTime.Now.Ticks < _configData.Shedules.Sunday.End.Ticks;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
               
                ScheduleAction(_configData.Shedules.Monday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Monday.End, () => { _timeForRaid = false; });            ScheduleAction(_configData.Shedules.Tuesday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Tuesday.End, () => { _timeForRaid = false; });            ScheduleAction(_configData.Shedules.Wednesday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Wednesday.End, () => { _timeForRaid = false; });            ScheduleAction(_configData.Shedules.Thursday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Thursday.End, () => { _timeForRaid = false; });            ScheduleAction(_configData.Shedules.Friday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Friday.End, () => { _timeForRaid = false; });            ScheduleAction(_configData.Shedules.Saturday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Saturday.End, () => { _timeForRaid = false; });            ScheduleAction(_configData.Shedules.Sunday.Start, () => {_timeForRaid = true; });
                ScheduleAction(_configData.Shedules.Sunday.End, () => { _timeForRaid = false; });
               
                //Schedule an action to call this method at the start of the week
            }        private void ScheduleAction(DateTime time, Action action)
            {
                TimeSpan delta = time - DateTime.Now;            if (delta > TimeSpan.Zero)
                {
                    timer.Once((float) delta.TotalSeconds, action);
                }
            }
     
  3. Oh great I just analyzed what you're doing and I hadn't thought about it at all. However I have a question if I create a cmd to change the time of a day on the fly and I execute the sheduleAction again with the day and start for example, will it overwrite the programmed action?