public NonQueryResult InsertJudgeDraw(JudgeDraw judgeDraw) { StringBuilder query = new StringBuilder(); query.AppendLine("INSERT INTO judge_draw (draw_id, judge_id, debate_id, number) "); query.AppendFormat("VALUES('{0}', '{1}', '{2}', '{3}')", judgeDraw.DrawId, judgeDraw.JudgeId, judgeDraw.DebateId, judgeDraw.Number); return(ExecuteNonQuery(query.ToString(), true)); }
public NonQueryResult UpdateJudgeDraw(JudgeDraw judgeDraw) { StringBuilder query = new StringBuilder(); query.AppendLine("UPDATE judge_draw "); query.AppendFormat("SET judge_id = '{0}', ", judgeDraw.JudgeId); query.AppendFormat("debate_id = '{0}', ", judgeDraw.DebateId); query.AppendFormat("number = {0} ", judgeDraw.Number); query.AppendFormat("WHERE draw_id = '{0}'", judgeDraw.DrawId); return(ExecuteNonQuery(query.ToString(), true)); }
private void MoveDraggedValue(object sender, DataGridViewCellMouseEventArgs e) { if (!e.Button.HasFlag(MouseButtons.Left)) { return; } if (_currentlySelectedCell == null) { return; } if (_currentlySelectedCell.ColumnIndex == e.ColumnIndex && _currentlySelectedCell.RowIndex == e.RowIndex) { return; } SqlHelper helper = new SqlHelper(_tournament.Database); bool isVenueDrag = _currentlySelectedCell.ColumnIndex == dgv_Round.Columns["Venue"].Index && dgv_Round.Rows[e.RowIndex].Tag != null; if (isVenueDrag) { Guid draggedDebateId = (Guid)dgv_Round.Rows[_currentlySelectedCell.RowIndex].Tag; Guid debateToSwitchItWithId = (Guid)dgv_Round.Rows[e.RowIndex].Tag; Debate draggedDebate = helper.GetDebate(draggedDebateId).Result; Guid draggedVenue = draggedDebate.VenueId; Debate debateToSwitchItWith = helper.GetDebate(debateToSwitchItWithId).Result; Guid switchedVenue = debateToSwitchItWith.VenueId; draggedDebate.VenueId = switchedVenue; debateToSwitchItWith.VenueId = draggedVenue; helper.UpdateDebate(draggedDebate); helper.UpdateDebate(debateToSwitchItWith); SwitchRows(e.RowIndex, _currentlySelectedCell.RowIndex); } bool isAdjudicatorDrag = dgv_Round.Columns[_currentlySelectedCell.ColumnIndex].Name.StartsWith("Adj") && dgv_Round.Columns[e.ColumnIndex].Name.StartsWith("Adj"); if (isAdjudicatorDrag) { Guid draggedJudgeDrawId = dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Tag != null ? (Guid)dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Tag : Guid.Empty; if (draggedJudgeDrawId.Equals(Guid.Empty)) { _currentlySelectedCell = null; Cursor = Cursors.Arrow; return; } bool doesDraggedRowPossiblyNeedToBeRearranged = false; for (int i = int.Parse(dgv_Round.Columns[_currentlySelectedCell.ColumnIndex].Name.Replace("Adj", string.Empty)) + 1; i <= 5; i++) { DataGridViewCell nextDraggedCell = dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[string.Format("Adj{0}", i)]; if (nextDraggedCell.Value != null) { doesDraggedRowPossiblyNeedToBeRearranged = true; break; } } JudgeDraw draggedJudgeDraw = helper.GetJudgeDraw(draggedJudgeDrawId).Result; bool areWeSwitchingWithAnotherCell = true; JudgeDraw judgeDrawToSwitchItWith; int columnIndexToSwitchTo = e.ColumnIndex; int adjudicatorNumber = int.Parse(dgv_Round.Columns[e.ColumnIndex].Name.Replace("Adj", string.Empty)); for (int i = 1; i < adjudicatorNumber; i++) { if (dgv_Round.Rows[e.RowIndex].Cells[string.Format("Adj{0}", i)].Value == null) { columnIndexToSwitchTo = dgv_Round.Columns[string.Format("Adj{0}", i)].Index; break; } } Guid judgeDrawToSwitchItWithId = dgv_Round.Rows[e.RowIndex].Cells[columnIndexToSwitchTo].Tag != null ? (Guid)dgv_Round.Rows[e.RowIndex].Cells[columnIndexToSwitchTo].Tag : Guid.Empty; if (judgeDrawToSwitchItWithId.Equals(Guid.Empty)) { judgeDrawToSwitchItWith = null; areWeSwitchingWithAnotherCell = false; } else { judgeDrawToSwitchItWith = helper.GetJudgeDraw(judgeDrawToSwitchItWithId).Result; } bool judgeInSameDebate; if (judgeDrawToSwitchItWith == null) { if (_currentlySelectedCell.RowIndex == e.RowIndex) { _currentlySelectedCell = null; Cursor = Cursors.Arrow; return; } else { judgeInSameDebate = false; } } else { judgeInSameDebate = draggedJudgeDraw.DebateId.Equals(judgeDrawToSwitchItWith.DebateId); } int newJudgeNumber = 0; if (judgeDrawToSwitchItWith != null) { newJudgeNumber = judgeDrawToSwitchItWith.Number; } else { newJudgeNumber = int.Parse(dgv_Round.Columns[e.ColumnIndex].Name.Replace("Adj", string.Empty)); } int draggedJudgeDrawNumber = draggedJudgeDraw.Number; int judgeDrawToSwitchItWithNumber = newJudgeNumber; draggedJudgeDraw.Number = judgeDrawToSwitchItWithNumber; if (areWeSwitchingWithAnotherCell) { judgeDrawToSwitchItWith.Number = draggedJudgeDrawNumber; } if (!judgeInSameDebate) { Guid draggedJudgeDrawDebateId = draggedJudgeDraw.DebateId; Guid judgeDrawToSwitchItWithDebateId = (Guid)dgv_Round.Rows[e.RowIndex].Tag; draggedJudgeDraw.DebateId = judgeDrawToSwitchItWithDebateId; if (areWeSwitchingWithAnotherCell) { judgeDrawToSwitchItWith.DebateId = draggedJudgeDrawDebateId; } } helper.UpdateJudgeDraw(draggedJudgeDraw); if (areWeSwitchingWithAnotherCell) { helper.UpdateJudgeDraw(judgeDrawToSwitchItWith); } string draggedJudgeDrawValue = (string)dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Value; string judgeDrawToSwitchItWithValue = (string)dgv_Round.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Value = judgeDrawToSwitchItWithValue; Guid draggedTag = (Guid)dgv_Round[_currentlySelectedCell.ColumnIndex, _currentlySelectedCell.RowIndex].Tag; Guid switchTag = (Guid)dgv_Round[e.ColumnIndex, e.RowIndex].Tag; dgv_Round[_currentlySelectedCell.ColumnIndex, _currentlySelectedCell.RowIndex].Tag = switchTag; dgv_Round[e.ColumnIndex, e.RowIndex].Tag = draggedTag; if (doesDraggedRowPossiblyNeedToBeRearranged && !areWeSwitchingWithAnotherCell) { for (int i = 1; i < 5; i++) { DataGridViewCell thisCell = dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[string.Format("Adj{0}", i)]; DataGridViewCell nextCell = dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[string.Format("Adj{0}", i + 1)]; if (thisCell.Value == null) { thisCell.Value = nextCell.Value; thisCell.Tag = nextCell.Tag; Guid nextId = nextCell.Tag != null ? (Guid)nextCell.Tag : Guid.Empty; if (nextId != Guid.Empty) { JudgeDraw nextJudgeDraw = helper.GetJudgeDraw(nextId).Result; nextJudgeDraw.Number = i; helper.UpdateJudgeDraw(nextJudgeDraw); } nextCell.Value = null; nextCell.Tag = null; } } } dgv_Round.Rows[e.RowIndex].Cells[columnIndexToSwitchTo].Value = draggedJudgeDrawValue; Refresh(); } Position position; bool isSpeakerDrag = Enum.TryParse(dgv_Round.Columns[_currentlySelectedCell.ColumnIndex].Name, out position) && Enum.TryParse(dgv_Round.Columns[_currentlySelectedCell.ColumnIndex].Name, out position); if (isSpeakerDrag) { Guid draggedSpeakerDrawId = (Guid)dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Tag; SpeakerDraw draggedSpeakerDraw = helper.GetSpeakerDraw(draggedSpeakerDrawId).Result; Guid speakerDrawToSwitchItWithId = (Guid)dgv_Round.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag; SpeakerDraw speakerDrawToSwitchItWith = helper.GetSpeakerDraw(speakerDrawToSwitchItWithId).Result; bool speakerInSameDebate = draggedSpeakerDraw.DebateId.Equals(speakerDrawToSwitchItWith.DebateId); Position draggedSpeakerDrawPosition = draggedSpeakerDraw.Position; Position speakerDrawToSwitchItWithPosition = speakerDrawToSwitchItWith.Position; draggedSpeakerDraw.Position = speakerDrawToSwitchItWithPosition; speakerDrawToSwitchItWith.Position = draggedSpeakerDrawPosition; if (!speakerInSameDebate) { Guid draggedSpeakerDrawDebateId = draggedSpeakerDraw.DebateId; Guid speakerDrawToSwitchItWithDebateId = speakerDrawToSwitchItWith.DebateId; draggedSpeakerDraw.DebateId = speakerDrawToSwitchItWithDebateId; speakerDrawToSwitchItWith.DebateId = draggedSpeakerDrawDebateId; } helper.UpdateSpeakerDraw(draggedSpeakerDraw); helper.UpdateSpeakerDraw(speakerDrawToSwitchItWith); string draggedSpeakerDrawValue = (string)dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Value; string speakerDrawToSwitchItWithValue = (string)dgv_Round.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; dgv_Round.Rows[_currentlySelectedCell.RowIndex].Cells[_currentlySelectedCell.ColumnIndex].Value = speakerDrawToSwitchItWithValue; dgv_Round.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = draggedSpeakerDrawValue; } ShowClashes(_currentlySelectedRound.RoundId); _currentlySelectedCell = null; Cursor = Cursors.Arrow; }
private static Round DrawPowerpairedRound(Tournament tournament, DataContext context, string motion) { SqlHelper helper = new SqlHelper(tournament.Database); Round round = new Round() { Motion = motion, IsRandom = false, RoundNumber = tournament.RoundNumber }; NonQueryResult insert = helper.InsertRound(round); List <Debate> debates = new List <Debate>(); List <Speaker> speakers = new List <Speaker>(); speakers.AddRange(context.Speakers.Where(a => a.Active)); speakers.AsParallel().ForAll(a => a.Draws = helper.GetSpeakerDraws(a.SpeakerId).Result); speakers = speakers.OrderByDescending(a => a.Draws.Sum(d => d.Result.Points())).ThenByDescending(s => s.Draws.Sum(d => d.SpeakerPoints)).ToList(); int numberOfRooms = speakers.Count / 6; int numberOfJudgesPerRoom = context.Judges.Count / numberOfRooms; List <Judge> judges = new List <Judge>(); judges.AddRange(context.Judges.Where(j => j.Active)); judges = judges.OrderByDescending(j => (int)j.Level).ToList(); List <Venue> venues = new List <Venue>(); venues.AddRange(context.Venues); for (int i = 1; i <= numberOfRooms; i++) { Venue v = venues.First(); venues.Remove(v); Debate debate = new Debate() { RoundId = round.RoundId, VenueId = v.VenueId }; debates.Add(debate); insert = helper.InsertDebate(debate); List <Speaker> toInsert = speakers.Take(6).ToList(); SetSpeakersResult result = SetSpeakers(toInsert, debate.DebateId); foreach (SpeakerDraw speakerDraw in result.SpeakerDraws) { helper.InsertSpeakerDraw(speakerDraw); } foreach (Speaker speaker in toInsert) { speakers.Remove(speaker); } Judge judge = judges.First(); JudgeDraw judgeDraw = new JudgeDraw() { DebateId = debate.DebateId, Number = 1, JudgeId = judge.JudgeId }; judges.Remove(judge); insert = helper.InsertJudgeDraw(judgeDraw); } foreach (Debate debate in debates) { for (int i = 2; i <= numberOfJudgesPerRoom; i++) { Judge j = judges.First(); JudgeDraw judgeDraw = new JudgeDraw() { DebateId = debate.DebateId, Number = i, JudgeId = j.JudgeId }; judges.Remove(j); insert = helper.InsertJudgeDraw(judgeDraw); } } while (judges.Any()) { Debate debate = debates.OrderBy(a => a.DebateId).First(); debates.Remove(debate); Judge j = judges.First(); JudgeDraw judgeDraw = new JudgeDraw() { DebateId = debate.DebateId, Number = numberOfJudgesPerRoom + 1, JudgeId = j.JudgeId }; judges.Remove(j); insert = helper.InsertJudgeDraw(judgeDraw); } return(round); }
private static Round DrawNonPowerpairedRound(Tournament tournament, DataContext context, string motion) { SqlHelper helper = new SqlHelper(tournament.Database); Round round = new Round() { Motion = motion, IsRandom = true, RoundNumber = tournament.RoundNumber }; NonQueryResult insert = helper.InsertRound(round); Random r = new Random(); List <Debate> debates = new List <Debate>(); List <Speaker> initialSpeakers = context.Speakers.Where(s => s.Active).OrderBy(a => Guid.NewGuid()).ToList(); Queue <Speaker> speakers = new Queue <Speaker>(context.Speakers.Count(s => s.Active)); int count = context.Speakers.Count(s => s.Active); while (initialSpeakers.Any()) { Speaker s = initialSpeakers.First(); speakers.Enqueue(s); initialSpeakers.Remove(s); } int numberOfRooms = speakers.Count / 6; int numberOfJudgesPerRoom = context.Judges.Count(j => j.Active) / numberOfRooms; List <Judge> judges = new List <Judge>(); judges.AddRange(context.Judges.Where(j => j.Active)); judges = judges.OrderByDescending(j => (int)j.Level).ToList(); List <Venue> venues = new List <Venue>(); venues.AddRange(context.Venues.Where(v => v.Active)); for (int i = 1; i <= numberOfRooms; i++) { Venue v = venues.First(); venues.Remove(v); Debate debate = new Debate() { RoundId = round.RoundId, VenueId = v.VenueId }; debates.Add(debate); insert = helper.InsertDebate(debate); foreach (Position p in Enum.GetValues(typeof(Position))) { if (p == Position.Invalid) { continue; } Speaker s = speakers.Dequeue(); SpeakerDraw speakerDraw = new SpeakerDraw() { DebateId = debate.DebateId, Position = p, SpeakerId = s.SpeakerId }; insert = helper.InsertSpeakerDraw(speakerDraw); } } foreach (Debate debate in debates.OrderBy(a => Guid.NewGuid())) { Judge j = judges.First(); JudgeDraw judgeDraw = new JudgeDraw() { DebateId = debate.DebateId, Number = 1, JudgeId = j.JudgeId }; judges.Remove(j); insert = helper.InsertJudgeDraw(judgeDraw); } foreach (Debate debate in debates.OrderBy(a => Guid.NewGuid())) { for (int i = 2; i <= numberOfJudgesPerRoom; i++) { Judge j = judges.First(); JudgeDraw judgeDraw = new JudgeDraw() { DebateId = debate.DebateId, Number = i, JudgeId = j.JudgeId }; judges.Remove(j); insert = helper.InsertJudgeDraw(judgeDraw); } } while (judges.Any()) { Debate debate = debates.OrderBy(a => a.DebateId).First(); debates.Remove(debate); Judge j = judges.First(); JudgeDraw judgeDraw = new JudgeDraw() { DebateId = debate.DebateId, Number = numberOfJudgesPerRoom + 1, JudgeId = j.JudgeId }; judges.Remove(j); insert = helper.InsertJudgeDraw(judgeDraw); } return(round); }