2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
2025-02-23 15:39:06 -06:00
|
|
|
|
using LANCommander.SDK.Enums;
|
|
|
|
|
|
using LANCommander.SDK.PowerShell;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-03-18 00:44:31 -05:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-03-18 00:44:31 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class ServerService(
|
|
|
|
|
|
ILogger<ServerService> logger,
|
2025-11-26 01:09:52 -06:00
|
|
|
|
PowerShellScriptFactory powerShellScriptFactory,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-02-23 15:39:06 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory,
|
2026-06-13 23:51:37 -05:00
|
|
|
|
ServerManager serverManager,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
UserService userService) : BaseDatabaseService<Data.Models.Server>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2023-03-18 00:44:31 -05:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
public override async Task<Data.Models.Server> AddAsync(Data.Models.Server entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
await cache.ExpireGameCacheAsync();
|
|
|
|
|
|
|
2026-03-11 23:27:33 -05:00
|
|
|
|
entity = await base.AddAsync(entity, async context =>
|
2025-02-23 08:49:43 -06:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
});
|
2026-06-13 23:51:37 -05:00
|
|
|
|
|
2026-03-11 23:27:33 -05:00
|
|
|
|
// Update tracking, helpful if tracked server has changed engines
|
2026-06-13 23:51:37 -05:00
|
|
|
|
await serverManager.RefreshTrackingAsync();
|
2026-03-11 23:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
return entity;
|
2025-02-23 08:49:43 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-25 12:46:35 -06:00
|
|
|
|
public override async Task<Data.Models.Server> UpdateAsync(Data.Models.Server entity)
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(entity.GameId);
|
2026-03-11 23:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
entity = await base.UpdateAsync(entity, async context =>
|
2025-02-02 14:58:07 -06:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
});
|
2026-06-13 23:51:37 -05:00
|
|
|
|
|
2026-03-11 23:27:33 -05:00
|
|
|
|
// Update tracking, helpful if tracked server has changed engines
|
2026-06-13 23:51:37 -05:00
|
|
|
|
await serverManager.RefreshTrackingAsync();
|
2026-03-11 23:27:33 -05:00
|
|
|
|
|
|
|
|
|
|
return entity;
|
2025-01-25 12:46:35 -06:00
|
|
|
|
}
|
2025-02-23 15:39:06 -06:00
|
|
|
|
|
2025-04-11 02:31:29 -05:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
})
|
2025-10-05 19:41:24 -05:00
|
|
|
|
.GetAsync(serverId);
|
2025-04-11 02:31:29 -05:00
|
|
|
|
|
2025-10-05 19:41:24 -05:00
|
|
|
|
return mapper.Map<SDK.Models.Manifest.Server>(server);
|
2025-04-11 02:31:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-23 15:39:06 -06:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2025-11-26 01:09:52 -06:00
|
|
|
|
var scriptContext = powerShellScriptFactory.Create(ScriptType.GameStarted);
|
2025-02-23 15:39:06 -06:00
|
|
|
|
|
|
|
|
|
|
scriptContext.AddVariable("Server", mapper.Map<SDK.Models.Server>(server));
|
|
|
|
|
|
scriptContext.AddVariable("Game", mapper.Map<SDK.Models.Game>(server.Game));
|
|
|
|
|
|
scriptContext.AddVariable("User", mapper.Map<SDK.Models.User>(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
|
|
|
|
|
|
{
|
2025-11-26 01:09:52 -06:00
|
|
|
|
var scriptContext = powerShellScriptFactory.Create(ScriptType.GameStopped);
|
2025-02-23 15:39:06 -06:00
|
|
|
|
|
|
|
|
|
|
scriptContext.AddVariable("Server", mapper.Map<SDK.Models.Server>(server));
|
|
|
|
|
|
scriptContext.AddVariable("Game", mapper.Map<SDK.Models.Game>(server.Game));
|
|
|
|
|
|
scriptContext.AddVariable("User", mapper.Map<SDK.Models.User>(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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-18 00:44:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|