Skip to content

Commit

Permalink
rework some calls async
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley Dimant committed Jul 11, 2022
1 parent 0e0a75a commit b8950ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
22 changes: 4 additions & 18 deletions MareSynchronosServer/MareSynchronosServer/Hubs/BaseHub.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using MareSynchronosServer.Data;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
Expand All @@ -21,26 +22,11 @@ protected BaseHub(MareDbContext context, ILogger<T> logger)

protected string AuthenticatedUserId => Context.User?.Claims?.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "Unknown";

protected Models.User GetAuthenticatedUserUntracked()
protected async Task<Models.User> GetAuthenticatedUserUntrackedAsync()
{
return DbContext.Users.AsNoTrackingWithIdentityResolution().Single(u => u.UID == AuthenticatedUserId);
return await DbContext.Users.AsNoTrackingWithIdentityResolution().SingleAsync(u => u.UID == AuthenticatedUserId);
}

protected Models.User? GetUserFromCID(string cid)
{
return DbContext.Users.SingleOrDefault(c => c.CharacterIdentification == cid);
}

protected Models.User? GetUserFromUID(string uid)
{
return DbContext.Users.SingleOrDefault(c => c.UID == uid);
}

protected bool IsUserOnline(string uid)
{
return DbContext.Users.Any(c => c.UID == uid && !string.IsNullOrEmpty(c.CharacterIdentification));
}


public static string GenerateRandomString(int length, string allowableChars = null)
{
if (string.IsNullOrEmpty(allowableChars))
Expand Down
52 changes: 26 additions & 26 deletions MareSynchronosServer/MareSynchronosServer/Hubs/UserHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<List<string>> GetOnlineCharacters()
{
Logger.LogInformation("User " + AuthenticatedUserId + " requested online characters");

var ownUser = GetAuthenticatedUserUntracked();
var ownUser = await GetAuthenticatedUserUntrackedAsync();

var otherUsers = await DbContext.ClientPairs.AsNoTracking()
.Include(u => u.User)
Expand Down Expand Up @@ -102,23 +102,23 @@ public async Task<List<ClientPairDto>> GetPairedClients()

public override async Task OnDisconnectedAsync(Exception exception)
{
var user = DbContext.Users.AsNoTracking().SingleOrDefault(u => u.UID == AuthenticatedUserId);
var user = await DbContext.Users.AsNoTracking().SingleOrDefaultAsync(u => u.UID == AuthenticatedUserId);
if (user != null && !string.IsNullOrEmpty(user.CharacterIdentification))
{
Logger.LogInformation("Disconnect from " + AuthenticatedUserId);

var otherUsers = DbContext.ClientPairs.AsNoTracking()
var otherUsers = await DbContext.ClientPairs.AsNoTracking()
.Include(u => u.User)
.Include(u => u.OtherUser)
.Where(w => w.User.UID == user.UID && !w.IsPaused)
.Where(w => !string.IsNullOrEmpty(w.OtherUser.CharacterIdentification))
.Select(e => e.OtherUser).ToList();
var otherEntries = DbContext.ClientPairs.AsNoTracking().Include(u => u.User)
.Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser.UID == user.UID && !u.IsPaused).ToList();
.Select(e => e.OtherUser).ToListAsync();
var otherEntries = await DbContext.ClientPairs.AsNoTracking().Include(u => u.User)
.Where(u => otherUsers.Any(e => e == u.User) && u.OtherUser.UID == user.UID && !u.IsPaused).ToListAsync();
await Clients.Users(otherEntries.Select(e => e.User.UID)).SendAsync(UserHubAPI.OnRemoveOnlinePairedPlayer, user.CharacterIdentification);


DbContext.Users.Single(u => u.UID == AuthenticatedUserId).CharacterIdentification = null;
(await DbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId)).CharacterIdentification = null;
await DbContext.SaveChangesAsync();

await Clients.All.SendAsync("UsersOnline",
Expand All @@ -134,19 +134,19 @@ public async Task PushCharacterDataToVisibleClients(CharacterCacheDto characterC
{
Logger.LogInformation("User " + AuthenticatedUserId + " pushing character data to " + visibleCharacterIds.Count + " visible clients");

var user = GetAuthenticatedUserUntracked();
var senderPairedUsers = DbContext.ClientPairs.AsNoTracking()
var user = await GetAuthenticatedUserUntrackedAsync();
var senderPairedUsers = await DbContext.ClientPairs.AsNoTracking()
.Include(w => w.User)
.Include(w => w.OtherUser)
.Where(w => w.User.UID == user.UID && !w.IsPaused
&& visibleCharacterIds.Contains(w.OtherUser.CharacterIdentification))
.Select(u => u.OtherUser).ToList();
.Select(u => u.OtherUser).ToListAsync();

foreach (var pairedUser in senderPairedUsers)
{
var isPaused = DbContext.ClientPairs.AsNoTracking()
.FirstOrDefault(w =>
w.User.UID == pairedUser.UID && w.OtherUser.UID == user.UID)?.IsPaused ?? true;
var isPaused = (await DbContext.ClientPairs.AsNoTracking()
.FirstOrDefaultAsync(w =>
w.User.UID == pairedUser.UID && w.OtherUser.UID == user.UID))?.IsPaused ?? true;
if (isPaused) continue;
await Clients.User(pairedUser.UID).SendAsync(UserHubAPI.OnReceiveCharacterData, characterCache,
user.CharacterIdentification);
Expand Down Expand Up @@ -194,13 +194,13 @@ public async Task SendPairedClientAddition(string uid)
{
if (uid == AuthenticatedUserId) return;
uid = uid.Trim();
var user = DbContext.Users.Single(u => u.UID == AuthenticatedUserId);
var user = await DbContext.Users.SingleAsync(u => u.UID == AuthenticatedUserId);

var otherUser = await DbContext.Users
.SingleOrDefaultAsync(u => u.UID == uid);
var existingEntry =
await DbContext.ClientPairs.AsNoTracking()
.SingleOrDefaultAsync(p =>
.FirstOrDefaultAsync(p =>
p.User.UID == AuthenticatedUserId && p.OtherUser.UID == uid);
if (otherUser == null || existingEntry != null) return;
Logger.LogInformation("User " + AuthenticatedUserId + " adding " + uid + " to whitelist");
Expand All @@ -218,19 +218,11 @@ await Clients.User(user.UID)
{
OtherUID = otherUser.UID,
IsPaused = false,
IsPausedFromOthers = false,
IsPausedFromOthers = otherEntry?.IsPaused ?? false,
IsSynced = otherEntry != null
}, string.Empty);
if (otherEntry != null)
{
if (!string.IsNullOrEmpty(otherUser.CharacterIdentification))
{
await Clients.User(user.UID)
.SendAsync(UserHubAPI.OnAddOnlinePairedPlayer, otherUser.CharacterIdentification);
await Clients.User(otherUser.UID)
.SendAsync(UserHubAPI.OnAddOnlinePairedPlayer, user.CharacterIdentification);
}

await Clients.User(uid).SendAsync(UserHubAPI.OnUpdateClientPairs,
new ClientPairDto()
{
Expand All @@ -239,6 +231,14 @@ await Clients.User(uid).SendAsync(UserHubAPI.OnUpdateClientPairs,
IsPausedFromOthers = false,
IsSynced = true
}, user.CharacterIdentification);

if (!string.IsNullOrEmpty(otherUser.CharacterIdentification))
{
await Clients.User(user.UID)
.SendAsync(UserHubAPI.OnAddOnlinePairedPlayer, otherUser.CharacterIdentification);
await Clients.User(otherUser.UID)
.SendAsync(UserHubAPI.OnAddOnlinePairedPlayer, user.CharacterIdentification);
}
}
}

Expand All @@ -247,8 +247,8 @@ await Clients.User(uid).SendAsync(UserHubAPI.OnUpdateClientPairs,
public async Task SendPairedClientPauseChange(string uid, bool isPaused)
{
if (uid == AuthenticatedUserId) return;
var user = DbContext.Users.AsNoTracking()
.Single(u => u.UID == AuthenticatedUserId);
var user = await DbContext.Users.AsNoTracking()
.SingleAsync(u => u.UID == AuthenticatedUserId);
var otherUser = await DbContext.Users.AsNoTracking()
.SingleOrDefaultAsync(u => u.UID == uid);
if (otherUser == null) return;
Expand Down

0 comments on commit b8950ca

Please sign in to comment.