Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions NetCord/GuildUserRoles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace NetCord;

public readonly struct GuildUserRoles : IEnumerable<Role>
{
private readonly IReadOnlyList<ulong> _roleIds;
private readonly IReadOnlyDictionary<ulong, Role> _roles;

internal GuildUserRoles(IReadOnlyList<ulong> roleIds, IReadOnlyDictionary<ulong, Role> roles)
{
_roleIds = roleIds;
_roles = roles;
}

public Enumerator GetEnumerator() => new(_roleIds, _roles);

IEnumerator<Role> IEnumerable<Role>.GetEnumerator() => GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public struct Enumerator : IEnumerator<Role>
{
private readonly IReadOnlyList<ulong> _roleIds;
private readonly IReadOnlyDictionary<ulong, Role> _roles;
private int _position;

internal Enumerator(IReadOnlyList<ulong> roleIds, IReadOnlyDictionary<ulong, Role> roles)
{
_roleIds = roleIds;
_roles = roles;
}

[AllowNull]
public Role Current
{
readonly get
{
if (field is not { } current)
ThrowInvalidOperation();

return current;

[DoesNotReturn]
[StackTraceHidden]
static void ThrowInvalidOperation()
{
throw new InvalidOperationException("Enumerator is positioned before the first element of the collection or after the last element.");
}
}
private set;
}

readonly object IEnumerator.Current => Current;

public bool MoveNext()
{
int count = _roleIds.Count;

while (_position < count)
{
var roleId = _roleIds[_position++];

if (_roles.TryGetValue(roleId, out var role))
{
Current = role;
return true;
}
}

Current = null;
return false;
}

void IEnumerator.Reset()
{
_position = 0;
Current = null;
}

readonly void IDisposable.Dispose()
{
}
}
}
16 changes: 3 additions & 13 deletions NetCord/PartialGuildUserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,16 @@ namespace NetCord;
public static class PartialGuildUserExtensions
{
/// <summary>
/// Returns an <see cref="IEnumerable{Role}"/> object belonging to the <see cref="PartialGuildUser"/> by acquiring it from the specificied <see cref="RestGuild"/>.
/// Returns roles of the <see cref="PartialGuildUser"/> by acquiring them from the specificied <see cref="RestGuild"/>.
/// </summary>
/// <remarks>
/// IDs of roles that do not exist in the <see cref="RestGuild"/> will be ignored.
/// </remarks>
/// <param name="user">The <see cref="PartialGuildUser"/> to acquire roles for.</param>
/// <param name="guild">The <see cref="RestGuild"/> to acquire the roles from.</param>
public static IEnumerable<Role> GetRoles(this PartialGuildUser user, RestGuild guild)
public static GuildUserRoles GetRoles(this PartialGuildUser user, RestGuild guild)
{
var roles = guild.Roles;

var roleIds = user.RoleIds;

int count = roleIds.Count;

for (int i = 0; i < count; i++)
{
if (roles.TryGetValue(roleIds[i], out var role))
yield return role;
}
return new(user.RoleIds, guild.Roles);
}

/// <summary>
Expand Down
34 changes: 22 additions & 12 deletions NetCord/Rest/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,38 @@ public int Compare(PartialGuildUser? x, PartialGuildUser? y)
if (y is null)
return 1;

var (xId, yId) = (x.Id, y.Id);

if (xId == yId)
return 0;

var ownerId = OwnerId;
if (xId == ownerId)
return 1;

if (yId == ownerId)
if (x.Id == ownerId)
return y.Id == ownerId ? 0 : 1;

if (y.Id == ownerId)
return -1;

if (x.RoleIds.Count is 0)
return -y.RoleIds.Count;
using var xRoleEnumerator = x.GetRoles(this).GetEnumerator();

if (!xRoleEnumerator.MoveNext())
{
using var yRoleEnumerator = y.GetRoles(this).GetEnumerator();

var xHighestPosition = x.GetRoles(this).Max(r => r.Position);
return yRoleEnumerator.MoveNext() ? -1 : 0;
}

var xPosition = xRoleEnumerator.Current.Position;

while (xRoleEnumerator.MoveNext())
{
var currentPosition = xRoleEnumerator.Current.Position;

if (currentPosition > xPosition)
xPosition = currentPosition;
}

int result = 1;

foreach (var role in y.GetRoles(this))
{
var comparisonResult = xHighestPosition.CompareTo(role.Position);
int comparisonResult = xPosition.CompareTo(role.Position);

if (comparisonResult < 0)
return -1;
Expand Down
Loading