101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
using PluginLoader.GUI;
|
|
using PluginLoader.Stats.Model;
|
|
using PluginLoader.Tools;
|
|
|
|
namespace PluginLoader.Stats;
|
|
|
|
public static class StatsClient
|
|
{
|
|
// API address
|
|
private static string baseUri = "https://pluginstats.ferenczi.eu";
|
|
private static string playerHash;
|
|
|
|
// Latest voting token received
|
|
private static string votingToken;
|
|
|
|
// API endpoints
|
|
private static string ConsentUri => $"{baseUri}/Consent";
|
|
private static string StatsUri => $"{baseUri}/Stats";
|
|
private static string TrackUri => $"{baseUri}/Track";
|
|
private static string VoteUri => $"{baseUri}/Vote";
|
|
|
|
// Hashed Steam ID of the player
|
|
private static string PlayerHash =>
|
|
playerHash ??= Tools.Tools.Sha1HexDigest($"{Tools.Tools.GetSteamId()}").Substring(0, 20);
|
|
|
|
public static void OverrideBaseUrl(string uri)
|
|
{
|
|
if (string.IsNullOrEmpty(uri))
|
|
return;
|
|
|
|
baseUri = uri;
|
|
}
|
|
|
|
public static bool Consent(bool consent)
|
|
{
|
|
if (consent)
|
|
LogFile.Log.Debug("Registering player consent on the statistics server");
|
|
else
|
|
LogFile.Log.Debug("Withdrawing player consent, removing user data from the statistics server");
|
|
|
|
var consentRequest = new ConsentRequest
|
|
{
|
|
PlayerHash = PlayerHash,
|
|
Consent = consent
|
|
};
|
|
|
|
return SimpleHttpClient.Post(ConsentUri, consentRequest);
|
|
}
|
|
|
|
// This function may be called from another thread.
|
|
public static PluginStats DownloadStats()
|
|
{
|
|
if (!PlayerConsent.ConsentGiven)
|
|
{
|
|
LogFile.Log.Info("Downloading plugin statistics anonymously...");
|
|
votingToken = null;
|
|
return SimpleHttpClient.Get<PluginStats>(StatsUri);
|
|
}
|
|
|
|
LogFile.Log.Info("Downloading plugin statistics, ratings and votes for " + PlayerHash);
|
|
|
|
var parameters = new Dictionary<string, string> { ["playerHash"] = PlayerHash };
|
|
var pluginStats = SimpleHttpClient.Get<PluginStats>(StatsUri, parameters);
|
|
|
|
votingToken = pluginStats?.VotingToken;
|
|
|
|
return pluginStats;
|
|
}
|
|
|
|
public static bool Track(string[] pluginIds)
|
|
{
|
|
var trackRequest = new TrackRequest
|
|
{
|
|
PlayerHash = PlayerHash,
|
|
EnabledPluginIds = pluginIds
|
|
};
|
|
|
|
return SimpleHttpClient.Post(TrackUri, trackRequest);
|
|
}
|
|
|
|
public static PluginStat Vote(string pluginId, int vote)
|
|
{
|
|
if (votingToken == null)
|
|
{
|
|
LogFile.Log.Debug("Voting token is not available, cannot vote");
|
|
return null;
|
|
}
|
|
|
|
LogFile.Log.Debug($"Voting {vote} on plugin {pluginId}");
|
|
var voteRequest = new VoteRequest
|
|
{
|
|
PlayerHash = PlayerHash,
|
|
PluginId = pluginId,
|
|
VotingToken = votingToken,
|
|
Vote = vote
|
|
};
|
|
|
|
var stat = SimpleHttpClient.Post<PluginStat, VoteRequest>(VoteUri, voteRequest);
|
|
return stat;
|
|
}
|
|
} |