From 9d00d5a16a563c852b15a7152333bdf370e8caae Mon Sep 17 00:00:00 2001 From: RaidMax Date: Thu, 2 Aug 2018 20:52:35 -0500 Subject: [PATCH] removed event controller, and added status to api controller get time passed returns weeks after 90 days and months after 365 --- Application/Localization/IW4MAdmin.en-US.json | 6 +- Application/Localization/IW4MAdmin.es-EC.json | 6 +- Application/Localization/IW4MAdmin.pt-BR.json | 6 +- Application/Localization/IW4MAdmin.ru-RU.json | 6 +- Master/Master.pyproj | 2 +- Plugins/Stats/Web/Views/Stats/_List.cshtml | 2 +- SharedLibraryCore/Commands/NativeCommands.cs | 2 +- SharedLibraryCore/Database/DatabaseContext.cs | 9 +- SharedLibraryCore/Interfaces/IPageList.cs | 11 ++ SharedLibraryCore/Utilities.cs | 103 +++++++++++------- WebfrontCore/Controllers/API/APIController.cs | 46 +++++++- .../Controllers/API/EventController.cs | 18 --- .../Views/Client/Profile/Index.cshtml | 2 +- 13 files changed, 139 insertions(+), 80 deletions(-) create mode 100644 SharedLibraryCore/Interfaces/IPageList.cs delete mode 100644 WebfrontCore/Controllers/API/EventController.cs diff --git a/Application/Localization/IW4MAdmin.en-US.json b/Application/Localization/IW4MAdmin.en-US.json index 3e4883022..cabf6d3c4 100644 --- a/Application/Localization/IW4MAdmin.en-US.json +++ b/Application/Localization/IW4MAdmin.en-US.json @@ -114,11 +114,11 @@ "COMMANDS_WARNCLEAR_DESC": "remove all warnings for a client", "COMMANDS_WARNCLEAR_SUCCESS": "All warning cleared for", "COMMANDS_WHO_DESC": "give information about yourself", - "GLOBAL_DAYS": "days", + "GLOBAL_TIME_DAYS": "days", "GLOBAL_ERROR": "Error", - "GLOBAL_HOURS": "hours", + "GLOBAL_TIME_HOURS": "hours", "GLOBAL_INFO": "Info", - "GLOBAL_MINUTES": "minutes", + "GLOBAL_TIME_MINUTES": "minutes", "GLOBAL_REPORT": "If you suspect someone of ^5CHEATING ^7use the ^5!report ^7command", "GLOBAL_VERBOSE": "Verbose", "GLOBAL_WARNING": "Warning", diff --git a/Application/Localization/IW4MAdmin.es-EC.json b/Application/Localization/IW4MAdmin.es-EC.json index 38c32afae..441c54958 100644 --- a/Application/Localization/IW4MAdmin.es-EC.json +++ b/Application/Localization/IW4MAdmin.es-EC.json @@ -114,11 +114,11 @@ "COMMANDS_WARNCLEAR_DESC": "eliminar todas las advertencias de un cliente", "COMMANDS_WARNCLEAR_SUCCESS": "Todas las advertencias borradas para", "COMMANDS_WHO_DESC": "da información sobre ti", - "GLOBAL_DAYS": "días", + "GLOBAL_TIME_DAYS": "días", "GLOBAL_ERROR": "Error", - "GLOBAL_HOURS": "horas", + "GLOBAL_TIME_HOURS": "horas", "GLOBAL_INFO": "Información", - "GLOBAL_MINUTES": "minutos", + "GLOBAL_TIME_MINUTES": "minutos", "GLOBAL_REPORT": "Si sospechas que alguien ^5usa cheats ^7usa el comando ^5!report", "GLOBAL_VERBOSE": "Detallado", "GLOBAL_WARNING": "Advertencia", diff --git a/Application/Localization/IW4MAdmin.pt-BR.json b/Application/Localization/IW4MAdmin.pt-BR.json index 000055c05..dbab9ce51 100644 --- a/Application/Localization/IW4MAdmin.pt-BR.json +++ b/Application/Localization/IW4MAdmin.pt-BR.json @@ -114,11 +114,11 @@ "COMMANDS_WARNCLEAR_DESC": "remove todos os avisos para um cliente", "COMMANDS_WARNCLEAR_SUCCESS": "Todos as advertências foram apagados para", "COMMANDS_WHO_DESC": "dá informações sobre você", - "GLOBAL_DAYS": "dias", + "GLOBAL_TIME_DAYS": "dias", "GLOBAL_ERROR": "Erro", - "GLOBAL_HOURS": "horas", + "GLOBAL_TIME_HOURS": "horas", "GLOBAL_INFO": "Informação", - "GLOBAL_MINUTES": "minutos", + "GLOBAL_TIME_MINUTES": "minutos", "GLOBAL_REPORT": "Se você está suspeitando alguém de alguma ^5TRAPAÇA ^7use o comando ^5!report", "GLOBAL_VERBOSE": "Detalhe", "GLOBAL_WARNING": "AVISO", diff --git a/Application/Localization/IW4MAdmin.ru-RU.json b/Application/Localization/IW4MAdmin.ru-RU.json index 15818e7e5..a92b6d9e2 100644 --- a/Application/Localization/IW4MAdmin.ru-RU.json +++ b/Application/Localization/IW4MAdmin.ru-RU.json @@ -114,11 +114,11 @@ "COMMANDS_WARNCLEAR_DESC": "удалить все предупреждения у игрока", "COMMANDS_WARNCLEAR_SUCCESS": "Все предупреждения очищены у", "COMMANDS_WHO_DESC": "предоставить информацию о себе", - "GLOBAL_DAYS": "дней", + "GLOBAL_TIME_DAYS": "дней", "GLOBAL_ERROR": "Ошибка", - "GLOBAL_HOURS": "часов", + "GLOBAL_TIME_HOURS": "часов", "GLOBAL_INFO": "Информация", - "GLOBAL_MINUTES": "минут", + "GLOBAL_TIME_MINUTES": "минут", "GLOBAL_REPORT": "Если вы подозреваете кого-то в ^5ЧИТЕРСТВЕ^7, используйте команду ^5!report", "GLOBAL_VERBOSE": "Подробно", "GLOBAL_WARNING": "Предупреждение", diff --git a/Master/Master.pyproj b/Master/Master.pyproj index 5a74cdd4c..c2511de07 100644 --- a/Master/Master.pyproj +++ b/Master/Master.pyproj @@ -125,7 +125,7 @@ X64 - + diff --git a/Plugins/Stats/Web/Views/Stats/_List.cshtml b/Plugins/Stats/Web/Views/Stats/_List.cshtml index d5d8f329f..66588a0f3 100644 --- a/Plugins/Stats/Web/Views/Stats/_List.cshtml +++ b/Plugins/Stats/Web/Views/Stats/_List.cshtml @@ -53,7 +53,7 @@ @stat.KDR @loc["PLUGINS_STATS_TEXT_KDR"] @stat.Kills @loc["PLUGINS_STATS_TEXT_KILLS"] @stat.Deaths @loc["PLUGINS_STATS_TEXT_DEATHS"]
- @loc["WEBFRONT_PROFILE_PLAYER"] @stat.TimePlayed @loc["GLOBAL_HOURS"]
+ @loc["WEBFRONT_PROFILE_PLAYER"] @stat.TimePlayed @loc["GLOBAL_TIME_HOURS"]
@loc["WEBFRONT_PROFILE_LSEEN"] @stat.LastSeen @loc["WEBFRONT_PENALTY_TEMPLATE_AGO"] diff --git a/SharedLibraryCore/Commands/NativeCommands.cs b/SharedLibraryCore/Commands/NativeCommands.cs index 517ab0b0f..40f3e4f1b 100644 --- a/SharedLibraryCore/Commands/NativeCommands.cs +++ b/SharedLibraryCore/Commands/NativeCommands.cs @@ -521,7 +521,7 @@ namespace SharedLibraryCore.Commands { TimeSpan uptime = DateTime.Now - System.Diagnostics.Process.GetCurrentProcess().StartTime; var loc = Utilities.CurrentLocalization.LocalizationIndex; - await E.Origin.Tell($"IW4M Admin {loc["COMMANDS_UPTIME_TEXT"]} {uptime.Days} {loc["GLOBAL_DAYS"]}, {uptime.Hours} {loc["GLOBAL_HOURS"]}, {uptime.Minutes} {loc["GLOBAL_MINUTES"]}"); + await E.Origin.Tell($"IW4M Admin {loc["COMMANDS_UPTIME_TEXT"]} {uptime.Days} {loc["GLOBAL_TIME_DAYS"]}, {uptime.Hours} {loc["GLOBAL_TIME_HOURS"]}, {uptime.Minutes} {loc["GLOBAL_TIME_MINUTES"]}"); } } diff --git a/SharedLibraryCore/Database/DatabaseContext.cs b/SharedLibraryCore/Database/DatabaseContext.cs index f04550e01..e801e3512 100644 --- a/SharedLibraryCore/Database/DatabaseContext.cs +++ b/SharedLibraryCore/Database/DatabaseContext.cs @@ -20,6 +20,9 @@ namespace SharedLibraryCore.Database public DbSet EFMeta { get; set; } public DbSet EFChangeHistory { get; set; } + /// + /// this only works if there's one connection string + /// private static string _ConnectionString; public DatabaseContext(DbContextOptions opt) : base(opt) { } @@ -29,10 +32,6 @@ namespace SharedLibraryCore.Database _ConnectionString = connStr; } - public DatabaseContext() - { - } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (string.IsNullOrEmpty(_ConnectionString)) @@ -55,7 +54,6 @@ namespace SharedLibraryCore.Database } } - protected override void OnModelCreating(ModelBuilder modelBuilder) { // make network id unique @@ -96,6 +94,7 @@ namespace SharedLibraryCore.Database modelBuilder.Entity().ToTable("EFAliasLinks"); modelBuilder.Entity().ToTable("EFPenalties"); + // adapted from // https://aleemkhan.wordpress.com/2013/02/28/dynamically-adding-dbset-properties-in-dbcontext-for-entity-framework-code-first/ #if !DEBUG foreach (string dllPath in Directory.GetFiles($"{Utilities.OperatingDirectory}Plugins")) diff --git a/SharedLibraryCore/Interfaces/IPageList.cs b/SharedLibraryCore/Interfaces/IPageList.cs new file mode 100644 index 000000000..fde5fac05 --- /dev/null +++ b/SharedLibraryCore/Interfaces/IPageList.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SharedLibraryCore.Interfaces +{ + public interface IPageList + { + IDictionary Pages { get; set; } + } +} diff --git a/SharedLibraryCore/Utilities.cs b/SharedLibraryCore/Utilities.cs index d44b226e5..2a9e8cd48 100644 --- a/SharedLibraryCore/Utilities.cs +++ b/SharedLibraryCore/Utilities.cs @@ -211,30 +211,38 @@ namespace SharedLibraryCore public static String GetTimePassed(DateTime start, bool includeAgo) { TimeSpan Elapsed = DateTime.UtcNow - start; - string ago = includeAgo ? " " + CurrentLocalization.LocalizationIndex["WEBFRONT_PENALTY_TEMPLATE_AGO"] : ""; + string ago = includeAgo ? $" {CurrentLocalization.LocalizationIndex["WEBFRONT_PENALTY_TEMPLATE_AGO"]}" : ""; - if (Elapsed.TotalSeconds < 30 && includeAgo) - return "just now"; + if (Elapsed.TotalSeconds < 30) + { + return CurrentLocalization.LocalizationIndex["GLOBAL_TIME_JUSTNOW"]; + } if (Elapsed.TotalMinutes < 120) { if (Elapsed.TotalMinutes < 1.5) - return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_MINUTES"]}{ago}"; - return Math.Round(Elapsed.TotalMinutes, 0) + $" {CurrentLocalization.LocalizationIndex["GLOBAL_MINUTES"]}{ago}"; + return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_MINUTES"]}{ago}"; + return Math.Round(Elapsed.TotalMinutes, 0) + $" {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_MINUTES"]}{ago}"; } if (Elapsed.TotalHours <= 24) { if (Elapsed.TotalHours < 1.5) - return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_HOURS"]}{ago}"; - return Math.Round(Elapsed.TotalHours, 0) + $" { CurrentLocalization.LocalizationIndex["GLOBAL_HOURS"]}{ago}"; + return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_HOURS"]}{ago}"; + return Math.Round(Elapsed.TotalHours, 0) + $" { CurrentLocalization.LocalizationIndex["GLOBAL_TIME_HOURS"]}{ago}"; + } + if (Elapsed.TotalDays <= 90) + { + if (Elapsed.TotalDays < 1.5) + return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_DAYS"]}{ago}"; + return Math.Round(Elapsed.TotalDays, 0) + $" {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_DAYS"]}{ago}"; } if (Elapsed.TotalDays <= 365) { - if (Elapsed.TotalDays < 1.5) - return $"1 {CurrentLocalization.LocalizationIndex["GLOBAL_DAYS"]}{ago}"; - return Math.Round(Elapsed.TotalDays, 0) + $" {CurrentLocalization.LocalizationIndex["GLOBAL_DAYS"]}{ago}"; + return $"{Math.Round(Elapsed.TotalDays / 7)} {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_WEEKS"]}{ago}"; } else - return $"a very long time{ago}"; + { + return $"{Math.Round(Elapsed.TotalDays / 30, 0)} {CurrentLocalization.LocalizationIndex["GLOBAL_TIME_MONTHS"]}{ago}"; + } } public static Game GetGame(string gameName) @@ -269,42 +277,59 @@ namespace SharedLibraryCore if (!expressionMatch.Success) // fallback to default tempban length of 1 hour return new TimeSpan(1, 0, 0); - char lengthDenote = expressionMatch.Value[expressionMatch.Value.Length - 1]; + char lengthDenote = expressionMatch.Value.ToLower()[expressionMatch.Value.Length - 1]; int length = Int32.Parse(expressionMatch.Value.Substring(0, expressionMatch.Value.Length - 1)); - switch (lengthDenote) + var loc = CurrentLocalization.LocalizationIndex; + + if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_MINUTES"][0])) { - case 'm': - return new TimeSpan(0, length, 0); - case 'h': - return new TimeSpan(length, 0, 0); - case 'd': - return new TimeSpan(length, 0, 0, 0); - case 'w': - return new TimeSpan(length * 7, 0, 0, 0); - case 'y': - return new TimeSpan(length * 365, 0, 0, 0); - default: - return new TimeSpan(1, 0, 0); + return new TimeSpan(0, length, 0); } + + if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_HOURS"].First())) + { + return new TimeSpan(length, 0, 0); + } + + if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_DAYS"].First())) + { + return new TimeSpan(length, 0, 0, 0); + } + + if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_WEEKS"].First())) + { + return new TimeSpan(length * 7, 0, 0, 0); + } + + if (lengthDenote == char.ToLower(loc["GLOBAL_TIME_YEARS"].First())) + { + return new TimeSpan(length * 365, 0, 0, 0); + } + + return new TimeSpan(1, 0, 0); } public static string TimeSpanText(this TimeSpan span) { - if (span.TotalMinutes < 60) - return $"{span.Minutes} minute(s)"; - else if (span.Hours >= 1 && span.TotalHours < 24) - return $"{span.Hours} hour(s)"; - else if (span.TotalDays >= 1 && span.TotalDays < 7) - return $"{span.Days} day(s)"; - else if (span.TotalDays >= 7 && span.TotalDays < 365) - return $"{Math.Ceiling(span.Days / 7.0)} week(s)"; - else if (span.TotalDays >= 365 && span.TotalDays < 36500) - return $"{Math.Ceiling(span.Days / 365.0)} year(s)"; - else if (span.TotalDays >= 36500) - return "Forever"; + var loc = CurrentLocalization.LocalizationIndex; - return "1 hour"; + if (span.TotalMinutes < 60) + return $"{span.Minutes} {loc["GLOBAL_TIME_MINUTES"]}"; + else if (span.Hours >= 1 && span.TotalHours < 24) + return $"{span.Hours} {loc["GLOBAL_TIME_HOURS"]}"; + else if (span.TotalDays >= 1 && span.TotalDays < 7) + return $"{span.Days} {loc["GLOBAL_TIME_DAYS"]}"; + else if (span.TotalDays >= 7 && span.TotalDays < 90) + return $"{Math.Round(span.Days / 7.0, 0)} {loc["GLOBAL_TIME_WEEKS"]}"; + else if (span.TotalDays >= 90 && span.TotalDays < 365) + return $"{Math.Round(span.Days / 30.0, 0)} {loc["GLOBAL_TIME_MONTHS"]}"; + else if (span.TotalDays >= 365 && span.TotalDays < 36500) + return $"{Math.Round(span.Days / 365.0, 0)} {loc["GLOBAL_TIME_YEARS"]}"; + else if (span.TotalDays >= 36500) + return loc["GLOBAL_TIME_FOREVER"]; + + return "unknown"; } public static Player AsPlayer(this Database.Models.EFClient client) @@ -410,7 +435,7 @@ namespace SharedLibraryCore public static Task SetDvarAsync(this Server server, string dvarName, object dvarValue) => server.RconParser.SetDvarAsync(server.RemoteConnection, dvarName, dvarValue); public static async Task ExecuteCommandAsync(this Server server, string commandName) => await server.RconParser.ExecuteCommandAsync(server.RemoteConnection, commandName); - + public static Task> GetStatusAsync(this Server server) => server.RconParser.GetStatusAsync(server.RemoteConnection); public static async Task> GetInfoAsync(this Server server) diff --git a/WebfrontCore/Controllers/API/APIController.cs b/WebfrontCore/Controllers/API/APIController.cs index ca51822c2..0969c9505 100644 --- a/WebfrontCore/Controllers/API/APIController.cs +++ b/WebfrontCore/Controllers/API/APIController.cs @@ -6,8 +6,50 @@ using System.Threading.Tasks; namespace WebfrontCore.Controllers.API { - [Route("api")] - public class APIController : BaseController + public class ApiController : BaseController { + public IActionResult Index() => Ok($"IW4MAdmin API"); + + [HttpGet] + public IActionResult Event(bool shouldConsume = true) + { + var events = Manager.GetEventApi().GetEvents(shouldConsume); + return Json(events); + } + + [HttpGet] + public IActionResult Status(int id) + { + var serverInfo = Manager.GetServers() + .Select(server => new + { + Id = server.GetHashCode(), + Name = server.Hostname, + MaxPlayers = server.MaxClients, + CurrentPlayers = server.GetPlayersAsList().Count, + Map = server.CurrentMap, + GameMode = server.Gametype, + Port = server.GetPort(), + Game = server.GameName.ToString(), + Players = server.GetPlayersAsList() + .Select(player => new + { + player.Name, + player.Score, + player.Ping, + player.State, + player.ClientNumber, + player.ConnectionTime, + player.Level, + }) + }); + + if (id != 0) + { + serverInfo = serverInfo.Where(server => server.Id == id); + } + + return Json(serverInfo); + } } } diff --git a/WebfrontCore/Controllers/API/EventController.cs b/WebfrontCore/Controllers/API/EventController.cs deleted file mode 100644 index 0ff9544c4..000000000 --- a/WebfrontCore/Controllers/API/EventController.cs +++ /dev/null @@ -1,18 +0,0 @@ -using SharedLibraryCore.Dtos; -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc; - - -namespace WebfrontCore.Controllers.API -{ - public class EventController : APIController - { - [HttpGet] - [Route("event")] - public ActionResult Index(bool shouldConsume = true) - { - var events = Manager.GetEventApi().GetEvents(shouldConsume); - return Json(events); - } - } -} diff --git a/WebfrontCore/Views/Client/Profile/Index.cshtml b/WebfrontCore/Views/Client/Profile/Index.cshtml index cf4b3726f..2fd07afee 100644 --- a/WebfrontCore/Views/Client/Profile/Index.cshtml +++ b/WebfrontCore/Views/Client/Profile/Index.cshtml @@ -74,7 +74,7 @@
@Model.Level
- @loc["WEBFRONT_PROFILE_PLAYER"] @Model.TimePlayed @loc["GLOBAL_HOURS"] + @loc["WEBFRONT_PROFILE_PLAYER"] @Model.TimePlayed @loc["GLOBAL_TIME_HOURS"]
@loc["WEBFRONT_PROFILE_FSEEN"] @Model.FirstSeen @loc["WEBFRONT_PENALTY_TEMPLATE_AGO"]