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

FixArcadePlayers #509

Merged
merged 1 commit into from
Aug 3, 2024
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
8 changes: 3 additions & 5 deletions src/SC2ArcadeCrawler/CrawlerService.MapPlayers.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using dsstats.db8;
using dsstats.shared.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using dsstats.shared;

namespace pax.dsstats.web.Server.Services.Arcade;

Expand All @@ -21,9 +20,8 @@ private async Task MapPlayers(List<ArcadeReplay> replays, CancellationToken toke
{
continue;
}
int playerId = await importService.GetPlayerIdAsync(new(replayDsPlayer.Player.RegionId,
replayDsPlayer.Player.RealmId,
replayDsPlayer.Player.ToonId),
PlayerId dsplayerId = new(replayDsPlayer.Player.ToonId, replayDsPlayer.Player.RealmId, replayDsPlayer.Player.RegionId);
int playerId = await importService.GetPlayerIdAsync(dsplayerId,
replayDsPlayer.Name);
replayDsPlayer.Player = null;
replayDsPlayer.PlayerId = playerId;
Expand Down
19 changes: 0 additions & 19 deletions src/SC2ArcadeCrawler/CrawlerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,6 @@ public record PlayerSuccess
public double Winrate => Games == 0 ? 0 : Math.Round(Wins * 100.0 / (double)Games, 2);
}

public record PlayerId
{
public PlayerId()
{

}

public PlayerId(int regionId, int realmId, int profileId)
{
RegionId = regionId;
RealmId = realmId;
ProfileId = profileId;
}

public int RegionId { get; set; }
public int RealmId { get; set; }
public int ProfileId { get; set; }
}

public record LobbyHistoryResponse
{
public ResponsePage Page { get; set; } = new();
Expand Down
11 changes: 7 additions & 4 deletions src/dsstats.api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@

if (app.Environment.IsProduction())
{
var importService = scope.ServiceProvider.GetRequiredService<IImportService>();
importService.FixArcadePlayers().Wait();

var uploadSerivce = scope.ServiceProvider.GetRequiredService<UploadService>();
uploadSerivce.ImportInit();

Expand All @@ -180,10 +183,10 @@
}
else
{
//var crawlerService = scope.ServiceProvider.GetRequiredService<CrawlerService>();
//crawlerService.MapArcadePlayersToPlayers().Wait();
var tourneyService = scope.ServiceProvider.GetRequiredService<ITourneysService>();
tourneyService.SeedTourneys().Wait();
// var importService = scope.ServiceProvider.GetRequiredService<IImportService>();
// importService.FixArcadePlayers().Wait();
var comboRatings = scope.ServiceProvider.GetRequiredService<ComboRatings>();
comboRatings.CombineDsstatsSc2ArcadeReplays(add: false).Wait();
}

app.UseRateLimiter();
Expand Down
101 changes: 101 additions & 0 deletions src/dsstats.db8services/Import/ImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,105 @@ private int GetUpgradeId(string name)
}
return upgradeId;
}

public async Task FixArcadePlayers()
{
if (!IsInit)
{
await Init();
}
await importSs.WaitAsync();

try
{
using var scope = serviceProvider.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayContext>();

DateTime fromDate = new DateTime(2024, 07, 21);
var arcadeReplayPlayers = await context.ArcadeReplayDsPlayers
.Include(i => i.Player)
.Where(x => x.ArcadeReplay!.CreatedAt > fromDate)
.ToListAsync();

HashSet<int> deletePlayerIds = [];
Dictionary<PlayerId, KeyValuePair<string, int>> toCreatePlayers = [];
Dictionary<int, int> wrongCorrectedPlayerIdsMap = [];

var players = arcadeReplayPlayers.Select(s => s.Player!).Distinct().ToList();

foreach (var player in players)
{
if (player.ToonId > 3 && player.RegionId < 5)
{
continue;
}
deletePlayerIds.Add(player.PlayerId);
PlayerId correctedPlayerId = new(player.RegionId, player.RealmId, player.ToonId);
if (PlayerIds.TryGetValue(correctedPlayerId, out int dsPlayerId))
{
wrongCorrectedPlayerIdsMap[player.PlayerId] = dsPlayerId;
}
else
{
toCreatePlayers.Add(correctedPlayerId, new(player.Name, player.PlayerId));
}
}
await CreatePlayersAndUpdateMap(toCreatePlayers, wrongCorrectedPlayerIdsMap, context);

foreach (var arcadeReplayPlayer in arcadeReplayPlayers)
{
if (wrongCorrectedPlayerIdsMap.TryGetValue(arcadeReplayPlayer.Player!.PlayerId, out int correctedPlayerId))
{
arcadeReplayPlayer.Player = null;
arcadeReplayPlayer.PlayerId = correctedPlayerId;
}
}
await context.SaveChangesAsync();

await DeletePlayers(deletePlayerIds);

}
finally
{
importSs.Release();
}
}

private async Task DeletePlayers(ICollection<int> playerIds)
{
logger.LogWarning("deleting wrong players {count}", playerIds.Count);
using var scope = serviceProvider.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayContext>();

await context.Players
.Where(x => playerIds.Contains(x.PlayerId))
.ExecuteDeleteAsync();
}

private async Task CreatePlayersAndUpdateMap(Dictionary<PlayerId, KeyValuePair<string, int>> toCreatePlayers,
Dictionary<int, int> wrongCorrectedPlayerIdsMap,
ReplayContext context)
{
logger.LogWarning("creating corrected players {count}", toCreatePlayers.Count);
List<Player> players = [];
foreach (var ent in toCreatePlayers)
{
players.Add(new()
{
ToonId = ent.Key.ToonId,
RegionId = ent.Key.RegionId,
RealmId = ent.Key.RealmId,
Name = ent.Value.Key
});
}
context.Players.AddRange(players);
await context.SaveChangesAsync();
foreach (var player in players)
{
var playerId = new PlayerId(player.ToonId, player.RealmId, player.RegionId);
PlayerIds[playerId] = player.PlayerId;
int wrongId = toCreatePlayers[playerId].Value;
wrongCorrectedPlayerIdsMap[wrongId] = player.PlayerId;
}
}
}
4 changes: 4 additions & 0 deletions src/dsstats.ratings/ComboRatings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public async Task CombineDsstatsSc2ArcadeReplays(bool add = true)

foreach (var dsstatsReplay in dsstatsReplays)
{
if (dsstatsReplay.ReplayId == 625546)
{
logger.LogWarning("indahouse");
}
var arcadeReplay = await FindSc2ArcadeReplay(dsstatsReplay, matchedArcadeIds);
if (arcadeReplay is not null)
{
Expand Down
1 change: 1 addition & 0 deletions src/dsstats.shared/Interfaces/IImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface IImportService
Task<ImportResult> Import(List<ReplayDto> replayDtos, List<PlayerId>? uploaderPlayerIds = null);
Task Init();
Task SetPreRatings();
Task FixArcadePlayers();
}
Loading