Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed error handling of map module #174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
ports:
- 2350:2350/udp
- 2350:2350/tcp
- "127.0.0.1:5001:5000/tcp" # Be careful opening XMLRPC! Only if you really need to.
- "127.0.0.1:5000:5000/tcp" # Be careful opening XMLRPC! Only if you really need to.
environment:
MASTER_LOGIN: "SERVERLOGIN"
MASTER_PASSWORD: "SERVERPASS"
Expand All @@ -26,6 +26,8 @@ services:
pgadmin:
image: dpage/pgadmin4
restart: always
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: CHANGEME
Expand Down
7 changes: 7 additions & 0 deletions examples/SimpleModule/SimpleModule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
AtomicLiquid marked this conversation as resolved.
Show resolved Hide resolved
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions src/EvoSC.CLI/EvoSC.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

Expand Down
4 changes: 4 additions & 0 deletions src/EvoSC.Commands/EvoSC.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

<ItemGroup>
<PackageReference Include="SimpleInjector" Version="5.4.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/EvoSC.Common/Database/Models/Maps/DbMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DbMap : IMap
public string Name { get; set; }

[Column]
public string ExternalId { get; set; }
public string? ExternalId { get; set; }

[Column]
public DateTime? ExternalVersion { get; set; }
Expand Down
11 changes: 6 additions & 5 deletions src/EvoSC.Common/Database/Repository/Maps/MapRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EvoSC.Common.Database.Models.Maps;
using EvoSC.Common.Database.Models.Player;
using EvoSC.Common.Exceptions.DatabaseExceptions;
using EvoSC.Common.Interfaces.Database;
using EvoSC.Common.Interfaces.Database.Repository;
using EvoSC.Common.Interfaces.Models;
Expand Down Expand Up @@ -50,11 +51,11 @@ public async Task<IMap> AddMapAsync(MapMetadata map, IPlayer author, string file
await transaction.CommitAsync();
dbMap.Id = Convert.ToInt64(id);
}
catch (Exception ex)
catch (Exception e)
{
_logger.LogError(ex, "Failed to add map");
_logger.LogError(e, "Failed adding map with UID {MapMapUid} to the database", map.MapUid);
await transaction.RollbackAsync();
throw;
throw new EvoScDatabaseException($"Failed adding map with UID {map.MapUid} to the database", e);
}

return dbMap;
Expand Down Expand Up @@ -91,9 +92,9 @@ await Table<DbMap>()
}
catch (Exception e)
{
_logger.LogError(e, "Failed to update map");
_logger.LogError(e, "Failed to update map with UID {MapMapUid}", map.MapUid);
await transaction.RollbackAsync();
throw;
throw new EvoScDatabaseException($"Failed to update map with UID {map.MapUid}", e);
}

return new Map(updatedMap);
Expand Down
4 changes: 4 additions & 0 deletions src/EvoSC.Common/EvoSC.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
<PackageReference Include="MySqlConnector" Version="2.2.6" />
<PackageReference Include="Samboy063.Tomlet" Version="5.0.1" />
<PackageReference Include="SimpleInjector" Version="5.4.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions src/EvoSC.Common/Exceptions/DuplicateMapException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Runtime.Serialization;

namespace EvoSC.Common.Exceptions;

public class DuplicateMapException : EvoSCException
{
public DuplicateMapException()
{
}

protected DuplicateMapException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

public DuplicateMapException(string? message) : base(message)
{
}

public DuplicateMapException(string? message, Exception? innerException) : base(message, innerException)
{
}
}
2 changes: 1 addition & 1 deletion src/EvoSC.Common/Interfaces/Models/IMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface IMap
/// <summary>
/// The maps external ID. E.g. the TrackmaniaIo ID.
/// </summary>
public string ExternalId { get; set; }
public string? ExternalId { get; set; }

/// <summary>
/// The maps external version. E.g. the "last updated" value of the TrackmaniaIo map.
Expand Down
2 changes: 1 addition & 1 deletion src/EvoSC.Common/Models/Maps/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Map: IMap
public IPlayer Author { get; set; }
public string FilePath { get; set; }
public bool Enabled { get; set; }
public string ExternalId { get; set; }
public string? ExternalId { get; set; }
public DateTime? ExternalVersion { get; set; }
public MapProviders? ExternalMapProvider { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/EvoSC.Common/Models/Maps/MapMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class MapMetadata
public required string MapName { get; init; }
public required string AuthorId { get; init; }
public required string AuthorName { get; init; }
public required string ExternalId { get; init; }
public required string? ExternalId { get; init; }
public required DateTime? ExternalVersion { get; init; }
public required MapProviders? ExternalMapProvider { get; init; }
}
117 changes: 45 additions & 72 deletions src/EvoSC.Common/Services/MapService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Data;
using EvoSC.Common.Config.Models;
using EvoSC.Common.Database.Repository.Maps;
using EvoSC.Common.Config.Models;
using EvoSC.Common.Exceptions;
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Database.Repository;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Common.Interfaces.Services;
using EvoSC.Common.Models.Maps;
Expand All @@ -12,13 +12,13 @@ namespace EvoSC.Common.Services;

public class MapService : IMapService
{
private readonly MapRepository _mapRepository;
private readonly IMapRepository _mapRepository;
private readonly ILogger<MapService> _logger;
private readonly IEvoScBaseConfig _config;
private readonly IPlayerManagerService _playerService;
private readonly IServerClient _serverClient;

public MapService(MapRepository mapRepository, ILogger<MapService> logger, IEvoScBaseConfig config,
public MapService(IMapRepository mapRepository, ILogger<MapService> logger, IEvoScBaseConfig config,
IPlayerManagerService playerService, IServerClient serverClient)
{
_mapRepository = mapRepository;
Expand All @@ -40,9 +40,8 @@ public async Task<IMap> AddMapAsync(MapStream mapStream)
IMap? existingMap = await GetMapByUidAsync(mapMetadata.MapUid);
if (existingMap != null && MapVersionExistsInDb(existingMap, mapMetadata))
{
// TODO: #79 Expand Map module with more accurate exceptions https://github.com/EvoTM/EvoSC-sharp/issues/79
_logger.LogDebug("Map with UID {MapUid} already exists in database.", mapMetadata.MapUid);
throw new DuplicateNameException($"Map with UID {mapMetadata.MapUid} already exists in database");
_logger.LogDebug("Map with UID {MapUid} already exists in database", mapMetadata.MapUid);
throw new DuplicateMapException($"Map with UID {mapMetadata.MapUid} already exists in database");
}

var fileName = $"{mapMetadata.MapName}.Map.Gbx";
Expand All @@ -54,49 +53,32 @@ public async Task<IMap> AddMapAsync(MapStream mapStream)
? mapMetadata.AuthorId
: PlayerUtils.ConvertLoginToAccountId(mapMetadata.AuthorId);

var author = await GetMapAuthorAsync(playerId, mapMetadata.AuthorName);
var author = await _playerService.GetOrCreatePlayerAsync(playerId);

IMap map;

if (existingMap != null)
{
try
{
_logger.LogDebug("Updating map with ID {MapId} to the database", existingMap.Id);
map = await _mapRepository.UpdateMapAsync(existingMap.Id, mapMetadata);
}
catch (Exception e)
{
_logger.LogWarning(e, "Something went wrong while trying to update map with ID {MapId} to the database",
existingMap.Id);
throw;
}
_logger.LogDebug("Updating map with ID {MapId} to the database", existingMap.Id);
map = await _mapRepository.UpdateMapAsync(existingMap.Id, mapMetadata);
}
else
{
try
{
_logger.LogDebug("Adding map to the database");
map = await _mapRepository.AddMapAsync(mapMetadata, author, filePath);
}
catch (Exception e)
{
_logger.LogWarning(e, $"Something went wrong while trying to add a map to the database");
throw;
}
_logger.LogDebug("Adding map {Name} ({Uid}) to the database", mapMetadata.MapName, mapMetadata.MapUid);
map = await _mapRepository.AddMapAsync(mapMetadata, author, filePath);
}

await _serverClient.Remote.InsertMapAsync($"EvoSC/{fileName}");
await _serverClient.Remote.SaveMatchSettingsAsync($"MatchSettings/{_config.Path.DefaultMatchSettings}");
return map;
}

public async Task<IEnumerable<IMap>> AddMapsAsync(List<MapStream> mapObjects)
public async Task<IEnumerable<IMap>> AddMapsAsync(List<MapStream> mapStreams)
{
var maps = new List<IMap>();
foreach (var mapObject in mapObjects)
foreach (var mapStream in mapStreams)
{
var map = await AddMapAsync(mapObject);
var map = await AddMapAsync(mapStream);
maps.Add(map);
}

Expand Down Expand Up @@ -142,13 +124,42 @@ public async Task AddCurrentMapListAsync()
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to add current map to the database: {Name} ({Uid})",
_logger.LogError(ex, "Failed to add current map to the database: {Name} ({Uid})",
serverMap.Name,
serverMap.UId);
}
}
}

public async Task<IMap> GetOrAddCurrentMapAsync()
{
var currentMap = await _serverClient.Remote.GetCurrentMapInfoAsync();
var map = await GetMapByUidAsync(currentMap.UId);

if (map != null)
{
return map;
}

var mapAuthor =
await _playerService.GetOrCreatePlayerAsync(PlayerUtils.ConvertLoginToAccountId(currentMap.Author));

var mapMeta = new MapMetadata
{
MapUid = currentMap.UId,
MapName = currentMap.Name,
AuthorId = mapAuthor.AccountId,
AuthorName = mapAuthor.NickName,
ExternalId = currentMap.UId,
ExternalVersion = null,
ExternalMapProvider = null
};

map = await _mapRepository.AddMapAsync(mapMeta, mapAuthor, currentMap.FileName);

return map;
}

private static bool MapVersionExistsInDb(IMap map, MapMetadata mapMetadata)
{
return map.ExternalVersion == mapMetadata.ExternalVersion;
Expand All @@ -173,42 +184,4 @@ private async Task SaveMapFileAsync(Stream mapStream, string filePath, string fi
throw;
}
}

private async Task<IPlayer> GetMapAuthorAsync(string authorId, string? name)
{
var dbPlayer = await _playerService.GetPlayerAsync(authorId);

if (dbPlayer == null)
{
return await _playerService.CreatePlayerAsync(authorId, name);
}

return dbPlayer;
}

public async Task<IMap> GetOrAddCurrentMapAsync()
{
var currentMap = await _serverClient.Remote.GetCurrentMapInfoAsync();
var map = await GetMapByUidAsync(currentMap.UId);

if (map == null)
{
var mapAuthor = await _playerService.GetOrCreatePlayerAsync(PlayerUtils.ConvertLoginToAccountId(currentMap.Author));

var mapMeta = new MapMetadata
{
MapUid = currentMap.UId,
MapName = currentMap.Name,
AuthorId = mapAuthor.AccountId,
AuthorName = mapAuthor.NickName,
ExternalId = currentMap.UId,
ExternalVersion = null,
ExternalMapProvider = null
};

map = await _mapRepository.AddMapAsync(mapMeta, mapAuthor, currentMap.FileName);
}

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Samboy063.Tomlet" Version="5.0.1" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/EvoSC.Modules/EvoSC.Modules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/EvoSC.Testing/EvoSC.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<ItemGroup>
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/EvoSC/EvoSC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.4.0-alpha.22272.1" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Modules/CurrentMapModule/CurrentMapModule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

<ItemGroup>
<PackageReference Include="ISO3166" Version="1.0.4" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.5.0.73987">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
Loading
Loading