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 <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")); }