1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-22 11:28:47 +00:00

refactor!: don't fetch user on join. fetch lazily

This commit is contained in:
Oliver Booth 2024-01-20 17:28:24 +00:00
parent 73ae7e365b
commit 63e775855a
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
4 changed files with 21 additions and 10 deletions

View File

@ -13,6 +13,7 @@ namespace VpSharp.Entities;
public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar> public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
{ {
private readonly VirtualParadiseClient _client; private readonly VirtualParadiseClient _client;
private VirtualParadiseUser? _user;
internal VirtualParadiseAvatar(VirtualParadiseClient client, int session) internal VirtualParadiseAvatar(VirtualParadiseClient client, int session)
{ {
@ -60,10 +61,10 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
public int Type { get; internal set; } public int Type { get; internal set; }
/// <summary> /// <summary>
/// Gets the user associated with this avatar. /// Gets the user ID associated with this avatar.
/// </summary> /// </summary>
/// <value>The user.</value> /// <value>The user ID.</value>
public VirtualParadiseUser User { get; internal set; } = null!; public int UserId { get; internal set; }
/// <summary> /// <summary>
/// Determines if two <see cref="VirtualParadiseAvatar" /> instances are equal. /// Determines if two <see cref="VirtualParadiseAvatar" /> instances are equal.
@ -150,7 +151,7 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
return true; return true;
} }
return Session == other.Session && User.Equals(other.User); return Session == other.Session && UserId.Equals(other.UserId);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -163,10 +164,20 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
public override int GetHashCode() public override int GetHashCode()
{ {
// ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable NonReadonlyMemberInGetHashCode
return HashCode.Combine(Session, User); return HashCode.Combine(Session, UserId);
// ReSharper restore NonReadonlyMemberInGetHashCode // ReSharper restore NonReadonlyMemberInGetHashCode
} }
/// <summary>
/// Gets the user associated with this avatar.
/// </summary>
/// <returns>The user.</returns>
public async Task<VirtualParadiseUser> GetUserAsync()
{
_user ??= await _client.GetUserAsync(UserId).ConfigureAwait(false);
return _user;
}
/// <summary> /// <summary>
/// Sends a console message to the avatar with no name. /// Sends a console message to the avatar with no name.
/// </summary> /// </summary>
@ -449,6 +460,6 @@ public sealed class VirtualParadiseAvatar : IEquatable<VirtualParadiseAvatar>
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {
return $"Avatar #{Session}; {User.Name} ({Name})"; return $"Avatar #{Session}; User #{UserId} ({Name})";
} }
} }

View File

@ -36,7 +36,7 @@ public sealed partial class VirtualParadiseClient
existing.Location = avatar.Location; existing.Location = avatar.Location;
existing.Application = avatar.Application; existing.Application = avatar.Application;
existing.Type = avatar.Type; existing.Type = avatar.Type;
existing.User = avatar.User; existing.UserId = avatar.UserId;
return existing; return existing;
}); });
} }
@ -62,7 +62,8 @@ public sealed partial class VirtualParadiseClient
{ {
Name = vp_string(sender, StringAttribute.AvatarName), Name = vp_string(sender, StringAttribute.AvatarName),
Location = new Location(CurrentWorld!, position, rotation), Location = new Location(CurrentWorld!, position, rotation),
Application = new Application(applicationName, applicationVersion) Application = new Application(applicationName, applicationVersion),
UserId = vp_int(sender, IntegerAttribute.UserId)
}; };
} }
} }

View File

@ -89,7 +89,6 @@ public sealed partial class VirtualParadiseClient
private void OnAvatarAddNativeEvent(nint sender) private void OnAvatarAddNativeEvent(nint sender)
{ {
VirtualParadiseAvatar avatar = ExtractAvatar(sender); VirtualParadiseAvatar avatar = ExtractAvatar(sender);
avatar.User = GetUserAsync(vp_int(sender, IntegerAttribute.UserId)).ConfigureAwait(false).GetAwaiter().GetResult();
avatar = AddOrUpdateAvatar(avatar); avatar = AddOrUpdateAvatar(avatar);
_avatarJoined.OnNext(avatar); _avatarJoined.OnNext(avatar);
} }

View File

@ -397,7 +397,7 @@ public sealed partial class VirtualParadiseClient : IDisposable
Application = _configuration.Application!, Application = _configuration.Application!,
Name = $"[{_configuration.BotName}]", Name = $"[{_configuration.BotName}]",
Location = new Location(world, Vector3d.Zero, Rotation.None), Location = new Location(world, Vector3d.Zero, Rotation.None),
User = CurrentUser! UserId = CurrentUser!.Id
}; };
if (CurrentWorld is not null) if (CurrentWorld is not null)