embed plugin loader directly into the launcher
This commit is contained in:
101
PluginLoader/Stats/StatsClient.cs
Normal file
101
PluginLoader/Stats/StatsClient.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
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.WriteLine("Registering player consent on the statistics server");
|
||||
else
|
||||
LogFile.WriteLine("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.WriteGameLog("Downloading plugin statistics anonymously...");
|
||||
votingToken = null;
|
||||
return SimpleHttpClient.Get<PluginStats>(StatsUri);
|
||||
}
|
||||
|
||||
LogFile.WriteGameLog("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.WriteLine("Voting token is not available, cannot vote");
|
||||
return null;
|
||||
}
|
||||
|
||||
LogFile.WriteLine($"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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user