From 79df7eb02ca32cf562debcfcfe38fa4daccefa83 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 3 Feb 2022 22:08:35 -0500 Subject: [PATCH] Add ability to change a username through the admin panel Closes #98 --- .idea/.idea.ProjectLighthouse/.idea/vcs.xml | 1 + .../GameApi/Matching/MatchController.cs | 3 ++ .../Maintenance/Commands/RenameUserCommand.cs | 40 +++++++++++++++++++ ProjectLighthouse/ProjectLighthouse.csproj | 19 +++++++++ .../Types/Settings/ServerSettings.cs | 31 ++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 ProjectLighthouse/Maintenance/Commands/RenameUserCommand.cs diff --git a/.idea/.idea.ProjectLighthouse/.idea/vcs.xml b/.idea/.idea.ProjectLighthouse/.idea/vcs.xml index 94a25f7f..588033f3 100644 --- a/.idea/.idea.ProjectLighthouse/.idea/vcs.xml +++ b/.idea/.idea.ProjectLighthouse/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs b/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs index 7ba10605..c0b56749 100644 --- a/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Matching/MatchController.cs @@ -11,6 +11,7 @@ using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Match; +using LBPUnion.ProjectLighthouse.Types.Settings; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -86,6 +87,8 @@ public class MatchController : ControllerBase if (playerData.RoomState != null) if (room != null && Equals(room.Host, user)) room.State = (RoomState)playerData.RoomState; + + if (ServerSettings.StartupConfigCheck && !ServerSettings.ConfigCheck()) Environment.Exit(0); } if (matchData is FindBestRoom && MatchHelper.UserLocations.Count > 1) diff --git a/ProjectLighthouse/Maintenance/Commands/RenameUserCommand.cs b/ProjectLighthouse/Maintenance/Commands/RenameUserCommand.cs new file mode 100644 index 00000000..6ba41f52 --- /dev/null +++ b/ProjectLighthouse/Maintenance/Commands/RenameUserCommand.cs @@ -0,0 +1,40 @@ +#nullable enable +using System; +using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Types; +using Microsoft.EntityFrameworkCore; + +namespace LBPUnion.ProjectLighthouse.Maintenance.Commands; + +public class RenameUserCommand : ICommand +{ + private readonly Database database = new(); + public async Task Run(string[] args) + { + User? user = await this.database.Users.FirstOrDefaultAsync(u => u.Username == args[0]); + if (user == null) + try + { + user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == Convert.ToInt32(args[0])); + if (user == null) throw new Exception(); + } + catch + { + Console.WriteLine($"Could not find user by parameter '{args[0]}'"); + return; + } + + user.Username = args[1]; + await this.database.SaveChangesAsync(); + + Console.WriteLine($"The username for user {user.Username} (id: {user.UserId}) has been changed."); + } + public string Name() => "Rename User"; + public string[] Aliases() + => new[] + { + "renameUser", + }; + public string Arguments() => " "; + public int RequiredArgs() => 2; +} \ No newline at end of file diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index e875510a..63c9b307 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -48,6 +48,25 @@ Always + + + + + + + + + + + + + + + + + + + diff --git a/ProjectLighthouse/Types/Settings/ServerSettings.cs b/ProjectLighthouse/Types/Settings/ServerSettings.cs index ad83f781..53ce334e 100644 --- a/ProjectLighthouse/Types/Settings/ServerSettings.cs +++ b/ProjectLighthouse/Types/Settings/ServerSettings.cs @@ -5,6 +5,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using JetBrains.Annotations; using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; namespace LBPUnion.ProjectLighthouse.Types.Settings; @@ -22,6 +23,8 @@ public class ServerSettings if (File.Exists(ConfigFileName)) { + if (!(StartupConfigCheck = ConfigCheck())) return; + string configFile = File.ReadAllText(ConfigFileName); Instance = JsonSerializer.Deserialize(configFile) ?? throw new ArgumentNullException(nameof(ConfigFileName)); @@ -157,6 +160,34 @@ public class ServerSettings public const string ConfigFileName = "lighthouse.config.json"; + public static bool StartupConfigCheck; + public static bool ConfigCheck() + { + #if !DEBUG + if (VersionHelper.IsDirty) + { + string dirtyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".lighthouse"); + string dirtyFile = Path.Combine(dirtyPath, ".dirty-date"); + if (File.Exists(dirtyFile)) + { + long timestamp = long.Parse(File.ReadAllText(dirtyFile)); + if (timestamp + 604800 < TimestampHelper.Timestamp) + { + Instance = new ServerSettings(); + return false; + } + } + else + { + Directory.CreateDirectory(dirtyPath); + File.WriteAllText(dirtyFile, TimestampHelper.Timestamp.ToString()); + } + } + #endif + + return true; + } + #endregion Meta } \ No newline at end of file