2024-06-03 19:06:25 -05:00
|
|
|
|
using JetBrains.Annotations;
|
2024-08-04 17:53:19 -05:00
|
|
|
|
using LANCommander.Launcher.Data;
|
|
|
|
|
|
using LANCommander.Launcher.Data.Models;
|
2024-07-15 18:17:04 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-09-11 00:24:34 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-06-03 19:06:25 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2024-08-04 17:53:19 -05:00
|
|
|
|
namespace LANCommander.Launcher.Services
|
2024-06-03 19:06:25 -05:00
|
|
|
|
{
|
|
|
|
|
|
public class PlaySessionService : BaseDatabaseService<PlaySession>
|
|
|
|
|
|
{
|
2024-07-08 19:51:17 -05:00
|
|
|
|
|
2024-09-11 00:24:34 -05:00
|
|
|
|
public PlaySessionService(DatabaseContext dbContext, SDK.Client client, ILogger<CollectionService> logger) : base(dbContext, client, logger) { }
|
2024-06-03 19:06:25 -05:00
|
|
|
|
|
2024-07-15 18:17:04 -05:00
|
|
|
|
public async Task<PlaySession> GetLatestSession(Guid gameId, Guid userId)
|
|
|
|
|
|
{
|
2025-01-19 00:03:21 -06:00
|
|
|
|
return await Query(ps => ps.GameId == gameId && ps.UserId == userId).OrderByDescending(ps => ps.End).FirstOrDefaultAsync();
|
2024-07-15 18:17:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-03 19:06:25 -05:00
|
|
|
|
public async Task StartSession(Guid gameId, Guid userId)
|
|
|
|
|
|
{
|
2024-09-16 21:00:42 -05:00
|
|
|
|
try
|
2024-06-03 19:06:25 -05:00
|
|
|
|
{
|
2025-01-19 00:03:21 -06:00
|
|
|
|
var existingSession = Query(ps => ps.GameId == gameId && ps.UserId == userId && ps.End == null).FirstOrDefault();
|
2024-06-03 19:06:25 -05:00
|
|
|
|
|
2024-09-16 21:00:42 -05:00
|
|
|
|
if (existingSession != null)
|
2025-01-19 00:03:21 -06:00
|
|
|
|
await DeleteAsync(existingSession);
|
2024-07-08 19:51:17 -05:00
|
|
|
|
|
2024-09-16 21:00:42 -05:00
|
|
|
|
var session = new PlaySession()
|
|
|
|
|
|
{
|
|
|
|
|
|
GameId = gameId,
|
|
|
|
|
|
UserId = userId,
|
|
|
|
|
|
Start = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-01-19 00:03:21 -06:00
|
|
|
|
await AddAsync(session);
|
2024-10-02 12:28:46 -05:00
|
|
|
|
|
2025-01-15 02:28:11 -06:00
|
|
|
|
if (Client.IsConnected())
|
2025-02-23 15:39:06 -06:00
|
|
|
|
await Client.Games.StartedAsync(gameId);
|
2024-09-16 21:00:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "An unknown error occurred while trying to start session recording for game with ID {GameId}", gameId);
|
|
|
|
|
|
}
|
2024-06-03 19:06:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task EndSession(Guid gameId, Guid userId)
|
|
|
|
|
|
{
|
2024-09-16 21:00:42 -05:00
|
|
|
|
try
|
2024-06-03 19:06:25 -05:00
|
|
|
|
{
|
2025-01-19 00:03:21 -06:00
|
|
|
|
var existingSession = Query(ps => ps.GameId == gameId && ps.UserId == userId && ps.End == null).FirstOrDefault();
|
2024-06-03 19:06:25 -05:00
|
|
|
|
|
2024-09-16 21:00:42 -05:00
|
|
|
|
if (existingSession != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
existingSession.End = DateTime.UtcNow;
|
2024-07-08 19:51:17 -05:00
|
|
|
|
|
2025-01-19 00:03:21 -06:00
|
|
|
|
await UpdateAsync(existingSession);
|
2024-09-16 21:00:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "An unknown error occurred while trying to end session recording for game with ID {GameId}", gameId);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
2025-02-23 15:39:06 -06:00
|
|
|
|
await Client.Games.StoppedAsync(gameId);
|
2024-09-16 21:00:42 -05:00
|
|
|
|
}
|
2024-06-03 19:06:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|