Esempio n. 1
0
        private void UpdateDays(bool init = false)
        {
            var t = TimeManager.LocalUrmobiServerDT;

            if (t >= NextDate.AddDays(1d))
            {
                CurrentDailyDay = 0;
                NextDailyDay    = 1;
                //SetupCurrentRewards();
                NextDate = new System.DateTime(t.Year, t.Month, t.Day, 0, 0, 0).AddDays(1);
                UserDataControl.Instance.UserData.RewardsData.nextDate = NextDate.ToString();
            }
            else if (t >= NextDate && t < NextDate.AddDays(1))
            {
                CurrentDailyDay = NextDailyDay;
                NextDailyDay    = NextDailyDay < daysCycle - 1 ? (byte)(CurrentDailyDay + 1) : (byte)0;
                //SetupCurrentRewards();
                NextDate = new System.DateTime(t.Year, t.Month, t.Day, 0, 0, 0).AddDays(1);
                UserDataControl.Instance.UserData.RewardsData.currentDailyDay = CurrentDailyDay;
                UserDataControl.Instance.UserData.RewardsData.nextDate        = NextDate.ToString();
                EventManager.Notify(this, new GameEventArgs(Events.Daily.NEX_DAY));
            }
            else if (t >= NextDate)
            {
                //SetupCurrentRewards();
            }

            if (init)
            {
                Inited?.Invoke();
            }
        }
Esempio n. 2
0
        public string GenerateXact()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(Times.TimesCommon.Current.FormatDate(NextDate, FormatTypeEnum.FMT_WRITTEN));
            NextDate = NextDate.AddDays(SixGen.Value());
            if (TruthGen.Value())
            {
                sb.Append('=');
                sb.Append(Times.TimesCommon.Current.FormatDate(NextAuxDate, FormatTypeEnum.FMT_WRITTEN));
                NextAuxDate = NextAuxDate.AddDays(SixGen.Value());
            }
            sb.Append(' ');

            sb.Append(GenerateState());
            sb.Append(GenerateCode());
            sb.Append(GeneratePayee());
            if (TruthGen.Value())
            {
                sb.Append(GenerateNote());
            }
            sb.AppendLine();

            int  count          = ThreeGen.Value() * 2;
            bool hasMustBalance = false;

            for (int i = 0; i < count; i++)
            {
                if (GeneratePost(sb))
                {
                    hasMustBalance = true;
                }
            }
            if (hasMustBalance)
            {
                GeneratePost(sb, true);
            }

            sb.AppendLine();

            return(sb.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the first date after the specified date that is represented by the COSEMDate object
        /// </summary>
        /// <param name="date">The date to get from</param>
        /// <returns>The first valid date</returns>
        public DateTime?GetClosestFutureDate(DateTime date)
        {
            DateTime?FutureDate = null;

            if (IsSpecificDate)
            {
                // The date is a specific date so as long as it's in the future we can return the date
                FutureDate = new DateTime(m_Year, (int)m_Month, (int)m_DayOfMonth);

                if (FutureDate < date.Date)
                {
                    // The date is in the past so set it back to null
                    FutureDate = null;
                }
            }
            else
            {
                // Start out with the specified date and figure it out from there
                DateTime NextDate = date.Date;

                if (m_Year != YEAR_NOT_SPECIFIED)
                {
                    if (m_Year < NextDate.Year)
                    {
                        // No valid future date
                        return(null);
                    }
                    else
                    {
                        NextDate = NextDate.AddYears(m_Year - NextDate.Year);
                    }
                }

                if (m_Month != COSEMMonth.NotSpecified)
                {
                    if (m_Month == COSEMMonth.DaylightSavingsBegin)
                    {
                        // TODO: Figure out how to get the DST Month here
                    }
                    else if (m_Month == COSEMMonth.DaylightSavingsEnd)
                    {
                        // TODO: Figure out how to get the DST Month here
                    }
                    else
                    {
                        // Add one to the month until we reach the desired month so that the year will change as necessary
                        while (NextDate.Month != (int)m_Month)
                        {
                            NextDate = NextDate.AddMonths(1);
                        }
                    }
                }

                if (m_DayOfMonth != COSEMDayOfMonth.NotSpecified)
                {
                    if (m_DayOfMonth == COSEMDayOfMonth.LastDay || m_DayOfMonth == COSEMDayOfMonth.SecondToLastDay)
                    {
                        // Add one day until the next day causes the month to change
                        while (NextDate.Month == NextDate.AddDays(1).Month)
                        {
                            NextDate = NextDate.AddDays(1);
                        }

                        // For second to last day just subtract a day
                        if (m_DayOfMonth == COSEMDayOfMonth.SecondToLastDay)
                        {
                            NextDate = NextDate.AddDays(-1);
                        }
                    }
                    else
                    {
                        if ((int)m_DayOfMonth >= NextDate.Day && (int)m_DayOfMonth <= DateTime.DaysInMonth(NextDate.Year, NextDate.Month))
                        {
                            // We have a valid date in the current month
                            NextDate = NextDate.AddDays((int)m_DayOfMonth - NextDate.Day);
                        }
                        else if (m_Month == COSEMMonth.NotSpecified)
                        {
                            // The month is not specified and we have already passed the day in the current month so we should move on to the next month
                            NextDate = new DateTime(NextDate.Year, NextDate.Month, 1, 0, 0, 0, NextDate.Kind).AddMonths(1);

                            // Since the length of a month may vary lets just keep adding one day until it matches (if 29, 30 , or 31 is picked)
                            while (NextDate.Day != (int)m_DayOfMonth)
                            {
                                NextDate = NextDate.AddDays(1);
                            }
                        }
                        else if (m_Year == YEAR_NOT_SPECIFIED)
                        {
                            // In most cases we will need to add one year so let's go ahead and do that.
                            NextDate = new DateTime(NextDate.Year, NextDate.Month, 1, 0, 0, 0, NextDate.Kind).AddYears(1);

                            // The month is specified but the year is not. First we should check and see if the day of month is even valid at all for the month
                            // February is the only month that the number of days in the month can vary so lets check that first
                            if (m_Month == COSEMMonth.February)
                            {
                                if (m_DayOfMonth == COSEMDayOfMonth.Twentyninth)
                                {
                                    // Increase the year until there are 29 days in the month
                                    while (DateTime.DaysInMonth(NextDate.Year, 2) != 29)
                                    {
                                        NextDate = NextDate.AddYears(1);
                                    }

                                    // Set the day to the 29th. We should be at the first so add 28 days
                                    NextDate = NextDate.AddDays(28);
                                }
                                else if (m_DayOfMonth <= COSEMDayOfMonth.Twentyeighth)
                                {
                                    // We have already adjusted forward one year so just set the date
                                    NextDate = NextDate.AddDays((int)m_DayOfMonth - 1);
                                }
                                else
                                {
                                    // The date will never be valid for this month
                                    return(null);
                                }
                            }
                            else if ((int)m_DayOfMonth <= DateTime.DaysInMonth(NextDate.Year, NextDate.Month))
                            {
                                // We have already adjusted forward one year so just set the date
                                NextDate = NextDate.AddDays((int)m_DayOfMonth - 1);
                            }
                            else
                            {
                                // The date will never be valid for this month
                                return(null);
                            }
                        }
                    }
                }

                if (m_DayOfWeek != COSEMDayOfWeek.NotSpecified)
                {
                    if (m_DayOfMonth == COSEMDayOfMonth.NotSpecified)
                    {
                        // Since the date is not specified we should be able to adjust the date forward
                        int            DaysToAdjust     = 0;
                        COSEMDayOfWeek CurrentDayOfWeek = ConvertDateTimeDayOfWeekToCOSEMDayOfWeek(NextDate.DayOfWeek);

                        if (m_DayOfWeek >= CurrentDayOfWeek)
                        {
                            DaysToAdjust = (int)m_DayOfWeek - (int)CurrentDayOfWeek;
                        }
                        else
                        {
                            // We have to adjust through the end of the week and then to the correct day of the week
                            DaysToAdjust = (int)COSEMDayOfWeek.Sunday - (int)CurrentDayOfWeek + (int)m_DayOfWeek;
                        }

                        if (m_Year != YEAR_NOT_SPECIFIED && m_Year != NextDate.AddDays(DaysToAdjust).Year)
                        {
                            // If the adjustment causes the year to change but a specific year is specified then there are no future dates
                            return(null);
                        }
                        else if (m_Month != COSEMMonth.NotSpecified)
                        {
                        }
                    }
                }
            }

            return(FutureDate);
        }
        public void UpdateCalendar()
        {
            ApplicationDbContext        db           = new ApplicationDbContext();
            DateTime                    DateNow      = DateTime.Now; // take date now so it is locked in (in case near midnight)
            List <CalendarItem>         TimetableOld = db.CalendarItems.OrderBy(x => x.GymClassTime).ToList <CalendarItem>();
            List <CalendarItem>         TimetableNew = new List <CalendarItem>();
            List <StdGymClassTimetable> StdTimetable = db.StdGymClassTimetables.ToList <StdGymClassTimetable>();

            if (!StdTimetable.Any())
            {
                return;
            }
            if (!TimetableOld.Any())
            {
                // create a whole new list from -7 days to + 28 days. -7 to keep a record of class goers
                CreateTimetable(DateNow.AddDays(-7), (7 + 28));
            }
            else
            {
                // search from start of TimetableOld until you get to a date max 7 days before today
                int i = 0;
                while (i < TimetableOld.Count && TimetableOld[i].GymClassTime.Date < DateNow.AddDays(-7).Date)
                {
                    i++;                     // change to a lower number to test or positive
                }
                if (i >= TimetableOld.Count) // If not found (i.e. we are more than 28 days since last checked) then create a whole new list
                {
                    CreateTimetable(DateNow.AddDays(-7), (7 + 28));
                }

                if (i == 0)
                {
                    return;         // no change so no need to create a new timetable
                }
                // int startDatePositionInNewTimetable = i;  // not needed
                // If found create new list starting from that point to end of data
                while (i < TimetableOld.Count)
                {
                    TimetableNew.Add(TimetableOld[i]);
                    i++;
                }
                // and then add new days to max + 28 days
                // days to add would be 7 + 28 less days in new timetable (Last date in old timetable i-1 less date in start of new timetable)
                TimeSpan span     = TimetableOld[i - 1].GymClassTime.Subtract(TimetableNew[0].GymClassTime);
                int      days     = 7 + 28 - span.Days - 1;
                DateTime dateFrom = TimetableOld[i - 1].GymClassTime.AddDays(1); // might need to add (Date)? Or change date to a non nullable property
                CreateTimetable(dateFrom, days);
            }

            // create up to date calendar using std timetable as basis and old timetable as start
            void CreateTimetable(DateTime NextDate, int totalDays)
            {
                ///// DayOfWeek change
                DayOfWeek DayOfWeekOfFirstItem = NextDate.DayOfWeek; // Enum: Sunday 0, Monday 1 etc Saturday 6
                // Find first occurence of day of the week in the std timetable (or followng day if no classes on that day)
                // either loop through from start to find the index
                // add each item of the std timetable to the new timetable each day until + 28 days looping around the std timetable
                var StdTimetableArray = StdTimetable.OrderBy(a => a.Day).ThenBy(a => a.Hour).ToArray();
                int j = 0;

                while (StdTimetableArray[j].Day < DayOfWeekOfFirstItem)
                {
                    j++;
                }

                // int startPositionInStdTimetable = j; // not needed
                // now loop through all items in standard timetable to end (to Saturday) then loop back to start (Sunday) then on to day before start (j-1)
                DayOfWeek currentDayOfWeek = DayOfWeekOfFirstItem; // Sunday 0, Monday 1 etc Saturday 6

                for (int i = 0; i < totalDays; i++)                //loop through adding days to new timetable (35 days (7 + 28)) some of which may be taken from previous timetable
                {
                    // j is std timetable array item number. Need to loop back to start when end is reached
                    while (j < StdTimetableArray.Count() && StdTimetableArray[j].Day == currentDayOfWeek) //loop through each day's classes and add to new timetable
                    {
                        if (StdTimetableArray[j].Deleted == false)
                        {
                            int      Hour         = (int)StdTimetableArray[j].Hour;
                            int      Minute       = (int)StdTimetableArray[j].Minute;
                            TimeSpan ts           = new TimeSpan(Hour, Minute, 0);
                            DateTime GymClassTime = NextDate.Date + ts;

                            CalendarItem item = new CalendarItem
                            {
                                GymClassId   = StdTimetableArray[j].GymClassId,
                                Instructor   = StdTimetableArray[j].Instructor,
                                Duration     = StdTimetableArray[j].Duration,
                                Hall         = StdTimetableArray[j].Hall,
                                UserIds      = "",
                                GymClassTime = GymClassTime,
                                MaxPeople    = StdTimetableArray[j].MaxPeople
                            };

                            TimetableNew.Add(item);
                        }

                        j++;
                    }
                    currentDayOfWeek++;
                    if (currentDayOfWeek > DayOfWeek.Saturday)
                    {
                        currentDayOfWeek = 0;
                    }
                    NextDate = NextDate.AddDays(1);
                    // loop back to start of std timetable if end reached
                    if (j >= StdTimetableArray.Count())
                    {
                        j = 0;
                    }
                }
                // delete old calendar and save new
                db.Database.ExecuteSqlCommand("delete from CalendarItems");
                //db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('CalendarItems', RESEED, 0)"); // reseed if wanted (set id to 0) - specific to MS SQL
                db.CalendarItems.AddRange(TimetableNew); // saves in random order, can't save in order.
                db.SaveChanges();
            }
        }