private async Task updateNotifications(long userid, UserDataDto data) { var pilots = await _pilotRepo.GetAll(userid); foreach (var p in pilots) { var pd = data.Pilots.FirstOrDefault(x => x.Name == p.Name); Debug.Assert(pd != null); var actualManufacturingCount = data.Jobs.Count(x => x.Owner == p.Name && x.IsManufacturing); var actualResearchCount = data.Jobs.Count(x => x.Owner == p.Name && !x.IsManufacturing); if (p.FreeManufacturingJobsNofificationCount > 0) { if (actualManufacturingCount >= pd.MaxManufacturingJobs) { _logger.Debug("{method} resetting notification", "JobRepo::updateNotifications"); await _pilotRepo.SetFreeManufacturingJobsNofificationCount(p.PilotId, 0); // reset notification - maximum number of jobs running } } else { if (actualManufacturingCount < pd.MaxManufacturingJobs) { // notify about free manufacturing slots _logger.Debug("{method} scheduling man notification for {pilot}", "JobRepo::updateNotifications", p.Name); await _notificationRepo.IssueNew(userid, p.Name, $"{pd.MaxManufacturingJobs - actualManufacturingCount} free manufacturing slots"); await _pilotRepo.SetFreeManufacturingJobsNofificationCount(p.PilotId, 1); } } if (p.FreeResearchJobsNofificationCount > 0) { if (actualResearchCount >= pd.MaxResearchJobs) { _logger.Debug("{method} resetting notification", "JobRepo::updateNotifications"); await _pilotRepo.SetFreeResearchJobsNofificationCount(p.PilotId, 0); // reset notification - maximum number of jobs running } } else { if (actualResearchCount < pd.MaxResearchJobs) { // notify about free research slots _logger.Debug("{method} scheduling research notification for {pilot}", "JobRepo::updateNotifications", p.Name); await _notificationRepo.IssueNew(userid, p.Name, $"{pd.MaxResearchJobs - actualResearchCount} free research slots"); await _pilotRepo.SetFreeResearchJobsNofificationCount(p.PilotId, 1); } } } }
public async Task Update(long userId, UserDataDto data) { _logger.Debug("{method} {userid}", "SkillRepo::Update", userId); using (var ctx = _accountContextProvider.Context) { var pilots = await ctx.Pilots.Include(c => c.Skills).Where(x => x.UserId == userId).ToListAsync(); foreach (var p in pilots) { var pd = data.Pilots.FirstOrDefault(x => x.Name == p.Name); Debug.Assert(pd != null); var storedSkills = p.Skills; bool suspendNotification = storedSkills.Count == 0; // suspend notification if the pilot is seen for the first time var toremove = storedSkills.Where(x => pd.Skills.All(z => z.SkillName != x.SkillName)); // it is not expected that we need to remove a skill. Probably it could happen if skills are renamed or skills are lost due to clone kill foreach (var r in toremove) { _logger.Debug("{method} removing skill {skill} for {pilot}", "SkillRepo::Update", r.SkillName, p.Name); await _notificationRepo.IssueNew(userId, p.Name, $"{r.SkillName} {r.Level} removed"); } ctx.Skills.RemoveRange(toremove); var toadd = pd.Skills.Where(x => storedSkills.All(y => y.SkillName != x.SkillName)); foreach (var a in toadd) { _logger.Debug("{method} adding skill {skill} for {pilot}", "SkillRepo::Update", a, p.Name); var skill = new Skill() { PilotId = p.PilotId, SkillName = a.SkillName, Level = a.Level }; p.Skills.Add(skill); ctx.Skills.Add(skill); if (!suspendNotification) { await _notificationRepo.IssueNew(userId, p.Name, $"{a.SkillName} {a.Level} trained"); } } // Changed level foreach (var s in pd.Skills) { var found = storedSkills.FirstOrDefault(x => x.SkillName == s.SkillName && x.Level != s.Level); if (found != null) { found.Level = s.Level; if (!suspendNotification) { await _notificationRepo.IssueNew(userId, p.Name, $"{s.SkillName} {s.Level} trained"); } } } } await ctx.SaveChangesAsync(); } }