public static void EnsureInDatabase(long id, Data.WaitlistDataContext _Db) { var corporation = _Db.Corporation.Find(id); if (corporation != null) { return; } var result = EsiWrapper.GetCorporation(id); corporation = new Corporation { Id = id, Name = result.Result.Name, AllianceId = result.Result.AllianceId }; //Corporation is not in an alliance if (corporation.AllianceId != 0) { Alliance.EnsureInDatabase((int)corporation.AllianceId, _Db); } _Db.Add(corporation); _Db.SaveChanges(); return; }
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> NewShip(IFormCollection request) { string hullType = request._str("ship_name"); int queue_id = request._int("queue_id"); ShipType ship = await _Db.ShipTypes.Where(c => c.Name.ToLower() == hullType.ToLower()).FirstOrDefaultAsync(); if (ship != null) { ship.Queue = (Queue)queue_id; await _Db.SaveChangesAsync(); return(Accepted()); } SearchResults x = await EsiWrapper.Search(hullType, true, SearchCategory.InventoryType); if (x.InventoryTypes == null) { return(NotFound($"{hullType} could not be found. Is the name spelt correctly?")); } await _Db.AddAsync(new ShipType { Id = (int)x.InventoryTypes[0], Name = hullType, Queue = (Queue)queue_id }); await _Db.SaveChangesAsync(); return(Ok()); }
EnsureInDatabase(int typeId, Data.WaitlistDataContext _Db) { ShipType ship = await _Db.ShipTypes.FindAsync(typeId); if (ship != null) { return; } var esiResponse = await EsiWrapper.GetShipTypeAsync(typeId); if (esiResponse.FirstOrDefault() == null) { return; } ship = new ShipType { Id = typeId, Name = esiResponse[0].Name }; await _Db.AddAsync(ship); await _Db.SaveChangesAsync(); return; }
public async Task <int> GetDistanceFromStaging() { if (InfestedSystems.Count > 0) { int[] jumps = await EsiWrapper.GetJumps(DefaultStagingSystemId, InfestedSystems.First().Value.SystemId); return(jumps.Length); } return(0); }
public async Task <IActionResult> Invite(int fleetId, int pilotId, IFormCollection request) { Fleet fleet = await _Db.Fleets.Where(c => c.Id == fleetId).Include(c => c.BossPilot).FirstOrDefaultAsync(); if (fleet == null) { return(BadRequest("The fleet was not found")); } Pilot boss = await _Db.Pilots.FindAsync(fleet.BossPilotId); if (boss == null) { return(BadRequest("The fleet boss was not found")); } if (!boss.ESIValid) { return(Unauthorized("Could not validate the FCs ESI Tokens")); } await boss.UpdateToken(); await _Db.SaveChangesAsync(); try { long.TryParse(request._str("squadId"), out long squad_id); long.TryParse(request._str("wingId"), out long wing_id); DefaultSquad squadPosition; if (squad_id == 0) { squadPosition = fleet.DefaultSquad(); } else { squadPosition = new DefaultSquad { squad_id = squad_id, wing_id = wing_id }; } var x = EsiWrapper.FleetInvite((AuthorizedCharacterData)boss, fleet.EveFleetId, squadPosition.squad_id, squadPosition.wing_id, pilotId); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> SetDestination(IFormCollection request) { int target_id = request._int("target_id"); Pilot pilot = await _Db.Pilots.FindAsync(Request.Cookies.PreferredPilotId()); await pilot.UpdateToken(); EsiWrapper.SetDestination((AuthorizedCharacterData)pilot, target_id); await _Db.SaveChangesAsync(); return(Ok()); }
private async Task <int?> CheckWaitlistForPilot(int characterId, Fleet fleet) { WaitingPilot x = await _Db.WaitingPilots.Where(c => c.PilotId == characterId && c.RemovedByAccountId == null).FirstOrDefaultAsync(); if (x == null) { Pilot pilot = await _Db.Pilots.FindAsync(characterId); if (pilot == null) { // We don't know who this pilot is, let's look them up with ESI and create some records for them var pilotInfo = await EsiWrapper.PilotLookupAsync(characterId); Corporation.EnsureInDatabase(pilotInfo.CorporationId, _Db); await _Db.AddAsync(new Pilot { Account = null, CharacterID = characterId, CharacterName = pilotInfo.Name, CorporationID = pilotInfo.CorporationId, RegisteredAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }); } // Create a waiting pilot! WaitingPilot waitlistPilot = new WaitingPilot { RemovedByAccountId = fleet.BossPilot.AccountId, PilotId = characterId, FleetAssignment = null, SystemId = null, IsOffline = false, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; await _Db.AddAsync(waitlistPilot); await _Db.SaveChangesAsync(); //Return that waiting pilot return(waitlistPilot.Id); } x.RemovedByAccountId = fleet.BossPilot.AccountId; x.UpdatedAt = DateTime.UtcNow; return(x.Id); }
public async Task SetInfestedSystems(long[] systemIDs) { if (InfestedSystems == null) { InfestedSystems = new Dictionary <long, SolarSystem>(); } foreach (long systemID in systemIDs) { if (InfestedSystems.ContainsKey(systemID)) { InfestedSystems[systemID] = await EsiWrapper.GetSystem((int)systemID); } else { InfestedSystems.Add(systemID, await EsiWrapper.GetSystem((int)systemID)); } } }
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."); }
public static void EnsureInDatabase(int id, Data.WaitlistDataContext _Db) { var alliance = _Db.Alliance.Find(id); if (alliance != null) { return; } var result = EsiWrapper.GetAlliance(id); alliance = new Alliance { Id = id, Name = result.Result.Name }; _Db.Add(alliance); _Db.SaveChanges(); return; }
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."); }
public async Task <IActionResult> Callback(string code, string state) { // Verify a code and state query parameter was returned. if (code == null || state == null) { _Logger.LogWarning("Eve Callback Error: One or more of the query parameters are missing. State: {0}. Code: {1}", state, code); return(StatusCode(452)); } // Verify the state to protect against CSRF attacks. if (HttpContext.Session.GetString("state") != state) { _Logger.LogWarning("Eve Callback Error: Invalid state returned."); HttpContext.Session.Remove("state"); return(StatusCode(452)); } // Clear the state session HttpContext.Session.Remove("state"); ESI.NET.EsiClient s_client = EsiWrapper.GetEsiClient(); SsoToken token = await s_client.SSO.GetToken(GrantType.AuthorizationCode, code); AuthorizedCharacterData n_pilot = await s_client.SSO.Verify(token); long corporation_id = (long)n_pilot.CorporationID; Corporation.EnsureInDatabase(corporation_id, _Db); var pilot = await _Db.Pilots.FindAsync(n_pilot.CharacterID); if (pilot == null) { // User doesn't exist, create a new account pilot = new Pilot() { CharacterID = n_pilot.CharacterID, AccountId = User.AccountId(), CharacterName = n_pilot.CharacterName, CorporationID = corporation_id, RefreshToken = n_pilot.RefreshToken, Token = n_pilot.Token, UpdatedAt = DateTime.UtcNow, RegisteredAt = DateTime.UtcNow, }; await _Db.AddAsync(pilot); await _Db.SaveChangesAsync(); _Logger.LogDebug("{0} has linked the pilot {1} to their account.", User.FindFirst("name").Value, pilot.CharacterName); //TODO: alert user that it worked return(Redirect("/pilot-select")); } else if (!pilot.IsLinked() || pilot.BelongsToAccount(int.Parse(User.FindFirst("id").Value))) { // Update the pilot information - This may include a new account if it was unlinked. pilot.AccountId = User.AccountId(); pilot.CharacterName = n_pilot.CharacterName; pilot.CorporationID = corporation_id; pilot.RefreshToken = n_pilot.RefreshToken; pilot.Token = n_pilot.Token; pilot.UpdatedAt = DateTime.UtcNow; await _Db.SaveChangesAsync(); _Logger.LogDebug("{0} has updated the pilot {1} that is linked to their account.", User.FindFirst("name").Value, pilot.CharacterName); //TODO: alert user that it worked return(Redirect("/pilot-select")); } //TODO: alert user that it failed _Logger.LogDebug("{0} has tried to link {1} to their account, however it is linked to someone else’s account.", User.AccountName(), pilot.CharacterName); return(Redirect("/pilot-select")); }
// Get a constellations region name. public static async Task <string> GetRegionName(this ESI.NET.Models.Universe.Constellation constellation) { ESI.NET.Models.Universe.Region region = await EsiWrapper.GetRegion((int)constellation.RegionId); return(region.Name); }
public async Task SetConstellation(int constellationID) { m_constellation = await EsiWrapper.GetConstellation(constellationID); m_regionName = await Constellation.GetRegionName(); }