From e05a82d4bc1546c553c27d16ca2fecbcf216effd Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Wed, 22 May 2024 18:05:58 +0200 Subject: [PATCH] feat: config helper --- .../Controllers/ConfigurationController.cs | 9 ++-- .../Controllers/McoController.cs | 19 ++++---- .../Controllers/TrialController.cs | 11 +++-- .../Controllers/WorldsController.cs | 6 ++- .../Helpers/Config/ConfigHelper.cs | 47 +++++++++++++++++++ .../Helpers/Config/Settings.cs | 9 ++++ Minecraft-Realms-Emulator/Helpers/Database.cs | 37 +++++++-------- 7 files changed, 102 insertions(+), 36 deletions(-) create mode 100644 Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs create mode 100644 Minecraft-Realms-Emulator/Helpers/Config/Settings.cs diff --git a/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs b/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs index 84fce0a..d693725 100644 --- a/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs +++ b/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Entities; +using Minecraft_Realms_Emulator.Helpers; namespace Minecraft_Realms_Emulator.Controllers { @@ -16,10 +17,12 @@ namespace Minecraft_Realms_Emulator.Controllers } [HttpGet] - public ActionResult GetConfigurationAsync() + public ActionResult GetConfiguration() { - var configuration = _context.Configuration; - return Ok(configuration); + var config = new ConfigHelper(_context); + var settings = config.GetSettings(); + + return Ok(settings); } } } \ No newline at end of file diff --git a/Minecraft-Realms-Emulator/Controllers/McoController.cs b/Minecraft-Realms-Emulator/Controllers/McoController.cs index f03e93a..b3f5834 100644 --- a/Minecraft-Realms-Emulator/Controllers/McoController.cs +++ b/Minecraft-Realms-Emulator/Controllers/McoController.cs @@ -1,8 +1,8 @@ using Microsoft.AspNetCore.Mvc; using Minecraft_Realms_Emulator.Attributes; using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Helpers; using Minecraft_Realms_Emulator.Responses; -using Newtonsoft.Json; namespace Minecraft_Realms_Emulator.Controllers { @@ -19,28 +19,29 @@ namespace Minecraft_Realms_Emulator.Controllers } [HttpGet("available")] - public bool GetAvailable() + public ActionResult GetAvailable() { - return true; + return Ok(true); } [HttpGet("client/compatible")] - public string GetCompatible() + public ActionResult GetCompatible() { - return Compatility.COMPATIBLE.ToString(); + return Ok("COMPATIBLE"); } [HttpGet("v1/news")] - public NewsResponse GetNews() + public ActionResult GetNews() { - var newsLink = _context.Configuration.FirstOrDefault(s => s.Key == "newsLink"); + var config = new ConfigHelper(_context); + var newsLink = config.GetSetting("newsLink"); var news = new NewsResponse { - NewsLink = JsonConvert.DeserializeObject(newsLink.Value), + NewsLink = newsLink.Value }; - return news; + return Ok(news); } } } \ No newline at end of file diff --git a/Minecraft-Realms-Emulator/Controllers/TrialController.cs b/Minecraft-Realms-Emulator/Controllers/TrialController.cs index b75fb56..5d53c58 100644 --- a/Minecraft-Realms-Emulator/Controllers/TrialController.cs +++ b/Minecraft-Realms-Emulator/Controllers/TrialController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Minecraft_Realms_Emulator.Attributes; using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Helpers; namespace Minecraft_Realms_Emulator.Controllers { @@ -16,9 +17,13 @@ namespace Minecraft_Realms_Emulator.Controllers _context = context; } - [HttpGet(Name = "GetTrial")] - public bool Get() { - return bool.Parse(_context.Configuration.FirstOrDefault(x => x.Key == "trialMode").Value); + [HttpGet] + public ActionResult GetTrial() + { + var config = new ConfigHelper(_context); + var trialMode = config.GetSetting("trialMode"); + + return Ok(trialMode.Value); } } } diff --git a/Minecraft-Realms-Emulator/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Controllers/WorldsController.cs index 2490700..7ffdad8 100644 --- a/Minecraft-Realms-Emulator/Controllers/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Controllers/WorldsController.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using Minecraft_Realms_Emulator.Attributes; using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Entities; +using Minecraft_Realms_Emulator.Helpers; using Minecraft_Realms_Emulator.Requests; using Minecraft_Realms_Emulator.Responses; using Newtonsoft.Json; @@ -233,12 +234,13 @@ namespace Minecraft_Realms_Emulator.Controllers world.State = "OPEN"; world.Subscription = subscription; - var defaultServerAddress = _context.Configuration.FirstOrDefault(x => x.Key == "defaultServerAddress"); + var config = new ConfigHelper(_context); + var defaultServerAddress = config.GetSetting("defaultServerAddress"); var connection = new Connection { World = world, - Address = JsonConvert.DeserializeObject(defaultServerAddress.Value) + Address = defaultServerAddress.Value }; Slot slot = new() diff --git a/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs b/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs new file mode 100644 index 0000000..34e9182 --- /dev/null +++ b/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs @@ -0,0 +1,47 @@ +using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Entities; +using Newtonsoft.Json; + +namespace Minecraft_Realms_Emulator.Helpers +{ + public class ConfigHelper + { + private readonly DataContext Db; + + public ConfigHelper(DataContext db) + { + Db = db; + } + + public List GetSettings() + { + List settings = []; + + foreach (var setting in Db.Configuration) + { + Configuration settingTyped = new() + { + Key = setting.Key, + Value = JsonConvert.DeserializeObject(setting.Value) + }; + + settings.Add(settingTyped); + } + + return settings; + } + + public Configuration? GetSetting(string key) + { + var setting = Db.Configuration.Find(key); + + if (setting == null) return null; + + return new() + { + Key = setting.Key, + Value = JsonConvert.DeserializeObject(setting.Value) + }; + } + } +} diff --git a/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs b/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs new file mode 100644 index 0000000..7ee2c50 --- /dev/null +++ b/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs @@ -0,0 +1,9 @@ +namespace Minecraft_Realms_Emulator.Helpers.Config +{ + public class Settings : Dictionary + { + public string DefaultServerAddress { get; set; } = "127.0.0.1"; + public string NewsLink { get; set; } = "https://github.com/CyberL1/Minecraft-Realms-Emulator"; + public bool TrialMode { get; set; } = true; + } +} diff --git a/Minecraft-Realms-Emulator/Helpers/Database.cs b/Minecraft-Realms-Emulator/Helpers/Database.cs index 6e119c9..1106c79 100644 --- a/Minecraft-Realms-Emulator/Helpers/Database.cs +++ b/Minecraft-Realms-Emulator/Helpers/Database.cs @@ -1,6 +1,8 @@ using Microsoft.EntityFrameworkCore; using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Entities; +using Minecraft_Realms_Emulator.Helpers.Config; +using Newtonsoft.Json; namespace Minecraft_Realms_Emulator.Helpers { @@ -13,37 +15,34 @@ namespace Minecraft_Realms_Emulator.Helpers db.Database.Migrate(); - if (!db.Configuration.Any(s => s.Key == "newsLink")) + var config = new ConfigHelper(db); + var settings = new Settings(); + + if (config.GetSetting("newsLink") == null) { - var newsLink = new Configuration + db.Configuration.Add(new Configuration { Key = "newsLink", - Value = "\"https://github.com/CyberL1/Minecraft-Realms-Emulator\"" - }; - - db.Configuration.Add(newsLink); + Value = JsonConvert.SerializeObject(settings.NewsLink) + }); } - - if (!db.Configuration.Any(s => s.Key == "defaultServerAddress")) + + if (config.GetSetting("defaultServerAddress") == null) { - var defaultServerAddress = new Configuration + db.Configuration.Add(new Configuration { Key = "defaultServerAddress", - Value = "\"127.0.0.1\"" - }; - - db.Configuration.Add(defaultServerAddress); + Value = JsonConvert.SerializeObject(settings.DefaultServerAddress) + }); } - if (!db.Configuration.Any(x => x.Key == "trialMode")) + if (config.GetSetting("trialMode") == null) { - var trialMode = new Configuration + db.Configuration.Add(new Configuration { Key = "trialMode", - Value = true - }; - - db.Configuration.Add(trialMode); + Value = JsonConvert.SerializeObject(settings.TrialMode) + }); } db.SaveChanges();