public static async Task <DsPlayerName> GetPlayerName(sc2dsstatsContext context, DsUploadRequest request) { var playerName = await context.DsPlayerNames.FirstOrDefaultAsync(f => f.AppId == request.AppId); if (playerName == null) { playerName = await context.DsPlayerNames.FirstOrDefaultAsync(f => f.Hash == request.Hash); } if (playerName == null) { var restPlayer = await context.DSRestPlayers.FirstOrDefaultAsync(f => f.Name == request.Hash); playerName = new DsPlayerName() { AppId = request.AppId, DbId = Guid.NewGuid(), Hash = request.Hash, Name = String.Empty, AppVersion = request.Version, LatestReplay = restPlayer == null ? new DateTime(2018, 1, 1) : restPlayer.LastRep }; context.DsPlayerNames.Add(playerName); } // TODO DEBUG // else if (playerName.AppId == new Guid()) // { playerName.AppId = request.AppId; // } playerName.TotlaReplays = request.Total; playerName.LatestUpload = DateTime.UtcNow; playerName.AppVersion = request.Version; await context.SaveChangesAsync(); return(playerName); }
public static async Task <DsPlayerName> GetPlayerName(sc2dsstatsContext context, DsUploadInfo request) { var playerName = await context.DsPlayerNames.FirstOrDefaultAsync(f => f.Hash == request.Name); if (playerName == null) { var restPlayer = await context.DSRestPlayers.FirstOrDefaultAsync(f => f.Name == request.Name); playerName = new DsPlayerName() { AppId = new Guid(), DbId = Guid.NewGuid(), Hash = request.Name, Name = String.Empty, AppVersion = request.Version, LatestReplay = restPlayer == null ? new DateTime(2018, 1, 1) : restPlayer.LastRep }; context.DsPlayerNames.Add(playerName); await context.SaveChangesAsync(); } return(playerName); }
private async Task InsertReplays() { lock (lockobject) { if (jobRunning) { return; } else { jobRunning = true; tokenSource = new CancellationTokenSource(); } } logger.LogInformation($"consumer job working."); using (var scope = scopeFactory.CreateScope()) { int i = 0; HashSet <DsPlayerName> playerCache = new HashSet <DsPlayerName>(); try { var context = scope.ServiceProvider.GetRequiredService <sc2dsstatsContext>(); while (await ReplayChannel.Reader.WaitToReadAsync(tokenSource.Token)) { Dsreplay replay; if (ReplayChannel.Reader.TryRead(out replay)) { if (!await context.Dsreplays.AnyAsync(a => a.Hash == replay.Hash)) { HashSet <string> names = new HashSet <string>(); foreach (var pl in replay.Dsplayers) { if (names.Contains(pl.Name)) { continue; } var player = playerCache.FirstOrDefault(f => f.Name == pl.Name); if (player == null) { player = await context.DsPlayerNames.FirstOrDefaultAsync(f => f.Name == pl.Name); } if (player == null) { player = new DsPlayerName() { Name = pl.Name, }; names.Add(pl.Name); playerCache.Add(player); } pl.PlayerName = player; } context.Dsreplays.Add(replay); } } if (i % 1000 == 0) { await context.SaveChangesAsync(); OnReplaysInserted(new InsertEventArgs() { insertCount = i }); } i++; } await context.SaveChangesAsync(); } catch (OperationCanceledException) { } catch (Exception ex) { logger.LogError(ex.Message); } finally { OnReplaysInserted(new InsertEventArgs() { insertCount = i, Done = true }); Reset(); } } logger.LogInformation($"consumer job finished."); }