public void BadgeAssignment(Guid userId, Guid habitId, int streak) { List <Guid> userBadge = listOfUserBadge(userId); var badge = ListBadge(); if (streak == 4) { var DominatingBadgeId = Guid.Parse("3FDFE700-D823-49A9-97CB-14DC44B7ED76"); if (!userBadge.Contains(DominatingBadgeId)) { BadgeUse bu = BadgeUseFactory.addNewBagdeUse(DominatingBadgeId, userId); _context.BadgeUses.Add(bu); } } else if (streak == 10) { var EpicComebackBadgeId = Guid.Parse("9535D8EA-03EA-4B70-B7D4-0C6FC93A4710"); if (_logrepo.countLogGap(habitId)) { if (!userBadge.Contains(EpicComebackBadgeId)) { BadgeUse bu = BadgeUseFactory.addNewBagdeUse(EpicComebackBadgeId, userId); _context.BadgeUses.Add(bu); } } } }
public void addStreakandLog(Guid habitId, Guid userId) { var habit = GetSpecificHabitByIdAndUserId(habitId, userId); var logcheck = _logrepo.searchLog(habitId); int checkTime; if (logcheck == null) { checkTime = -1; } else { var logtime = _logrepo.lastLog(habitId); checkTime = _logrepo.checkLogDay(logtime); } if (checkTime == 1) { //first add log count habit.LogCount += 1; //check if streak reset or below, so longestStreak didnt get reset if (habit.CurrentStreak == 0) { habit.LongestStreak = habit.LongestStreak; } else if (habit.LongestStreak > habit.CurrentStreak) { habit.LongestStreak = habit.LongestStreak; } //after checking, adding current streak habit.CurrentStreak += 1; //checking if streak is surpass longestStreak, make it right again if (habit.LongestStreak < habit.CurrentStreak) { habit.LongestStreak = habit.CurrentStreak; } } else if (checkTime == 0) //didnt add log count since it is on the same day { habit.LogCount = habit.LogCount; habit.CurrentStreak = habit.CurrentStreak; habit.LongestStreak = habit.LongestStreak; } else if (checkTime > 1) //skip 1 or more days { var daysoff = GetHabitDaysOff(habitId); var x = new { daysoff.daysoff }; string[] strArrayOne = new string[] { "" }; strArrayOne = x.daysoff.Split(", "); List <int> day = new List <int>(); foreach (var y in strArrayOne) { if (y.Equals("Mon")) { day.Add(1); } else if (y.Equals("Tue")) { day.Add(2); } else if (y.Equals("Wed")) { day.Add(3); } else if (y.Equals("Thu")) { day.Add(4); } else if (y.Equals("Fri")) { day.Add(5); } else if (y.Equals("Sat")) { day.Add(6); } else if (y.Equals("Sun")) { day.Add(7); } } var logtime = _logrepo.lastLog(habitId); checkTime = _logrepo.checkLogDay(logtime); if (logtime == DateTime.MinValue) { habit.LogCount += 1; habit.CurrentStreak += 1; habit.LongestStreak = habit.CurrentStreak; } else if (checkTime == 2) { for (int i = 0; i < strArrayOne.Count(); i++) { if ((int)logtime.DayOfWeek == day[i]) { habit.LogCount += 1; habit.CurrentStreak += 1; habit.LongestStreak = habit.CurrentStreak; break; } } } else { habit.LogCount += 1; habit.CurrentStreak = 0; habit.LongestStreak = habit.LongestStreak; } } else { habit.LogCount += 1; habit.CurrentStreak += 1; habit.LongestStreak = habit.CurrentStreak; } if (habit.CurrentStreak == 4 || habit.CurrentStreak == 10) { _badgerepo.BadgeAssignment(userId, habitId, habit.CurrentStreak); } else { List <Guid> userBadge = _badgerepo.listOfUserBadge(userId); var badge = _badgerepo.ListBadge(); var WorkaholicBadgeId = Guid.Parse("C300B347-299B-4965-9BCE-615B7F74C7AD"); if (!userBadge.Contains(WorkaholicBadgeId)) { var daysoff = GetHabitDaysOff(habitId); var x = new { daysoff.daysoff }; List <DateTime> log = _logrepo.searchLog(habitId); int temp = 0; string[] strArrayOne = new string[] { "" }; strArrayOne = x.daysoff.Split(", "); List <int> day = new List <int>(); List <int> logDay = new List <int>(); foreach (var z in log) { logDay.Add((int)z.DayOfWeek); } foreach (var y in strArrayOne) { if (y.Equals("Mon")) { day.Add(1); } else if (y.Equals("Tue")) { day.Add(2); } else if (y.Equals("Wed")) { day.Add(3); } else if (y.Equals("Thu")) { day.Add(4); } else if (y.Equals("Fri")) { day.Add(5); } else if (y.Equals("Sat")) { day.Add(6); } else if (y.Equals("Sun")) { day.Add(7); } } for (int i = 0; i < logDay.Count(); i++) { for (int j = 0; j < day.Count; j++) { if (logDay[i] == day[j]) { temp++; } if (temp == 10) { BadgeUse bu = BadgeUseFactory.addNewBagdeUse(WorkaholicBadgeId, userId); _context.BadgeUses.Add(bu); break; } } } } } }