Theoretically move friend/blocked data to Redis

Untested due to shenanigans with Redis.
This commit is contained in:
jvyden 2022-05-15 13:33:28 -04:00
parent 82091c6d8b
commit bd41997aae
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
11 changed files with 166 additions and 133 deletions

View file

@ -1,24 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse API" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH"
value="$PROJECT_DIR$/ProjectLighthouse.API/bin/Debug/net6.0/LBPUnion.ProjectLighthouse.API"/>
<option name="PROGRAM_PARAMETERS" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse"/>
<option name="PASS_PARENT_ENVS" value="1"/>
<option name="USE_EXTERNAL_CONSOLE" value="0"/>
<option name="USE_MONO" value="0"/>
<option name="RUNTIME_ARGUMENTS" value=""/>
<option name="PROJECT_PATH"
value="$PROJECT_DIR$/ProjectLighthouse.Servers.API/ProjectLighthouse.Servers.API.csproj"/>
<option name="PROJECT_EXE_PATH_TRACKING" value="1"/>
<option name="PROJECT_ARGUMENTS_TRACKING" value="1"/>
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0"/>
<option name="PROJECT_KIND" value="DotNetCore"/>
<option name="PROJECT_TFM" value="net6.0"/>
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Database"
run_configuration_type="docker-deploy"/>
<option name="Build"/>
</method>
</configuration>
<configuration default="false" name="Lighthouse API" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.API/bin/Debug/net6.0/LBPUnion.ProjectLighthouse.Servers.API" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.API/ProjectLighthouse.Servers.API.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net6.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
</component>

View file

@ -1,24 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse Gameserver" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH"
value="$PROJECT_DIR$/ProjectLighthouse.Servers.GameServer/bin/Debug/net6.0/LBPUnion.ProjectLighthouse.Servers.GameServer"/>
<option name="PROGRAM_PARAMETERS" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse"/>
<option name="PASS_PARENT_ENVS" value="1"/>
<option name="USE_EXTERNAL_CONSOLE" value="0"/>
<option name="USE_MONO" value="0"/>
<option name="RUNTIME_ARGUMENTS" value=""/>
<option name="PROJECT_PATH"
value="$PROJECT_DIR$/ProjectLighthouse.Servers.GameServer/ProjectLighthouse.Servers.GameServer.csproj"/>
<option name="PROJECT_EXE_PATH_TRACKING" value="1"/>
<option name="PROJECT_ARGUMENTS_TRACKING" value="1"/>
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0"/>
<option name="PROJECT_KIND" value="DotNetCore"/>
<option name="PROJECT_TFM" value="net6.0"/>
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Database"
run_configuration_type="docker-deploy"/>
<option name="Build"/>
</method>
</configuration>
<configuration default="false" name="Lighthouse Gameserver" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.GameServer/bin/Debug/net6.0/LBPUnion.ProjectLighthouse.Servers.GameServer" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.GameServer/ProjectLighthouse.Servers.GameServer.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net6.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
</component>

View file

@ -1,24 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse Website" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH"
value="$PROJECT_DIR$/ProjectLighthouse.Servers.Website/bin/Debug/net6.0/LBPUnion.ProjectLighthouse.Servers.Website"/>
<option name="PROGRAM_PARAMETERS" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse"/>
<option name="PASS_PARENT_ENVS" value="1"/>
<option name="USE_EXTERNAL_CONSOLE" value="0"/>
<option name="USE_MONO" value="0"/>
<option name="RUNTIME_ARGUMENTS" value=""/>
<option name="PROJECT_PATH"
value="$PROJECT_DIR$/ProjectLighthouse.Servers.Website/ProjectLighthouse.Servers.Website.csproj"/>
<option name="PROJECT_EXE_PATH_TRACKING" value="1"/>
<option name="PROJECT_ARGUMENTS_TRACKING" value="1"/>
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0"/>
<option name="PROJECT_KIND" value="DotNetCore"/>
<option name="PROJECT_TFM" value="net6.0"/>
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Database"
run_configuration_type="docker-deploy"/>
<option name="Build"/>
</method>
</configuration>
<configuration default="false" name="Lighthouse Website" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.Website/bin/Debug/net6.0/LBPUnion.ProjectLighthouse.Servers.Website" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/ProjectLighthouse.Servers.Website/ProjectLighthouse.Servers.Website.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net6.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
</component>

View file

@ -1,22 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Lighthouse" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse/bin/Debug/net6.0/LBPUnion.ProjectLighthouse"/>
<option name="PROGRAM_PARAMETERS" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse"/>
<option name="PASS_PARENT_ENVS" value="1"/>
<option name="USE_EXTERNAL_CONSOLE" value="0"/>
<option name="USE_MONO" value="0"/>
<option name="RUNTIME_ARGUMENTS" value=""/>
<option name="PROJECT_PATH" value="$PROJECT_DIR$/ProjectLighthouse/ProjectLighthouse.csproj"/>
<option name="PROJECT_EXE_PATH_TRACKING" value="1"/>
<option name="PROJECT_ARGUMENTS_TRACKING" value="1"/>
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1"/>
<option name="PROJECT_KIND" value="Unloaded"/>
<option name="PROJECT_TFM" value="net6.0"/>
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Database"
run_configuration_type="docker-deploy"/>
<option name="Build"/>
</method>
</configuration>
<configuration default="false" name="Lighthouse" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ProjectLighthouse/bin/Debug/net6.0/LBPUnion.ProjectLighthouse" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ProjectLighthouse" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/ProjectLighthouse/ProjectLighthouse.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="Unloaded" />
<option name="PROJECT_TFM" value="net6.0" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Development Databases" run_configuration_type="docker-deploy" />
<option name="Build" />
</method>
</configuration>
</component>

View file

@ -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;

View file

@ -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<int, int[]> FriendIdsByUserId = new();
public static readonly Dictionary<int, int[]> BlockedIdsByUserId = new();
}

View file

@ -8,22 +8,22 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3"/>
<PackageReference Include="DDSReader" Version="1.0.8-pre"/>
<PackageReference Include="Discord.Net.Webhook" Version="3.6.1"/>
<PackageReference Include="InfluxDB.Client" Version="4.1.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.5"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5"/>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="DDSReader" Version="1.0.8-pre" />
<PackageReference Include="Discord.Net.Webhook" Version="3.6.1" />
<PackageReference Include="InfluxDB.Client" Version="4.1.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1"/>
<PackageReference Include="Redis.OM" Version="0.1.9"/>
<PackageReference Include="SharpZipLib" Version="1.3.3"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1"/>
<PackageReference Include="YamlDotNet" Version="11.2.1"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
<PackageReference Include="Redis.OM" Version="0.1.9" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>
<ItemGroup>
@ -39,28 +39,29 @@
<EmbeddedResource Include="gitRemotes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitUnpushed.txt"/>
<None Remove="gitUnpushed.txt" />
<EmbeddedResource Include="gitUnpushed.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="chatCensoredList.txt"/>
<None Remove="chatCensoredList.txt" />
<EmbeddedResource Include="chatCensoredList.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Compile Remove="Extensions\SerializationExtensions.cs"/>
<Compile Remove="Extensions\SerializationExtensions.cs" />
<Compile Remove="Helpers\FriendHelper.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Startup\Middlewares"/>
<Folder Include="Startup\Middlewares" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;"/>
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;"/>
<Exec Command="git remote -v &gt; &quot;$(ProjectDir)/gitRemotes.txt&quot;"/>
<Exec Command="git log --branches --not --remotes --oneline &gt; &quot;$(ProjectDir)/gitUnpushed.txt&quot;"/>
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;" />
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;" />
<Exec Command="git remote -v &gt; &quot;$(ProjectDir)/gitRemotes.txt&quot;" />
<Exec Command="git log --branches --not --remotes --oneline &gt; &quot;$(ProjectDir)/gitUnpushed.txt&quot;" />
</Target>
</Project>

View file

@ -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<Room> GetRooms() => provider.RedisCollection<Room>();
private static IRedisCollection<UserFriendStore> userFriendStoreCollection => provider.RedisCollection<UserFriendStore>();
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);
}
}

View file

@ -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);

View file

@ -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; } = "";

View file

@ -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<int> FriendIds { get; set; }
[Indexed]
public List<int> BlockedIds { get; set; }
}