//create new season with fake start and end dates (shoul be set later automaticcally by analyzers) public LeagueSeason CreateAndSaveLeagueSeason(int startYear, FootballLeague league) { if (Db.Database.Connection.State == System.Data.ConnectionState.Closed) { Db.Database.Connection.Open(); } var newSeason = (from n in Db.LeagueSeasons where n.StartYear == startYear && n.League.LeagueId == league.LeagueId select n).FirstOrDefault(); if (newSeason == null) { newSeason = new LeagueSeason { StartYear = startYear, League = league, StartDate = new DateTime(startYear, 1, 1), EndDate = new DateTime(startYear + 1, 1, 1) }; Db.LeagueSeasons.Add(newSeason); Db.SaveChanges(); var repo = new LogRepository(); repo.WriteLog(Database.SystemData.Severity.Information, "Insert to LeagueSeasons table new record", nameof(LeagueDataRepository), "localhost", "[LeagueId = " + league.LeagueId + "] [StartYear = " + newSeason.StartYear + "]", ""); } Db.Database.Connection.Close(); return(newSeason); }
/// <summary> /// Read the data from the CSV file for the provided football league. /// </summary> /// <param name="League"></param> /// <returns></returns> public IEnumerable<string> ReadFile(FootballLeague League = FootballLeague.PREMIER_LEAGUE) { switch (League) { case FootballLeague.PREMIER_LEAGUE: return File.ReadLines(Path.Combine(DesktopPath, "PremierLeague.csv")); case FootballLeague.CHAMPIONSHIP: return File.ReadLines(Path.Combine(DesktopPath, "Championship.csv")); case FootballLeague.LEAGUE_ONE: return File.ReadLines(Path.Combine(DesktopPath, "LeagueOne.csv")); case FootballLeague.LEAGUE_TWO: return File.ReadLines(Path.Combine(DesktopPath, "LeagueTwo.csv")); case FootballLeague.CONFERENCE: return File.ReadLines(Path.Combine(DesktopPath, "Conference.csv")); } return null; }
private static void AddMatch(int matchId, string homeTeam, string awayTeam, int homeTeamGoals, int awayTeamGoals) { if (!FootballLeague.Teams.Any(t => t.Name == homeTeam)) { throw new InvalidOperationException(string.Format("There isn't team {0} in the league", homeTeam)); } if (!FootballLeague.Teams.Any(t => t.Name == awayTeam)) { throw new InvalidOperationException(string.Format("There isn't team {0} in the league", awayTeam)); } var firstTeam = FootballLeague.Teams.First(t => t.Name == homeTeam); var secondTeam = FootballLeague.Teams.First(t => t.Name == awayTeam); var match = new Match(firstTeam, secondTeam, new Score(homeTeamGoals, awayTeamGoals), matchId); FootballLeague.AddMatch(match); }
//get all league seasons public List <LeagueSeason> GetAllLeagueSeasons(FootballLeague league) { return(GetAllLeagueSeasons(league.LeagueId)); }
public DataProcessor(FootballLeague League) { var FileHandler = new FileHandler(); var data = FileHandler.ReadFile(League); var LeagueTable = new LeagueTable { Table = new List<FootballTeam>() }; foreach (var line in data) { var lineData = line.Split(','); // Parse teams var homeTeamName = lineData[0]; var awayTeamName = lineData[1]; var homeTeam = LeagueTable.FindOrCreateTeam(homeTeamName); var awayTeam = LeagueTable.FindOrCreateTeam(awayTeamName); // Parse score var score = lineData[2].Split('-'); var homeTeamScore = int.Parse(score[0]); var awayTeamScore = int.Parse(score[1]); // Apply result to the league table if (homeTeamScore > awayTeamScore) { // Home team win homeTeam.Points += 3; homeTeam.Wins += 1; awayTeam.Defeats += 1; } else if (awayTeamScore > homeTeamScore) { // Away team win awayTeam.Points += 3; awayTeam.Wins += 1; homeTeam.Defeats += 1; } else { // Draw homeTeam.Points += 1; awayTeam.Points += 1; homeTeam.Draws += 1; awayTeam.Draws += 1; } // Apply goal difference, this happens regardless of the score homeTeam.GoalsFor += homeTeamScore; homeTeam.GoalsAgainst += awayTeamScore; awayTeam.GoalsFor += awayTeamScore; awayTeam.GoalsAgainst += homeTeamScore; // Increment number of games played, this happens regardless of score homeTeam.Played++; awayTeam.Played++; } LeagueTable.DisplayTable(); // A bit of spacing Console.WriteLine(); Console.WriteLine(); }
private static void AddTeam(string teamName, string teamNickname, DateTime dateOfFounding) { var team = new Team(teamName, teamNickname, dateOfFounding); FootballLeague.AddTeam(team); }
public LeagueView(Program program, FootballLeague league) : base($"{league.Name} Standings", program) { this.League = league; }
public IHttpActionResult LeagueTable(string leagueName) { using (var seasonClient = new HttpClient()) { // I may have to find a way of dynamically changing the season at some point later on seasonClient.BaseAddress = new Uri(apiURL); seasonClient.DefaultRequestHeaders.Accept.Clear(); seasonClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage seasonResponse = seasonClient.GetAsync("v1/soccerseasons/?season=2015").Result; if (seasonResponse.IsSuccessStatusCode) { JArray seasonArray = seasonResponse.Content.ReadAsAsync <JArray>().Result; foreach (JObject seasonObject in seasonArray) { string leagueCode = seasonObject["league"].ToString(); if (leagueCode == leagueName.ToUpper()) { string objectURL = seasonObject["_links"]["leagueTable"]["href"].ToString(); string leagueURL = objectURL.Remove(objectURL.IndexOf(apiURL), apiURL.Length); // I need to make a second http call in order to get the required league object using (var leagueClient = new HttpClient()) { leagueClient.BaseAddress = new Uri(apiURL); leagueClient.DefaultRequestHeaders.Accept.Clear(); leagueClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage leagueResponse = seasonClient.GetAsync(leagueURL).Result; if (leagueResponse.IsSuccessStatusCode) { JObject leagueObject = leagueResponse.Content.ReadAsAsync <JObject>().Result; JArray teamsArray = leagueObject["standing"] as JArray; FootballTeam[] leagueTeams = new FootballTeam[teamsArray.Count]; for (int i = 0; i < teamsArray.Count; i++) { leagueTeams[i] = new FootballTeam { teamName = teamsArray[i]["teamName"].ToString(), leagueRanking = Convert.ToInt32(teamsArray[i]["position"]), points = Convert.ToInt32(teamsArray[i]["playedGames"]), gamesPlayed = Convert.ToInt32(teamsArray[i]["points"]), wins = Convert.ToInt32(teamsArray[i]["wins"]), draws = Convert.ToInt32(teamsArray[i]["draws"]), losses = Convert.ToInt32(teamsArray[i]["losses"]) }; } FootballLeague league = new FootballLeague { leagueName = leagueObject["leagueCaption"].ToString(), matchday = Convert.ToInt32(leagueObject["matchday"]), teams = leagueTeams }; return(Ok(league)); } else { // Something is wrong with the link provided by the API Log.Debug("Retrieved League ID was incorrect. Probably due to change in the third party API"); return(NotFound()); } } } } // Not found compared with user input. Probably user input is wrong Log.Debug("User input must have entered incorrect League ID. Input = " + leagueName); return(NotFound()); } else { // Didn't get any result from their API Log.Debug("Could not establish a connection to the third party API"); return(NotFound()); } } }