Přidejte soubory projektu.

master
Jan Beníček 2023-01-28 19:04:07 +01:00
parent e88e0d8699
commit c82247f383
6 changed files with 255 additions and 0 deletions

13
Lib_MCPlayerStats.csproj Normal file
View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
</Project>

25
Lib_MCPlayerStats.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib_MCPlayerStats", "Lib_MCPlayerStats.csproj", "{D09C0088-FF5D-42AA-A790-8415AB21E11A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D09C0088-FF5D-42AA-A790-8415AB21E11A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D09C0088-FF5D-42AA-A790-8415AB21E11A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D09C0088-FF5D-42AA-A790-8415AB21E11A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D09C0088-FF5D-42AA-A790-8415AB21E11A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5DCEC488-14AC-4868-A2F5-048BB8B934D8}
EndGlobalSection
EndGlobal

24
MojangApi.cs Normal file
View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lib_MCPlayerStats
{
internal class MojangApi
{
public string? id;
public string? name;
public Cape? properties;
}
internal class Cape
{
public string? name;
public string? value;
public string? signature;
}
}

40
Player_Stats.cs Normal file
View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lib_MCPlayerStats
{
public class Player_Stats
{
public SortedList<string, int>? picked_up;
public SortedList<string, int>? mined;
public SortedList<string, int>? crafted;
public SortedList<string, int>? broken;
public SortedList<string, int>? killed;
public SortedList<string, int>? dropped;
public SortedList<string, int>? custom;
public SortedList<string, int>? used;
public SortedList<string, int>? killed_by;
public string? Username;
}
}

103
PlayersStats.cs Normal file
View File

@ -0,0 +1,103 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace Lib_MCPlayerStats
{
public class PlayersStats
{
/// <summary>
/// Load player Stats from file
/// </summary>
/// <param name="file">File path</param>
/// <returns></returns>
public static Task<Player_Stats> LoadPlayerAsync(string file)
{
Player_Stats player = new();
if (!File.Exists(file))
{
player.Username = "File not Exist";
return Task.FromResult(player); //If Deserialized object is null return empty stats object with ErrorMessage in Username
}
Stats_Internal? loaded = JsonConvert.DeserializeObject<Stats_Internal>(File.ReadAllText(file)); //Desearialize Stats file to internal object
if (loaded == null)
{
player.Username = "Deserialized object is null";
return Task.FromResult(player); //If Deserialized object is null return empty stats object with ErrorMessage in Username
}
player.Username = GetNameByUUID(file.Split(@"/").Last().Replace("-", "").Replace(".json", ""));
//Move data from loaded stats to object with username
player.picked_up = loaded.stats.picked_up;
player.mined = loaded.stats.mined;
player.crafted = loaded.stats.crafted;
player.broken = loaded.stats.broken;
player.killed = loaded.stats.killed;
player.dropped = loaded.stats.dropped;
player.custom = loaded.stats.custom;
player.used = loaded.stats.used;
player.killed_by = loaded.stats.killed_by;
return Task.FromResult(player); //Return object with player Stats
}
/// <summary>
/// Load all player stats from files in folder
/// </summary>
/// <param name="folder">Folder path</param>
/// <returns></returns>
public static async Task<List<Player_Stats>> LoadPlayersAsync(string folder)
{
List<Player_Stats> stats = new();
foreach (string file in Directory.GetFiles(folder))
{
if (file.EndsWith(".json"))
{
stats.Add(await LoadPlayerAsync(file));
}
}
return stats;
}
/// <summary>
/// Get Username from UUID by Mojang Api
/// </summary>
/// <param name="UUID"></param>
/// <returns></returns>
private static string GetNameByUUID(string UUID)
{
HttpClient client = new();
MojangApi? data = null;
HttpResponseMessage response = client.GetAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{UUID}").Result;
if (response.IsSuccessStatusCode)
{
#pragma warning disable CS8604
data = JsonConvert.DeserializeObject<MojangApi>(response.Content.ToString());
#pragma warning restore CS8604
}
if (data == null)
return UUID;
#pragma warning disable CS8603
return data.name;
#pragma warning restore CS8603
}
}
}

50
Stats_Internal.cs Normal file
View File

@ -0,0 +1,50 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lib_MCPlayerStats
{
internal class Stats_Internal
{
#pragma warning disable CS8618 // Pole, které nemůže být null, musí při ukončování konstruktoru obsahovat hodnotu, která není null. Zvažte možnost deklarovat ho jako pole s možnou hodnotou null.
public Stats stats;
#pragma warning restore CS8618 // Pole, které nemůže být null, musí při ukončování konstruktoru obsahovat hodnotu, která není null. Zvažte možnost deklarovat ho jako pole s možnou hodnotou null.
public int DataVersion;
}
internal class Stats
{
public SortedList<string, int>? picked_up;
public SortedList<string, int>? mined;
public SortedList<string, int>? crafted;
public SortedList<string, int>? broken;
public SortedList<string, int>? killed;
public SortedList<string, int>? dropped;
public SortedList<string, int>? custom;
public SortedList<string, int>? used;
public SortedList<string, int>? killed_by;
}
}