From f7e0027b866ead589f243af906406de94bf95ff1 Mon Sep 17 00:00:00 2001 From: game4353 <73753515+game4353@users.noreply.github.com> Date: Wed, 29 May 2024 16:27:10 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8FProtocol.Echelon=5FSave?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/ProtocolHandlers/Echelon.cs | 22 +++++- SCHALE.GameServer/GameServer.cs | 74 ++++++++++++++----- SCHALE.GameServer/SCHALE.GameServer.csproj | 1 + SCHALE.GameServer/Services/AutoMapper.cs | 13 ++++ 4 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 SCHALE.GameServer/Services/AutoMapper.cs diff --git a/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs b/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs index 0eaf63a..8357395 100644 --- a/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs +++ b/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Echelon.cs @@ -1,4 +1,5 @@ -using SCHALE.Common.Database; +using AutoMapper; +using SCHALE.Common.Database; using SCHALE.Common.Database.ModelExtensions; using SCHALE.Common.FlatData; using SCHALE.Common.NetworkProtocol; @@ -10,13 +11,16 @@ namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers IProtocolHandlerFactory protocolHandlerFactory, ISessionKeyService _sessionKeyService, SCHALEContext _context, - ExcelTableService _excelTableService + ExcelTableService _excelTableService, + IMapper _mapper ) : ProtocolHandlerBase(protocolHandlerFactory) { private readonly ISessionKeyService sessionKeyService = _sessionKeyService; private readonly SCHALEContext context = _context; private readonly ExcelTableService excelTableService = _excelTableService; + private readonly IMapper mapper = _mapper; + [ProtocolHandler(Protocol.Echelon_List)] public ResponsePacket ListHandler(EchelonListRequest req) { @@ -29,7 +33,19 @@ namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers public ResponsePacket SaveHandler(EchelonSaveRequest req) { var db = req.EchelonDB; - context.Echelons.Add(db); + var old = context.Echelons.FirstOrDefault(e => + e.AccountServerId == db.AccountServerId + && e.EchelonType == db.EchelonType + && e.EchelonNumber == db.EchelonNumber + && e.ExtensionType == db.ExtensionType + ); + if (old == null) + context.Echelons.Add(db); + else + { + db.ServerId = old.ServerId; + mapper.Map(db, old); + } context.SaveChanges(); return new EchelonSaveResponse() { EchelonDB = db, }; } diff --git a/SCHALE.GameServer/GameServer.cs b/SCHALE.GameServer/GameServer.cs index 777eb7f..5210127 100644 --- a/SCHALE.GameServer/GameServer.cs +++ b/SCHALE.GameServer/GameServer.cs @@ -1,15 +1,16 @@ -using Microsoft.AspNetCore.Server.Kestrel.Core; -using Serilog.Events; -using Serilog; +using System.Net.NetworkInformation; using System.Reflection; +using AutoMapper; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.EntityFrameworkCore; using SCHALE.Common.Database; +using SCHALE.GameServer.Commands; using SCHALE.GameServer.Controllers.Api.ProtocolHandlers; using SCHALE.GameServer.Services; -using Microsoft.EntityFrameworkCore; using SCHALE.GameServer.Services.Irc; -using SCHALE.GameServer.Commands; using SCHALE.GameServer.Utils; -using System.Net.NetworkInformation; +using Serilog; +using Serilog.Events; namespace SCHALE.GameServer { @@ -20,16 +21,26 @@ namespace SCHALE.GameServer var config = new ConfigurationBuilder() .SetBasePath(Path.GetDirectoryName(AppContext.BaseDirectory)!) .AddJsonFile("appsettings.json") - .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true) + .AddJsonFile( + $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", + true + ) .AddJsonFile("appsettings.Local.json", true) .Build(); { - var logFilePath = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory)!, "logs", "log.txt"); + var logFilePath = Path.Combine( + Path.GetDirectoryName(AppContext.BaseDirectory)!, + "logs", + "log.txt" + ); if (File.Exists(logFilePath)) { - var prevLogFilePath = Path.Combine(Path.GetDirectoryName(logFilePath)!, "log-prev.txt"); + var prevLogFilePath = Path.Combine( + Path.GetDirectoryName(logFilePath)!, + "log-prev.txt" + ); if (File.Exists(prevLogFilePath)) File.Delete(prevLogFilePath); @@ -37,10 +48,14 @@ namespace SCHALE.GameServer } Log.Logger = new LoggerConfiguration() - .WriteTo.Console() - .WriteTo.File(logFilePath, restrictedToMinimumLevel: LogEventLevel.Verbose, shared: true) - .ReadFrom.Configuration(config) - .CreateBootstrapLogger(); + .WriteTo.Console() + .WriteTo.File( + logFilePath, + restrictedToMinimumLevel: LogEventLevel.Verbose, + shared: true + ) + .ReadFrom.Configuration(config) + .CreateBootstrapLogger(); } Log.Information("Starting..."); @@ -54,26 +69,49 @@ namespace SCHALE.GameServer if (Config.Instance.Address == "127.0.0.1") { - Config.Instance.Address = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.OperationalStatus == OperationalStatus.Up).First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().Address.ToString(); + Config.Instance.Address = NetworkInterface + .GetAllNetworkInterfaces() + .Where(i => + i.NetworkInterfaceType != NetworkInterfaceType.Loopback + && i.OperationalStatus == OperationalStatus.Up + ) + .First() + .GetIPProperties() + .UnicastAddresses.Where(a => + a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork + ) + .First() + .Address.ToString(); Config.Save(); } var builder = WebApplication.CreateBuilder(args); - builder.Services.Configure(op => op.AllowSynchronousIO = true); + builder.Services.Configure(op => + op.AllowSynchronousIO = true + ); builder.Host.UseSerilog(); // Add services to the container. - builder.Services.AddSQLServerProvider(config.GetConnectionString("SQLServer") ?? throw new ArgumentNullException("ConnectionStrings/SQLServer in appsettings is missing")); + builder.Services.AddSQLServerProvider( + config.GetConnectionString("SQLServer") + ?? throw new ArgumentNullException( + "ConnectionStrings/SQLServer in appsettings is missing" + ) + ); builder.Services.AddControllers(); + builder.Services.AddAutoMapper(typeof(GameServer)); builder.Services.AddProtocolHandlerFactory(); builder.Services.AddMemorySessionKeyService(); builder.Services.AddExcelTableService(); builder.Services.AddIrcService(); // Add all Handler Groups - var handlerGroups = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(ProtocolHandlerBase))); - + var handlerGroups = Assembly + .GetExecutingAssembly() + .GetTypes() + .Where(t => t.IsSubclassOf(typeof(ProtocolHandlerBase))); + foreach (var handlerGroup in handlerGroups) builder.Services.AddProtocolHandlerGroupByType(handlerGroup); diff --git a/SCHALE.GameServer/SCHALE.GameServer.csproj b/SCHALE.GameServer/SCHALE.GameServer.csproj index 8fd2e70..ef5400b 100644 --- a/SCHALE.GameServer/SCHALE.GameServer.csproj +++ b/SCHALE.GameServer/SCHALE.GameServer.csproj @@ -7,6 +7,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/SCHALE.GameServer/Services/AutoMapper.cs b/SCHALE.GameServer/Services/AutoMapper.cs new file mode 100644 index 0000000..48b2675 --- /dev/null +++ b/SCHALE.GameServer/Services/AutoMapper.cs @@ -0,0 +1,13 @@ +using AutoMapper; +using SCHALE.Common.Database; + +namespace SCHALE.GameServer.Services +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + CreateMap(); + } + } +}