public override ICollection <Club> ChooseClubs() { //throw exception when Parent or Child Competition is null if (Competition.ParentCompetitionId == null || Competition.ChildCompetitionId == null) { throw new ArgumentNullException(); } //if (Competition.ParentCompetition.RelegateToChildCompetition != Competition.ChildCompetition.PromoteToParentCompetition) //not supported for now, but maybe it can be useful to enable //throw new InvalidOperationException(); // pick clubs from both leagues ICollection <CompetitionLeagueTable> parent_div_league = CompetitionLeagueTable.GetStandingsBackwards(Competition.ParentCompetition, (int)Competition.Country.Season) .Skip(Competition.ParentCompetition.RelegateToChildCompetition) .Take(AmountFromParentCompetition) .ToList(); ICollection <CompetitionLeagueTable> child_div_league = CompetitionLeagueTable.GetStandings(Competition.ChildCompetition, (int)Competition.Country.Season) .Skip(Competition.ChildCompetition.PromoteToParentCompetition) .Take(AmountFromChildCompetition) .ToList(); List <Club> all_clubs = new List <Club>(); all_clubs.AddRange(parent_div_league.Select <CompetitionLeagueTable, Club>(o => o.Club)); all_clubs.AddRange(child_div_league.Select <CompetitionLeagueTable, Club>(o => o.Club)); //choice-algorithm (easy-way): floor club-count to a two-power number, to make a perfect knock-out round //INTRODUCES A PROMOTION BUG WHERE (all_clubs no power of 2? unbalanced chosen clubs from competition because of random chosen_amount) int i = 0; int clubcount = all_clubs.Count(); while (clubcount > 1) { i++; clubcount >>= 1; } int chosen_amount = clubcount << i; all_clubs = SortRandom(all_clubs); var chosen_clubs = all_clubs.Take(chosen_amount); // log clubs in CompetitionClubsRelation foreach (Club club in chosen_clubs) { var playing_in_competition_relation = new CompetitionClubRelation() { Club = club, Competition = Competition }; WorldState.GetDatabaseContext().CompetitionClubRelations.Add(playing_in_competition_relation); } WorldState.GetDatabaseContext().SaveChanges(); // return clubs return(chosen_clubs.ToList()); }
public CompetitionLeagueTableViewModel(CompetitionLeagueTable source) { ClubName = source.Club.Name; Won = source.Won; Tied = source.Tied; Lost = source.Lost; GoalsFor = source.GoalsFor; GoalsAgainst = source.GoalsAgainst; Points = source.Points; }
private async void OnCompetitionStandingsChanged(Competition competition) { var standings = CompetitionLeagueTable.GetStandings(competition, (int)competition.Country.Season); await Clients.All.SendAsync("OnCompetitionStandingsChanged", JsonConvert.SerializeObject( new { CompetitionId = competition.Id, LeagueTable = SerializeStandings(standings) } )); }
private void CreateCompetitionTable(ICollection <Club> participants) { foreach (Club team in participants) { CompetitionLeagueTable league_table = new CompetitionLeagueTable() { Club = team, Season = (int)Competition.Country.Season, CompetitionEvent = this, Competition = Competition }; WorldState.GetDatabaseContext().CompetitionLeagueTable.Add(league_table); } WorldState.GetDatabaseContext().SaveChanges(); }
public override void OnMatchComplete(Match match) { //write score of both clubs to the CompetitionLeagueTable SHOULD BE A CALLABLE FUNCTION IN CLUB? CompetitionLeagueTable homeclub_leagueitem = WorldState.GetDatabaseContext().CompetitionLeagueTable .Where(o => o.Season == match.Season && o.CompetitionEventId == Id && o.ClubId == match.HomeClubId) .First(); homeclub_leagueitem.GoalsFor += (int)match.HomeScore; homeclub_leagueitem.GoalsAgainst += (int)match.AwayScore; if (match.HomeClubWinning()) { homeclub_leagueitem.Won += 1; homeclub_leagueitem.Points += 3; } else if (match.ClubsBothTied()) { homeclub_leagueitem.Tied += 1; homeclub_leagueitem.Points += 1; } else { homeclub_leagueitem.Lost += 1; } var awayclub_leagueitem = WorldState.GetDatabaseContext().CompetitionLeagueTable .First(o => o.Season == match.Season && o.CompetitionEventId == Id && o.ClubId == match.AwayClubId); awayclub_leagueitem.GoalsFor += (int)match.AwayScore; awayclub_leagueitem.GoalsAgainst += (int)match.HomeScore; if (match.AwayClubWinning()) { awayclub_leagueitem.Won += 1; awayclub_leagueitem.Points += 3; } else if (match.ClubsBothTied()) { awayclub_leagueitem.Tied += 1; awayclub_leagueitem.Points += 1; } else { awayclub_leagueitem.Lost += 1; } WorldState.GetDatabaseContext().SaveChanges(); //if all matches complete var upcoming_match = WorldState.GetDatabaseContext().Matches .Where(o => o.CompetitionEventId == Id && o.HasEnded == false) .FirstOrDefault(); if (upcoming_match == null) { // matches are all done, this is the last time this function runs, so call the next event OnFinishedEvent(); //call next event, if exists if (NextCompetitionEvent != null) { NextCompetitionEvent.Execute(); } } }
public async void CompetitionDataRequest(int id, int?season, int?round) { using (var context = SoccerWorldDatabaseContext.GetService()) { var comp = context.Competitions .Where(c => c.Id == id) .Include(c => c.Country).First(); if (comp.Country.Season == null) { await Clients.Caller.SendAsync("OnCompetitionDataRequest", JsonConvert.SerializeObject( new { Competition = new CompetitionViewModel(comp), CompetitionSeasons = new Array[0], LeagueTable = new Array[0], Matches = new Array[0] } )); return;//nullable-int syndrome solution #1 } if (season == null) { season = comp.Country.Season; } var country_seasons = context.CompetitionLeagueTable.Where(o => o.Competition == comp) .Select(o => o.Season) .Distinct().ToList(); //TODO.OrderBy(); // var standings = CompetitionLeagueTable.GetStandings(comp, (int)season); if (round == null) { var chosen_match = context.Matches .Where(m => m.Season == season && m.CompetitionEvent.CompetitionId == comp.Id && m.HasEnded == false) .OrderBy(m => m.RoundNumber).FirstOrDefault(); if (chosen_match == null) { chosen_match = context.Matches .Where(m => m.Season == season && m.CompetitionEvent.CompetitionId == comp.Id && m.HasEnded == true) .OrderByDescending(m => m.RoundNumber).FirstOrDefault(); } round = chosen_match?.RoundNumber; } var matches = context.Matches .Where(m => m.Season == season && m.CompetitionEvent.CompetitionId == comp.Id && m.RoundNumber == round) .Include(m => m.HomeClub) .Include(m => m.AwayClub) .ToList(); await Clients.Caller.SendAsync("OnCompetitionDataRequest", JsonConvert.SerializeObject( new { Competition = new CompetitionViewModel(comp), CompetitionSeasons = country_seasons, LeagueTable = SerializeStandings(standings), Matches = SerializeMatches(matches) } )); } }
public IActionResult Index() { var worlddate = _context.WorldState.First().CurrentDateTime; var continents = _context.Continents; var selectedcontinent = continents.First(); List <SelectListItem> continentlist = new List <SelectListItem>(); foreach (var continent in continents) { continentlist.Add(new SelectListItem() { Text = continent.Name, Value = continent.Id.ToString() }); } var countries = _context.Countries.Where(o => o.Continent == selectedcontinent); var selectedcountry = countries.First(); List <SelectListItem> countrylist = new List <SelectListItem>(); foreach (var country in countries) { countrylist.Add(new SelectListItem() { Text = country.Name, Value = country.Id.ToString() }); } var country_seasons = _context.CompetitionLeagueTable.Select(o => o.Season).Distinct();//TODO.OrderBy(); List <SelectListItem> countryseasonslist = new List <SelectListItem>(); foreach (var season in country_seasons) { countryseasonslist.Add(new SelectListItem() { Text = season.ToString(), Value = season.ToString(), Selected = (season == selectedcountry.Season) }); } var competitions = _context.Competitions.Where(o => o.Country == selectedcountry); var selectedcompetition = competitions.First(); List <SelectListItem> competitionslist = new List <SelectListItem>(); foreach (var competition in competitions) { competitionslist.Add(new SelectListItem() { Text = competition.Name, Value = competition.Id.ToString() }); } IEnumerable <CompetitionLeagueTable> compstandings = null; if (selectedcountry.Season != null) { compstandings = CompetitionLeagueTable.GetStandings(selectedcompetition, (int)selectedcountry.Season); } else { compstandings = new List <CompetitionLeagueTable>(); } var chosen_match = _context.Matches .Where(m => m.Season == selectedcountry.Season && m.CompetitionEvent.CompetitionId == selectedcompetition.Id && m.HasEnded == false) .OrderBy(m => m.RoundNumber).FirstOrDefault(); if (chosen_match == null) { chosen_match = _context.Matches .Where(m => m.Season == selectedcountry.Season && m.CompetitionEvent.CompetitionId == selectedcompetition.Id && m.HasEnded == true) .OrderByDescending(m => m.RoundNumber).FirstOrDefault(); } IEnumerable <Match> matches; if (chosen_match != null) { var round = chosen_match.RoundNumber; matches = _context.Matches .Where(m => m.Season == selectedcountry.Season && m.CompetitionEvent.CompetitionId == selectedcompetition.Id && m.RoundNumber == round) .Include(m => m.HomeClub) .Include(m => m.AwayClub) .ToList(); } else { matches = new List <Match>(); } var VM = new HomeViewModel() { WorldDate = worlddate, Season = selectedcountry.Season, Continents = continentlist, ContinentCountries = countrylist, CountrySeasons = countryseasonslist, CountryCompetitions = competitionslist, CompetitionStandings = compstandings.ToList(), CompetitionRoundMatches = matches.ToList() }; return(View(VM)); }