2024-02-16 00:22:01 -06:00
|
|
|
|
using AutoMapper;
|
2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2023-01-14 15:09:45 -06:00
|
|
|
|
using LANCommander.SDK;
|
2023-12-14 19:09:00 -06:00
|
|
|
|
using LANCommander.SDK.Enums;
|
2024-05-07 23:08:22 -05:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using LANCommander.Server.Services.Models;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-10-11 01:09:12 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public class GameService(
|
|
|
|
|
|
ILogger<GameService> logger,
|
|
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory,
|
|
|
|
|
|
ArchiveService archiveService,
|
2025-02-16 10:08:05 -06:00
|
|
|
|
MediaService mediaService) : BaseDatabaseService<Game>(logger, cache, mapper, httpContextAccessor, contextFactory)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task<Game> AddAsync(Game entity)
|
2024-05-07 23:08:22 -05:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Actions);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Archives);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.BaseGame);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Categories);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Collections);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.CustomFields);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Developers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Engine);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Genres);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Keys);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Libraries);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Media);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.MultiplayerModes);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Pages);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Platforms);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Publishers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Redistributables);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.SavePaths);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Scripts);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Tags);
|
|
|
|
|
|
});
|
2024-05-07 23:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task<ExistingEntityResult<Game>> AddMissingAsync(Expression<Func<Game, bool>> predicate, Game entity)
|
2024-05-07 23:08:22 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(entity.Id);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return await base.AddMissingAsync(predicate, entity);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task<Game> UpdateAsync(Game entity)
|
2024-05-07 23:08:22 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(entity.Id);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
|
2025-02-09 14:52:26 -06:00
|
|
|
|
if (entity.Media != null)
|
|
|
|
|
|
foreach (var media in entity.Media.Where(m => m.Id == Guid.Empty && String.IsNullOrWhiteSpace(m.Crc32)).ToList())
|
|
|
|
|
|
entity.Media.Remove(media);
|
2025-02-02 14:58:07 -06:00
|
|
|
|
|
|
|
|
|
|
var update = await base.UpdateAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Actions);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Archives);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.BaseGame);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Categories);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Collections);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.CustomFields);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Developers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Engine);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Genres);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Keys);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Libraries);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Media);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.MultiplayerModes);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Pages);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Platforms);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Publishers);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Redistributables);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.SavePaths);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Scripts);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(g => g.Tags);
|
|
|
|
|
|
});
|
2024-09-30 20:19:02 -05:00
|
|
|
|
|
2025-02-02 14:58:07 -06:00
|
|
|
|
return update;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
2023-01-09 01:35:08 -06:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public override async Task DeleteAsync(Game game)
|
2023-01-09 01:35:08 -06:00
|
|
|
|
{
|
2024-11-25 22:29:56 -06:00
|
|
|
|
game = await Include(
|
|
|
|
|
|
g => g.Archives,
|
|
|
|
|
|
g => g.Media)
|
|
|
|
|
|
.GetAsync(game.Id);
|
|
|
|
|
|
|
2025-02-09 14:52:26 -06:00
|
|
|
|
if (game.Archives != null)
|
|
|
|
|
|
foreach (var archive in game.Archives.ToList())
|
|
|
|
|
|
await archiveService.DeleteAsync(archive);
|
2023-09-01 11:40:29 -05:00
|
|
|
|
|
2025-02-09 14:52:26 -06:00
|
|
|
|
if (game.Media != null)
|
|
|
|
|
|
foreach (var media in game.Media.ToList())
|
|
|
|
|
|
await mediaService.DeleteAsync(media);
|
2024-05-07 23:08:22 -05:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
2025-02-09 14:52:26 -06:00
|
|
|
|
await base.DeleteAsync(game);
|
2023-01-09 01:35:08 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-23 02:17:06 -05:00
|
|
|
|
public async Task<ICollection<Game>> GetAddonsAsync(Game game)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await GetAsync(g => g.AddonTypes.Contains(g.Type));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-11 02:31:29 -05:00
|
|
|
|
public async Task<SDK.Models.Manifest.Game> GetManifestAsync(Guid id)
|
2023-01-14 15:09:45 -06:00
|
|
|
|
{
|
2025-01-25 12:46:24 -06:00
|
|
|
|
var game = await Query(q =>
|
|
|
|
|
|
{
|
2025-01-30 19:21:02 -06:00
|
|
|
|
return q
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
2025-01-25 12:46:24 -06:00
|
|
|
|
.Include(g => g.Actions)
|
|
|
|
|
|
.Include(g => g.Archives)
|
|
|
|
|
|
.Include(g => g.BaseGame)
|
|
|
|
|
|
.Include(g => g.Categories)
|
|
|
|
|
|
.Include(g => g.Collections)
|
2025-01-30 19:21:02 -06:00
|
|
|
|
.Include(g => g.CustomFields)
|
2025-01-25 12:46:24 -06:00
|
|
|
|
.Include(g => g.DependentGames)
|
|
|
|
|
|
.Include(g => g.Developers)
|
|
|
|
|
|
.Include(g => g.Engine)
|
|
|
|
|
|
.Include(g => g.Genres)
|
|
|
|
|
|
.Include(g => g.Media)
|
|
|
|
|
|
.Include(g => g.MultiplayerModes)
|
|
|
|
|
|
.Include(g => g.Platforms)
|
|
|
|
|
|
.Include(g => g.Publishers)
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
2025-03-13 00:35:58 -05:00
|
|
|
|
.Include(g => g.SavePaths)
|
2025-01-25 12:46:24 -06:00
|
|
|
|
.Include(g => g.Tags);
|
2025-04-11 02:31:29 -05:00
|
|
|
|
}).GetAsync<SDK.Models.Manifest.Game>(id);
|
2023-01-14 15:09:45 -06:00
|
|
|
|
|
2025-04-11 02:31:29 -05:00
|
|
|
|
return game;
|
2024-02-16 00:22:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
|
public async Task<GameCustomField> GetCustomFieldAsync(Guid id, string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.Include(g => g.CustomFields)
|
|
|
|
|
|
.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
return game.CustomFields.FirstOrDefault(c => c.Name == name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<GameCustomField> SetCustomFieldAsync(Guid id, string name, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
var game = await AsNoTracking()
|
|
|
|
|
|
.AsSplitQuery()
|
|
|
|
|
|
.Include(g => g.CustomFields)
|
|
|
|
|
|
.GetAsync(id);
|
|
|
|
|
|
|
|
|
|
|
|
if (game.CustomFields.Any(c => c.Name == name))
|
|
|
|
|
|
foreach (var customField in game.CustomFields.Where(c => c.Name == name))
|
|
|
|
|
|
customField.Value = value;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
game.CustomFields.Add(new GameCustomField());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return await GetCustomFieldAsync(id, name);
|
|
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|