From 1a14ff3370340bb5bbb78d04c719a844c8238eae Mon Sep 17 00:00:00 2001 From: JayArrowz Date: Sat, 17 Jul 2021 03:48:39 +0100 Subject: [PATCH] Add ability to change player object --- NetScape.Core/ServerHandler.cs | 23 +++++++++------ .../EntityFrameworkPlayerSerializerTests.cs | 12 ++++---- NetScape.Modules.DAL/DALModule.cs | 5 ++-- NetScape.Modules.DAL/DatabaseContext.cs | 6 ++-- .../EntityFrameworkPlayerRepository.cs | 29 ++++++++++++++----- .../20201226151013_AddInitial.Designer.cs | 3 +- ...3212503_RemoveAppearanceFields.Designer.cs | 3 +- .../DatabaseContextModelSnapshot.cs | 3 +- NetScape/DesignTimeDbContextFactory.cs | 11 +++---- NetScape/Kernel.cs | 5 ++-- 10 files changed, 63 insertions(+), 37 deletions(-) diff --git a/NetScape.Core/ServerHandler.cs b/NetScape.Core/ServerHandler.cs index e1bf245c..53140226 100644 --- a/NetScape.Core/ServerHandler.cs +++ b/NetScape.Core/ServerHandler.cs @@ -17,18 +17,20 @@ using System; using System.Collections.Generic; using System.IO; +using NetScape.Abstractions.Model.Game; namespace NetScape.Core { public static class ServerHandler { - public static ILifetimeScope RunServer(string configFileName, Action dbOptions, List modules) + public static ILifetimeScope RunServer(string configFileName, Action dbOptions, List modules) + where TPlayer : Player, new() { var serviceCollection = new ServiceCollection(); - var config = ConfigureServices(serviceCollection, configFileName, dbOptions); + var config = ConfigureServices(serviceCollection, configFileName, dbOptions); var containerBuilder = new ContainerBuilder(); containerBuilder.Populate(serviceCollection); - ConfigureAutofac(containerBuilder, config, modules); + ConfigureAutofac(containerBuilder, config, modules); containerBuilder.RegisterBuildCallback(t => t.Resolve().Container = (IContainer)t); var container = containerBuilder.Build(); var serviceProvider = new AutofacServiceProvider(container); @@ -39,11 +41,12 @@ public static ILifetimeScope RunServer(string configFileName, Action(this ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot) + where TPlayer : Player, new() { containerBuilder.RegisterModule(new SeriLogModule(configurationRoot)); containerBuilder.RegisterModule(new CacheModule()); - containerBuilder.RegisterModule(new DALModule()); + containerBuilder.RegisterModule(new DALModule()); containerBuilder.RegisterModule(new GameServerModule(configurationRoot["BindAddr"], ushort.Parse(configurationRoot["BindPort"]))); containerBuilder.RegisterModule(new WorldModule()); containerBuilder.RegisterModule(new RegionModule()); @@ -53,17 +56,19 @@ public static void ConfigureCore(this ContainerBuilder containerBuilder, IConfig containerBuilder.RegisterType().SingleInstance(); } - private static void ConfigureAutofac(ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot, List modules) + private static void ConfigureAutofac(ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot, List modules) + where TPlayer : Player, new() { foreach (var module in modules) { containerBuilder.RegisterModule(module); } - containerBuilder.ConfigureCore(configurationRoot); + containerBuilder.ConfigureCore(configurationRoot); } - public static IConfigurationRoot ConfigureServices(this IServiceCollection serviceCollection, string configFileName, Action optionsAction) + public static IConfigurationRoot ConfigureServices(this IServiceCollection serviceCollection, string configFileName, Action optionsAction) + where TPlayer : Player, new() { var configurationRoot = CreateConfigurationRoot(configFileName); @@ -71,7 +76,7 @@ public static IConfigurationRoot ConfigureServices(this IServiceCollection servi serviceCollection.AddLogging(); //Build DB Connection - serviceCollection.AddDbContextFactory(opts => optionsAction(opts, configurationRoot)); + serviceCollection.AddDbContextFactory>(opts => optionsAction(opts, configurationRoot)); // Add access to generic IConfigurationRoot serviceCollection.AddSingleton(configurationRoot); diff --git a/NetScape.Modules.DAL.Test/EntityFrameworkPlayerSerializerTests.cs b/NetScape.Modules.DAL.Test/EntityFrameworkPlayerSerializerTests.cs index 0ed7b5ba..49b6d241 100644 --- a/NetScape.Modules.DAL.Test/EntityFrameworkPlayerSerializerTests.cs +++ b/NetScape.Modules.DAL.Test/EntityFrameworkPlayerSerializerTests.cs @@ -11,19 +11,19 @@ namespace NetScape.Modules.DAL.Test { public class EntityFrameworkPlayerSerializerTests { - private readonly EntityFrameworkPlayerRepository _entityFrameworkPlayerRepository; - private readonly IDbContextFactory _fakeDbContextFactory; + private readonly EntityFrameworkPlayerRepository _entityFrameworkPlayerRepository; + private readonly IDbContextFactory> _fakeDbContextFactory; public EntityFrameworkPlayerSerializerTests() { var seedDbId = Guid.NewGuid().ToString(); - _fakeDbContextFactory = Substitute.For>(); + _fakeDbContextFactory = Substitute.For>>(); _fakeDbContextFactory.CreateDbContext().ReturnsForAnyArgs(x => - new DatabaseContext( - new DbContextOptionsBuilder().UseInMemoryDatabase(seedDbId).Options + new DatabaseContext( + new DbContextOptionsBuilder>().UseInMemoryDatabase(seedDbId).Options ) ); - _entityFrameworkPlayerRepository = new EntityFrameworkPlayerRepository(_fakeDbContextFactory); + _entityFrameworkPlayerRepository = new EntityFrameworkPlayerRepository(_fakeDbContextFactory); } [Fact] diff --git a/NetScape.Modules.DAL/DALModule.cs b/NetScape.Modules.DAL/DALModule.cs index adc13cbb..57563900 100644 --- a/NetScape.Modules.DAL/DALModule.cs +++ b/NetScape.Modules.DAL/DALModule.cs @@ -1,13 +1,14 @@ using Autofac; using NetScape.Abstractions.FileSystem; +using NetScape.Abstractions.Model.Game; namespace NetScape.Modules.DAL { - public class DALModule : Module + public class DALModule : Module where TPlayer : Player, new() { protected override void Load(ContainerBuilder builder) { - builder.RegisterType().As(); + builder.RegisterType>().As(); } } } diff --git a/NetScape.Modules.DAL/DatabaseContext.cs b/NetScape.Modules.DAL/DatabaseContext.cs index bd79834c..0ac117be 100644 --- a/NetScape.Modules.DAL/DatabaseContext.cs +++ b/NetScape.Modules.DAL/DatabaseContext.cs @@ -9,17 +9,17 @@ namespace NetScape.Modules.DAL { - public partial class DatabaseContext : DbContext + public partial class DatabaseContext : DbContext where TPlayer : Player { private readonly ILoggerFactory _loggerFactory; - public DatabaseContext(DbContextOptions options, ILoggerFactory loggerFactory = null) + public DatabaseContext(DbContextOptions> options, ILoggerFactory loggerFactory = null) : base(options) { _loggerFactory = loggerFactory; } - public DbSet Players { get; set; } + public DbSet Players { get; set; } public DbSet Appearances { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/NetScape.Modules.DAL/EntityFrameworkPlayerRepository.cs b/NetScape.Modules.DAL/EntityFrameworkPlayerRepository.cs index 5211837c..7f201510 100644 --- a/NetScape.Modules.DAL/EntityFrameworkPlayerRepository.cs +++ b/NetScape.Modules.DAL/EntityFrameworkPlayerRepository.cs @@ -14,16 +14,16 @@ namespace NetScape.Modules.DAL /// Serializes/Deserializes players using entity framework /// /// - public class EntityFrameworkPlayerRepository : IPlayerRepository + public class EntityFrameworkPlayerRepository : IPlayerRepository where TPlayer : Player, new() { - private readonly IDbContextFactory _dbContextFactory; + private readonly IDbContextFactory> _dbContextFactory; - public EntityFrameworkPlayerRepository(IDbContextFactory dbContextFactory) + public EntityFrameworkPlayerRepository(IDbContextFactory> dbContextFactory) { _dbContextFactory = dbContextFactory; } - public async Task GetAsync(string name) + public async Task GetAsync(string name) { using (var dbContext = _dbContextFactory.CreateDbContext()) { @@ -32,7 +32,17 @@ public async Task GetAsync(string name) } } + async Task IPlayerRepository.GetOrCreateAsync(PlayerCredentials playerCredentials) + { + return await GetOrCreateAsync(playerCredentials); + } + public async Task AddOrUpdateAsync(Player player) + { + return await AddOrUpdateAsync((TPlayer) player); + } + + public async Task AddOrUpdateAsync(TPlayer player) { using (var dbContext = _dbContextFactory.CreateDbContext()) { @@ -61,7 +71,7 @@ public async Task AddOrUpdateAsync(Player player) } } - private Task GetAsync(string name, DatabaseContext databaseContext) + private Task GetAsync(string name, DatabaseContext databaseContext) { var normalizedName = name.ToLower(); return databaseContext.Players @@ -69,7 +79,12 @@ private Task GetAsync(string name, DatabaseContext databaseContext) .FirstOrDefaultAsync(player => player.Username.ToLower().Equals(normalizedName)); } - public async Task GetOrCreateAsync(PlayerCredentials playerCredentials) + async Task IPlayerRepository.GetAsync(string name) + { + return await GetAsync(name); + } + + public async Task GetOrCreateAsync(PlayerCredentials playerCredentials) { using (var dbContext = _dbContextFactory.CreateDbContext()) { @@ -77,7 +92,7 @@ public async Task GetOrCreateAsync(PlayerCredentials playerCredentials) var playerInDatabase = await GetAsync(playerCredentials.Username, dbContext); if (playerInDatabase == null) { - var defaultPlayer = new Player + var defaultPlayer = new TPlayer { Username = playerCredentials.Username, Password = playerCredentials.Password, diff --git a/NetScape.Modules.DAL/Migrations/20201226151013_AddInitial.Designer.cs b/NetScape.Modules.DAL/Migrations/20201226151013_AddInitial.Designer.cs index ca55f9ac..7cf74175 100644 --- a/NetScape.Modules.DAL/Migrations/20201226151013_AddInitial.Designer.cs +++ b/NetScape.Modules.DAL/Migrations/20201226151013_AddInitial.Designer.cs @@ -3,12 +3,13 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using NetScape.Abstractions.Model.Game; using NetScape.Modules.DAL; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace NetScape.Modules.DAL.Migrations { - [DbContext(typeof(DatabaseContext))] + [DbContext(typeof(DatabaseContext))] [Migration("20201226151013_AddInitial")] partial class AddInitial { diff --git a/NetScape.Modules.DAL/Migrations/20210123212503_RemoveAppearanceFields.Designer.cs b/NetScape.Modules.DAL/Migrations/20210123212503_RemoveAppearanceFields.Designer.cs index 64c01e22..b59fea3a 100644 --- a/NetScape.Modules.DAL/Migrations/20210123212503_RemoveAppearanceFields.Designer.cs +++ b/NetScape.Modules.DAL/Migrations/20210123212503_RemoveAppearanceFields.Designer.cs @@ -3,12 +3,13 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using NetScape.Abstractions.Model.Game; using NetScape.Modules.DAL; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace NetScape.Modules.DAL.Migrations { - [DbContext(typeof(DatabaseContext))] + [DbContext(typeof(DatabaseContext))] [Migration("20210123212503_RemoveAppearanceFields")] partial class RemoveAppearanceFields { diff --git a/NetScape.Modules.DAL/Migrations/DatabaseContextModelSnapshot.cs b/NetScape.Modules.DAL/Migrations/DatabaseContextModelSnapshot.cs index 6765b3b3..ecb75d4b 100644 --- a/NetScape.Modules.DAL/Migrations/DatabaseContextModelSnapshot.cs +++ b/NetScape.Modules.DAL/Migrations/DatabaseContextModelSnapshot.cs @@ -2,12 +2,13 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using NetScape.Abstractions.Model.Game; using NetScape.Modules.DAL; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace NetScape.Modules.DAL.Migrations { - [DbContext(typeof(DatabaseContext))] + [DbContext(typeof(DatabaseContext))] partial class DatabaseContextModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) diff --git a/NetScape/DesignTimeDbContextFactory.cs b/NetScape/DesignTimeDbContextFactory.cs index af52376f..a180c6fe 100644 --- a/NetScape/DesignTimeDbContextFactory.cs +++ b/NetScape/DesignTimeDbContextFactory.cs @@ -1,21 +1,22 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; +using NetScape.Abstractions.Model.Game; using NetScape.Core; using NetScape.Modules.DAL; namespace NetScape { - public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory + public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory> { - public DatabaseContext CreateDbContext(string[] args) + public DatabaseContext CreateDbContext(string[] args) { var configRoot = ServerHandler.CreateConfigurationRoot("appsettings.json"); - var optionsBuilder = new DbContextOptionsBuilder(); + var optionsBuilder = new DbContextOptionsBuilder>(); optionsBuilder.UseNpgsql(configRoot.GetConnectionString("NetScape"), - x => x.MigrationsAssembly(typeof(DatabaseContext) + x => x.MigrationsAssembly(typeof(DatabaseContext) .Assembly.GetName().Name)); - return new DatabaseContext(optionsBuilder.Options); + return new DatabaseContext(optionsBuilder.Options); } } } diff --git a/NetScape/Kernel.cs b/NetScape/Kernel.cs index 08286e6d..d2a20a07 100644 --- a/NetScape/Kernel.cs +++ b/NetScape/Kernel.cs @@ -10,6 +10,7 @@ using NetScape.Modules.ThreeOneSeven.LoginProtocol; using NetScape.Modules.ThreeOneSeven.World.Updating; using System.Collections.Generic; +using NetScape.Abstractions.Model.Game; namespace NetScape { @@ -27,14 +28,14 @@ public static void Main(string[] args) new ThreeOneSevenLoginModule(), new ThreeOneSevenUpdatingModule() }; - ServerHandler.RunServer("appsettings.json", BuildDbOptions, modules); + ServerHandler.RunServer("appsettings.json", BuildDbOptions, modules); Console.ReadLine(); } private static void BuildDbOptions(DbContextOptionsBuilder optionsBuilder, IConfigurationRoot configurationRoot) { optionsBuilder.UseNpgsql(configurationRoot.GetConnectionString("NetScape"), - x => x.MigrationsAssembly(typeof(DatabaseContext) + x => x.MigrationsAssembly(typeof(DatabaseContext) .Assembly.FullName)); } }