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

Include author nickname when adding a map to the database. #288

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
9 changes: 9 additions & 0 deletions src/EvoSC.Common/Interfaces/Services/IPlayerManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public interface IPlayerManagerService
/// <returns></returns>
public Task<IPlayer> GetOrCreatePlayerAsync(string accountId);

/// <summary>
/// Get a player by their account ID. If the player does not
/// exist in the database, create the entry.
/// </summary>
/// <param name="accountId">Account ID of the player.</param>
/// <param name="name">Name of the new player if created.</param>
/// <returns></returns>
public Task<IPlayer> GetOrCreatePlayerAsync(string accountId, string? name);

/// <summary>
/// Create a new entry for a player in the database.
/// </summary>
Expand Down
68 changes: 61 additions & 7 deletions src/EvoSC.Common/Services/MapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
using EvoSC.Common.Interfaces.Services;
using EvoSC.Common.Models.Maps;
using EvoSC.Common.Util;
using GbxRemoteNet;
using GbxRemoteNet.Interfaces;
using GbxRemoteNet.Structs;
using GbxRemoteNet.XmlRpc.ExtraTypes;
using Microsoft.Extensions.Logging;

namespace EvoSC.Common.Services;
Expand Down Expand Up @@ -85,9 +89,9 @@ public async Task RemoveMapAsync(long mapId)

public async Task AddCurrentMapListAsync()
{
var serverMapList = await serverClient.Remote.GetMapListAsync(-1, 0);
var maplist = await GetRemoteMapListAsync();

foreach (var serverMap in serverMapList)
foreach (var serverMap in maplist)
{
try
{
Expand All @@ -97,9 +101,9 @@ public async Task AddCurrentMapListAsync()
{
continue;
}

var authorAccountId = PlayerUtils.ConvertLoginToAccountId(serverMap.Author);
var author = await playerService.GetOrCreatePlayerAsync(authorAccountId);
var author = await playerService.GetOrCreatePlayerAsync(authorAccountId, serverMap.AuthorNickname);

var mapMeta = new MapMetadata
{
Expand All @@ -115,7 +119,7 @@ public async Task AddCurrentMapListAsync()
logger.LogDebug("Adding map {Name} ({Uid}) to the database", serverMap.Name, serverMap.UId);
var map = await mapRepository.AddMapAsync(mapMeta, author, serverMap.FileName);

var mapDetails = await FetchMapDetailsAsync(map);
var mapDetails = new ParsedMap(serverMap, map);
await mapRepository.AddMapDetailsAsync(mapDetails, map);
}
catch (Exception ex)
Expand All @@ -137,8 +141,8 @@ public async Task<IMap> GetOrAddCurrentMapAsync()
return map;
}

var mapAuthor =
await playerService.GetOrCreatePlayerAsync(PlayerUtils.ConvertLoginToAccountId(currentMap.Author));
var authorAccountId = PlayerUtils.ConvertLoginToAccountId(currentMap.Author);
var mapAuthor = await playerService.GetOrCreatePlayerAsync(authorAccountId, currentMap.AuthorNickname);

var mapMeta = new MapMetadata
{
Expand Down Expand Up @@ -235,4 +239,54 @@ private async Task SaveMapFileAsync(Stream mapStream, string filePath)
throw;
}
}

private async Task<List<TmMapInfo>> GetRemoteMapListAsync()
{
var serverMapList = await serverClient.Remote.GetMapListAsync(-1, 0);
var calls = new List<MultiCall>();

// server can have more maps than calls allowed in a multicall, so split it up
for (var i = 0; i < serverMapList.Length; i++)
{
if (i % 20 == 0)
{
calls.Add(new MultiCall());
}

calls.Last().Add("GetMapInfo", serverMapList[i].FileName);
}

var maplist = new List<TmMapInfo>();

foreach (var call in calls)
{
var mapsWithDetails = await serverClient.Remote.MultiCallAsync(call);

foreach (GbxDynamicObject serverMap in mapsWithDetails)
{
maplist.Add(new TmMapInfo
{
UId = (string)serverMap["UId"],
Name = (string)serverMap["Name"],
FileName = (string)serverMap["FileName"],
Author = (string)serverMap["Author"],
AuthorNickname = (string)serverMap["AuthorNickname"],
Environnement = (string)serverMap["Environnement"],
Mood = (string)serverMap["Mood"],
BronzeTime = (int)serverMap["BronzeTime"],
SilverTime = (int)serverMap["SilverTime"],
GoldTime = (int)serverMap["GoldTime"],
AuthorTime = (int)serverMap["AuthorTime"],
CopperPrice = (int)serverMap["CopperPrice"],
LapRace = (bool)serverMap["LapRace"],
NbLaps = (int)serverMap["NbLaps"],
NbCheckpoints = (int)serverMap["NbCheckpoints"],
MapType = (string)serverMap["MapType"],
MapStyle = (string)serverMap["MapStyle"]
});
}
}

return maplist;
}
}
6 changes: 4 additions & 2 deletions src/EvoSC.Common/Services/PlayerManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class PlayerManagerService(IPlayerRepository playerRepository, IPlayerCac
public async Task<IPlayer?> GetPlayerAsync(string accountId) =>
await playerRepository.GetPlayerByAccountIdAsync(accountId);

public async Task<IPlayer> GetOrCreatePlayerAsync(string accountId)
public Task<IPlayer> GetOrCreatePlayerAsync(string accountId) => GetOrCreatePlayerAsync(accountId, null);

public async Task<IPlayer> GetOrCreatePlayerAsync(string accountId, string? name)
{
try
{
Expand All @@ -34,7 +36,7 @@ public async Task<IPlayer> GetOrCreatePlayerAsync(string accountId)
accountId);
}

return await CreatePlayerAsync(accountId);
return await CreatePlayerAsync(accountId, name);
}

public Task<IPlayer> CreatePlayerAsync(string accountId) => CreatePlayerAsync(accountId, null);
Expand Down
32 changes: 29 additions & 3 deletions tests/EvoSC.Common.Tests/Services/MapServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
using EvoSC.Common.Models.Maps;
using EvoSC.Common.Services;
using EvoSC.Testing;
using GbxRemoteNet;
using GbxRemoteNet.Interfaces;
using GbxRemoteNet.Structs;
using GbxRemoteNet.XmlRpc.ExtraTypes;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
Expand Down Expand Up @@ -370,6 +372,7 @@ public async Task Add_Current_Map_List_Adds_Maplist()
{
Name = "snippens dream",
Author = "0efeba8a-9cda-49fa-ab25-35f1d9218c95",
AuthorNickname = "snippen",
AuthorTime = 1337,
BronzeTime = 1337,
CopperPrice = 1337,
Expand All @@ -388,10 +391,32 @@ public async Task Add_Current_Map_List_Adds_Maplist()
UpdatedAt = new DateTime(),
LastVisit = new DateTime()
};

_server.Remote.Setup(r => r.GetMapListAsync(It.IsAny<int>(), It.IsAny<int>()))
.Returns(Task.FromResult(new[] { tmMapInfo }));
_playerService.Setup(p => p.GetOrCreatePlayerAsync(It.IsAny<string>()))
_server.Remote.Setup(r => r.MultiCallAsync(It.IsAny<MultiCall>()))
.ReturnsAsync([
new GbxDynamicObject()
{
{ "UId", tmMapInfo.UId },
{ "Name", tmMapInfo.Name },
{ "FileName", tmMapInfo.FileName },
{ "Author", tmMapInfo.Author },
{ "AuthorNickname", tmMapInfo.AuthorNickname },
{ "Environnement", tmMapInfo.Environnement },
{ "Mood", tmMapInfo.Mood },
{ "BronzeTime", tmMapInfo.BronzeTime },
{ "SilverTime", tmMapInfo.SilverTime },
{ "GoldTime", tmMapInfo.GoldTime },
{ "AuthorTime", tmMapInfo.AuthorTime },
{ "CopperPrice", tmMapInfo.CopperPrice },
{ "LapRace", tmMapInfo.LapRace },
{ "NbLaps", tmMapInfo.NbLaps },
{ "NbCheckpoints", tmMapInfo.NbCheckpoints },
{ "MapType", tmMapInfo.MapType },
{ "MapStyle", tmMapInfo.MapStyle }
}
]);
_playerService.Setup(p => p.GetOrCreatePlayerAsync(It.IsAny<string>(), "snippen"))
.Returns(Task.FromResult((IPlayer)player));

await _mapService.AddCurrentMapListAsync();
Expand All @@ -407,6 +432,7 @@ public async Task Get_Or_Add_Current_Map_Returns_Current_Map()
{
Name = "snippens track",
Author = "0efeba8a-9cda-49fa-ab25-35f1d9218c95",
AuthorNickname = "snippen",
AuthorTime = 1337,
BronzeTime = 1337,
CopperPrice = 1337,
Expand Down Expand Up @@ -452,7 +478,7 @@ public async Task Get_Or_Add_Current_Map_Returns_Current_Map()
.Returns(Task.FromResult((IMap?)null));
_mapRepository.Setup(m => m.AddMapAsync(It.IsAny<MapMetadata>(), It.IsAny<IPlayer>(), It.IsAny<string>()))
.Returns(Task.FromResult((IMap)map));
_playerService.Setup(p => p.GetOrCreatePlayerAsync(It.IsAny<string>()))
_playerService.Setup(p => p.GetOrCreatePlayerAsync(It.IsAny<string>(), "snippen"))
.Returns(Task.FromResult((IPlayer)player));

var retrievedMap = await _mapService.GetOrAddCurrentMapAsync();
Expand Down
Loading