Skip to content

Commit

Permalink
Invert if for uhhh yeah
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed May 23, 2024
1 parent 421edbe commit 0d5ab9f
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions ReplayBrowser/Helpers/Ss14ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,40 @@ public Ss14ApiHelper(IMemoryCache cache)

public async Task<PlayerData?> FetchPlayerDataFromGuid(Guid guid)
{
if (!_cache.TryGetValue(guid.ToString(), out PlayerData? playerKey))
if (_cache.TryGetValue(guid.ToString(), out PlayerData? playerKey))
return playerKey;

playerKey = new PlayerData()
{
playerKey = new PlayerData()
{
PlayerGuid = guid
};
PlayerGuid = guid
};

HttpResponseMessage response = null;
try
HttpResponseMessage response = null;
try
{
var httpClient = new HttpClient();
response = await httpClient.GetAsync($"https://central.spacestation14.io/auth/api/query/userid?userid={playerKey.PlayerGuid}");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var username = JsonSerializer.Deserialize<UsernameResponse>(responseString).userName;
playerKey.Username = username;
}
catch (Exception e)
{
Log.Error("Unable to fetch username for player with GUID {PlayerGuid}: {Error}", playerKey.PlayerGuid, e.Message);
if (e.Message.Contains("'<' is an")) // This is a hacky way to check if we got sent a website.
{
var httpClient = new HttpClient();
response = await httpClient.GetAsync($"https://central.spacestation14.io/auth/api/query/userid?userid={playerKey.PlayerGuid}");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var username = JsonSerializer.Deserialize<UsernameResponse>(responseString).userName;
playerKey.Username = username;
// Probably got sent a website? Log full response.
Log.Error("Website might have been sent: {Response}", response?.Content.ReadAsStringAsync().Result);
}
catch (Exception e)
{
Log.Error("Unable to fetch username for player with GUID {PlayerGuid}: {Error}", playerKey.PlayerGuid, e.Message);
if (e.Message.Contains("'<' is an")) // This is a hacky way to check if we got sent a website.
{
// Probably got sent a website? Log full response.
Log.Error("Website might have been sent: {Response}", response?.Content.ReadAsStringAsync().Result);
}

playerKey.Username = "Unable to fetch username (API error)";
}
playerKey.Username = "Unable to fetch username (API error)";
}

var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(60));
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(60));

_cache.Set(guid.ToString(), playerKey, cacheEntryOptions);
}
_cache.Set(guid.ToString(), playerKey, cacheEntryOptions);

return playerKey;
}
Expand Down

0 comments on commit 0d5ab9f

Please sign in to comment.