Skip to content

Commit

Permalink
Add ability to change player object
Browse files Browse the repository at this point in the history
  • Loading branch information
JayArrowz committed Jul 17, 2021
1 parent 99880c2 commit 1a14ff3
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 37 deletions.
23 changes: 14 additions & 9 deletions NetScape.Core/ServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DbContextOptionsBuilder, IConfigurationRoot> dbOptions, List<Module> modules)
public static ILifetimeScope RunServer<TPlayer>(string configFileName, Action<DbContextOptionsBuilder, IConfigurationRoot> dbOptions, List<Module> modules)
where TPlayer : Player, new()
{
var serviceCollection = new ServiceCollection();
var config = ConfigureServices(serviceCollection, configFileName, dbOptions);
var config = ConfigureServices<TPlayer>(serviceCollection, configFileName, dbOptions);
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
ConfigureAutofac(containerBuilder, config, modules);
ConfigureAutofac<TPlayer>(containerBuilder, config, modules);
containerBuilder.RegisterBuildCallback(t => t.Resolve<ContainerProvider>().Container = (IContainer)t);
var container = containerBuilder.Build();
var serviceProvider = new AutofacServiceProvider(container);
Expand All @@ -39,11 +41,12 @@ public static ILifetimeScope RunServer(string configFileName, Action<DbContextOp
return scope;
}

public static void ConfigureCore(this ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot)
public static void ConfigureCore<TPlayer>(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<TPlayer>());
containerBuilder.RegisterModule(new GameServerModule(configurationRoot["BindAddr"], ushort.Parse(configurationRoot["BindPort"])));
containerBuilder.RegisterModule(new WorldModule());
containerBuilder.RegisterModule(new RegionModule());
Expand All @@ -53,25 +56,27 @@ public static void ConfigureCore(this ContainerBuilder containerBuilder, IConfig
containerBuilder.RegisterType<ContainerProvider>().SingleInstance();
}

private static void ConfigureAutofac(ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot, List<Module> modules)
private static void ConfigureAutofac<TPlayer>(ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot, List<Module> modules)
where TPlayer : Player, new()
{
foreach (var module in modules)
{
containerBuilder.RegisterModule(module);
}

containerBuilder.ConfigureCore(configurationRoot);
containerBuilder.ConfigureCore<TPlayer>(configurationRoot);
}

public static IConfigurationRoot ConfigureServices(this IServiceCollection serviceCollection, string configFileName, Action<DbContextOptionsBuilder, IConfigurationRoot> optionsAction)
public static IConfigurationRoot ConfigureServices<TPlayer>(this IServiceCollection serviceCollection, string configFileName, Action<DbContextOptionsBuilder, IConfigurationRoot> optionsAction)
where TPlayer : Player, new()
{
var configurationRoot = CreateConfigurationRoot(configFileName);

// Add logging
serviceCollection.AddLogging();

//Build DB Connection
serviceCollection.AddDbContextFactory<DatabaseContext>(opts => optionsAction(opts, configurationRoot));
serviceCollection.AddDbContextFactory<DatabaseContext<TPlayer>>(opts => optionsAction(opts, configurationRoot));

// Add access to generic IConfigurationRoot
serviceCollection.AddSingleton(configurationRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ namespace NetScape.Modules.DAL.Test
{
public class EntityFrameworkPlayerSerializerTests
{
private readonly EntityFrameworkPlayerRepository _entityFrameworkPlayerRepository;
private readonly IDbContextFactory<DatabaseContext> _fakeDbContextFactory;
private readonly EntityFrameworkPlayerRepository<Player> _entityFrameworkPlayerRepository;
private readonly IDbContextFactory<DatabaseContext<Player>> _fakeDbContextFactory;

public EntityFrameworkPlayerSerializerTests()
{
var seedDbId = Guid.NewGuid().ToString();
_fakeDbContextFactory = Substitute.For<IDbContextFactory<DatabaseContext>>();
_fakeDbContextFactory = Substitute.For<IDbContextFactory<DatabaseContext<Player>>>();
_fakeDbContextFactory.CreateDbContext().ReturnsForAnyArgs(x =>
new DatabaseContext(
new DbContextOptionsBuilder<DatabaseContext>().UseInMemoryDatabase(seedDbId).Options
new DatabaseContext<Player>(
new DbContextOptionsBuilder<DatabaseContext<Player>>().UseInMemoryDatabase(seedDbId).Options
)
);
_entityFrameworkPlayerRepository = new EntityFrameworkPlayerRepository(_fakeDbContextFactory);
_entityFrameworkPlayerRepository = new EntityFrameworkPlayerRepository<Player>(_fakeDbContextFactory);
}

[Fact]
Expand Down
5 changes: 3 additions & 2 deletions NetScape.Modules.DAL/DALModule.cs
Original file line number Diff line number Diff line change
@@ -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<TPlayer> : Module where TPlayer : Player, new()
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<EntityFrameworkPlayerRepository>().As<IPlayerRepository>();
builder.RegisterType<EntityFrameworkPlayerRepository<TPlayer>>().As<IPlayerRepository>();
}
}
}
6 changes: 3 additions & 3 deletions NetScape.Modules.DAL/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

namespace NetScape.Modules.DAL
{
public partial class DatabaseContext : DbContext
public partial class DatabaseContext<TPlayer> : DbContext where TPlayer : Player
{
private readonly ILoggerFactory _loggerFactory;

public DatabaseContext(DbContextOptions<DatabaseContext> options, ILoggerFactory loggerFactory = null)
public DatabaseContext(DbContextOptions<DatabaseContext<TPlayer>> options, ILoggerFactory loggerFactory = null)
: base(options)
{
_loggerFactory = loggerFactory;
}

public DbSet<Player> Players { get; set; }
public DbSet<TPlayer> Players { get; set; }
public DbSet<Appearance> Appearances { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
29 changes: 22 additions & 7 deletions NetScape.Modules.DAL/EntityFrameworkPlayerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ namespace NetScape.Modules.DAL
/// Serializes/Deserializes players using entity framework
/// </summary>
/// <seealso cref="IPlayerRepository" />
public class EntityFrameworkPlayerRepository : IPlayerRepository
public class EntityFrameworkPlayerRepository<TPlayer> : IPlayerRepository where TPlayer : Player, new()
{
private readonly IDbContextFactory<DatabaseContext> _dbContextFactory;
private readonly IDbContextFactory<DatabaseContext<TPlayer>> _dbContextFactory;

public EntityFrameworkPlayerRepository(IDbContextFactory<DatabaseContext> dbContextFactory)
public EntityFrameworkPlayerRepository(IDbContextFactory<DatabaseContext<TPlayer>> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}

public async Task<Player> GetAsync(string name)
public async Task<TPlayer> GetAsync(string name)
{
using (var dbContext = _dbContextFactory.CreateDbContext())
{
Expand All @@ -32,7 +32,17 @@ public async Task<Player> GetAsync(string name)
}
}

async Task<Player> IPlayerRepository.GetOrCreateAsync(PlayerCredentials playerCredentials)
{
return await GetOrCreateAsync(playerCredentials);
}

public async Task<int> AddOrUpdateAsync(Player player)
{
return await AddOrUpdateAsync((TPlayer) player);
}

public async Task<int> AddOrUpdateAsync(TPlayer player)
{
using (var dbContext = _dbContextFactory.CreateDbContext())
{
Expand Down Expand Up @@ -61,23 +71,28 @@ public async Task<int> AddOrUpdateAsync(Player player)
}
}

private Task<Player> GetAsync(string name, DatabaseContext databaseContext)
private Task<TPlayer> GetAsync(string name, DatabaseContext<TPlayer> databaseContext)
{
var normalizedName = name.ToLower();
return databaseContext.Players
.Include(t => t.Appearance)
.FirstOrDefaultAsync(player => player.Username.ToLower().Equals(normalizedName));
}

public async Task<Player> GetOrCreateAsync(PlayerCredentials playerCredentials)
async Task<Player> IPlayerRepository.GetAsync(string name)
{
return await GetAsync(name);
}

public async Task<TPlayer> GetOrCreateAsync(PlayerCredentials playerCredentials)
{
using (var dbContext = _dbContextFactory.CreateDbContext())
{

var playerInDatabase = await GetAsync(playerCredentials.Username, dbContext);
if (playerInDatabase == null)
{
var defaultPlayer = new Player
var defaultPlayer = new TPlayer
{
Username = playerCredentials.Username,
Password = playerCredentials.Password,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Player>))]
partial class DatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
Expand Down
11 changes: 6 additions & 5 deletions NetScape/DesignTimeDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -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<DatabaseContext>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<DatabaseContext<Player>>
{
public DatabaseContext CreateDbContext(string[] args)
public DatabaseContext<Player> CreateDbContext(string[] args)
{
var configRoot = ServerHandler.CreateConfigurationRoot("appsettings.json");
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext<Player>>();
optionsBuilder.UseNpgsql(configRoot.GetConnectionString("NetScape"),
x => x.MigrationsAssembly(typeof(DatabaseContext)
x => x.MigrationsAssembly(typeof(DatabaseContext<Player>)
.Assembly.GetName().Name));
return new DatabaseContext(optionsBuilder.Options);
return new DatabaseContext<Player>(optionsBuilder.Options);
}
}
}
5 changes: 3 additions & 2 deletions NetScape/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,14 +28,14 @@ public static void Main(string[] args)
new ThreeOneSevenLoginModule(),
new ThreeOneSevenUpdatingModule()
};
ServerHandler.RunServer("appsettings.json", BuildDbOptions, modules);
ServerHandler.RunServer<Player>("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<Player>)
.Assembly.FullName));
}
}
Expand Down

0 comments on commit 1a14ff3

Please sign in to comment.