Ejemplo n.º 1
0
        public static AbcApplication Create(List <Habit> listHabit)
        {
            if (listHabit == null)
            {
                throw new Exception("the Habit is not created yet");
            }

            AbcApplication      daily        = new HabitTracker(listHabit);
            Habit               habits       = null;
            DominatingHandler   dominating   = new DominatingHandler(new DominatingGain());
            WorkaholicHandler   workaholic   = new WorkaholicHandler(new WorkaholicGain());
            EpicComebackHandler epiccomeback = new EpicComebackHandler(new EpicComebackGain());

            habits.Attach(dominating);
            habits.Attach(workaholic);
            habits.Attach(epiccomeback);

            return(daily);
        }
Ejemplo n.º 2
0
        public static Log AddLog(Habit habit, DateTime currentDate, IBadgeRepository badgeRepo = null, IHabitRepository habitRepo = null)
        {
            string[] days_off   = habit.DaysOff;
            string   currentDay = (currentDate.DayOfWeek.ToString()).Substring(0, 3);
            int      count      = 0;

            DateTime[] allLog       = habit.Log;
            DateTime   lastDateTime = DateTime.Now;

            if (allLog.Length == 0)
            {
                lastDateTime = currentDate.AddDays(-1);
            }
            else if (allLog.Length != 0)
            {
                lastDateTime = allLog[allLog.Length - 1];
            }

            int current_streak = habit.CurrentStreak;
            int longest_streak = habit.LongestStreak;

            int diff = (currentDate - lastDateTime).Days;

            if (diff <= 0)
            {
                throw new Exception("Today, You already Logs it");
            }
            else if (diff == 1)
            {
                current_streak++;
            }
            else if (diff > 1 && diff < 7)
            {
                for (int i = 1; i < diff; i++)
                {
                    if (Array.Exists(days_off, element => element == ((lastDateTime.AddDays(i).DayOfWeek.ToString()).Substring(0, 3))) == false)
                    {
                        break;
                    }
                    else
                    {
                        count++;
                    }
                }
                if (count == (diff - 1))
                {
                    current_streak++;
                }
                else
                {
                    current_streak = 5;
                }
            }
            else
            {
                current_streak = 1;
            }

            if (longest_streak < current_streak)
            {
                longest_streak = current_streak;
            }
            if (current_streak == 0 || longest_streak == 0)
            {
                current_streak = 1;
                longest_streak = 1;
            }

            if (Array.Exists(days_off, element => element == ((currentDate.DayOfWeek.ToString()).Substring(0, 3))) == true)
            {
                count = 1;
            }

            DateTime[] logDate = { currentDate };
            string[]   logDay  = { currentDay };

            Log log = new Log(current_streak, longest_streak, logDate, logDay);

            //Domain Event
            TriggerHandler Dominating   = new DominatingHandler(badgeRepo, new ActivityDominating());
            TriggerHandler Workaholic   = new WorkaholicHandler(badgeRepo, new ActivityWorkaholic());
            TriggerHandler EpicComeback = new EpicComebackHandler(badgeRepo, new ActivityEpicComeback());

            habit.Attach(Dominating);
            habit.Attach(Workaholic);
            habit.Attach(EpicComeback);

            if (isDominating(habit.UserID, badgeRepo, current_streak))
            {
                habit.Broadcast(new Dominating(habit.UserID));
            }
            if (isWorkaholic(habit.UserID, badgeRepo, habitRepo, count))
            {
                habit.Broadcast(new Workaholic(habit.UserID));
            }
            if (isEpicComeback(habit.UserID, badgeRepo, current_streak, habit))
            {
                habit.Broadcast(new EpicComeback(habit.UserID));
            }

            return(log);
        }