Skip to content

Commit

Permalink
fix: XboxGameAccountCollection return accounts in wrong order
Browse files Browse the repository at this point in the history
it should order accounts by LastAccess in descending order
  • Loading branch information
AlphaBs committed Dec 30, 2024
1 parent da2ac7c commit 0268ca8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<RepositoryUrl>https://github.com/CmlLib/CmlLib.Core.Auth.Microsoft</RepositoryUrl>
<Authors>ksi123456ab</Authors>
<PackageTags>CmlLib Minecraft Login Auth Authentication Microsoft Xbox Live XboxLive</PackageTags>
<Version>3.2.1</Version>
<Version>3.2.2</Version>
<PackageIcon>icon.png</PackageIcon>
<RepositoryType>git</RepositoryType>
<PackageId>CmlLib.Core.Auth.Microsoft</PackageId>
Expand Down
2 changes: 1 addition & 1 deletion src/XboxAuthNet.Game.Msal/XboxAuthNet.Game.Msal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<RepositoryUrl>https://github.com/AlphaBs/XboxAuthNet</RepositoryUrl>
<Authors>ksi123456ab</Authors>
<PackageTags>login authentication msal microsoft xbox game</PackageTags>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
<PackageIcon>icon.png</PackageIcon>
<RepositoryType>git</RepositoryType>
<PackageId>XboxAuthNet.Game.Msal</PackageId>
Expand Down
9 changes: 1 addition & 8 deletions src/XboxAuthNet.Game/Accounts/XboxGameAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ public int CompareTo(object? other)
if (other is not XboxGameAccount account)
return 1;

if (Equals(other))
return LastAccess.CompareTo(account.LastAccess);
else
{
var thisIdentifier = Identifier ?? "";
var otherIdentifier = account.Identifier ?? "";
return thisIdentifier.CompareTo(otherIdentifier);
}
return LastAccess.CompareTo(account.LastAccess);
}

public override bool Equals(object? obj)
Expand Down
4 changes: 2 additions & 2 deletions src/XboxAuthNet.Game/Accounts/XboxGameAccountCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ private IEnumerable<IXboxGameAccount> getAccounts()
{
// 1) Ignore accounts which has empty Identifier
// 2) Remove duplicated accounts which has same Identifier
// 2-1) If two accounts has same identifier, remove olds and take the most recent one
// 2-1) If two accounts has same identifier, remove old ones and take the most recent one
// 3) Order by the most recently accessed account

return _accounts
.Where(account => !string.IsNullOrEmpty(account.Identifier))
.GroupBy(account => account.Identifier)
.Select(group => group.OrderByDescending(_ => _).First())
.OrderBy(_ => _);
.OrderByDescending(_ => _);
}

public IXboxGameAccount GetAccount(string identifier)
Expand Down
2 changes: 1 addition & 1 deletion src/XboxAuthNet.Game/XboxAuthNet.Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<RepositoryUrl>https://github.com/CmlLib/CmlLib.Core.Auth.Microsoft</RepositoryUrl>
<Authors>ksi123456ab</Authors>
<PackageTags>microsoft xbox game auth</PackageTags>
<Version>1.3.1</Version>
<Version>1.3.2</Version>
<PackageIcon>icon.png</PackageIcon>
<RepositoryType>git</RepositoryType>
<PackageId>XboxAuthNet.Game</PackageId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public void TestTryGetAccountForNonExisting()
[Test]
public void TestEnumerate()
{
var account1 = TestAccount.Create("a", DateTime.MaxValue);
var account2 = TestAccount.Create("a", DateTime.MinValue);
var account3 = TestAccount.Create("b", DateTime.MinValue);
var account4 = TestAccount.Create("b", DateTime.MaxValue);
var account1 = TestAccount.Create("z", DateTime.MinValue.AddDays(8));
var account2 = TestAccount.Create("z", DateTime.MinValue.AddDays(1));
var account3 = TestAccount.Create("a", DateTime.MinValue.AddDays(3));
var account4 = TestAccount.Create("a", DateTime.MinValue.AddDays(6));
var collection = new XboxGameAccountCollection()
{
account1, account2, account3, account4
Expand All @@ -105,7 +105,8 @@ public void TestEnumerate()
IXboxGameAccount[] enumerated = collection.ToArray();
Assert.That(enumerated, Is.EqualTo(new IXboxGameAccount[]
{
account1, account4
account1, // account1 and account2 have a same identifier, choose more recent one
account4 // accounts are ordered by access time with descending order
}));
}
}

0 comments on commit 0268ca8

Please sign in to comment.