From 160c90cff5a11b410373b8d030e7bcd28a0a14d3 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Thu, 10 Aug 2023 11:12:47 +0200 Subject: [PATCH] feat: add testing flag for account creation on login --- Server/PacketHandlers/LoginInfoHandler.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Server/PacketHandlers/LoginInfoHandler.cs b/Server/PacketHandlers/LoginInfoHandler.cs index 4454fb3..aa5faac 100644 --- a/Server/PacketHandlers/LoginInfoHandler.cs +++ b/Server/PacketHandlers/LoginInfoHandler.cs @@ -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 { private readonly ILogger _logger; private readonly WonderkingContext _wonderkingContext; + private readonly IConfiguration _configuration; - public LoginHandler(ILogger logger, WonderkingContext wonderkingContext) + public LoginHandler(ILogger logger, WonderkingContext wonderkingContext, IConfiguration configuration) { _logger = logger; _wonderkingContext = wonderkingContext; + _configuration = configuration; } public void Handle(LoginInfoPacket packet) @@ -25,10 +28,19 @@ public class LoginHandler : IPacketHandler 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("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)); }); } } \ No newline at end of file