// GET: api/Division/5 public Rootobject Get(Guid divisionId) { using (var context = new TournamentModels.TournamentTreeAppEntities()) { //this greatly improves perfomance by preloading divisions schools and participants for a given tournament var tournament = context.Tournaments.Where(t => t.Divisions.Any(d => d.DivisionId == divisionId)).Include(t => t.Divisions).Include(t => t.Schools).Include(t => t.Participants).FirstOrDefault(); var division = tournament.Divisions // .Include(d => d.ParticipantDivisionInts) .FirstOrDefault(d => d.DivisionId == divisionId); if (division.DrawBracket && division.ParticipantDivisionInts.Count > 1) { var bracketList = division.BracketParticipants(context).ToArray(); var teams = new string[(int)(bracketList.Length / 2)][]; for (int i = 0; i < (int)(bracketList.Length / 2); i++) { var element1 = bracketList[2 * i]; var element2 = bracketList[2 * i + 1]; var name1 = element1.School == null ? element1.Name : element1.Name + "|" + element1.School.Name; var name2 = element2.School == null ? element2.Name : element2.Name + "|" + element2.School.Name; teams[i] = new string[] { name1, name2 }; } var init = new Init() /*results = new int?[1][][][],*/ teams {
private ActionResult GetDivisionView(Guid divisionId) { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); using (var context = new TournamentModels.TournamentTreeAppEntities()) { var division = context.Divisions.FirstOrDefault(d => d.DivisionId == divisionId); var list = division.Tournament.Divisions.OrderBy(d => d.OrderId).ToList(); int index = list.IndexOf(division); // find the index of the given number // find the index of next and the previous number // by taking into account that // the given number might be the first or the last number in the list int prev = index > 0 ? index - 1 : -1; int next = index < list.Count - 1 ? index + 1 : -1; return(View(new MinimalDivision() { Id = divisionId, Name = division.Name, TournamentName = division.Tournament.Name, PreviousDivision = prev != -1 ? list[prev] : null, NextDivision = next != -1 ? list[next] : null, PreviousAsBracket = prev != -1 ? list[prev].DrawBracket && list[prev].ParticipantDivisionInts.Any() : false, NextAsBracket = next != -1 ? list[next].DrawBracket && list[next].ParticipantDivisionInts.Any() : false, Title = division.Title, TournamentId = division.TournamentId })); } }
// GET: api/Division public Rootobject Get() { using (var context = new TournamentModels.TournamentTreeAppEntities()) { //return context.Divisions.Select(d=>d.Name); return(new Rootobject()); } }
public ActionResult Print(Guid divisionId) { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); using (var context = new TournamentModels.TournamentTreeAppEntities()) { var division = context.Divisions.FirstOrDefault(d => d.DivisionId == divisionId); return(View(new MinimalDivision() { Id = divisionId, Name = division.Name, TournamentName = division.Tournament.Name, Title = division.Title, TournamentId = division.TournamentId })); } }
public List <Participant> BracketParticipants(TournamentModels.TournamentTreeAppEntities context) { var allHaveOrder = !this.ParticipantDivisionInts.Any(pdi => pdi.OrderId == null); if (allHaveOrder) { //get them in the order of the database var parray = this.ParticipantDivisionInts.OrderBy(pdi => pdi.OrderId).Select(pint => pint.Participant).ToList(); var h = (int)(Math.Pow(2, Math.Ceiling(Math.Log(parray.Count(), 2)))); if (parray.Count <= 2) { return(parray); } var result = new List <Participant>(); int neededDummies = h - parray.Count(); while (parray.Count > 0) { var sp = parray.FirstOrDefault(); if (sp != null) { result.Add(sp); parray.Remove(sp); if (neededDummies-- > 0) { result.Add(new Participant() { Name = " ===>" /*"advance ==>"*/, Dummy = true, School = null }); } } } return(result); } else { ///this is necessary to have consistent output. however, ordering by GUID is equivalent with a random order that becomes consistent var participants = this.ParticipantDivisionInts.OrderBy(pdi => pdi.ParticipantDivisionIntId).Select(pint => pint.Participant).ToList(); if (participants.Count <= 2) { return(participants); } var result = new List <Participant>(); var parray = participants.OrderBy(p => p.School.SchoolId).ToList(); var h = (int)(Math.Pow(2, Math.Ceiling(Math.Log(parray.Count(), 2)))); var schoolsDescendentByCount = participants.GroupBy(n => n.School.SchoolId). Select(group => new { Id = group.Key, Count = group.Count() }).OrderByDescending(sg => sg.Count).Select(sg => sg.Id).Distinct(); int order = 0; int neededDummies = h - parray.Count(); while (parray.Count > 0) { foreach (var schoolId in schoolsDescendentByCount) { var sp = parray.FirstOrDefault(p => p.School.SchoolId == schoolId); if (sp != null) { result.Add(sp); parray.Remove(sp); if (context != null) { var pdiRec = this.ParticipantDivisionInts.FirstOrDefault(pdi => pdi.ParticipantId == sp.ParticipantId); if (pdiRec != null) { pdiRec.OrderId = order++; //save the new order to database context.SaveChanges(); } } if (neededDummies-- > 0) { result.Add(new Participant() { Name = " ===>" /*"advance ==>"*/, Dummy = true, School = null }); } } } } return(result); } }