diff --git a/.run/Lighthouse API.run.xml b/.run/Lighthouse API.run.xml index bd337dfc..08ab248a 100644 --- a/.run/Lighthouse API.run.xml +++ b/.run/Lighthouse API.run.xml @@ -1,24 +1,21 @@ - - + + \ No newline at end of file diff --git a/.run/Lighthouse Game API.run.xml b/.run/Lighthouse Game API.run.xml index 7f8c19a2..73f4a935 100644 --- a/.run/Lighthouse Game API.run.xml +++ b/.run/Lighthouse Game API.run.xml @@ -1,24 +1,21 @@ - - + + \ No newline at end of file diff --git a/.run/Lighthouse Website.run.xml b/.run/Lighthouse Website.run.xml index c9f59380..4734f1b8 100644 --- a/.run/Lighthouse Website.run.xml +++ b/.run/Lighthouse Website.run.xml @@ -1,24 +1,21 @@ - - + + \ No newline at end of file diff --git a/.run/Lighthouse.run.xml b/.run/Lighthouse.run.xml index 2cd1414b..9c9d716a 100644 --- a/.run/Lighthouse.run.xml +++ b/.run/Lighthouse.run.xml @@ -1,22 +1,21 @@ - - + + \ No newline at end of file diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/FriendsController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/FriendsController.cs index 0c609de6..c6afeb3e 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/FriendsController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/FriendsController.cs @@ -53,14 +53,13 @@ public class FriendsController : ControllerBase blockedUsers.Add(blockedUser.UserId); } - if (FriendHelper.FriendIdsByUserId.ContainsKey(user.UserId)) - { - FriendHelper.FriendIdsByUserId.Remove(user.UserId); - FriendHelper.BlockedIdsByUserId.Remove(user.UserId); - } + UserFriendStore? friendStore = Redis.GetUserFriendStore(user.UserId); + if (friendStore == null) friendStore = Redis.CreateUserFriendStore(user.UserId); - FriendHelper.FriendIdsByUserId.Add(user.UserId, friends.Select(u => u.UserId).ToArray()); - FriendHelper.BlockedIdsByUserId.Add(user.UserId, blockedUsers.ToArray()); + friendStore.FriendIds = friends.Select(u => u.UserId).ToList(); + friendStore.BlockedIds = blockedUsers; + + Redis.UpdateFriendStore(friendStore); string friendsSerialized = friends.Aggregate(string.Empty, (current, user1) => current + LbpSerializer.StringElement("npHandle", user1.Username)); @@ -78,11 +77,13 @@ public class FriendsController : ControllerBase User user = userAndToken.Value.Item1; GameToken gameToken = userAndToken.Value.Item2; - if (!FriendHelper.FriendIdsByUserId.TryGetValue(user.UserId, out int[]? friendIds) || friendIds == null) + UserFriendStore? friendStore = Redis.GetUserFriendStore(user.UserId); + + if (friendStore == null) return this.Ok(LbpSerializer.BlankElement("myFriends")); string friends = ""; - foreach (int friendId in friendIds) + foreach (int friendId in friendStore.FriendIds) { User? friend = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == friendId); if (friend == null) continue; diff --git a/ProjectLighthouse/Helpers/FriendHelper.cs b/ProjectLighthouse/Helpers/FriendHelper.cs deleted file mode 100644 index e5f0bf43..00000000 --- a/ProjectLighthouse/Helpers/FriendHelper.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Diagnostics.CodeAnalysis; - -namespace LBPUnion.ProjectLighthouse.Helpers; - -[NotMapped] -[SuppressMessage("ReSharper", "CollectionNeverQueried.Global")] -public static class FriendHelper -{ - public static readonly Dictionary FriendIdsByUserId = new(); - public static readonly Dictionary BlockedIdsByUserId = new(); -} \ No newline at end of file diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index 014ecd0e..02bc40c6 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -8,22 +8,22 @@ - - - - - - - + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + + + + + @@ -39,28 +39,29 @@ Always - + Always - + Always - + + - + - - - - + + + + diff --git a/ProjectLighthouse/Redis.cs b/ProjectLighthouse/Redis.cs index c2b26683..966a7345 100644 --- a/ProjectLighthouse/Redis.cs +++ b/ProjectLighthouse/Redis.cs @@ -1,6 +1,10 @@ +#nullable enable using System; +using System.Linq; using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; +using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Match; using LBPUnion.ProjectLighthouse.Types.Settings; using Redis.OM; @@ -18,7 +22,7 @@ public static class Redis provider = new RedisConnectionProvider(ServerConfiguration.Instance.RedisConnectionString); } - private static bool initialized = false; + private static bool initialized; public static async Task Initialize() { if (initialized) throw new InvalidOperationException("Redis has already been initialized."); @@ -26,6 +30,7 @@ public static class Redis IRedisConnection connection = getConnection(); await connection.CreateIndexAsync(typeof(Room)); + await connection.CreateIndexAsync(typeof(UserFriendStore)); initialized = true; Logger.LogSuccess("Initialized Redis.", LogArea.Redis); @@ -38,4 +43,25 @@ public static class Redis } public static IRedisCollection GetRooms() => provider.RedisCollection(); + + private static IRedisCollection userFriendStoreCollection => provider.RedisCollection(); + + public static UserFriendStore? GetUserFriendStore(int userId) => + userFriendStoreCollection.FirstOrDefault(s => s.UserId == userId); + + public static UserFriendStore CreateUserFriendStore(int userId) + { + UserFriendStore friendStore = new() + { + UserId = userId, + }; + + userFriendStoreCollection.Insert(friendStore); + return friendStore; + } + + public static void UpdateFriendStore(UserFriendStore friendStore) + { + userFriendStoreCollection.UpdateSync(friendStore); + } } \ No newline at end of file diff --git a/ProjectLighthouse/StartupTasks.cs b/ProjectLighthouse/StartupTasks.cs index 5ea135c8..6958a1c2 100644 --- a/ProjectLighthouse/StartupTasks.cs +++ b/ProjectLighthouse/StartupTasks.cs @@ -32,7 +32,7 @@ public static class StartupTasks // Referencing ServerSettings.Instance here loads the config, see ServerSettings.cs for more information Logger.LogSuccess("Loaded config file version " + ServerConfiguration.Instance.ConfigVersion, LogArea.Startup); - Logger.LogInfo("Determining if the database is available...", LogArea.Startup); + Logger.LogInfo("Connecting to the database...", LogArea.Startup); bool dbConnected = ServerStatics.DbConnected; if (!dbConnected) { @@ -40,7 +40,7 @@ public static class StartupTasks } else { - Logger.LogSuccess("Connected to the database.", LogArea.Startup); + Logger.LogSuccess("Connected!", LogArea.Startup); } if (!dbConnected) Environment.Exit(1); @@ -48,7 +48,7 @@ public static class StartupTasks Logger.LogInfo("Migrating database...", LogArea.Database); migrateDatabase(database); - + if (ServerConfiguration.Instance.InfluxDB.InfluxEnabled) { Logger.LogInfo("Influx logging is enabled. Starting influx logging...", LogArea.Startup); diff --git a/ProjectLighthouse/Types/Settings/ServerConfiguration.cs b/ProjectLighthouse/Types/Settings/ServerConfiguration.cs index 31727ac4..67e64c79 100644 --- a/ProjectLighthouse/Types/Settings/ServerConfiguration.cs +++ b/ProjectLighthouse/Types/Settings/ServerConfiguration.cs @@ -170,7 +170,7 @@ public class ServerConfiguration public string ApiListenUrl { get; set; } = "http://localhost:10062"; public string DbConnectionString { get; set; } = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse"; - public string RedisConnectionString { get; set; } = ""; + public string RedisConnectionString { get; set; } = "redis://localhost:6379"; public string ExternalUrl { get; set; } = "http://localhost:10060"; public bool ConfigReloading { get; set; } public string EulaText { get; set; } = ""; diff --git a/ProjectLighthouse/Types/UserFriendStore.cs b/ProjectLighthouse/Types/UserFriendStore.cs new file mode 100644 index 00000000..d71fe7bf --- /dev/null +++ b/ProjectLighthouse/Types/UserFriendStore.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using Redis.OM.Modeling; + +namespace LBPUnion.ProjectLighthouse.Types; + +[SuppressMessage("ReSharper", "CollectionNeverQueried.Global")] +[Document(StorageType = StorageType.Json)] +public class UserFriendStore +{ + private int userId; + public int UserId { + get => this.userId; + set { + this.RedisId = value.ToString(); + this.userId = value; + } + } + + [RedisIdField] + public string RedisId { get; set; } + + [Indexed] + public List FriendIds { get; set; } + + [Indexed] + public List BlockedIds { get; set; } +} \ No newline at end of file