public CompetitionPhase InsertNewCompetitionPhase(Competition competition, int stageId, CompetitionPhaseTypeEnum phaseType) { var newPhase = new CompetitionPhase() { Competition = competition, StageId = stageId, Settings = "", CompetitionPhaseInfoType = (short)phaseType }; DbContext.CompetitionPhases.Add(newPhase); return(newPhase); }
public void UpdateCompetitionPhaseSettings(CompetitionPhase competitionPhase, CompetitionAdvancedOptionsDTO advancedOptions, Dictionary <int, List <Match> > matches, JArray competitorAllocations, Dictionary <int, Competitor> competitorLookup) { switch (advancedOptions.CompetitionPhaseType) { case CompetitionPhaseTypeEnum.Table: updateTableCompetitionPhaseSettings(competitionPhase, advancedOptions, matches, competitorAllocations, competitorLookup); break; case CompetitionPhaseTypeEnum.Knockout: throw new NotImplementedException(); default: throw new Exception("Type not supported"); } }
public List <CompetitorPhaseInfo> InsertNewCompetitorPhaseInfos(CompetitionPhase competitionPhase, List <Competitor> competitors) { var newCompetitorPhaseInfos = new List <CompetitorPhaseInfo>(); foreach (var competitor in competitors) { var phaseInfo = new CompetitorPhaseInfo() { Competitor = competitor, CompetitionPhase = competitionPhase }; DbContext.CompetitorPhaseInfoes.Add(phaseInfo); newCompetitorPhaseInfos.Add(phaseInfo); } return(newCompetitorPhaseInfos); }
private void updateTableCompetitionPhaseSettings(CompetitionPhase competitionPhase, CompetitionAdvancedOptionsDTO advancedOptions, Dictionary <int, List <Match> > matches, JArray competitorAllocations, Dictionary <int, Competitor> competitorLookup) { var competitionSettings = new GroupPhaseSettings(); if (!string.IsNullOrEmpty(competitionPhase.Settings)) { competitionSettings.PopulateObject(competitionPhase.Settings); } Dictionary <int, List <int> > matchIds = new Dictionary <int, List <int> >(); foreach (var groupMathces in matches) { matchIds.Add(groupMathces.Key, new List <int>()); matchIds[groupMathces.Key].AddRange(groupMathces.Value.Select(x => x.Id)); } competitionSettings.MatchIds = matchIds; competitionSettings.CompetitorIds = getCompetitorsGroupedByGroup(competitorAllocations, competitorLookup); competitionSettings.MatchInfoType = advancedOptions.MatchInfoType; competitionSettings.CompetitorPhaseInfoType = advancedOptions.CompetititorInfoType; competitionPhase.Settings = competitionSettings.SerializeObject(); }
private List <Match> generateScheduleForEachGroup(List <Competitor> competitors, CompetitionPhase competitionPhase) { List <Match> matches = new List <Match>(); // need to add a dummy competitor if (competitors.Count % 2 == 1) { competitors.Add(new Competitor() { Id = Int32.MinValue }); } int numberOfLegs = competitors.Count - 1; List <Competitor> roundRobin = new List <Competitor>(); roundRobin.AddRange(competitors); // tomislav swap roundRobin = shiftCompetitors(roundRobin); for (var leg = 1; leg <= numberOfLegs; leg++) { var team1 = 0; var team2 = competitors.Count - 1; while (team1 < team2) { var competitor1 = roundRobin[team1]; var competitor2 = roundRobin[team2]; if (competitor1.Id == Int32.MinValue || competitor2.Id == Int32.MinValue) { team1++; team2--; continue; } matches.Add(new Match() { Competitor = competitor1, Competitor1 = competitor2, CompetitionPhase = competitionPhase, Leg = leg }); team1++; team2--; } roundRobin = shiftCompetitors(roundRobin); } return(matches); }
// TODO only expose generateScheduleForEachGroup, everything else can go to base class protected override Dictionary <int, List <Match> > generateScheduleInternal(List <List <int> > competitorAllocations, Dictionary <int, Competitor> competitorLookup, CompetitionPhase competitionPhase) { Dictionary <int, List <Match> > matches = new Dictionary <int, List <Match> >(); int groupKey = 0; foreach (var competitorGroup in competitorAllocations) { List <Competitor> competitors = new List <Competitor>(); foreach (var competitorId in competitorGroup) { competitors.Add(competitorLookup[competitorId]); } matches[groupKey] = new List <Match>(); matches[groupKey].AddRange(generateScheduleForEachGroup(competitors, competitionPhase)); groupKey++; } return(matches); }
protected abstract Dictionary <int, List <Match> > generateScheduleInternal(List <List <int> > competitorAllocations, Dictionary <int, Competitor> competitorLookup, CompetitionPhase competitionPhase);
// TODO mapping of competitor allocations to competitor lookup can be done here public Dictionary <int, List <Match> > GenerateSchedule(JArray competitorAllocations, Dictionary <int, Competitor> competitorLookup, CompetitionPhase competitionPhase) { return(generateScheduleInternal(getCompetitorAllocations(competitorAllocations), competitorLookup, competitionPhase)); }