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 <bool> Upload(HttpClient Http, sc2dsstatsContext context, UserConfig config, ILogger logger) { logger.LogInformation($"Uplading replay(s) {DateTime.UtcNow}"); if (!context.Dsreplays.Any()) { return(true); } string id; using (SHA256 sha256Hash = SHA256.Create()) { string names = String.Join(";", config.PlayersNames); id = GetHash(sha256Hash, names); } DsUploadRequest uploadRequest = new DsUploadRequest() { RealName = !String.IsNullOrEmpty(config.DisplayName) ? config.DisplayName : String.Empty, AppId = config.AppId, Hash = id, LastRep = (await context.Dsreplays.OrderByDescending(o => o.Gametime).FirstAsync()).Gametime.ToString("yyyyMMddHHmmss"), Total = await context.Dsreplays.CountAsync(), Version = Program.Version.ToString() }; DateTime lastUploadReplayGametime = DateTime.MinValue; try { var iresponse = await Http.PostAsJsonAsync("secure/data/uploadrequest", uploadRequest); if (iresponse.IsSuccessStatusCode) { lastUploadReplayGametime = await iresponse.Content.ReadFromJsonAsync <DateTime>(); } else { return(false); } } catch (Exception e) { logger.LogError($"failed getting upload request: {e.Message}"); return(false); } var replays = await context.Dsreplays .Include(i => i.Middles) .Include(i => i.Dsplayers) .ThenInclude(j => j.Breakpoints) .AsNoTracking() .AsSplitQuery() .Where(x => x.Gametime > lastUploadReplayGametime) .ToListAsync(); if (!replays.Any()) { return(true); } replays.SelectMany(s => s.Dsplayers).ToList().ForEach(f => f.Name = config.PlayersNames.Contains(f.Name) ? "player" : $"player{f.Realpos}"); replays.ForEach(f => f.Replaypath = String.Empty); var json = System.Text.Json.JsonSerializer.Serialize(replays.Select(s => s.GetDto()).ToList()); json = DSData.Zip(json); var response = await Http.PostAsJsonAsync($"secure/data/replayupload/{config.AppId}", json); if (response.IsSuccessStatusCode) { var uploadResponse = await response.Content.ReadFromJsonAsync <DsUploadResponse>(); config.DbId = uploadResponse.DbId; return(true); } else { return(false); } }
public async Task <ActionResult <DateTime> > UploadRequest([FromBody] DsUploadRequest uploadRequest) { var player = await PlayerService.GetPlayerName(context, uploadRequest); return(player.LatestReplay); }