2026-07-02 18:09:14 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data.Models;
|
2023-12-11 17:29:06 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-08-12 00:15:16 -05:00
|
|
|
|
using LANCommander.SDK.Enums;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-11-17 02:28:46 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-11-17 02:28:46 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class PlaySessionService(
|
|
|
|
|
|
ILogger<PlaySessionService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IFusionCache cache,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory,
|
2026-06-14 00:53:19 -05:00
|
|
|
|
PlaySessionKeepAliveTracker keepAliveTracker,
|
2026-07-02 18:09:14 -05:00
|
|
|
|
ServerService serverService) : BaseDatabaseService<PlaySession>(logger, settingsProvider, cache, httpContextAccessor, contextFactory)
|
2023-11-17 02:28:46 -06:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
public override async Task<PlaySession> AddAsync(PlaySession entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(ps => ps.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(ps => ps.User);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-02 14:58:07 -06:00
|
|
|
|
public override async Task<PlaySession> UpdateAsync(PlaySession entity)
|
2024-08-28 20:33:59 -05:00
|
|
|
|
{
|
2025-02-02 14:58:07 -06:00
|
|
|
|
return await base.UpdateAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(ps => ps.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(ps => ps.User);
|
|
|
|
|
|
});
|
2023-12-11 17:29:06 -06:00
|
|
|
|
}
|
2023-11-17 02:28:46 -06:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task StartSessionAsync(Guid gameId, Guid userId)
|
2023-11-17 02:28:46 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var existingSession = await FirstOrDefaultAsync(ps => ps.GameId == gameId && ps.UserId == userId && ps.End == null);
|
2023-11-17 02:28:46 -06:00
|
|
|
|
|
|
|
|
|
|
if (existingSession != null)
|
2024-11-12 18:32:46 -06:00
|
|
|
|
await DeleteAsync(existingSession);
|
2023-11-17 02:28:46 -06:00
|
|
|
|
|
|
|
|
|
|
var session = new PlaySession()
|
|
|
|
|
|
{
|
|
|
|
|
|
GameId = gameId,
|
|
|
|
|
|
UserId = userId,
|
|
|
|
|
|
Start = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
await AddAsync(session);
|
2026-06-14 00:53:19 -05:00
|
|
|
|
|
|
|
|
|
|
await keepAliveTracker.TouchAsync(gameId, userId);
|
2023-11-17 02:28:46 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task EndSessionAsync(Guid gameId, Guid userId)
|
2023-11-17 02:28:46 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var existingSession = await FirstOrDefaultAsync(ps => ps.GameId == gameId && ps.UserId == userId && ps.End == null);
|
2023-11-17 02:28:46 -06:00
|
|
|
|
|
|
|
|
|
|
if (existingSession != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
existingSession.End = DateTime.UtcNow;
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
await UpdateAsync(existingSession);
|
2023-11-17 02:28:46 -06:00
|
|
|
|
}
|
2026-06-14 00:53:19 -05:00
|
|
|
|
|
|
|
|
|
|
await keepAliveTracker.RemoveAsync(gameId, userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Records that the player is still in the game. Tracked via the cache; the sweep uses it to
|
|
|
|
|
|
/// detect sessions that have gone silent.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task KeepAliveAsync(Guid gameId, Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await keepAliveTracker.TouchAsync(gameId, userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ends any active session whose last keepalive is older than <paramref name="timeout"/>,
|
|
|
|
|
|
/// setting its End to that last known-alive time. Returns the affected games so callers can
|
|
|
|
|
|
/// schedule server autostop.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<IEnumerable<Guid>> EndStaleSessionsAsync(TimeSpan timeout)
|
|
|
|
|
|
{
|
|
|
|
|
|
var cutoff = DateTime.UtcNow - timeout;
|
|
|
|
|
|
|
|
|
|
|
|
var activeSessions = await GetAsync(ps => ps.End == null);
|
|
|
|
|
|
|
|
|
|
|
|
var affectedGameIds = new HashSet<Guid>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var session in activeSessions)
|
|
|
|
|
|
{
|
|
|
|
|
|
var lastSeen = await keepAliveTracker.GetOrSeedAsync(session.GameId.GetValueOrDefault(), session.UserId);
|
|
|
|
|
|
|
|
|
|
|
|
if (lastSeen >= cutoff)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
session.End = lastSeen;
|
|
|
|
|
|
|
|
|
|
|
|
await UpdateAsync(session);
|
|
|
|
|
|
|
|
|
|
|
|
await keepAliveTracker.RemoveAsync(session.GameId.GetValueOrDefault(), session.UserId);
|
|
|
|
|
|
|
|
|
|
|
|
if (session.GameId.HasValue)
|
|
|
|
|
|
affectedGameIds.Add(session.GameId.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return affectedGameIds;
|
2023-11-17 02:28:46 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|