diff --git a/.vs/TournamentManager/v16/.suo b/.vs/TournamentManager/v16/.suo index a60a3318e5e63d434f0569b2bd3d4a1f134533c5..879509338c32a17b7c9cd17b59ff62d149c10d69 100644 Binary files a/.vs/TournamentManager/v16/.suo and b/.vs/TournamentManager/v16/.suo differ diff --git a/TournamentManager.csproj.user b/TournamentManager.csproj.user index 175b490165694aa0fcf3de4467069bb5bedc5277..2135d6c013c2a648bbad0b07a6d391bda8a4dbd3 100644 --- a/TournamentManager.csproj.user +++ b/TournamentManager.csproj.user @@ -7,9 +7,6 @@ <Compile Update="src\ComputeControl.cs"> <SubType>UserControl</SubType> </Compile> - <Compile Update="src\ExportControl.cs"> - <SubType>UserControl</SubType> - </Compile> <Compile Update="src\FileNameForm.cs"> <SubType>Form</SubType> </Compile> @@ -22,14 +19,14 @@ <Compile Update="src\LoadFileForm.cs"> <SubType>Form</SubType> </Compile> - <Compile Update="src\ManagePlayOffControl.cs"> - <SubType>UserControl</SubType> - </Compile> <Compile Update="src\MenuForm.cs"> <SubType>Form</SubType> </Compile> <Compile Update="src\ManageParticipantsControl.cs"> <SubType>UserControl</SubType> </Compile> + <Compile Update="src\PlayOffControl.cs"> + <SubType>UserControl</SubType> + </Compile> </ItemGroup> </Project> \ No newline at end of file diff --git a/obj/Debug/net5.0-windows/TournamentManager.csproj.CoreCompileInputs.cache b/obj/Debug/net5.0-windows/TournamentManager.csproj.CoreCompileInputs.cache index 43ff0ceb24a779bb76551703487d703e57268a99..3b0453c933a4906ed71208aa9abab2dfb5a39c41 100644 --- a/obj/Debug/net5.0-windows/TournamentManager.csproj.CoreCompileInputs.cache +++ b/obj/Debug/net5.0-windows/TournamentManager.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -e19624a476748bac92b80373f4ba141365998171 +160e6250df4ddd545485f1180311e6597081c329 diff --git a/obj/Debug/net5.0-windows/TournamentManager.csproj.FileListAbsolute.txt b/obj/Debug/net5.0-windows/TournamentManager.csproj.FileListAbsolute.txt index 01cf7e004a022619b08593f34de8c50f955ab642..3e886b86888a7979ba89a22df00c4f1fde5e675c 100644 --- a/obj/Debug/net5.0-windows/TournamentManager.csproj.FileListAbsolute.txt +++ b/obj/Debug/net5.0-windows/TournamentManager.csproj.FileListAbsolute.txt @@ -51,6 +51,5 @@ C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0 C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.pdb C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.genruntimeconfig.cache C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.src.GroupControl.resources -C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.src.ExportControl.resources C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.src.ComputeControl.resources -C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.src.ManagePlayOffControl.resources +C:\Users\micha\School\RiderProjects\c-sharp-semestral-project-1\obj\Debug\net5.0-windows\TournamentManager.src.PlayOffControl.resources diff --git a/obj/Debug/net5.0-windows/TournamentManager.csprojAssemblyReference.cache b/obj/Debug/net5.0-windows/TournamentManager.csprojAssemblyReference.cache index e95e876e53b562933b52e7fe77aab54a4f3ec76e..06f02179af6ee23638d37c4dd9cbfba21f64e0c9 100644 Binary files a/obj/Debug/net5.0-windows/TournamentManager.csprojAssemblyReference.cache and b/obj/Debug/net5.0-windows/TournamentManager.csprojAssemblyReference.cache differ diff --git a/obj/Debug/net5.0-windows/TournamentManager.dll b/obj/Debug/net5.0-windows/TournamentManager.dll index c35b47e9a82a75ea6e86a97c4c43df9152d06692..c256a6a256a9c1bc21e86216f37b9348058f10fe 100644 Binary files a/obj/Debug/net5.0-windows/TournamentManager.dll and b/obj/Debug/net5.0-windows/TournamentManager.dll differ diff --git a/obj/Debug/net5.0-windows/TournamentManager.pdb b/obj/Debug/net5.0-windows/TournamentManager.pdb index c3cafb872867251c45fbea05921e89dd3e9e965e..0bfa0aa6a0056f267f47a29059d3ce3ac27243be 100644 Binary files a/obj/Debug/net5.0-windows/TournamentManager.pdb and b/obj/Debug/net5.0-windows/TournamentManager.pdb differ diff --git a/obj/Debug/net5.0-windows/TournamentManager.src.ManagePlayOffControl.resources b/obj/Debug/net5.0-windows/TournamentManager.src.ManagePlayOffControl.resources deleted file mode 100644 index 6c05a9776bd7cbae976fdcec7e3a254e93018279..0000000000000000000000000000000000000000 Binary files a/obj/Debug/net5.0-windows/TournamentManager.src.ManagePlayOffControl.resources and /dev/null differ diff --git a/obj/Debug/net5.0-windows/TournamentManager.src.ExportControl.resources b/obj/Debug/net5.0-windows/TournamentManager.src.PlayOffControl.resources similarity index 100% rename from obj/Debug/net5.0-windows/TournamentManager.src.ExportControl.resources rename to obj/Debug/net5.0-windows/TournamentManager.src.PlayOffControl.resources diff --git a/obj/Debug/net5.0-windows/ref/TournamentManager.dll b/obj/Debug/net5.0-windows/ref/TournamentManager.dll index 300d70b7200a46faf9d86efb6b44148889d3bab6..97aedc9d3d5381f72c4e871cf394b358fd841d19 100644 Binary files a/obj/Debug/net5.0-windows/ref/TournamentManager.dll and b/obj/Debug/net5.0-windows/ref/TournamentManager.dll differ diff --git a/src/GroupSelectedEventArgs.cs b/src/CbItemSelectedEventArgs.cs similarity index 58% rename from src/GroupSelectedEventArgs.cs rename to src/CbItemSelectedEventArgs.cs index 577945f7a6c13611f92773faa008e8c5729467af..c16c587b7ae786d7ea616b6aece260894805a2bb 100644 --- a/src/GroupSelectedEventArgs.cs +++ b/src/CbItemSelectedEventArgs.cs @@ -2,11 +2,11 @@ namespace TournamentManager { - public class GroupSelectedEventArgs : EventArgs + public class CbItemSelectedEventArgs : EventArgs { public string ItemName { get; } - public GroupSelectedEventArgs(string itemName) + public CbItemSelectedEventArgs(string itemName) { ItemName = itemName; } diff --git a/src/GroupControl.cs b/src/GroupControl.cs index 38a2ba9eaa71b27df00ce8f824bacd1b41ce98ba..42392f84e8eec4b04d7c601c35f11de43046ab69 100644 --- a/src/GroupControl.cs +++ b/src/GroupControl.cs @@ -14,18 +14,18 @@ namespace TournamentManager.src { public partial class GroupControl : UserControl { - public delegate void CbGroupItemSelectedDelegate(object sender, GroupSelectedEventArgs e); + public delegate void CbGroupItemSelectedDelegate(object sender, CbItemSelectedEventArgs e); public delegate void OpponentItemsSelectedDelegate(object sender, OpponentsSelectedEventArgs e); - public delegate void MatchSelectedDelegate(object sender, MatchSelectedEventArgs e); + public delegate void MatchSelectedDelegate(object sender, MatchSubmittedEventArgs e); public event CbGroupItemSelectedDelegate CbGroupItemSelected; public event OpponentItemsSelectedDelegate OpponentsSelected; public event MatchSelectedDelegate SubmitClicked; - protected void OnGroupSelected(GroupSelectedEventArgs e) => CbGroupItemSelected?.Invoke(this, e); + protected void OnGroupSelected(CbItemSelectedEventArgs e) => CbGroupItemSelected?.Invoke(this, e); protected void OnOpponentsSelected(OpponentsSelectedEventArgs e) => OpponentsSelected?.Invoke(this, e); - protected void OnSubmitClicked(MatchSelectedEventArgs e) => SubmitClicked?.Invoke(this, e); + protected void OnSubmitClicked(MatchSubmittedEventArgs e) => SubmitClicked?.Invoke(this, e); public GroupControl() @@ -55,7 +55,7 @@ namespace TournamentManager.src private void cbGroup_SelectedIndexChanged(object sender, EventArgs e) { ClearInputData(); - OnGroupSelected(new GroupSelectedEventArgs(cbGroup.SelectedItem.ToString())); + OnGroupSelected(new CbItemSelectedEventArgs(cbGroup.SelectedItem.ToString())); } private void cbFirstOpponent_SelectedIndexChanged(object sender, EventArgs e) @@ -88,7 +88,7 @@ namespace TournamentManager.src return; } - OnSubmitClicked(new MatchSelectedEventArgs(cbFirstOpponent.SelectedItem.ToString(), + OnSubmitClicked(new MatchSubmittedEventArgs(cbFirstOpponent.SelectedItem.ToString(), cbSecondOpponent.SelectedItem.ToString(), (firstScore, secondScore), index - 1)); } diff --git a/src/ManagePlayOffControl.cs b/src/ManagePlayOffControl.cs deleted file mode 100644 index aad78e61a214e0a4c9484e27d021cdc0614d8b09..0000000000000000000000000000000000000000 --- a/src/ManagePlayOffControl.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace TournamentManager.src -{ - public partial class ManagePlayOffControl : UserControl - { - public ManagePlayOffControl() - { - InitializeComponent(); - } - - private void cbRound_SelectedIndexChanged(object sender, EventArgs e) - { - - } - - private void cbMatch_SelectedIndexChanged(object sender, EventArgs e) - { - - } - - private void btnSubmit_Click(object sender, EventArgs e) - { - - } - } -} diff --git a/src/ManageTournamentForm.Designer.cs b/src/ManageTournamentForm.Designer.cs index 39a473825d8cc31dc15e90395ff634f34056aa2a..9ae1c587b3afb86b4f44b3360093e2304969b251 100644 --- a/src/ManageTournamentForm.Designer.cs +++ b/src/ManageTournamentForm.Designer.cs @@ -40,7 +40,7 @@ namespace TournamentManager this.groupBox4 = new System.Windows.Forms.GroupBox(); this.computeControl = new TournamentManager.src.ComputeControl(); this.groupBox5 = new System.Windows.Forms.GroupBox(); - this.managePlayOffControl1 = new TournamentManager.src.ManagePlayOffControl(); + this.playOffControl = new TournamentManager.src.PlayOffControl(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); @@ -103,36 +103,36 @@ namespace TournamentManager // groupBox4 // this.groupBox4.Controls.Add(this.computeControl); - this.groupBox4.Location = new System.Drawing.Point(394, 411); + this.groupBox4.Location = new System.Drawing.Point(394, 417); this.groupBox4.Name = "groupBox4"; - this.groupBox4.Size = new System.Drawing.Size(501, 138); + this.groupBox4.Size = new System.Drawing.Size(502, 139); this.groupBox4.TabIndex = 3; this.groupBox4.TabStop = false; this.groupBox4.Text = "Calculate"; // // computeControl // - this.computeControl.Location = new System.Drawing.Point(5, 22); + this.computeControl.Location = new System.Drawing.Point(6, 22); this.computeControl.Name = "computeControl"; this.computeControl.Size = new System.Drawing.Size(491, 111); this.computeControl.TabIndex = 0; // // groupBox5 // - this.groupBox5.Controls.Add(this.managePlayOffControl1); - this.groupBox5.Location = new System.Drawing.Point(394, 172); + this.groupBox5.Controls.Add(this.playOffControl); + this.groupBox5.Location = new System.Drawing.Point(394, 207); this.groupBox5.Name = "groupBox5"; - this.groupBox5.Size = new System.Drawing.Size(501, 183); + this.groupBox5.Size = new System.Drawing.Size(502, 172); this.groupBox5.TabIndex = 4; this.groupBox5.TabStop = false; this.groupBox5.Text = "Manage Play Off"; // - // managePlayOffControl1 + // playOffControl // - this.managePlayOffControl1.Location = new System.Drawing.Point(4, 22); - this.managePlayOffControl1.Name = "managePlayOffControl1"; - this.managePlayOffControl1.Size = new System.Drawing.Size(493, 150); - this.managePlayOffControl1.TabIndex = 0; + this.playOffControl.Location = new System.Drawing.Point(4, 22); + this.playOffControl.Name = "playOffControl"; + this.playOffControl.Size = new System.Drawing.Size(493, 139); + this.playOffControl.TabIndex = 0; // // ManageTournamentForm // @@ -146,7 +146,6 @@ namespace TournamentManager this.Controls.Add(this.groupBox1); this.Name = "ManageTournamentForm"; this.Text = "ManageTournamentForm"; - this.Load += new System.EventHandler(this.ManageTournamentForm_Load); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); @@ -167,6 +166,7 @@ namespace TournamentManager private System.Windows.Forms.GroupBox groupBox4; private src.ComputeControl computeControl; private System.Windows.Forms.GroupBox groupBox5; - private src.ManagePlayOffControl managePlayOffControl1; + private src.PlayOffControl _playOffControl1; + private src.PlayOffControl playOffControl; } } \ No newline at end of file diff --git a/src/ManageTournamentForm.cs b/src/ManageTournamentForm.cs index ac327edf4008e4130b3fd992a0e2fde63ba70f2d..914d4020669dba660f715b319f5ee100cafae638 100644 --- a/src/ManageTournamentForm.cs +++ b/src/ManageTournamentForm.cs @@ -30,26 +30,21 @@ namespace TournamentManager groupControl.CbGroupItemSelected += HandleOnGroupSelected; groupControl.OpponentsSelected += HandleOnGroupOpponentsSelected; groupControl.SubmitClicked += HandleOnGroupControlSubmitClicked; + playOffControl.RoundSelected += HandleOnRoundSelected; + playOffControl.MatchSelected += HandleOnMatchSelected; + playOffControl.SubmitPlayOffMatchClicked += HandleOnSubmitPlayOffMatchClicked; foreach (var p in _participants) { manageParticipantsControl.CbAddParticipant($"{p.FirstName} {p.Surname}"); } } - + #region eventHandlers protected void OnSubmitParticipantClicked(object sender, EventArgs e) { - if (_participants.Any(x => - x.FirstName.Equals(enterantCntrl.NewParticipant.FirstName) && - x.Surname.Equals(enterantCntrl.NewParticipant.Surname))) - { - MessageBox.Show(RemoveParticipantError, ErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); - return; - } - _participants.Add(enterantCntrl.NewParticipant); - enterantCntrl.ResetControl(); + } private void HandleParticipantsChanged(object sender, NotifyCollectionChangedEventArgs e) @@ -121,6 +116,7 @@ namespace TournamentManager var toKeep = (int)Math.Round(((double)_participants.Count / (double)100) * (double)(100 - e.Amount), 0); var sortedParticipants = (await ComputeAllGroupOrders()).Take(toKeep).ToList(); _playOff = new PlayOffTree(sortedParticipants); + playOffControl.SetRounds(_playOff.Depth); } catch (Exception ex) { @@ -132,7 +128,7 @@ namespace TournamentManager } } - private void HandleOnGroupSelected(object sender, GroupSelectedEventArgs e) + private void HandleOnGroupSelected(object sender, CbItemSelectedEventArgs e) { if (!int.TryParse(e.ItemName.Split(" ")[1], out var result)) { @@ -153,21 +149,21 @@ namespace TournamentManager return; } - var group = _groups[e.GroupIndex]; + var group = _groups[e.Index]; var firstOpponent = group.ExtractGroupMember(e.FirstOpponent); var secondOpponent = group.ExtractGroupMember(e.SecondOpponent); groupControl.SetTextBoxes(group.GetScore(firstOpponent, secondOpponent)); } - private void HandleOnGroupControlSubmitClicked(object sender, MatchSelectedEventArgs e) + private void HandleOnGroupControlSubmitClicked(object sender, MatchSubmittedEventArgs e) { if (e.FirstOpponent.Equals(e.SecondOpponent)) { //TODO:ERROR MESSAGE return; } - var group = _groups[e.GroupIndex]; + var group = _groups[e.Index]; Func<string[], Participant> matchOpponent = x => group.Members .FirstOrDefault(p => p.FirstName.Equals(x[0]) && p.Surname.Equals(x[1])); @@ -176,6 +172,24 @@ namespace TournamentManager group.Matches[group.GetIndex(firstOpponent), group.GetIndex(secondOpponent)] = e.Score.first; group.Matches[group.GetIndex(secondOpponent), group.GetIndex(firstOpponent)] = e.Score.second; } + + + private void HandleOnRoundSelected(object sender, CbItemSelectedEventArgs e) + { + var matches = _playOff.GetRoundMatches(int.Parse(e.ItemName)); + playOffControl.SetMatches(matches); + } + + private void HandleOnMatchSelected(object sender, CbItemSelectedEventArgs e) + { + throw new NotImplementedException(); + } + + private void HandleOnSubmitPlayOffMatchClicked(object sender, MatchSubmittedEventArgs e) + { + throw new NotImplementedException(); + } + #endregion #region HelperFunctions @@ -215,10 +229,5 @@ namespace TournamentManager } #endregion - - private void ManageTournamentForm_Load(object sender, EventArgs e) - { - - } } } \ No newline at end of file diff --git a/src/MatchSelectedEventArgs.cs b/src/MatchSelectedEventArgs.cs deleted file mode 100644 index 92554161f7a0f9807d7d6bf263733b40d381db34..0000000000000000000000000000000000000000 --- a/src/MatchSelectedEventArgs.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace TournamentManager -{ - public class MatchSelectedEventArgs : OpponentsSelectedEventArgs - { - public (int first, int second) Score { get; } - - public MatchSelectedEventArgs(string firstOpponentName, string secondOpponentName, - (int first, int second) score, int groupIndex) : - base(firstOpponentName, secondOpponentName, groupIndex) - { - Score = score; - } - - } -} \ No newline at end of file diff --git a/src/MatchSubmittedEventArgs.cs b/src/MatchSubmittedEventArgs.cs new file mode 100644 index 0000000000000000000000000000000000000000..1894d0035d0ae0b2ce9c96a081c8468b10085d04 --- /dev/null +++ b/src/MatchSubmittedEventArgs.cs @@ -0,0 +1,17 @@ +using System; + +namespace TournamentManager +{ + public class MatchSubmittedEventArgs : OpponentsSelectedEventArgs + { + public (int first, int second) Score { get; } + + public MatchSubmittedEventArgs(string firstOpponentName, string secondOpponentName, + (int first, int second) score, int index) : + base(firstOpponentName, secondOpponentName, index) + { + Score = score; + } + + } +} \ No newline at end of file diff --git a/src/OpponentsSelectedEventArgs.cs b/src/OpponentsSelectedEventArgs.cs index 0d9beb977bef6830712b1b14565bc0503f20852a..cd3e7f5d7f19d815e70d8ac8d6f02b2a6627e9a7 100644 --- a/src/OpponentsSelectedEventArgs.cs +++ b/src/OpponentsSelectedEventArgs.cs @@ -7,13 +7,13 @@ namespace TournamentManager public string FirstOpponent { get; } public string SecondOpponent { get; } - public int GroupIndex { get; } + public int Index { get; } - public OpponentsSelectedEventArgs(string firstOpponentName, string secondOpponentName, int groupIndex) + public OpponentsSelectedEventArgs(string firstOpponentName, string secondOpponentName, int index) { FirstOpponent = firstOpponentName; SecondOpponent = secondOpponentName; - GroupIndex = groupIndex; + Index = index; } } diff --git a/src/ManagePlayOffControl.Designer.cs b/src/PlayOffControl.Designer.cs similarity index 98% rename from src/ManagePlayOffControl.Designer.cs rename to src/PlayOffControl.Designer.cs index 86e316e7287131fea4ae3d0678edcf6d266f3fc1..966ed1b45ee5571b0d9fa5ee7acf82044cabe5a1 100644 --- a/src/ManagePlayOffControl.Designer.cs +++ b/src/PlayOffControl.Designer.cs @@ -1,7 +1,7 @@  namespace TournamentManager.src { - partial class ManagePlayOffControl + partial class PlayOffControl { /// <summary> /// Required designer variable. @@ -149,7 +149,7 @@ namespace TournamentManager.src this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.cbRound); - this.Name = "ManagePlayOffControl"; + this.Name = "PlayOffControl"; this.Size = new System.Drawing.Size(491, 139); this.ResumeLayout(false); this.PerformLayout(); diff --git a/src/PlayOffControl.cs b/src/PlayOffControl.cs new file mode 100644 index 0000000000000000000000000000000000000000..8b6e89a2ae84d97519d8dcabe95bbd8cd3809893 --- /dev/null +++ b/src/PlayOffControl.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace TournamentManager.src +{ + public partial class PlayOffControl : UserControl + { + public delegate void CbItemSelectedDelegate(object sender, CbItemSelectedEventArgs e); + + public delegate void MatchSubmittedDelegate(object sender, MatchSubmittedEventArgs e); + + public event CbItemSelectedDelegate RoundSelected; + public event CbItemSelectedDelegate MatchSelected; + public event MatchSubmittedDelegate SubmitPlayOffMatchClicked; + + protected void OnRoundSelected(CbItemSelectedEventArgs e) => RoundSelected?.Invoke(this, e); + protected void OnMatchSelected(CbItemSelectedEventArgs e) => MatchSelected?.Invoke(this, e); + protected void OnMatchSubmitted(MatchSubmittedEventArgs e) => SubmitPlayOffMatchClicked?.Invoke(this, e); + + public PlayOffControl() + { + InitializeComponent(); + } + + private void cbRound_SelectedIndexChanged(object sender, EventArgs e) + { + if (cbRound.SelectedItem == null) + { + return; + } + OnRoundSelected(new CbItemSelectedEventArgs(cbRound.SelectedItem.ToString())); + } + + private void cbMatch_SelectedIndexChanged(object sender, EventArgs e) + { + if (cbMatch.SelectedItem == null) + { + return; + } + OnMatchSelected(new CbItemSelectedEventArgs(cbMatch.SelectedItem.ToString())); + } + + private void btnSubmit_Click(object sender, EventArgs e) + { + if (cbRound == null || cbMatch.SelectedItem == null) + { + return; + } + + if (int.TryParse(tbFirst.Text, out var scoreFirst)|| + int.TryParse(tbSecond.Text, out var scoreSecond)) + { + //TODO: throw some error message + return; + } + + var names = cbMatch.SelectedItem.ToString()?.Split(" : "); + + if (names?.Length != 2 || !int.TryParse(cbRound.SelectedItem.ToString(), out var round)) + { + throw new Exception("This should never happen"); + } + + OnMatchSubmitted(new MatchSubmittedEventArgs(names[0], names[1], + (scoreFirst, scoreSecond), round)); + } + + public void SetRounds(int amount) + { + cbRound.Items.Clear(); + cbRound.Text = String.Empty; + cbRound.Items.AddRange((string[])Enumerable.Range(1, amount) + .Select(x => x.ToString()).ToArray()); + } + + public void SetMatches(string[] matches) + { + cbMatch.Items.Clear(); + cbMatch.Text = String.Empty; + cbMatch.Items.AddRange((string[])matches); + } + } +} diff --git a/src/ManagePlayOffControl.resx b/src/PlayOffControl.resx similarity index 100% rename from src/ManagePlayOffControl.resx rename to src/PlayOffControl.resx diff --git a/src/PlayOffTree.cs b/src/PlayOffTree.cs index 030f351cb441f0a5a41ec1e0d2cbd07bdded91ba..74191f514e8985e76ad5d27780c5773a53800129 100644 --- a/src/PlayOffTree.cs +++ b/src/PlayOffTree.cs @@ -7,26 +7,77 @@ namespace TournamentManager public class PlayOffTree { public PlayOffTreeNode Root { get; } + public int Depth { get; private set; } public PlayOffTree( List<Participant> participants) { Root = ComputeTree(participants); } + public string[] GetRoundMatches(int round) + { + if (round > Depth || round < 1) + { + throw new Exception("If this happened you're just dumb"); + } + var finalDepth = Depth - round + 1; + var result = new List<string>(); + GetMatchesInDepth(Root, finalDepth, ref result); + return result.Where(x => x.Length > 1) + .Select(x => + { + var names = x.Split("_"); + var resultName = ""; + + if (names[0].Length > 0) + resultName = names[0]; + + if (names[1].Length > 0) + { + if (resultName.Length > 0) + resultName += $" : {names[1]}"; + else + resultName = names[1]; + } + + return resultName; + }).ToArray(); + } + + private void GetMatchesInDepth(PlayOffTreeNode node, int depth, ref List<string> result) + { + switch (depth) + { + case 1: + result.Add($"{node.TreeMatch.Opponents?.Item1?.Surname}_" + + $"{node.TreeMatch.Opponents?.Item2.Surname}"); + break; + + default: + GetMatchesInDepth(node.PredecessorLeft, depth - 1, ref result); + GetMatchesInDepth(node.PredecessorRight, depth - 1, ref result); + break; + } + } + private PlayOffTreeNode ComputeTree(List<Participant> participants) { var depth = CalculateTreeDepth(participants.Count); + Depth = depth; + var unevenHalf = (int)Math.Pow(2, depth - 1); + var participantsUpper = participants.Take(unevenHalf).ToList(); var participantsLower = participants.Skip(unevenHalf).ToList(); - return ComputeTreeStep(ref participantsUpper, ref participantsLower, depth); + + return ComputeTreeStep(ref participantsUpper, ref participantsLower, null, depth); } private PlayOffTreeNode ComputeTreeStep(ref List<Participant> participantsUpper, - ref List<Participant> participantsLower, int depth) + ref List<Participant> participantsLower, PlayOffTreeNode successor, int depth) { var match = new Match(); - var node = new PlayOffTreeNode(match); + var node = new PlayOffTreeNode(match, successor); switch (depth) { case 1: @@ -36,12 +87,23 @@ namespace TournamentManager match.Opponents = new Tuple<Participant, Participant>(upper, lower); participantsUpper.Remove(upper); participantsLower.Remove(lower); + + var sibling = node.GetSibling(); + if (sibling == null) + { + break; + } + if (node.GetSecondOpponent() == null && sibling.GetSecondOpponent() == null) + { + successor.SetMatch(new Tuple<Participant, Participant>( + node.GetFirstOpponent(), sibling.GetFirstOpponent())); + } break; } default: { - node.PredecessorLeft = ComputeTreeStep(ref participantsUpper, ref participantsLower, depth - 1); - node.PredecessorRight = ComputeTreeStep(ref participantsUpper, ref participantsLower, depth - 1); + node.PredecessorLeft = ComputeTreeStep(ref participantsUpper, ref participantsLower, node, depth - 1); + node.PredecessorRight = ComputeTreeStep(ref participantsUpper, ref participantsLower, node, depth - 1); break; } } diff --git a/src/PlayOffTreeNode.cs b/src/PlayOffTreeNode.cs index ef6388f56ab336f1aa301579aadd2a4b27eb8666..9cded7b7a09de3bdf2fb664ddc2c77712f9ebf18 100644 --- a/src/PlayOffTreeNode.cs +++ b/src/PlayOffTreeNode.cs @@ -8,10 +8,40 @@ namespace TournamentManager public Match TreeMatch { get; set; } public PlayOffTreeNode PredecessorLeft { get; set; } public PlayOffTreeNode PredecessorRight { get; set; } + public PlayOffTreeNode Successor { get; private set; } - public PlayOffTreeNode( Match match) + public PlayOffTreeNode( Match match, PlayOffTreeNode successor) { TreeMatch = match; + Successor = successor; + } + + public PlayOffTreeNode GetSibling() + { + if (Successor == null) + return null; + return Successor.PredecessorLeft == this ? Successor.PredecessorRight : Successor.PredecessorLeft; + + } + + public Participant GetFirstOpponent() + { + return TreeMatch.Opponents.Item1; + } + + public Participant GetSecondOpponent() + { + return TreeMatch.Opponents.Item2; + } + + public (int firstScore, int secondScore) GetScore() + { + return TreeMatch.Scores; + } + + public void SetMatch(Tuple<Participant, Participant> Opponents) + { + TreeMatch.Opponents = Opponents; } } } \ No newline at end of file