private async void DoWork(object state) { _logger.LogInformation("Background Service Started: updating alliances."); List <Alliance> alliances = await _Db.Alliance.ToListAsync(); foreach (Alliance alliance in alliances) { // Skip the non-membership alliance if (alliance.Id == 0) { continue; } var result = await EsiWrapper.GetAlliance(alliance.Id); // Do not update the alliacne name if null/errors are returned. if (result != null) { alliance.Name = result.Name; } } await _Db.SaveChangesAsync(); _logger.LogInformation("Background Service Completed: alliances updated."); }
private async void DoWork(object state) { _logger.LogInformation("Background Service Started: updating waitlist."); List <WaitingPilot> waitlist = await _Db.WaitingPilots.Where(c => c.RemovedByAccount == null).ToListAsync(); foreach (WaitingPilot waiting_pilot in waitlist) { Pilot pilot = await _Db.Pilots.FindAsync(waiting_pilot.PilotId); // Update pilot system await pilot.UpdateToken(); if (!pilot.ESIValid) { continue; } var System = await EsiWrapper.GetSystem((AuthorizedCharacterData)pilot); waiting_pilot.SystemId = System?.SolarSystemId; // Update pilot online/offline await pilot.UpdateToken(); waiting_pilot.IsOffline = !await EsiWrapper.IsOnlineAsync((AuthorizedCharacterData)pilot); // Touch the updated at timestamp waiting_pilot.UpdatedAt = DateTime.UtcNow; } await _Db.SaveChangesAsync(); _logger.LogInformation("Background Service Completed: waitlist updated."); }
public async Task <IActionResult> Leave(IFormCollection request) { List <WaitingPilot> waitingPilots; if (request._str("pilot_id") == "") { waitingPilots = await _Db.WaitingPilots.Include(c => c.Pilot).Where(c => c.Pilot.AccountId == User.AccountId() && c.RemovedByAccount == null).ToListAsync(); } else { waitingPilots = await _Db.WaitingPilots.Where(c => c.PilotId == request._int("pilot_id") && c.RemovedByAccount == null).ToListAsync(); } foreach (WaitingPilot pilot in waitingPilots) { pilot.RemovedByAccountId = User.AccountId(); } await _Db.SaveChangesAsync(); return(Ok()); }
private async void DoWork(object state) { _logger.LogInformation("Background Service Started: updating corporations."); List <Corporation> corporations = await _Db.Corporation.ToListAsync(); foreach (Corporation corporation in corporations) { var result = await EsiWrapper.GetCorporation(corporation.Id); // Do not update the corporation information if null/errors are returned. if (result != null) { corporation.Name = result.Name; corporation.AllianceId = result.AllianceId; } } await _Db.SaveChangesAsync(); _logger.LogInformation("Background Service Completed: corporations updated."); }
private async void DoWork(object state) { _logger.LogInformation("Background Service Started: updating fleets."); List <Fleet> fleets = await _Db.Fleets.Include(ci => ci.BossPilot).Where(c => c.BossPilot != null && c.ClosedAt == null).ToListAsync(); if (fleets.Count == 0) { return; } foreach (Fleet fleet in fleets) { //I'm not sure why, but occasionally a fleet without a boss will bleed through //This happens if boss was null, and then updated by an FC before this system next runs. if (fleet.BossPilotId == null) { continue; } Pilot pilot = await _Db.Pilots.FindAsync(fleet.BossPilotId); try { // Update fleet location based on the Fleet Boss await pilot.UpdateToken(); if (!pilot.ESIValid) { throw new UnauthorizedAccessException("The Fleet Boss's ESI token is no longer valid"); } var System = await EsiWrapper.GetSystem((AuthorizedCharacterData)pilot); fleet.SystemId = System?.SolarSystemId; // Update Wings, Squads await pilot.UpdateToken(); fleet.Wings = await EsiWrapper.GetFleetLayout((AuthorizedCharacterData)pilot, fleet.EveFleetId); // Update the pilots in fleet await pilot.UpdateToken(); long fleetId = (long)fleet.EveFleetId; List <FleetAssignment> current_members = await _Db.FleetAssignments.Where(c => c.FleetId == fleet.Id && c.DeletedAt == null).Include(c => c.WaitingPilot).ToListAsync(); Dictionary <int, FleetAssignment> knownMembers = current_members.ToDictionary(x => x.WaitingPilot.PilotId, x => x); List <ESI.NET.Models.Fleets.Member> esiMembers = await EsiWrapper.GetFleetMembers((AuthorizedCharacterData)pilot, fleetId); if (esiMembers != null) { foreach (var fleetMember in esiMembers) { await ShipType.EnsureInDatabase(fleetMember.ShipTypeId, _Db); // If we know about the fleet member update their info if (knownMembers.ContainsKey(fleetMember.CharacterId)) { knownMembers[fleetMember.CharacterId].ShipTypeId = fleetMember.ShipTypeId; knownMembers[fleetMember.CharacterId].TakesFleetWarp = fleetMember.TakesFleetWarp; knownMembers[fleetMember.CharacterId].UpdatedAt = DateTime.UtcNow; knownMembers[fleetMember.CharacterId].SystemId = fleetMember.SolarSystemId; } else { // Otherwise they're new, let's add them to our fleet assignments var waitlistId = await CheckWaitlistForPilot(fleetMember.CharacterId, fleet); await _Db.FleetAssignments.AddAsync(new FleetAssignment { WaitingPilotId = waitlistId ?? null, FleetId = fleet.Id, IsExitCyno = false, ShipTypeId = fleetMember.ShipTypeId, TakesFleetWarp = fleetMember.TakesFleetWarp, SystemId = fleetMember.SolarSystemId, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }); } } // Delete pilots who have not been reported through ESI for more than 1 minutes. current_members = await _Db.FleetAssignments.Where(c => c.FleetId == fleet.Id && c.DeletedAt == null).ToListAsync(); foreach (FleetAssignment member in current_members) { if (member.UpdatedAt.Value.AddMinutes(1) < DateTime.UtcNow) { member.DeletedAt = DateTime.UtcNow; } } await _Db.SaveChangesAsync(); } // NO errors, resetting counter fleet.ErrorCount = null; } catch (Exception ex) { _logger.LogError("Error updating fleet {0} (FC: {1}). {2} ", fleet.Id, fleet.BossPilot.CharacterName, ex.Message); if (ex.Message == FleetErrorType.FleetDead.ToString()) { fleet.ClosedAt = DateTime.UtcNow; _logger.LogInformation("The fleet no longer exists and has been closed"); continue; } // Increase error counter int?errors = fleet.ErrorCount; fleet.ErrorCount = (errors != null) ? errors + 1 : 1; // Too many errors, deleting fleet boss to protect against error throttling if (errors >= 15) { fleet.BossPilotId = null; fleet.ErrorCount = null; } } // Touch Updated At timestamp fleet.UpdatedAt = DateTime.UtcNow; } await _Db.SaveChangesAsync(); _logger.LogInformation("Background Service Completed: fleets updated."); }