private async Task <bool> UpdateStatsAsync(SimplifiedMatchSubmissionView view, SummonerInfoEntity userPlayer) { var registeredPlayersTask = _summonerInfoRepository.GetAllValidSummonersAsync(); var previousListedMvpsTask = _matchMvpRepository.ReadAllForTeamScheduleId(view.ScheduleId); var registeredPlayers = (await registeredPlayersTask).ToDictionary(x => x.SummonerName.ToLowerInvariant(), x => x); var mvpDetails = (await previousListedMvpsTask).OrderBy(x => x.Game).ToDictionary(x => x.Game, x => x); var insertMvpDetails = new List <MatchMvpEntity>(); var updateMvpDetails = new List <MatchMvpEntity>(); foreach (var gameInfo in view.GameDetails) { if (gameInfo.HomeTeamForfeit || gameInfo.AwayTeamForfeit) { continue; } var matchList = new List <MatchDetailContract>(); CollectMatchMvpData(view, registeredPlayers, gameInfo, mvpDetails, updateMvpDetails, insertMvpDetails, userPlayer); } var insertMvpResult = await _matchMvpRepository.CreateAsync(insertMvpDetails); var updateMvpResult = await _matchMvpRepository.UpdateAsync(updateMvpDetails); return(insertMvpResult && updateMvpResult); }
public async Task <bool> SendRoflFilesAsync(SimplifiedMatchSubmissionView view, string csvDataFile) { var attachments = new List <Attachment>(); if (!csvDataFile.IsNullOrEmpty()) { attachments.Add(new Attachment(csvDataFile)); foreach (var screenshot in view.GameDetails.Select(x => x.PostGameScreenshot)) { using (var ms = new MemoryStream()) { screenshot.CopyTo(ms); var fileBytes = ms.ToArray(); var att = new Attachment(new MemoryStream(fileBytes), screenshot.FileName); attachments.Add(att); } } } var messageBody = "Match result for subject: "; var urls = view.GameDetails.Where(x => !x.MatchReplayUrl.IsNullOrEmpty()).Select(x => x.MatchReplayUrl).ToList(); messageBody += string.Join(", ", urls); await _emailService.SendEmailAsync("*****@*****.**", messageBody, view.FileName, attachments, new List <string> { "*****@*****.**" }); //var di = new DirectoryInfo(Path.Combine(_wwwRootDirectory, "MatchCsvs")); //foreach (var file in di.EnumerateFiles()) //{ // if(file.Name != csvDataFile) // file.Delete(); //} return(true); }
public async Task <IActionResult> SendMatchDetailsAsync(SimplifiedMatchSubmissionView view) { try { var user = await _userManager.GetUserAsync(User); view.GameDetails = view.GameDetails.Where(x => x.GamePlayed || x.AwayTeamForfeit || x.HomeTeamForfeit) .ToList(); var isNullCheck = false; if (user == null) { throw new Exception("You were logged out unexpectedly. Im sorry."); } var userPlayer = await _summonerInfoRepository.ReadOneByUserIdAsync(user.Id); foreach (var gameInfo in view.GameDetails) { isNullCheck = Properties <GameDetail> .HasEmptyProperties(gameInfo); if (isNullCheck) { break; } } if (!view.GameDetails.Any() || view.GameDetails.Count < 2 || isNullCheck) { throw new Exception("Form was not setup right"); } var result = await _matchDetailService.SendFileData(view, userPlayer); if (!result) { throw new Exception($"Failed to send match data for: {view.FileName}"); } StatusMessage = "Successfully submitted match data. Stats will be reflected soon."; } catch (RiotSharpException e) { StatusMessage = $"{e.Message} Note. This is an error with Riot's Api. Please contact Ansem571 to let him know"; _logger.LogError(e, StatusMessage); } catch (Exception e) { StatusMessage = e.Message; _logger.LogError(e, StatusMessage); } var playersList = await _accountService.GetAllValidPlayers(view.HomeTeamName, view.AwayTeamName); view.ValidPlayers = playersList; view.StatusMessage = StatusMessage; return(View(view)); }
public void CollectMatchMvpData(SimplifiedMatchSubmissionView view, Dictionary <string, SummonerInfoEntity> registeredPlayers, GameDetail gameInfo, Dictionary <int, MatchMvpEntity> mvpDetails, List <MatchMvpEntity> updateMvpDetails, List <MatchMvpEntity> insertMvpDetails, SummonerInfoEntity userPlayer) { registeredPlayers.TryGetValue(gameInfo.BlueMvp.ToLowerInvariant(), out var blueMvp); registeredPlayers.TryGetValue(gameInfo.RedMvp.ToLowerInvariant(), out var redMvp); registeredPlayers.TryGetValue(gameInfo.HonoraryBlueOppMvp.ToLowerInvariant(), out var honoraryBlueOppMvp); registeredPlayers.TryGetValue(gameInfo.HonoraryRedOppMvp.ToLowerInvariant(), out var honoraryRedOppMvp); if (mvpDetails.TryGetValue(gameInfo.GameNum, out var mvpEntity)) { if (!string.IsNullOrEmpty(gameInfo.BlueMvp) && blueMvp != null && blueMvp.Id != mvpEntity.BlueMvp) { mvpEntity.BlueMvp = blueMvp.Id; } if (!string.IsNullOrEmpty(gameInfo.RedMvp) && redMvp != null && redMvp.Id != mvpEntity.RedMvp) { mvpEntity.RedMvp = redMvp.Id; } if (!string.IsNullOrEmpty(gameInfo.HonoraryBlueOppMvp) && honoraryBlueOppMvp != null && honoraryBlueOppMvp.Id != mvpEntity.HonoraryBlueOppMvp) { mvpEntity.HonoraryBlueOppMvp = honoraryBlueOppMvp.Id; } if (!string.IsNullOrEmpty(gameInfo.HonoraryRedOppMvp) && honoraryRedOppMvp != null && honoraryRedOppMvp.Id != mvpEntity.HonoraryRedOppMvp) { mvpEntity.HonoraryRedOppMvp = honoraryRedOppMvp.Id; } mvpEntity.UpdatedBy = userPlayer.SummonerName; mvpEntity.UpdatedOn = DateTime.Now; updateMvpDetails.Add(mvpEntity); } else { mvpEntity = new MatchMvpEntity { Id = Guid.NewGuid(), BlueMvp = blueMvp?.Id, RedMvp = redMvp?.Id, HonoraryBlueOppMvp = honoraryBlueOppMvp?.Id, HonoraryRedOppMvp = honoraryRedOppMvp?.Id, Game = gameInfo.GameNum, TeamScheduleId = view.ScheduleId, CreatedBy = userPlayer.SummonerName, CreatedOn = DateTime.Now }; insertMvpDetails.Add(mvpEntity); } }
public async Task <bool> SendFileData(SimplifiedMatchSubmissionView view, SummonerInfoEntity userPlayer) { //var di = new DirectoryInfo(Path.Combine(_wwwRootDirectory, "MatchCsvs")); //foreach (var file in di.EnumerateFiles()) //{ // file.Delete(); //} var addMatchStats = await UpdateStatsAsync(view, userPlayer); if (!addMatchStats) { return(false); } var csvDataFile = CreateCsvDataFile(view); return(await SendRoflFilesAsync(view, csvDataFile)); }
public async Task <IActionResult> SendMatchDetailsAsync(int weekNumber, Guid scheduleId) { var scheduleTask = _scheduleRepository.GetScheduleAsync(scheduleId); var schedule = await scheduleTask; var matchInfoTask = _gameInfoService.GetGameInfoForMatch(scheduleId); var matchInfo = await matchInfoTask; var playersList = await _accountService.GetAllValidPlayers(matchInfo.HomeTeam, matchInfo.AwayTeam); var view = new SimplifiedMatchSubmissionView { Week = !schedule.IsPlayoffMatch ? $"Week {weekNumber}" : $"Playoff {weekNumber}", HomeTeamName = matchInfo.HomeTeam, AwayTeamName = matchInfo.AwayTeam, ScheduleId = scheduleId, ValidPlayers = playersList, GameDetails = matchInfo.GameDetails }; return(View(view)); }
private string CreateCsvDataFile(SimplifiedMatchSubmissionView view) { var csvFile = Path.Combine(_wwwRootDirectory, $"MatchCsvs\\{view.FileName}-{Guid.NewGuid()}.csv"); using (var writer = new StreamWriter(csvFile, false, Encoding.UTF8)) { using (var csvWriter = new CsvWriter(writer)) { //csvWriter.WriteField(""); var gameNum = 0; foreach (var gameInfo in view.GameDetails) { gameNum++; WriteHeader(csvWriter, gameNum, gameInfo); csvWriter.NextRecord(); csvWriter.WriteField(""); csvWriter.WriteField(gameInfo.MatchReplayUrl); csvWriter.WriteField(gameInfo.TeamOnBlueSide); if (gameInfo.HomeTeamForfeit) { csvWriter.WriteField("AwayTeam by HomeTeam forfeit"); break; } if (gameInfo.AwayTeamForfeit) { csvWriter.WriteField("HomeTeam by AwayTeam forfeit"); break; } csvWriter.WriteField(gameInfo.BlueSideWinner ? "Blue" : "Red"); csvWriter.WriteField(gameInfo.ProdraftSpectateLink); csvWriter.WriteField(gameInfo.PostGameScreenshot.FileName); csvWriter.NextRecord(); } } writer.Close(); } return(csvFile); }