public void AddMatch(Match match) { //Sort var sortedUserlist = match.PlayerList.OrderBy(x => x).ToList(); var addedList = string.Join("", sortedUserlist.ToArray()); //RecalculateLeaderboard hashstring var hashcode = addedList.GetHashCode(); //Find existing matchup historys that have the same players List<MatchupResult> matchingResults = _matchupResultRepository.GetByHashResult(hashcode); //Find the team constalation with same team setup var team1 = new List<string> { match.PlayerList[0], match.PlayerList[1] }.OrderBy(x => x).ToList(); var team2 = new List<string> { match.PlayerList[2], match.PlayerList[3] }.OrderBy(x => x).ToList(); MatchupResult correctMatchupResult = null; foreach (var matchResult in matchingResults) { var sortedFoundMatchResultTeam1 = new List<string> { matchResult.UserList[0], matchResult.UserList[1] }.OrderBy(x => x).ToList(); var sortedFoundMatchResultTeam2 = new List<string> { matchResult.UserList[2], matchResult.UserList[3] }.OrderBy(x => x).ToList(); if (team1[0] == sortedFoundMatchResultTeam1[0] && team1[1] == sortedFoundMatchResultTeam1[1]) { correctMatchupResult = matchResult; break; } else if (team2[0] == sortedFoundMatchResultTeam2[0] && team2[1] == sortedFoundMatchResultTeam2[1]) { correctMatchupResult = matchResult; break; } } //Add match if (correctMatchupResult != null) { AddMatchupResult(correctMatchupResult, match); _matchupResultRepository.SaveMatchupResult(correctMatchupResult); } else { //Create new matchcupresult var matchup = new MatchupResult { HashResult = hashcode, Last5Games = new List<MatchResult> {match.MatchResult}, Team1Wins = 0, Team2Wins = 0, UserList = match.PlayerList }; _matchupResultRepository.SaveMatchupResult(matchup); } }
private static void AddMatch(string homeTeam, string awayTeam, int homeTeamGoals, int awayTeamGoals) { Team leagueHomeTeam = League.Teams.FirstOrDefault(t => t.Name == homeTeam); Team awayHomeTeam = League.Teams.FirstOrDefault(t => t.Name == awayTeam); if (leagueHomeTeam == null || awayHomeTeam == null) throw new ArgumentException("Home and/or away team do/does not exists in this league."); var newMatch = new Models.Match(leagueHomeTeam, awayHomeTeam, new Score(homeTeamGoals, awayTeamGoals)); League.AddMatch(newMatch); Console.WriteLine(string.Format("{0} - {1} match successfully created.", homeTeam, awayTeam)); }
private static void AddMatch(string homeTeam, string awayTeam, int homeTeamGoals, int awayTeamGoals) { Team leagueHomeTeam = League.Teams.FirstOrDefault(t => t.Name == homeTeam); Team awayHomeTeam = League.Teams.FirstOrDefault(t => t.Name == awayTeam); if (leagueHomeTeam == null || awayHomeTeam == null) { throw new ArgumentException("Home and/or away team do/does not exists in this league."); } var newMatch = new Models.Match(leagueHomeTeam, awayHomeTeam, new Score(homeTeamGoals, awayTeamGoals)); League.AddMatch(newMatch); Console.WriteLine(string.Format("{0} - {1} match successfully created.", homeTeam, awayTeam)); }
private static void AddMatch(string homeTeam, string awayTeam, int scoreHT, int scoreAT, int id) { if (!CheckIfTeamExists(homeTeam) || !CheckIfTeamExists(awayTeam)) { throw new ArgumentException("Team(s) do(es) not exist"); } if (CheckIfMatchExists(id)) { throw new InvalidOperationException(id + " has already been added to the league"); } Score score = new Score(scoreAT, scoreHT); Match match = new Match(GetTeamByName(homeTeam), GetTeamByName(awayTeam), score, id); League.AddMatch(match); Console.WriteLine("The {0} match was added", match.ID); }
public void AddMatchupResult(MatchupResult existingMatchupResult, Match match) { //Find out if team 1 if (match.PlayerList[0] == existingMatchupResult.UserList[0] || match.PlayerList[0] == existingMatchupResult.UserList[1]) { if (match.MatchResult.Team1Won) { existingMatchupResult.Team1Wins++; } else { existingMatchupResult.Team2Wins++; } } else { if (match.MatchResult.Team1Won) { existingMatchupResult.Team2Wins++; } else { existingMatchupResult.Team1Wins++; } } if (existingMatchupResult.Last5Games.Count < 5) { //TODO Need to take care when team 1 of existing is not the same as team1 in match existingMatchupResult.Last5Games.Insert(0, match.MatchResult); } else { existingMatchupResult.Last5Games.RemoveAt(4); existingMatchupResult.Last5Games.Insert(0, match.MatchResult); } }
public LeaderboardViewEntry CreatePlayer(string playerName, Match match, double result, bool won) { return new LeaderboardViewEntry { UserName = playerName, EloRating = won ? 1500 + (int) result : 1500 - (int) result, NumberOfGames = 1, Wins = won ? 1 : 0, Losses = won ? 0 : 1, Form = won ? "W" : "L" }; }
public void AddMatchToLeaderboard(LeaderboardView leaderboardView, Match match) { var leaderboardEntries = leaderboardView.Entries; //Team1 var player1 = match.PlayerList[0]; var existingPlayer1 = leaderboardEntries.SingleOrDefault(x => x.UserName == player1); var player2 = match.PlayerList[1]; var existingPlayer2 = leaderboardEntries.SingleOrDefault(x => x.UserName == player2); var team1AvgElo = existingPlayer1 != null ? existingPlayer1.EloRating : 1500; team1AvgElo += existingPlayer2 != null ? existingPlayer2.EloRating : 1500; //Team2 var player3 = match.PlayerList[2]; var existingPlayer3 = leaderboardEntries.SingleOrDefault(x => x.UserName == player3); var player4 = match.PlayerList[3]; var existingPlayer4 = leaderboardEntries.SingleOrDefault(x => x.UserName == player4); var team2AvgElo = existingPlayer3 != null ? existingPlayer3.EloRating : 1500; team2AvgElo += existingPlayer4 != null ? existingPlayer4.EloRating : 1500; var elo = new EloRating(); var result = elo.CalculateRating(team1AvgElo/2, team2AvgElo/2, match.MatchResult.Team1Won); match.Points = (int)result; if (existingPlayer1 == null) { leaderboardEntries.Add(CreatePlayer(player1, match, result, match.MatchResult.Team1Won)); } else { UpdateExistingLeaderboardEntry(existingPlayer1.UserName, leaderboardEntries, match, result, match.MatchResult.Team1Won); } if (existingPlayer2 == null) { leaderboardEntries.Add(CreatePlayer(player2, match, result, match.MatchResult.Team1Won)); } else { UpdateExistingLeaderboardEntry(existingPlayer2.UserName, leaderboardEntries, match, result, match.MatchResult.Team1Won); } if (existingPlayer3 == null) { leaderboardEntries.Add(CreatePlayer(player3, match, result, !match.MatchResult.Team1Won)); } else { UpdateExistingLeaderboardEntry(existingPlayer3.UserName, leaderboardEntries, match, result, !match.MatchResult.Team1Won); } if (existingPlayer4 == null) { leaderboardEntries.Add(CreatePlayer(player4, match, result, !match.MatchResult.Team1Won)); } else { UpdateExistingLeaderboardEntry(existingPlayer4.UserName, leaderboardEntries, match, result, !match.MatchResult.Team1Won); } }
public void UpdateExistingLeaderboardEntry(string playerName, List<LeaderboardViewEntry> leaderboardEntries, Match match, double result, bool won) { var playerEntry = leaderboardEntries.Single(x => x.UserName == playerName); playerEntry.EloRating += won ? (int) result : -(int) result; playerEntry.NumberOfGames++; if (playerEntry.Form.Length < 5) { playerEntry.Form += won ? "W" : "L"; } else { playerEntry.Form = playerEntry.Form.Remove(0, 1); playerEntry.Form += won ? "W" : "L"; } if (won) { playerEntry.Wins++; } else { playerEntry.Losses++; } }
public Task GetTeamDetails(int TeamId) { DateTime lastScraped = new DateTime(1900, 1, 1); // Get ScrapeHistoryTeams record for latest scrape info for this team var history = db.ScrapeHistoryTeams.Where(p => p.TeamId == TeamId).OrderByDescending(k => k.LastDayScraped).ToList(); if (history.Any()) { lastScraped = history.FirstOrDefault().LastDayScraped; } int lCounter = 0; string url = $"" + $"http://www.hltv.org/?pageid=188&teamid={TeamId}"; bool bHasCreatedCurrentTeam = false; HtmlDocument teamhtml = HWeb.Load(url); var StatsTable2 = teamhtml.DocumentNode.SelectNodes($"/ html / body / div[2] / div / div[2] / div[1] / div / table / tbody / tr[position()>0]"); if (StatsTable2 != null) { foreach (var item in StatsTable2) { var statstableRow = item; var dateString = statstableRow.ChildNodes[1].InnerText; var dDate = NewDate(dateString); if (dDate < lastScraped.AddDays(-1) || dDate < DateTime.Now.AddMonths(-6)) { // And we have added some records if (lCounter > 0 || !history.Any()) { // We save history record when last scraped for this team. db.ScrapeHistoryTeams.Add(new ScrapeHistoryTeams { TeamId = TeamId, LastDayScraped = DateTime.Now }); db.SaveChanges(); } // And quit scraping return(Task.FromResult(0)); } int MatchID = GetMatchIDS(statstableRow.ChildNodes[1].InnerHtml); if (MatchID == 0 || db.Match.Any(s => s.MatchId == MatchID)) { continue; } var matchUrl = GetMatchUrl(statstableRow.ChildNodes[1].InnerHtml); var xx = matchUrl.Substring(1); var prefix = "https://www.hltv.org"; var fullUrl = prefix + xx; var rounds = GetRoundsV2(matchUrl, TeamId); var players = GetTeamLineupFromDetails(fullUrl); if (!players.Players.Any()) { db.ErrorLoggers.Add(new ErrorLogger { url = fullUrl, Error = $"{DateTime.Now} - No players, matchid: {MatchID}, date: {dDate}" }); db.SaveChanges(); // If we don't find any players, we quit. if (lCounter > 0 || !history.Any()) { // We save history record when last scraped for this team. db.ScrapeHistoryTeams.Add(new ScrapeHistoryTeams { TeamId = TeamId, LastDayScraped = DateTime.Now }); db.SaveChanges(); } // And quit scraping return(Task.FromResult(0)); } var Event = statstableRow.ChildNodes[3].InnerText; var opponent = statstableRow.ChildNodes[7].InnerText; var map = statstableRow.ChildNodes[9].InnerText; var result = getResult(statstableRow.ChildNodes[11].InnerText); var winOrLoss = statstableRow.ChildNodes[13].InnerText; // If we have moved past last scraped date, or year old data if (!bHasCreatedCurrentTeam) { CheckIfNeedToCreateTeam(Team1ID, Team1Name); bHasCreatedCurrentTeam = true; } CheckIfNeedToCreateTeam(Team2ID, Team2Name); var bSwitchTeams = Team1ID != TeamId; if (bSwitchTeams) { Team2ID = Team1ID; Team1ID = TeamId; } var match = new Match { MatchId = MatchID, Date = dDate, Map = map, Event = Event, ResultT1 = result.Item1, //Sækja value úr rounds ResultT2 = result.Item2, Team1Id = Team1ID, Team1RankValue = GetRankingValueForTeam(Team1ID, dDate), Team2Id = Team2ID, Team2RankValue = GetRankingValueForTeam(Team2ID, dDate), FirstRound1HWinTeamId = rounds.Count > 0 ? rounds.Where(x => x.Round1 == true).FirstOrDefault().TeamId : 0, FirstRound1HWinTerr = rounds.Count > 0 ? rounds.Where(x => x.Round1).FirstOrDefault().Terrorist:false, FirstRound1HWinCt = rounds.Count > 0 ? rounds.Where(x => x.Round1).FirstOrDefault().CounterTerrorist : false, FirstRound2HWinTeamId = rounds.Count > 0 ? rounds.Where(x => x.Round16).FirstOrDefault().TeamId:0, FirstRound2HWinTerr = rounds.Count > 0 ? rounds.Where(x => x.Round16).FirstOrDefault().Terrorist:false, FirstRound2HWinCT = rounds.Count > 0 ? rounds.Where(x => x.Round16).FirstOrDefault().CounterTerrorist:false }; var t1Players = players.Players.Where(x => x.TeamID == Team1ID).ToArray(); if (t1Players.Length > 0) { match.T1Player1Id = t1Players[0].PlayerId; } if (t1Players.Length > 1) { match.T1Player2Id = t1Players[1].PlayerId; } if (t1Players.Length > 2) { match.T1Player3Id = t1Players[2].PlayerId; } if (t1Players.Length > 3) { match.T1Player4Id = t1Players[3].PlayerId; } if (t1Players.Length > 4) { match.T1Player5Id = t1Players[4].PlayerId; } var t2Players = players.Players.Where(x => x.TeamID == Team2ID).ToArray(); if (t2Players.Length > 0) { match.T2Player1Id = t2Players[0].PlayerId; } if (t2Players.Length > 1) { match.T2Player2Id = t2Players[1].PlayerId; } if (t2Players.Length > 2) { match.T2Player3Id = t2Players[2].PlayerId; } if (t2Players.Length > 3) { match.T2Player4Id = t2Players[3].PlayerId; } if (t2Players.Length > 4) { match.T2Player5Id = t2Players[4].PlayerId; } if (db.Match.Any(s => s.MatchId == MatchID)) { continue; } db.Match.Add(match); db.SaveChanges(); lCounter++; } } // If we have added some records or have no record for this team if (lCounter > 0 || !history.Any()) { // We save history record when last scraped for this team. db.ScrapeHistoryTeams.Add(new ScrapeHistoryTeams { TeamId = TeamId, LastDayScraped = DateTime.Now }); db.SaveChanges(); } return(Task.FromResult(0)); }
private string GetPlayers(Match m, bool winners) { if (m.MatchResult.Team1Won == winners) { return m.PlayerList[0] + ";" + m.PlayerList[1]; } return m.PlayerList[2] + ";" + m.PlayerList[3]; }