LANCommander/LANCommander.Server.Services/ServerService.cs
2026-07-02 18:09:14 -05:00

149 lines
6 KiB
C#

using LANCommander.Server.Data;
using LANCommander.SDK.Enums;
using LANCommander.SDK.PowerShell;
using Microsoft.Extensions.Logging;
using LANCommander.Server.Services.Extensions;
using LANCommander.Server.Services.Mappers;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Services
{
public sealed class ServerService(
ILogger<ServerService> logger,
PowerShellScriptFactory powerShellScriptFactory,
SettingsProvider<Settings.Settings> settingsProvider,
IFusionCache cache,
SdkMapper sdkMapper,
ManifestMapper manifestMapper,
IHttpContextAccessor httpContextAccessor,
IDbContextFactory<DatabaseContext> contextFactory,
ServerManager serverManager,
UserService userService) : BaseDatabaseService<Data.Models.Server>(logger, settingsProvider, cache, httpContextAccessor, contextFactory)
{
public override async Task<Data.Models.Server> AddAsync(Data.Models.Server entity)
{
await cache.ExpireGameCacheAsync();
entity = await base.AddAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(s => s.Actions);
await context.UpdateRelationshipAsync(s => s.Game);
await context.UpdateRelationshipAsync(s => s.HttpPaths);
await context.UpdateRelationshipAsync(s => s.Pages);
await context.UpdateRelationshipAsync(s => s.Scripts);
await context.UpdateRelationshipAsync(s => s.ServerConsoles);
});
// Update tracking, helpful if tracked server has changed engines
await serverManager.RefreshTrackingAsync();
return entity;
}
public override async Task<Data.Models.Server> UpdateAsync(Data.Models.Server entity)
{
await cache.ExpireGameCacheAsync(entity.GameId);
entity = await base.UpdateAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(s => s.Actions);
await context.UpdateRelationshipAsync(s => s.Game);
await context.UpdateRelationshipAsync(s => s.HttpPaths);
await context.UpdateRelationshipAsync(s => s.Pages);
await context.UpdateRelationshipAsync(s => s.Scripts);
await context.UpdateRelationshipAsync(s => s.ServerConsoles);
});
// Update tracking, helpful if tracked server has changed engines
await serverManager.RefreshTrackingAsync();
return entity;
}
public async Task<SDK.Models.Manifest.Server> GetManifestAsync(Guid serverId)
{
var server = await AsNoTracking()
.AsSplitQuery()
.Query(q =>
{
return q
.Include(s => s.Actions)
.Include(s => s.HttpPaths)
.Include(s => s.ServerConsoles)
.Include(s => s.Scripts);
})
.GetAsync(serverId);
return manifestMapper.ToManifest(server);
}
public async Task RunGameStartedScriptsAsync(Guid serverId, Guid userId)
{
var user = await userService.GetAsync(userId);
var server = await
Include(s => s.Game)
.Include(s => s.Scripts)
.FirstOrDefaultAsync(s => s.Id == serverId);
foreach (var script in server.Scripts.Where(s => s.Type == ScriptType.GameStarted))
{
try
{
var scriptContext = powerShellScriptFactory.Create(ScriptType.GameStarted);
scriptContext.AddVariable("Server", sdkMapper.ToSdk(server));
scriptContext.AddVariable("Game", sdkMapper.ToSdk(server.Game));
scriptContext.AddVariable("User", sdkMapper.ToSdk(user));
scriptContext.UseWorkingDirectory(server.WorkingDirectory);
scriptContext.UseInline(script.Contents);
scriptContext.UseShellExecute();
_logger?.LogInformation("Executing script \"{ScriptName}\"", script.Name);
await scriptContext.ExecuteAsync<int>();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error running script \"{ScriptName}\" for server \"{ServerName}\"", script.Name, server.Name);
}
}
}
public async Task RunGameStoppedScriptsAsync(Guid serverId, Guid userId)
{
var user = await userService.GetAsync(userId);
var server = await
Include(s => s.Game)
.Include(s => s.Scripts)
.FirstOrDefaultAsync(s => s.Id == serverId);
foreach (var script in server.Scripts.Where(s => s.Type == ScriptType.GameStopped))
{
try
{
var scriptContext = powerShellScriptFactory.Create(ScriptType.GameStopped);
scriptContext.AddVariable("Server", sdkMapper.ToSdk(server));
scriptContext.AddVariable("Game", sdkMapper.ToSdk(server.Game));
scriptContext.AddVariable("User", sdkMapper.ToSdk(user));
scriptContext.UseWorkingDirectory(server.WorkingDirectory);
scriptContext.UseInline(script.Contents);
scriptContext.UseShellExecute();
_logger?.LogInformation("Executing script \"{ScriptName}\"", script.Name);
await scriptContext.ExecuteAsync<int>();
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error running script \"{ScriptName}\" for server \"{ServerName}\"", script.Name, server.Name);
}
}
}
}
}