Skip to content

Update SteamStatisticFactory.cs #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
68 changes: 35 additions & 33 deletions src/SAM/Stats/Factories/SteamStatisticFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,46 @@ public static class SteamStatisticFactory
/// </returns>
/// <exception cref="ArgumentNullException">Occurs when the <paramref name="client"/>, <paramref name="stat"/>, or the <paramref name="stat"/> <see cref="StatInfoBase.Id"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Occurs when an unkown <paramref name="stat"/> type is supplied.</exception>
public static SteamStatisticBase CreateStat(Client client, StatInfoBase stat)
public static SteamStatisticBase CreateStat(Client client, StatInfoBase stat)
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix this indentation, please?

if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrEmpty(stat?.Id)) throw new ArgumentNullException(nameof(stat));

switch (stat)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrEmpty(stat?.Id)) throw new ArgumentNullException(nameof(stat));
// STAT_INT
case IntegerStatInfo intDefinition:
{
if (!client.SteamUserStats.GetStatValue(intDefinition.Id, out int value)) break;

switch (stat)
intDefinition.Value = value;

return new IntegerSteamStatistic(intDefinition);
}
// STAT_AVGRATE
case AvgRateStatInfo avgRateDefinition:
{
// STAT_INT
case IntegerStatInfo intDefinition:
{
if (!client.SteamUserStats.GetStatValue(intDefinition.Id, out int value)) break;

intDefinition.Value = value;

return new IntegerSteamStatistic(intDefinition);
}
// STAT_AVGRATE
case AvgRateStatInfo avgRateDefinition:
{
if (!client.SteamUserStats.GetStatValue(avgRateDefinition.Id, out float value)) break;

avgRateDefinition.Value = value;

// NOTE: average rate stats are always treated as floats
return new FloatSteamStatistic(avgRateDefinition);
}
// STAT_FLOAT
case FloatStatInfo floatDefinition:
{
if (!client.SteamUserStats.GetStatValue(floatDefinition.Id, out float value)) break;

floatDefinition.Value = value;

return new FloatSteamStatistic(floatDefinition);
}
if (!client.SteamUserStats.GetStatValue(avgRateDefinition.Id, out float value)) break;

avgRateDefinition.Value = value;

// NOTE: average rate stats are always treated as floats
return new FloatSteamStatistic(avgRateDefinition);
}
// STAT_FLOAT
case FloatStatInfo floatDefinition:
{
if (!client.SteamUserStats.GetStatValue(floatDefinition.Id, out float value)) break;

throw new ArgumentOutOfRangeException(nameof(stat));
floatDefinition.Value = value;

return new FloatSteamStatistic(floatDefinition);
}
default:
// Log the type of the unhandled stat
Console.WriteLine($"Unhandled stat type: {stat.GetType()} with ID: {stat.Id}");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to log.Error(). I don't think this class has an ILogger yet, but you can add it at the top of the class. If you search for LogManager you'll find others.

private static readonly ILog log = LogManager.GetLogger(nameof(SteamStatisticFactory));

You'll need this namespace at the top too.

using log4net;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That GetType() is just the inherited .NET object's GetType method. It will just say it's a StatInfoBase. Logging the Id is good, we can at least look it up with that but IDs aren't unique across all apps. I would probably do this:

var sb = new StringBuilder();

sb.Append($"Unable to create user stat '{stat.Id}' in {client.AppId}. ");
sb.Append($"Stat '{stat.Id}' is type '{stat.UserStatType}' ({stat.UserStatType:D})");

// log the permissions flags if present
if (!string.IsNullOrEmpty(stat.Extra))
{
    sb.Append($" with permissions '{stat.Extra}'");
}

sb.Append(".");

log.Error(sb);

That should be enough info to figure out what's going on. I am guessing it's either a protected stat or something weird. I need to buy a game that has the issue so I can try and

break;
}

throw new ArgumentOutOfRangeException(nameof(stat), $"Unhandled stat type: {stat?.GetType()} with ID: {stat?.Id}");
}