🛠️Protocol.Echelon_Save

This commit is contained in:
game4353 2024-05-29 16:27:10 +08:00
parent 1b042d98e6
commit f7e0027b86
4 changed files with 89 additions and 21 deletions

View File

@ -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, };
}

View File

@ -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,25 +69,48 @@ 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<KestrelServerOptions>(op => op.AllowSynchronousIO = true);
builder.Services.Configure<KestrelServerOptions>(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);

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>

View File

@ -0,0 +1,13 @@
using AutoMapper;
using SCHALE.Common.Database;
namespace SCHALE.GameServer.Services
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<EchelonDB, EchelonDB>();
}
}
}