diff --git a/.gitignore b/.gitignore index ad9b26f..8f988d4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ artifacts/ *DS_Store *.sln.ide .idea +.vscode \ No newline at end of file diff --git a/API/src/Helper.cs b/API/src/Helper.cs index 10b8286..860d3f9 100644 --- a/API/src/Helper.cs +++ b/API/src/Helper.cs @@ -1,38 +1,429 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text.Json; -using System.Threading.Tasks; -using NWN.MasterList.Data; - -namespace NWN.MasterList { - public static class Helper { - public static string jsonUrl = "https://api.nwn.beamdog.net/v1/"; - - public static int MasterListPositionFromSessionName(this Client client, List servers, string sessionName) => - servers.Where(s => s.SessionName == sessionName).Select(self => servers.IndexOf(self)).FirstOrDefault(); - - public async static Task> GetAllFirstSeen(this Client client) { - var response = await client.GetServers(); - return response.OrderByDescending(x => x.FirstSeen); - } - public static async Task> GetServers(this Client client) { - string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers"); - return JsonSerializer.Deserialize>(response); - } - - public static async Task GetServer(this Client client, string publicKey) { - string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers{publicKey}"); - return JsonSerializer.Deserialize(response); - } - - public static async Task GetServer(this Client client, string ip, int port) { - string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers/{ip}/{port}"); - return JsonSerializer.Deserialize(response); - } - - public static async Task GetMe(this Client client) { - string response = await Client.HttpClient.GetStringAsync(jsonUrl); - return JsonSerializer.Deserialize(response); - } - } -} \ No newline at end of file +using System; +using System.Collections.Generic; +using System.Linq; +using NWN.MasterList.Data; + +namespace NWN.MasterList +{ + public static class Helper + { + public static int MasterListPositionFromSessionName(this IEnumerable servers, List nwServer, string sessionName) => + servers.Where(s => s.SessionName == sessionName).Select(self => nwServer.IndexOf(self)).FirstOrDefault(); + + public static IOrderedEnumerable GetAllBuild(this IEnumerable servers) => + servers.OrderBy(x => x.Build).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllBuildByType(this IEnumerable servers, string buildNumber) => + servers.Where(x => x.Build == buildNumber).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllConnectHint(this IEnumerable servers) => + servers.OrderBy(x => x.ConnectHint).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllCurrentPlayersByModuleName(this IEnumerable servers) => + servers.OrderByDescending(x => x.CurrentPlayers).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllElc(this IEnumerable servers) => + servers.OrderBy(x => x.ELC).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllElcByType(this IEnumerable servers, bool setting) => + servers.Where(x => x.ELC == setting).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllFirstSeen(this IEnumerable servers) + { + return servers.OrderBy(x => x.FirstSeen).ThenBy(x => x.ModuleName); + } + + public static IOrderedEnumerable GetAllGameType(this IEnumerable servers) => + servers.OrderBy(x => x.GameType).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllGameTypeByType(this IEnumerable servers, int gameType) => + servers.Where(x => x.GameType == gameType).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetILRAll(this IEnumerable servers) => + servers.OrderBy(x => x.ILR).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllIlrByType(this IEnumerable servers, bool ilr) => + servers.Where(x => x.ILR == ilr).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetIpsAll(this IEnumerable servers) => + servers.OrderBy(x => Version.Parse(x.IP)).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllServersByIp(this IEnumerable servers, string ip) => + servers.OrderBy(x => x.IP == ip).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllKxPk(this IEnumerable servers) => + servers.OrderBy(x => x.KxPk).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllServersByKxPk(this IEnumerable servers, string kxPkip) => + servers.Where(x => x.KxPk == kxPkip).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllLanguage(this IEnumerable servers) => + servers.OrderBy(x => x.Language).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllLanguageByType(this IEnumerable servers, int languageType) => + servers.Where(x => x.Language == languageType).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllLastAdvertisement(this IEnumerable servers) => + servers.OrderBy(x => x.LastAdvertisement).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllLatency(this IEnumerable servers) => + servers.OrderBy(x => x.Latency).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetLatencyGreaterThan(this IEnumerable servers, int latencyRate) => + servers.Where(x => x.Latency > latencyRate).OrderBy(x => x.Latency).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetLatencyLesserThan(this IEnumerable servers, int latencyRate) => + servers.Where(x => x.Latency < latencyRate).OrderBy(x => x.Latency).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetLatencyRange(this IEnumerable servers, int low = 0, int high = int.MaxValue) => + servers.Where(x => x.Latency >= low && x.Latency <= high).OrderBy(x => x.Latency).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetMaxLevelGreaterThan(this IEnumerable servers, int levelMax) => + servers.Where(x => x.MaxLevel > levelMax).OrderBy(x => x.MaxLevel).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetMaxLevelLesserThan(this IEnumerable servers, int levelMax) => + servers.Where(x => x.MaxLevel < levelMax).OrderBy(x => x.MaxLevel).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetMaxLevelRange(this IEnumerable servers, int low = 1, int high = int.MaxValue) => + servers.Where(x => x.MaxLevel >= low && x.MaxLevel <= high).OrderBy(x => x.MaxLevel).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetMinLevelGreaterThan(this IEnumerable servers, int levelMin) => + servers.Where(x => x.MinLevel > levelMin).OrderBy(x => x.MinLevel).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetMinLevelLesserThan(this IEnumerable servers, int levelMin) => + servers.Where(x => x.MinLevel < levelMin).OrderBy(x => x.MinLevel).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetMinLevelRange(this IEnumerable servers, int low = 1, int high = int.MaxValue) => + servers.Where(x => x.MinLevel >= low && x.MinLevel <= high).OrderBy(x => x.MinLevel).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllModuleDescription(this IEnumerable servers) => + servers.OrderBy(x => x.ModuleDescription).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllByModuleName(this IEnumerable servers) => + servers.OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllNwSyncUrls(this IEnumerable servers) + { + return servers.Where(x => !String.IsNullOrWhiteSpace(x.NwSync.URL)).OrderBy(x => x.NwSync.URL); + } + + // TODO #18 + /*public static IOrderedEnumerable GetAllNwSyncManifestHash(this IEnumerable servers) + { + return (servers.Select(x => x.NwSync.Manifests.Where(y => y.Hash != string.Empty))).OrderBy(x => x.ModuleName); + }*/ + + public static IOrderedEnumerable GetAllOneParty(this IEnumerable servers) => + servers.OrderBy(x => x.OneParty).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetOnePartyByType(this IEnumerable servers, bool oneParty) => + servers.Where(x => x.OneParty == oneParty).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllOS(this IEnumerable servers) => + servers.OrderBy(x => x.OS).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllOSByType(this IEnumerable servers, int osType) => + servers.Where(x => x.OS == osType).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllOsByTypePlayerDescending(this IEnumerable servers, int osType) => + servers.Where(x => x.OS == osType).OrderByDescending(x => x.CurrentPlayers).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllPassworded(this IEnumerable servers) => + servers.OrderBy(x => x.Passworded).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetPasswordedByType(this IEnumerable servers, bool password) => + servers.Where(x => x.Passworded == password).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllPlayerPause(this IEnumerable servers) => + servers.OrderBy(x => x.PlayerPause).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetPlayerPauseByType(this IEnumerable servers, bool pause) => + servers.Where(x => x.PlayerPause == pause).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetPortGreaterThan(this IEnumerable servers, int portNumber) => + servers.Where(x => x.Port > portNumber).OrderBy(x => x.Port).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetPortLesserThan(this IEnumerable servers, int portNumber) => + servers.Where(x => x.Port < portNumber).OrderBy(x => x.Port).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetPortRange(this IEnumerable servers, int low = 0, int high = 65535) => + servers.Where(x => x.Port >= low && x.Port <= high).OrderBy(x => x.Port).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllByPortNumber(this IEnumerable servers, int portNumber) => + servers.Where(x => x.Port == portNumber).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllPvp(this IEnumerable servers) => + servers.OrderBy(x => x.PVP).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllPvPByType(this IEnumerable servers, int pvptype) => + servers.Where(x => x.PVP == pvptype).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllRevision(this IEnumerable servers) => + servers.OrderBy(x => x.Revision).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllRevisionByType(this IEnumerable servers, int revisionType) => + servers.Where(x => x.Revision == revisionType).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllServerVault(this IEnumerable servers) => + servers.OrderBy(x => x.ServerVault).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllServerVaultByType(this IEnumerable servers, bool vaultType) => + servers.Where(x => x.ServerVault == vaultType).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllSessionName(this IEnumerable servers) => + servers.OrderBy(x => x.SessionName).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllSessionNameByType(this IEnumerable servers, string sessionName) => + servers.Where(x => x.SessionName == sessionName).OrderBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllSignPk(this IEnumerable servers) => + servers.OrderBy(x => x.SignPk).ThenBy(x => x.ModuleName); + + public static IOrderedEnumerable GetAllSignPkByType(this IEnumerable servers, string pk) => + servers.Where(x => x.SignPk == pk).OrderBy(x => x.ModuleName); + + public static int GetBuildCountByType(this IEnumerable servers, string buildType) => + servers.Count(x => x.Build == buildType); + + public static int GetElcCountByType(this IEnumerable servers, bool elc) => + servers.Count(x => x.ELC == elc); + + public static int GetTotalMaxPlayers(this IEnumerable servers) => + servers.Sum(x => x.MaxPlayers); + + public static int GetGameTypeCountByType(this IEnumerable servers, int gameType) => + servers.Count(x => x.GameType == gameType); + + public static int GetILRCountByType(this IEnumerable servers, bool ilr) => + servers.Count(x => x.ILR == ilr); + + public static int GetIPCountByType(this IEnumerable servers, string ip) => + servers.Count(x => x.IP == ip); + + public static int GetKxPkCountByType(this IEnumerable servers, string kxPkip) => + servers.Count(x => x.KxPk == kxPkip); + + public static int GetLanguageCountByType(this IEnumerable servers, int languageType) => + servers.Count(x => x.Language == languageType); + + public static int GetMaxLevelCountByType(this IEnumerable servers, int levelMax) => + servers.Count(x => x.MaxLevel == levelMax); + + public static int GetMinLevelCountByType(this IEnumerable servers, int levelMin) => + servers.Count(x => x.MinLevel == levelMin); + + public static int GetOnePartyCountByType(this IEnumerable servers, bool oneParty) => + servers.Count(x => x.OneParty == oneParty); + + public static int GetOsCountByType(this IEnumerable servers, int osType) => + servers.Count(x => x.OS == osType); + + public static int GetPasswordedCountByTyp(this IEnumerable servers, bool password) => + servers.Count(x => x.Passworded == password); + + public static int GetPlayerPauseCountByTye(this IEnumerable servers, bool pause) => + servers.Count(x => x.PlayerPause == pause); + + public static int GetPortCountByType(this IEnumerable servers, int port) => + servers.Count(x => x.Port == port); + + public static int GetPVPCountByType(this IEnumerable servers, int pvptype) => + servers.Count(x => x.PVP == pvptype); + + public static int GetRevisionCountByType(this IEnumerable servers, int revisionType) => + servers.Count(x => x.Revision == revisionType); + + public static int GetServerVaultCountByType(this IEnumerable servers, bool vaultType) => + servers.Count(x => x.ServerVault == vaultType); + + public static int GetSessionNameCountByTye(this IEnumerable servers, string sessionName) => + servers.Count(x => x.SessionName == sessionName); + + public static int GetSignPkCountByType(this IEnumerable servers, string pk) => + servers.Count(x => x.SignPk == pk); + + public static int GetTotalPlayerCount(this IEnumerable servers) => + servers.Sum(x => x.CurrentPlayers); + + public static int GetAveragePlayerCountPeServer(this IEnumerable servers) => + (int)(servers.Average(x => x.CurrentPlayers)); + + public static bool IsAllSameBuild(this IOrderedEnumerable servers, string buildNumber) => + servers.All(x => x.Build.Equals(buildNumber)); + + public static bool IsAllSameElc(this IOrderedEnumerable servers, bool elc) => + servers.All(x => x.ELC.Equals(elc)); + + public static bool IsAllSameGameType(this IOrderedEnumerable servers, int gameType) => + servers.All(x => x.GameType.Equals(gameType)); + + public static bool IsAllSameILR(this IOrderedEnumerable servers, bool ilr) => + servers.All(x => x.ILR.Equals(ilr)); + + public static bool IsAllSameIp(this IOrderedEnumerable servers, string ip) => + servers.All(x => x.IP.Equals(ip)); + + public static bool IsAllSameKxPk(this IOrderedEnumerable servers, string kxPkip) => + servers.All(x => x.KxPk.Equals(kxPkip)); + + public static bool IsAllSameLanguage(this IOrderedEnumerable servers, int languageType) => + servers.All(x => x.Language.Equals(languageType)); + + public static bool IsAllSameMaxLevel(this IOrderedEnumerable servers, int levelMax) => + servers.All(x => x.MaxLevel.Equals(levelMax)); + + public static bool IsAllSameMaxPlayers(this IOrderedEnumerable servers, int players) => + servers.All(x => x.MaxPlayers.Equals(players)); + + public static bool IsAllSameMinLevel(this IOrderedEnumerable servers, int players) => + servers.All(x => x.MinLevel.Equals(players)); + + public static bool IsAllSameModuleName(this IOrderedEnumerable servers, string name) => + servers.All(x => x.ModuleName.Equals(name)); + + public static bool IsAllSameOneParty(this IOrderedEnumerable servers, bool oneParty) => + servers.All(x => x.OneParty.Equals(oneParty)); + + public static bool IsAllSameOS(this IOrderedEnumerable servers, int osType) => + servers.All(x => x.OS.Equals(osType)); + + public static bool IsAllSamePassworded(this IOrderedEnumerable servers, bool password) => + servers.All(x => x.Passworded.Equals(password)); + + public static bool IsAllSamePlayerPause(this IOrderedEnumerable servers, bool pause) => + servers.All(x => x.PlayerPause.Equals(pause)); + + public static bool IsAllSamePort(this IOrderedEnumerable servers, int port) => + servers.All(x => x.Port.Equals(port)); + + public static bool IsAllSamePVP(this IOrderedEnumerable servers, int pvptype) => + servers.All(x => x.PVP.Equals(pvptype)); + + public static bool IsAllSameRevision(this IOrderedEnumerable servers, int revisionType) => + servers.All(x => x.Revision.Equals(revisionType)); + + public static bool IsAllSameServerVault(this IOrderedEnumerable servers, bool vaultType) => + servers.All(x => x.ServerVault.Equals(vaultType)); + + public static bool IsAllSameSessionName(this IOrderedEnumerable servers, string sessionName) => + servers.All(x => x.SessionName.Equals(sessionName)); + + public static bool IsAllSameSignPk(this IOrderedEnumerable servers, string sign) => + servers.All(x => x.SignPk.Equals(sign)); + + public static bool IsAnySameBuild(this IOrderedEnumerable servers, string buildNumber) => + servers.Any(x => x.Build.Equals(buildNumber)); + + public static bool IsAnySameElc(this IOrderedEnumerable servers, bool elc) => + servers.Any(x => x.ELC.Equals(elc)); + + public static bool IsAnySameGameType(this IOrderedEnumerable servers, int gameType) => + servers.Any(x => x.GameType.Equals(gameType)); + + public static bool IsAnySameILR(this IOrderedEnumerable servers, bool ilr) => + servers.Any(x => x.ILR.Equals(ilr)); + + public static bool IsAnySameIp(this IOrderedEnumerable servers, string ip) => + servers.Any(x => x.IP.Equals(ip)); + + public static bool IsAnySameKxPk(this IOrderedEnumerable servers, string kxPkip) => + servers.Any(x => x.KxPk.Equals(kxPkip)); + + public static bool IsAnySameLanguage(this IOrderedEnumerable servers, int languageType) => + servers.Any(x => x.Language.Equals(languageType)); + + public static bool IsAnySameMaxLevel(this IOrderedEnumerable servers, int levelMax) => + servers.Any(x => x.MaxLevel.Equals(levelMax)); + + public static bool IsAnySameMaxPlayers(this IOrderedEnumerable servers, int players) => + servers.Any(x => x.MaxPlayers.Equals(players)); + + public static bool IsAnySameMinLevel(this IOrderedEnumerable servers, int players) => + servers.Any(x => x.MinLevel.Equals(players)); + + public static bool IsAnySameModuleName(this IOrderedEnumerable servers, string name) => + servers.Any(x => x.ModuleName.Equals(name)); + + public static bool IsAnySameOneParty(this IOrderedEnumerable servers, bool oneParty) => + servers.Any(x => x.OneParty.Equals(oneParty)); + + public static bool IsAnySameOS(this IOrderedEnumerable servers, int osType) => + servers.Any(x => x.OS.Equals(osType)); + + public static bool IsAnySamePassworded(this IOrderedEnumerable servers, bool password) => + servers.Any(x => x.Passworded.Equals(password)); + + public static bool IsAnySamePlayerPause(this IOrderedEnumerable servers, bool pause) => + servers.Any(x => x.PlayerPause.Equals(pause)); + + public static bool IsAnySamePort(this IOrderedEnumerable servers, int port) => + servers.Any(x => x.Port.Equals(port)); + + public static bool IsAnySamePVP(this IOrderedEnumerable servers, int pvptype) => + servers.Any(x => x.PVP.Equals(pvptype)); + + public static bool IsAnySameRevision(this IOrderedEnumerable servers, int revisionType) => + servers.Any(x => x.Revision.Equals(revisionType)); + + public static bool IsAnySameServerVault(this IOrderedEnumerable servers, bool vaultType) => + servers.Any(x => x.ServerVault.Equals(vaultType)); + + public static bool IsAnySameSessionName(this IOrderedEnumerable servers, string sessionName) => + servers.Any(x => x.SessionName.Equals(sessionName)); + + public static bool IsAnySameSignPk(this IOrderedEnumerable servers, string sign) => + servers.Any(x => x.SignPk.Equals(sign)); + + public static int GetAverageCurrentPlayer(this IEnumerable servers) => + (int)(servers.Average(x => x.CurrentPlayers)); + + public static int GetAverageLatency(this IEnumerable servers) => + (int)(servers.Average(x => x.Latency)); + + public static int GetAverageMaxLevel(this IEnumerable servers) => + (int)(servers.Average(x => x.MaxLevel)); + public static int GetAverageMaxPlayers(this IEnumerable servers) => + (int)(servers.Average(x => x.MaxPlayers)); + + public static int GetAverageMinLevel(this IEnumerable servers) => + (int)(servers.Average(x => x.MinLevel)); + + public static List GetUniqueVersion(this IEnumerable servers) + { + IOrderedEnumerable collection = servers.OrderByDescending(x => x.Build).ThenByDescending(x => x.Revision); + List data = new List(); + + foreach (NwServer server in collection) + { + string temp = $"{server.Build}:{server.Revision}"; + + if (!data.Contains(temp)) + { + data.Add(temp); + } + } + return data; + } + + public static Dictionary GetUniqueVersionCount(this IEnumerable servers) + { + IOrderedEnumerable collection = servers.OrderByDescending(x => x.Build).ThenByDescending(x => x.Revision); + Dictionary data = new Dictionary(); + + foreach (NwServer server in collection) + { + string temp = $"{server.Build}:{server.Revision}"; + + if (data.ContainsKey(temp)) + { + data[temp]++; + } + else + { + data.Add(temp, 1); + } + } + return data; + } + } +} diff --git a/API/src/PrintJson.cs b/API/src/PrintJson.cs new file mode 100644 index 0000000..1f3f914 --- /dev/null +++ b/API/src/PrintJson.cs @@ -0,0 +1,22 @@ +using System.Text.Json; +namespace NWN.MasterList +{ + public static class PrintJson + { + // TODO #15 + private const string path = @""; + + //https://stackoverflow.com/a/63560258/11986604 + private static string PrettyJson(string unPrettyJson) + { + var options = new JsonSerializerOptions() + { + WriteIndented = true + }; + + var jsonElement = JsonSerializer.Deserialize(unPrettyJson); + + return JsonSerializer.Serialize(jsonElement, options); + } + } +} \ No newline at end of file diff --git a/API/src/Server.cs b/API/src/Server.cs new file mode 100644 index 0000000..8529743 --- /dev/null +++ b/API/src/Server.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Threading.Tasks; +using NWN.MasterList.Data; + +namespace NWN.MasterList +{ + public static class Server + { + public static string jsonUrl = "https://api.nwn.beamdog.net/v1/"; + + public static async Task> GetServers(this Client client) + { + string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers"); + return JsonSerializer.Deserialize>(response); + } + + public static async Task GetServer(this Client client, string publicKey) + { + string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers{publicKey}"); + return JsonSerializer.Deserialize(response); + } + public static async Task GetServer(this Client client, string ip, int port) + { + string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers/{ip}/{port}"); + return JsonSerializer.Deserialize(response); + } + + public static async Task GetMe(this Client client) + { + string response = await Client.HttpClient.GetStringAsync(jsonUrl); + return JsonSerializer.Deserialize(response); + } + + public static DateTime FromUnixTime(this long unixTime) + { + var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + return epoch.AddSeconds(unixTime); + } + + public static string ParsePvP(int pvp) + { + switch (pvp) + { + case 0: return "None"; + case 1: return "Party"; + case 2: return "Full"; + default: throw new ArgumentOutOfRangeException(); + } + } + + public static string ParseOS(int os) + { + switch (os) + { + case 0: return "Invalid"; + case 1: return "Windows(x86)"; + case 2: return "Windows(x64)"; + case 10: return "Linux(x86)"; + case 11: return "Linux(x64)"; + case 12: return "Linux(arm32)"; + case 13: return "Linux(arm64)"; + case 20: return "Mac(x86)"; + case 21: return "Mac(x64)"; + case 30: return "IOS"; + case 40: return "Android(arm32)"; + case 41: return "Android(rm64)"; + case 42: return "Android(x64)"; + case 50: return "Nintendo Switch"; + case 60: return "Xbox One"; + case 70: return "PS4"; + default: throw new ArgumentOutOfRangeException(); + } + } + + public static string ParseLanguage(int language) + { + switch (language) + { + case 0: return "English"; + case 1: return "French"; + case 2: return "German"; + case 3: return "Italian"; + case 4: return "Spanish"; + case 5: return "Polish"; + case 128: return "Korean"; + case 129: return "Chinese (traditional)"; + case 130: return "Chinese(simplified)"; + case 131: return "Japanese"; + default: throw new ArgumentOutOfRangeException(); + } + } + + public static string ParseGameType(int gameType) + { + switch (gameType) + { + case 0: return "Action"; + case 1: return "Story"; + case 2: return "Story Lite"; + case 3: return "Roleplay"; + case 4: return "Team"; + case 5: return "Melee"; + case 6: return "Arena"; + case 7: return "Social"; + case 8: return "Alternative"; + case 9: return "PW Action"; + case 10: return "PW Story"; + case 11: return "Solo"; + case 12: return "Tech Support"; + default: throw new ArgumentOutOfRangeException(); + } + } + } +} \ No newline at end of file diff --git a/Test/src/Client.Test.cs b/Test/src/Client.Test.cs index c0131b3..b27aeb5 100644 --- a/Test/src/Client.Test.cs +++ b/Test/src/Client.Test.cs @@ -1,21 +1,635 @@ -using Xunit; - -namespace NWN.MasterList.Test -{ - public class Client - { - [Fact] - public async void GetServersTest() - { - var connection = new MasterList.Client(); - Assert.NotEmpty(await connection.GetServers()); - } - - [Fact] - public async void TestGetAllFirstSeen() - { - var connection = new MasterList.Client(); - Assert.NotEmpty(await connection.GetAllFirstSeen()); - } - } -} \ No newline at end of file +using System.Collections.Generic; +using System.Linq; +using Xunit; +using Xunit.Abstractions; + +namespace NWN.MasterList.Test +{ + public class Client + { + private readonly ITestOutputHelper output; + + public Client(ITestOutputHelper output) => this.output = output; + + [Fact] + public async void GetServersTest() + { + var connection = new MasterList.Client(); + var collection = await connection.GetServers(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllFirstSeen() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.FirstSeen}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllLastAdvertisement() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.LastAdvertisement}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllBuild() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Build}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllBuildByType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Build}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllConnectionHint() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ConnectHint}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllCurrentPlayersByModuleName() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.CurrentPlayers}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetTotalPlayerCount() + { + var testB = 0; + var testA = (await new MasterList.Client().GetServers()).GetTotalPlayerCount(); + + foreach (var item in await new MasterList.Client().GetServers()) + { + testB += item.CurrentPlayers; + } + + Assert.Equal(testA, testB); + } + + [Fact] + public async void TestGetAllElC() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ELC}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllElcByType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ELC}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllGameType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.GameType}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllGameTypeByType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllGameTypeByType(10); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.GameType}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetIRLAll() + { + var collection = (await new MasterList.Client().GetServers()).GetILRAll(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ILR}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllIrlByType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllIlrByType(true); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ILR}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllServersByIp() + { + // A Carpathian Nightmare + var collection = (await new MasterList.Client().GetServers()).GetAllServersByIp("68.183.104.164"); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.IP}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllKxPk() + { + var collection = (await new MasterList.Client().GetServers()).GetAllKxPk(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.KxPk}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllServersByKxPk() + { + // A Carpathian Nightmare + var collection = (await new MasterList.Client().GetServers()).GetAllServersByKxPk("6FttLhv6ICT0EEIQC8DnBnZeGJUzTZE/iGbsXsdhrwI="); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.KxPk}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllLanguage() + { + var collection = (await new MasterList.Client().GetServers()).GetAllLanguage(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Language}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllLanguageByType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllLanguageByType(0); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Language}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllLatency() + { + var collection = (await new MasterList.Client().GetServers()).GetAllLatency(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Latency}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetLatencyGreaterThan() + { + var collection = (await new MasterList.Client().GetServers()).GetLatencyGreaterThan(200); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Latency}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetLatencyRange() + { + var collection = (await new MasterList.Client().GetServers()).GetLatencyRange(100, 200); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Latency}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetMaxLevelGreaterThan() + { + var collection = (await new MasterList.Client().GetServers()).GetMaxLevelGreaterThan(20); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.MaxLevel}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetMaxLevelRange() + { + var collection = (await new MasterList.Client().GetServers()).GetMaxLevelRange(20, 40); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.MaxLevel}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllModuleDescription() + { + var collection = (await new MasterList.Client().GetServers()).GetAllModuleDescription(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ModuleDescription}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllByModuleName() + { + var collection = (await new MasterList.Client().GetServers()).GetAllByModuleName(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.ModuleName}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllNwSyncUrls() + { + var collection = (await new MasterList.Client().GetServers()).GetAllNwSyncUrls(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.NwSync.URL}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + /*[Fact] + public async void TestGetAllNwSyncManifestHash() + { + var collection = (await new MasterList.Client().GetServers()).GetAllNwSyncManifestHash(); + + foreach (var item in collection) + { + //output.WriteLine($"{item.ModuleName} -> {item.NwSync.Manifests}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + }*/ + + [Fact] + public async void TestGetAllOneParty() + { + var collection = (await new MasterList.Client().GetServers()).GetAllOneParty(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.OneParty}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetOnePartyByType() + { + var collection = (await new MasterList.Client().GetServers()).GetOnePartyByType(true); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.OneParty}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllOS() + { + var collection = (await new MasterList.Client().GetServers()).GetAllOS(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.OS}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllOSByType() + { + var collection = (await new MasterList.Client().GetServers()).GetAllOSByType(1); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.OS}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllPassworded() + { + var collection = (await new MasterList.Client().GetServers()).GetAllPassworded(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Passworded}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetPasswordedByType() + { + var collection = (await new MasterList.Client().GetServers()).GetPasswordedByType(true); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Passworded}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllPlayerPause() + { + var collection = (await new MasterList.Client().GetServers()).GetAllPlayerPause(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.PlayerPause}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetPlayerPauseByType() + { + var collection = (await new MasterList.Client().GetServers()).GetPlayerPauseByType(true); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.PlayerPause}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllByPortNumber() + { + var collection = (await new MasterList.Client().GetServers()).GetAllByPortNumber(5121); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.Port}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllSignPk() + { + var collection = (await new MasterList.Client().GetServers()).GetAllSignPk(); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.SignPk}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllSignPkByType() + { + // Amia + var collection = (await new MasterList.Client().GetServers()).GetAllSignPkByType("1r7bU9BtpiAmiWLQz7xaCuZaLf3lQE8mn4T675u8HDQ="); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.SignPk}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAllOsByTypePlayerDescending() + { + var collection = (await new MasterList.Client().GetServers()).GetAllOsByTypePlayerDescending(1); + + foreach (var item in collection) + { + output.WriteLine($"{item.ModuleName} -> {item.OS} -> {item.CurrentPlayers}"); + } + + Assert.NotEmpty(collection); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetAverageLatency() + { + var collection = (await new MasterList.Client().GetServers()).GetAverageLatency(); + output.WriteLine($"{collection}"); + Assert.NotEqual(-1, collection); + } + + [Fact] + public async void GetBuildCountByBuildType() + { + var collection = (await new MasterList.Client().GetServers()).GetBuildCountByType("8193"); + output.WriteLine($"{collection}"); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetElcCountByType() + { + var collection = (await new MasterList.Client().GetServers()).GetElcCountByType(true); + output.WriteLine($"{collection}"); + Assert.NotEqual(-1, collection); + } + + [Fact] + public async void TestFromUnixTime() + { + var collection = (await new MasterList.Client().GetServers()).GetAllFirstSeen(); + var firstSeen = ((long)collection.FirstOrDefault().FirstSeen).FromUnixTime(); + var lastAdvertisement = ((long)collection.FirstOrDefault().LastAdvertisement).FromUnixTime(); + output.WriteLine($"FirstSeen -> {firstSeen}"); + output.WriteLine($"Last Advertisement -> {lastAdvertisement}"); + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetUniqueVersions() + { + var collection = (await new MasterList.Client().GetServers()).GetUniqueVersion(); + foreach (var item in collection) + { + output.WriteLine(item); + } + Assert.NotNull(collection); + } + + [Fact] + public async void TestGetUniqueVersionCount() + { + Dictionary collection = (await new MasterList.Client().GetServers()).GetUniqueVersionCount(); + foreach (KeyValuePair server in collection) + { + output.WriteLine($"{server.Key} -> {server.Value}"); + } + Assert.NotNull(collection); + } + } +}