feat: add testing flag for account creation on login

This commit is contained in:
Timothy Schenk 2023-08-10 11:12:47 +02:00
parent ac1d34ae92
commit 160c90cff5

View file

@ -1,4 +1,5 @@
using CouchDB.Driver.Query.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Server.DB;
@ -11,11 +12,13 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
{
private readonly ILogger<LoginHandler> _logger;
private readonly WonderkingContext _wonderkingContext;
private readonly IConfiguration _configuration;
public LoginHandler(ILogger<LoginHandler> logger, WonderkingContext wonderkingContext)
public LoginHandler(ILogger<LoginHandler> logger, WonderkingContext wonderkingContext, IConfiguration configuration)
{
_logger = logger;
_wonderkingContext = wonderkingContext;
_configuration = configuration;
}
public void Handle(LoginInfoPacket packet)
@ -25,10 +28,19 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
var account = _wonderkingContext.Accounts.FirstOrDefault(a => a.Username == packet.Username);
if (account == null)
{
_wonderkingContext.Accounts.AddAsync(new Account(packet.Username, packet.Password, "", 0));
if (_configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin"))
{
_wonderkingContext.Accounts.AddAsync(new Account(packet.Username, packet.Password, "", 0));
}
else
{
// TODO: Send Message that account does not exist
_logger.LogInformation("Requested account for user: {Username} does not exist", packet.Username);
}
}
var x = _wonderkingContext.Accounts.ToArray();
Parallel.ForEach(x.AsParallel(),
var accounts = _wonderkingContext.Accounts.ToArray();
Parallel.ForEach(accounts.AsParallel(),
(x, i) => { _logger.LogInformation("Account {Data}", JsonConvert.SerializeObject(x)); });
}
}