public async Task Update(long userId, UserDataDto data) { _logger.Debug("{method} {userid}", "SkillInQueueRepo::Update", userId); using (var ctx = _accountContextProvider.Context) { var pilots = await ctx.Pilots.Include(c => c.SkillsInQueue).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.SkillsInQueue; var toremove = storedSkills.Where(x => pd.SkillsInQueue.All(z => z.SkillName != x.SkillName || z.Level != x.Level)); ctx.SkillsInQueue.RemoveRange(toremove); var toadd = pd.SkillsInQueue.Where(x => storedSkills.All(y => y.SkillName != x.SkillName || y.Level != x.Level)) .Select(x => new SkillInQueue() { PilotId = p.PilotId, Length = x.Length, SkillName = x.SkillName, Level = x.Level, Order = x.Order }); p.SkillsInQueue.Clear(); foreach (var a in toadd) { p.SkillsInQueue.Add(a); } } await ctx.SaveChangesAsync(); } }
public async Task Update(long userid, UserDataDto data) { _logger.Debug("{method} {userid}", "CorporationRepo::Update", userid); using (var ctx = _accountContextProvider.Context) { var user = await ctx.Users.Include(c => c.Corporations).SingleOrDefaultAsync(u => u.UserId == userid); var validCorpoNames = data.Pilots.Select(x => x.Name); // remove inactive corporations var storedCorpos = user.Corporations.ToList(); var inactive = storedCorpos.Where(x => !validCorpoNames.Contains(x.Name)).ToList(); ctx.RemoveRange(inactive); foreach (var r in inactive) { _logger.Debug("{method} removing {pilot}", "CorporationRepo::Update", r.Name); } // add newly revealed pilots var toadd = data.Corporations; foreach (var a in toadd) { a.UserId = userid; if (storedCorpos.All(x => x.EveId != a.EveId)) { user.Corporations.Add(a); ctx.Corporations.Add(a); } } await ctx.SaveChangesAsync(); } }
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); } } } }
private async Task updateRepo(long userid, UserDataDto data) { using (var ctx = _accountContextProvider.Context) { ctx.Jobs.RemoveRange(await ctx.Jobs.Where(x => x.UserId == userid).ToListAsync()); foreach (var j in data.Jobs) { j.UserId = userid; } ctx.Jobs.AddRange(data.Jobs); await ctx.SaveChangesAsync(); } }
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(); } }
public async Task Update(long userid, UserDataDto data) { await updateRepo(userid, data); await updateNotifications(userid, data); }
public async Task Update(long userid, UserDataDto data) { _logger.Debug("{method} {userid}", "PilotRepo::Update", userid); using (var ctx = _accountContextProvider.Context) { var user = await ctx.Users.Include(c => c.Pilots).SingleOrDefaultAsync(u => u.UserId == userid); if (user == null) { throw new UserException(strings.SecurityException); } var validPilotNames = data.Pilots.Select(x => x.Name); // remove inactive pilots var storedPilots = user.Pilots.ToList(); var inactive = storedPilots.Where(x => !validPilotNames.Contains(x.Name)).ToList(); foreach (var r in inactive) { _logger.Debug("{method} removing {pilot}", "pilotRepo::updatePilotData", r.Name); } ctx.Pilots.RemoveRange(inactive); // add/update pilots var toadd = data.Pilots; foreach (var a in toadd) { _logger.Debug("{method} adding {pilot}", "pilotRepo::updatePilotData", a.Name); var stored = storedPilots.FirstOrDefault(x => x.EveId == a.EveId && x.UserId == userid); var pilot = a; pilot.FreeManufacturingJobsNofificationCount = stored == null ? 0 : stored.FreeManufacturingJobsNofificationCount; pilot.FreeResearchJobsNofificationCount = stored == null ? 0 : stored.FreeResearchJobsNofificationCount; // We deliberatly don't update children skills - so skillrepo may issue appropriate notifications pilot.KeyInfoId = a.KeyInfoId; pilot.UserId = userid; if (stored == null) { user.Pilots.Add(pilot); ctx.Pilots.Add(pilot); } else { stored.CurrentTrainingNameAndLevel = a.CurrentTrainingNameAndLevel; stored.CurrentTrainingEnd = a.CurrentTrainingEnd; stored.TrainingQueueEnd = a.TrainingQueueEnd; stored.TrainingActive = a.TrainingActive; stored.MaxManufacturingJobs = a.MaxManufacturingJobs; stored.MaxResearchJobs = a.MaxResearchJobs; stored.FreeManufacturingJobsNofificationCount = a.FreeManufacturingJobsNofificationCount; stored.FreeResearchJobsNofificationCount = a.FreeResearchJobsNofificationCount; } } await ctx.SaveChangesAsync(); } }