Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Feb 28, 2024
1 parent 59124fc commit 556f3fd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
21 changes: 9 additions & 12 deletions ArchiSteamFarm.OfficialPlugins.ItemsMatcher/RemoteCommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ internal async Task OnPersonaState(string? nickname = null, string? avatarHash =
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (asset.RealAppID, asset.Type, asset.Rarity);

if (state.TryGetValue(key, out Dictionary<ulong, uint>? set)) {
set[asset.ClassID] = set.TryGetValue(asset.ClassID, out uint amount) ? amount + asset.Amount : asset.Amount;
set[asset.ClassID] = set.GetValueOrDefault(asset.ClassID) + asset.Amount;
} else {
state[key] = new Dictionary<ulong, uint> { { asset.ClassID, asset.Amount } };
}
Expand Down Expand Up @@ -977,7 +977,7 @@ private async void MatchActively(object? state = null) {
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (asset.RealAppID, asset.Type, asset.Rarity);

if (setsState.TryGetValue(key, out Dictionary<ulong, uint>? set)) {
set[asset.ClassID] = set.TryGetValue(asset.ClassID, out uint amount) ? amount + asset.Amount : asset.Amount;
set[asset.ClassID] = set.GetValueOrDefault(asset.ClassID) + asset.Amount;
} else {
setsState[key] = new Dictionary<ulong, uint> { { asset.ClassID, asset.Amount } };
}
Expand Down Expand Up @@ -1331,15 +1331,16 @@ private async Task<bool> MatchActively(IReadOnlyCollection<ListedUser> listedUse
continue;
}

foreach ((ulong theirItem, uint theirTradableAmount) in theirTradableItems.OrderBy(item => ourFullSet.TryGetValue(item.Key, out uint ourAmountOfTheirItem) ? ourAmountOfTheirItem : 0)) {
foreach ((ulong theirItem, uint theirTradableAmount) in theirTradableItems.OrderBy(item => ourFullSet.GetValueOrDefault(item.Key))) {
if (ourFullSet.TryGetValue(theirItem, out uint ourAmountOfTheirItem) && (ourFullAmount <= ourAmountOfTheirItem + 1)) {
continue;
}

if (!listedUser.MatchEverything) {
// We have a potential match, let's check fairness for them
fairClassIDsToGive.TryGetValue(ourItem, out uint fairGivenAmount);
fairClassIDsToReceive.TryGetValue(theirItem, out uint fairReceivedAmount);
uint fairGivenAmount = fairClassIDsToGive.GetValueOrDefault(ourItem);
uint fairReceivedAmount = fairClassIDsToReceive.GetValueOrDefault(theirItem);

fairClassIDsToGive[ourItem] = ++fairGivenAmount;
fairClassIDsToReceive[theirItem] = ++fairReceivedAmount;

Expand Down Expand Up @@ -1373,11 +1374,11 @@ private async Task<bool> MatchActively(IReadOnlyCollection<ListedUser> listedUse
skippedSetsThisTrade.Add(set);

// Update our state based on given items
classIDsToGive[ourItem] = classIDsToGive.TryGetValue(ourItem, out uint ourGivenAmount) ? ourGivenAmount + 1 : 1;
classIDsToGive[ourItem] = classIDsToGive.GetValueOrDefault(ourItem) + 1;
ourFullSet[ourItem] = ourFullAmount - 1; // We don't need to remove anything here because we can guarantee that ourItem.Value is at least 2

// Update our state based on received items
classIDsToReceive[theirItem] = classIDsToReceive.TryGetValue(theirItem, out uint ourReceivedAmount) ? ourReceivedAmount + 1 : 1;
classIDsToReceive[theirItem] = classIDsToReceive.GetValueOrDefault(theirItem) + 1;
ourFullSet[theirItem] = ourAmountOfTheirItem + 1;

if (ourTradableAmount > 1) {
Expand Down Expand Up @@ -1515,11 +1516,7 @@ private async Task<bool> MatchActively(IReadOnlyCollection<ListedUser> listedUse
throw new InvalidOperationException(nameof(fullAmounts));
}

if (!fullAmounts.TryGetValue(itemToReceive.ClassID, out uint fullAmount)) {
fullAmount = 0;
}

fullAmounts[itemToReceive.ClassID] = itemToReceive.Amount + fullAmount;
fullAmounts[itemToReceive.ClassID] = fullAmounts.GetValueOrDefault(itemToReceive.ClassID) + itemToReceive.Amount;
}

skippedSetsThisUser.UnionWith(skippedSetsThisTrade);
Expand Down
6 changes: 3 additions & 3 deletions ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ internal static bool UnbanIP(IPAddress ipAddress) {
}

try {
bool hasFailedAuthorizations = FailedAuthorizations.TryGetValue(clientIP, out attempts);
attempts = FailedAuthorizations.GetValueOrDefault(clientIP);

if (hasFailedAuthorizations && (attempts >= MaxFailedAuthorizationAttempts)) {
if (attempts >= MaxFailedAuthorizationAttempts) {
return (HttpStatusCode.Forbidden, false);
}

if (!authorized) {
FailedAuthorizations[clientIP] = hasFailedAuthorizations ? ++attempts : (byte) 1;
FailedAuthorizations[clientIP] = ++attempts;
}
} finally {
AuthorizationTasks.TryRemove(clientIP, out _);
Expand Down
2 changes: 1 addition & 1 deletion ArchiSteamFarm/Steam/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ internal static string FormatBotResponse(string response, string botName) {
return (optimisticDiscovery ? appID : 0, DateTime.MinValue, true);
}

SteamApps.PICSRequest request = new(appID, tokenCallback.AppTokens.TryGetValue(appID, out ulong accessToken) ? accessToken : 0);
SteamApps.PICSRequest request = new(appID, tokenCallback.AppTokens.GetValueOrDefault(appID));

AsyncJobMultiple<SteamApps.PICSProductInfoCallback>.ResultSet? productInfoResultSet = null;

Expand Down
12 changes: 6 additions & 6 deletions ArchiSteamFarm/Steam/Exchange/Trading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public static bool IsFairExchange(IReadOnlyCollection<Asset> itemsToGive, IReadO

foreach (Asset item in itemsToGive) {
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
itemsToGiveAmounts[key] = itemsToGiveAmounts.TryGetValue(key, out uint amount) ? amount + item.Amount : item.Amount;
itemsToGiveAmounts[key] = itemsToGiveAmounts.GetValueOrDefault(key) + item.Amount;
}

Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity), uint> itemsToReceiveAmounts = new();

foreach (Asset item in itemsToReceive) {
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);
itemsToReceiveAmounts[key] = itemsToReceiveAmounts.TryGetValue(key, out uint amount) ? amount + item.Amount : item.Amount;
itemsToReceiveAmounts[key] = itemsToReceiveAmounts.GetValueOrDefault(key) + item.Amount;
}

// Ensure that amount of items to give is at least amount of items to receive (per all fairness factors)
Expand Down Expand Up @@ -210,7 +210,7 @@ internal static (Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rar
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);

if (fullState.TryGetValue(key, out Dictionary<ulong, uint>? fullSet)) {
fullSet[item.ClassID] = fullSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
fullSet[item.ClassID] = fullSet.GetValueOrDefault(item.ClassID) + item.Amount;
} else {
fullState[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
Expand All @@ -220,7 +220,7 @@ internal static (Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rar
}

if (tradableState.TryGetValue(key, out Dictionary<ulong, uint>? tradableSet)) {
tradableSet[item.ClassID] = tradableSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
tradableSet[item.ClassID] = tradableSet.GetValueOrDefault(item.ClassID) + item.Amount;
} else {
tradableState[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
Expand All @@ -240,7 +240,7 @@ internal static (Dictionary<(uint RealAppID, Asset.EType Type, Asset.ERarity Rar
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);

if (tradableState.TryGetValue(key, out Dictionary<ulong, uint>? tradableSet)) {
tradableSet[item.ClassID] = tradableSet.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
tradableSet[item.ClassID] = tradableSet.GetValueOrDefault(item.ClassID) + item.Amount;
} else {
tradableState[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
Expand Down Expand Up @@ -391,7 +391,7 @@ internal async Task OnNewTrade() {
(uint RealAppID, Asset.EType Type, Asset.ERarity Rarity) key = (item.RealAppID, item.Type, item.Rarity);

if (state.TryGetValue(key, out Dictionary<ulong, uint>? set)) {
set[item.ClassID] = set.TryGetValue(item.ClassID, out uint amount) ? amount + item.Amount : item.Amount;
set[item.ClassID] = set.GetValueOrDefault(item.ClassID) + item.Amount;
} else {
state[key] = new Dictionary<ulong, uint> { { item.ClassID, item.Amount } };
}
Expand Down
10 changes: 4 additions & 6 deletions ArchiSteamFarm/Steam/Interaction/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,14 +1906,12 @@ internal void OnNewLicenseList() {
return null;
}

Dictionary<string, (ushort Count, string GameName)> ownedGamesStats = new(StringComparer.Ordinal);
Dictionary<string, (ushort Count, string? GameName)> ownedGamesStats = new(StringComparer.Ordinal);

foreach ((string gameID, string gameName) in validResults.Where(static validResult => validResult.OwnedGames.Count > 0).SelectMany(static validResult => validResult.OwnedGames)) {
if (ownedGamesStats.TryGetValue(gameID, out (ushort Count, string GameName) ownedGameStats)) {
ownedGameStats.Count++;
} else {
ownedGameStats.Count = 1;
}
(ushort Count, string? GameName) ownedGameStats = ownedGamesStats.GetValueOrDefault(gameID);

ownedGameStats.Count++;

if (!string.IsNullOrEmpty(gameName)) {
ownedGameStats.GameName = gameName;
Expand Down

0 comments on commit 556f3fd

Please sign in to comment.