Skip to content

Commit

Permalink
Merge pull request #504 from ipax77/dev
Browse files Browse the repository at this point in the history
TourneysService Create1v1Event
  • Loading branch information
ipax77 authored Jul 28, 2024
2 parents e6f745d + b6bddca commit e78be3a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/dsstats.api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@
{
//var crawlerService = scope.ServiceProvider.GetRequiredService<CrawlerService>();
//crawlerService.MapArcadePlayersToPlayers().Wait();
var tourneyService = scope.ServiceProvider.GetRequiredService<ITourneysService>();
tourneyService.SeedTourneys().Wait();
}

app.UseRateLimiter();
Expand Down
39 changes: 35 additions & 4 deletions src/dsstats.db8services/Tourneys/TourneysService.New.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,28 @@ private async Task CreateNewTournament(string tourneyPath, int eventId)
foreach (var jsonReplay in jsonReplays)
{
var replay = JsonSerializer.Deserialize<ReplayDto>(File.ReadAllText(jsonReplay));
if (replay is null)
if (replay is null || replays.Any(a => a.ReplayHash == replay.ReplayHash))
{
continue;
}
replays.Add(replay);
}
await ImportReplays(replays);

var groups = replays.GroupBy(g => Path.GetDirectoryName(g.FileName));
foreach (var group in groups)
if (replays.All(a => a.Playercount == 2))
{
await CreateReplayEvent([.. group], eventId);
foreach (var replay in replays)
{
await Create1v1Event(replay, eventId);
}
}
else
{
var groups = replays.GroupBy(g => Path.GetDirectoryName(g.FileName));
foreach (var group in groups)
{
await CreateReplayEvent([.. group], eventId);
}
}
}

Expand Down Expand Up @@ -120,6 +130,27 @@ private async Task CreateReplayEvent(List<ReplayDto> replays, int eventId)
await context.SaveChangesAsync();
}

private async Task Create1v1Event(ReplayDto replay, int eventId)
{
string winnerTeam = replay.ReplayPlayers.Where(x => x.Team == replay.WinnerTeam).FirstOrDefault()?.Name ?? "";
string runnerTeam = replay.ReplayPlayers.Where(x => x.Team != replay.WinnerTeam).FirstOrDefault()?.Name ?? "";
ReplayEvent replayEvent = new()
{
WinnerTeam = winnerTeam,
RunnerTeam = runnerTeam,
Round = "tbd",
EventId = eventId
};
var dbReplay = await context.Replays.FirstOrDefaultAsync(f => f.ReplayHash == replay.ReplayHash);
if (dbReplay is not null)
{
dbReplay.ReplayEvent = replayEvent;
dbReplay.FileName = replay.FileName ?? string.Empty;
context.ReplayEvents.Add(replayEvent);
await context.SaveChangesAsync();
}
}

private (string, string) GetTeamNames(string path)
{
var teamInfo = Path.GetFileName(Path.GetDirectoryName(path));
Expand Down

0 comments on commit e78be3a

Please sign in to comment.