From 8d3e6fdfa46c3243524a966890eaadfae9ff3fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 12 Apr 2020 21:17:01 +0200 Subject: [PATCH] Tournament Dashboard fixed, started to wire up tournamentviewer form --- TrackerLibrary/DataAccess/IDataConnection.cs | 1 + TrackerLibrary/DataAccess/SqlConnector.cs | 76 ++++++++++++- TrackerLibrary/DataAccess/TextConnector.cs | 7 ++ .../DataAccess/TextConnectorProcessor.cs | 104 ++++++++++++++---- TrackerLibrary/Models/MatchupEntryModel.cs | 8 ++ TrackerLibrary/Models/MatchupModel.cs | 35 +++++- TrackerUI/Program.cs | 4 +- TrackerUI/TournamentDashboardForm.Designer.cs | 2 + TrackerUI/TournamentDashboardForm.cs | 23 ++++ TrackerUI/TournamentViewerForm.Designer.cs | 1 + TrackerUI/TournamentViewerForm.cs | 62 ++++++++++- 11 files changed, 294 insertions(+), 29 deletions(-) diff --git a/TrackerLibrary/DataAccess/IDataConnection.cs b/TrackerLibrary/DataAccess/IDataConnection.cs index 6cb9a99..e6d2b2e 100644 --- a/TrackerLibrary/DataAccess/IDataConnection.cs +++ b/TrackerLibrary/DataAccess/IDataConnection.cs @@ -13,5 +13,6 @@ namespace TrackerLibrary.DataAccess void CreateTournament(TournamentModel model); List GetTeam_All(); List GetPerson_All(); + List GetTournament_All(); } } diff --git a/TrackerLibrary/DataAccess/SqlConnector.cs b/TrackerLibrary/DataAccess/SqlConnector.cs index c34d60b..7ddb246 100644 --- a/TrackerLibrary/DataAccess/SqlConnector.cs +++ b/TrackerLibrary/DataAccess/SqlConnector.cs @@ -100,7 +100,7 @@ namespace TrackerLibrary.DataAccess SaveTournamentEntries(model, connection); SaveTournamentRounds(model, connection); - } + } } private void SaveTournament(TournamentModel model, IDbConnection connection) @@ -157,7 +157,7 @@ namespace TrackerLibrary.DataAccess p = new DynamicParameters(); p.Add("@MatchupId", matchup.Id); - if(entry.ParentMatchup == null) + if (entry.ParentMatchup == null) { p.Add("@ParentMatchupId", null); } @@ -185,7 +185,7 @@ namespace TrackerLibrary.DataAccess } - private static void SaveTournamentPrizes(TournamentModel model, IDbConnection connection) + private static void SaveTournamentPrizes(TournamentModel model, IDbConnection connection) { foreach (PrizeModel pz in model.Prizes) { @@ -226,5 +226,75 @@ namespace TrackerLibrary.DataAccess } return output; } + + public List GetTournament_All() + { + List output; + using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) + { + output = connection.Query("dbo.spTournaments_GetAll").ToList(); + var p = new DynamicParameters(); + foreach (TournamentModel t in output) + { + // Populate prizes + p.Add("@TournamentId", t.Id); + t.Prizes = connection.Query("dbo.spPrizes_GetByTournament", p, commandType: CommandType.StoredProcedure).ToList(); + // Populate Teams + t.EnteredTeams = connection.Query("dbo.spTeam_GetByTournament", p, commandType: CommandType.StoredProcedure).ToList(); + foreach (TeamModel team in t.EnteredTeams) + { + p = new DynamicParameters(); + p.Add("@TeamId", team.Id); + team.TeamMembers = connection.Query("dbo.spTeamMembers_GetByTeam", p, commandType: CommandType.StoredProcedure).ToList(); + } + // Populate Rounds + p = new DynamicParameters(); + p.Add("@TournamentId", t.Id); + List matchups = connection.Query("dbo.spMatchups_GetByTournament", p, commandType: CommandType.StoredProcedure).ToList(); + foreach (MatchupModel m in matchups) + { + p = new DynamicParameters(); + p.Add("@MatchupId", m.Id); + m.Entries = connection.Query("dbo.spMatchupEntries_GetByMatchup", p, commandType: CommandType.StoredProcedure).ToList(); + + List allTeams = GetTeam_All(); + + if (m.WinnerId > 0) + { + m.Winner = allTeams.Where(x => x.Id == m.WinnerId).First(); + } + + foreach (var me in m.Entries) + { + if (me.TeamCompetingId > 0) + { + me.TeamCompeting = allTeams.Where(x => x.Id == me.TeamCompetingId).First(); + } + if (me.ParentMatchupId > 0) + { + me.ParentMatchup = matchups.Where(x => x.Id == me.ParentMatchupId).First(); + } + } + } + + // List> + List currRow = new List(); + int currRound = 1; + foreach (MatchupModel m in matchups) + { + if(m.MatchupRound > currRound) + { + t.Rounds.Add(currRow); + currRow = new List(); + currRound += 1; + } + currRow.Add(m); + } + + t.Rounds.Add(currRow); + } + } + return output; + } } } diff --git a/TrackerLibrary/DataAccess/TextConnector.cs b/TrackerLibrary/DataAccess/TextConnector.cs index a2ecd26..670b326 100644 --- a/TrackerLibrary/DataAccess/TextConnector.cs +++ b/TrackerLibrary/DataAccess/TextConnector.cs @@ -124,5 +124,12 @@ namespace TrackerLibrary.DataAccess tournaments.SaveToTournamentFile(TournamentFile); } + public List GetTournament_All() + { + return TournamentFile + .FullFilePath() + .LoadFile() + .ConvertToTournamentModels(TeamFile, PeopleFile, PrizesFile); + } } } diff --git a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs index e9d75b3..4f7463b 100644 --- a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs +++ b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs @@ -108,6 +108,8 @@ namespace TrackerLibrary.DataAccess.TextHelpers List output = new List(); List teams = teamFileName.FullFilePath().LoadFile().ConvertToTeamModels(peopleFileName); List prizes = prizesFileName.FullFilePath().LoadFile().ConvertToPrizeModels(); + List matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels(); + foreach (string line in lines) { string[] cols = line.Split(','); @@ -120,13 +122,27 @@ namespace TrackerLibrary.DataAccess.TextHelpers { tm.EnteredTeams.Add(teams.Where(x => x.Id == int.Parse(teamId)).First()); } - string[] prizeIds = cols[4].Split('|'); - foreach (string prizeId in prizeIds) + if (cols[4].Length > 0) { - tm.Prizes.Add(prizes.Where(x => x.Id == int.Parse(prizeId)).First()); + string[] prizeIds = cols[4].Split('|'); + foreach (string prizeId in prizeIds) + { + tm.Prizes.Add(prizes.Where(x => x.Id == int.Parse(prizeId)).First()); + } } // Capture rounds information + string[] rounds = cols[5].Split('|'); + foreach (string round in rounds) + { + List ms = new List(); + string[] mstext = round.Split('^'); + foreach (string matchupModelTextId in mstext) + { + ms.Add(matchups.Where(x => x.Id == int.Parse(matchupModelTextId)).First()); + } + tm.Rounds.Add(ms); + } output.Add(tm); @@ -197,7 +213,14 @@ namespace TrackerLibrary.DataAccess.TextHelpers string[] cols = line.Split(','); MatchupEntryModel me = new MatchupEntryModel(); me.Id = int.Parse(cols[0]); - me.TeamCompeting = LookupTeamById(int.Parse( cols[1])); + if(cols[1].Length == 0) + { + me.TeamCompeting = null; + } + else + { + me.TeamCompeting = LookupTeamById(int.Parse(cols[1])); + } me.Score = double.Parse( cols[2]); int parentId = 0; @@ -207,7 +230,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers } else { - me.ParentMatchup = null); + me.ParentMatchup = null; } output.Add(me); @@ -219,25 +242,54 @@ namespace TrackerLibrary.DataAccess.TextHelpers { string[] ids = input.Split('|'); List output = new List(); - List entries = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntryModels(); - + List entries = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile(); + List matchingEntries = new List(); foreach (string id in ids) { - output.Add(entries.Where(x => x.Id == int.Parse(id)).First()); + foreach (string entry in entries) + { + string[] cols = entry.Split(','); + if (cols[0] == id) { + matchingEntries.Add(entry); + } + } } + + output = matchingEntries.ConvertToMatchupEntryModels(); + return output; } private static TeamModel LookupTeamById(int id) { - List teams = GlobalConfig.TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(GlobalConfig.PeopleFile); - return teams.Where(x => x.Id == id).First(); + List teams = GlobalConfig.TeamFile.FullFilePath().LoadFile(); + foreach (string team in teams) + { + string[] cols = team.Split(','); + if (cols[0] == id.ToString()) + { + List matchingTeams = new List(); + matchingTeams.Add(team); + return matchingTeams.ConvertToTeamModels(GlobalConfig.PeopleFile).First(); + } + } + return null; } private static MatchupModel LookupMatchupById(int id) { - List matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels(); - return matchups.Where(x => x.Id == id).First(); + List matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile(); + foreach (string matchup in matchups) + { + string[] cols = matchup.Split(','); + if (cols[0] == id.ToString()) + { + List matchingMatchups = new List(); + matchingMatchups.Add(matchup); + return matchingMatchups.ConvertToMatchupModels().First(); + } + } + return null; } public static List ConvertToMatchupModels(this List lines) @@ -251,7 +303,14 @@ namespace TrackerLibrary.DataAccess.TextHelpers MatchupModel p = new MatchupModel(); p.Id = int.Parse(cols[0]); p.Entries = ConvertStringToMatchupEntryModels(cols[1]); - p.Winner = LookupTeamById(int.Parse(cols[2])); + if (cols[2].Length == 0) + { + p.Winner = null; + } + else + { + p.Winner = LookupTeamById(int.Parse(cols[2])); + } p.MatchupRound = int.Parse(cols[3]); output.Add(p); } @@ -268,6 +327,9 @@ namespace TrackerLibrary.DataAccess.TextHelpers currentId = matchups.OrderByDescending(x => x.Id).First().Id + 1; } matchup.Id = currentId; + + matchups.Add(matchup); + foreach (MatchupEntryModel entry in matchup.Entries) { entry.SaveEntryToFile(matchupEntryFile); @@ -304,9 +366,14 @@ namespace TrackerLibrary.DataAccess.TextHelpers string parent = ""; if(e.ParentMatchup != null) { - parent = e.ParentMatchup.Id.ToString(); ; + parent = e.ParentMatchup.Id.ToString(); } - lines.Add($"{e.Id},{e.TeamCompeting.Id},{e.Score},{parent}"); + string teamCompeting = ""; + if(e.TeamCompeting != null) + { + teamCompeting = e.TeamCompeting.Id.ToString(); + } + lines.Add($"{e.Id},{teamCompeting},{e.Score},{parent}"); } File.WriteAllLines(GlobalConfig.MatchupEntryFile.FullFilePath(), lines); @@ -319,12 +386,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers List lines = new List(); foreach (TournamentModel tm in models) { - lines.Add($@"{tm.Id}, - {tm.TournamentName}, - {tm.EntryFee}, - {ConvertTeamListToString(tm.EnteredTeams)}, - {ConvertPrizeListToString(tm.Prizes)}, - {ConvertRoundListToString(tm.Rounds)}"); + lines.Add($@"{tm.Id},{tm.TournamentName},{tm.EntryFee},{ConvertTeamListToString(tm.EnteredTeams)},{ConvertPrizeListToString(tm.Prizes)},{ConvertRoundListToString(tm.Rounds)}"); } File.WriteAllLines(fileName.FullFilePath(), lines); diff --git a/TrackerLibrary/Models/MatchupEntryModel.cs b/TrackerLibrary/Models/MatchupEntryModel.cs index 5ef7109..9389630 100644 --- a/TrackerLibrary/Models/MatchupEntryModel.cs +++ b/TrackerLibrary/Models/MatchupEntryModel.cs @@ -11,6 +11,10 @@ namespace TrackerLibrary.Models /// public int Id { get; set; } /// + /// The unique identifier for the team + /// + public int TeamCompetingId { get; set; } + /// /// Represents one team in the matchup /// public TeamModel TeamCompeting { get; set; } @@ -19,6 +23,10 @@ namespace TrackerLibrary.Models /// public double Score { get; set; } /// + /// The unique identifier for the parent matchup (team) + /// + public int ParentMatchupId { get; set; } + /// /// Represents the matchup that this team came /// from as winner /// diff --git a/TrackerLibrary/Models/MatchupModel.cs b/TrackerLibrary/Models/MatchupModel.cs index e051215..6b1e677 100644 --- a/TrackerLibrary/Models/MatchupModel.cs +++ b/TrackerLibrary/Models/MatchupModel.cs @@ -12,12 +12,16 @@ namespace TrackerLibrary.Models /// /// The unique identifier for the matchup /// - public int Id { get; set; } + public int Id { get; set; } /// /// The set of teams that were involved in this match. /// public List Entries { get; set; } = new List(); /// + /// The Id from database that will be used to lookup the winner + /// + public int WinnerId { get; set; } + /// /// The winner of the match. /// public TeamModel Winner { get; set; } @@ -25,5 +29,34 @@ namespace TrackerLibrary.Models /// Which round this match is a part of. /// public int MatchupRound { get; set; } + + public string DisplayName + { + get + { + string output = ""; + foreach (MatchupEntryModel me in Entries) + { + if (me.TeamCompeting != null) + { + if (output.Length == 0) + { + output = me.TeamCompeting.TeamName; + } + else + { + output += $" vs. {me.TeamCompeting.TeamName}"; + } + + } + else + { + output = "Matchup not yet Determined"; + break; + } + } + return output; + } + } } } diff --git a/TrackerUI/Program.cs b/TrackerUI/Program.cs index 473bdd0..c8ed92e 100644 --- a/TrackerUI/Program.cs +++ b/TrackerUI/Program.cs @@ -20,9 +20,7 @@ namespace TrackerUI // Initialize the database connections GlobalConfig.InitializeConnections(DatabaseType.Sql); - Application.Run(new CreateTournamentForm()); - - //Application.Run(new TournamentDashboardForm()); + Application.Run(new TournamentDashboardForm()); } } } diff --git a/TrackerUI/TournamentDashboardForm.Designer.cs b/TrackerUI/TournamentDashboardForm.Designer.cs index 0a8ef69..634690f 100644 --- a/TrackerUI/TournamentDashboardForm.Designer.cs +++ b/TrackerUI/TournamentDashboardForm.Designer.cs @@ -80,6 +80,7 @@ this.loadTournamentButton.TabIndex = 25; this.loadTournamentButton.Text = "Load Tournament"; this.loadTournamentButton.UseVisualStyleBackColor = true; + this.loadTournamentButton.Click += new System.EventHandler(this.loadTournamentButton_Click); // // createTournamentButton // @@ -94,6 +95,7 @@ this.createTournamentButton.TabIndex = 26; this.createTournamentButton.Text = "Create Tournament"; this.createTournamentButton.UseVisualStyleBackColor = true; + this.createTournamentButton.Click += new System.EventHandler(this.createTournamentButton_Click); // // TournamentDashboardForm // diff --git a/TrackerUI/TournamentDashboardForm.cs b/TrackerUI/TournamentDashboardForm.cs index 59020c3..f63e3df 100644 --- a/TrackerUI/TournamentDashboardForm.cs +++ b/TrackerUI/TournamentDashboardForm.cs @@ -7,14 +7,37 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using TrackerLibrary; +using TrackerLibrary.Models; namespace TrackerUI { public partial class TournamentDashboardForm : Form { + List tournaments = GlobalConfig.Connection.GetTournament_All(); public TournamentDashboardForm() { InitializeComponent(); + WireUpLists(); + } + + private void WireUpLists() + { + loadExistingTournamentDropDown.DataSource = tournaments; + loadExistingTournamentDropDown.DisplayMember = "TournamentName"; + } + + private void createTournamentButton_Click(object sender, EventArgs e) + { + CreateTournamentForm frm = new CreateTournamentForm(); + frm.Show(); + } + + private void loadTournamentButton_Click(object sender, EventArgs e) + { + TournamentModel tm = (TournamentModel)loadExistingTournamentDropDown.SelectedItem; + TournamentViewerForm frm = new TournamentViewerForm(tm); + frm.Show(); } } } diff --git a/TrackerUI/TournamentViewerForm.Designer.cs b/TrackerUI/TournamentViewerForm.Designer.cs index 8d30546..2233b54 100644 --- a/TrackerUI/TournamentViewerForm.Designer.cs +++ b/TrackerUI/TournamentViewerForm.Designer.cs @@ -86,6 +86,7 @@ this.roundDropDown.Name = "roundDropDown"; this.roundDropDown.Size = new System.Drawing.Size(245, 38); this.roundDropDown.TabIndex = 3; + this.roundDropDown.SelectedIndexChanged += new System.EventHandler(this.roundDropDown_SelectedIndexChanged); // // unplayedOnlyCheckbox // diff --git a/TrackerUI/TournamentViewerForm.cs b/TrackerUI/TournamentViewerForm.cs index 0cfcb75..7ac2a65 100644 --- a/TrackerUI/TournamentViewerForm.cs +++ b/TrackerUI/TournamentViewerForm.cs @@ -7,14 +7,74 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using TrackerLibrary.Models; namespace TrackerUI { public partial class TournamentViewerForm : Form { - public TournamentViewerForm() + private readonly TournamentModel tournament; + List rounds = new List(); + List selectedMatchups = new List(); + public TournamentViewerForm(TournamentModel tournamentModel) { InitializeComponent(); + tournament = tournamentModel; + LoadFormData(); + LoadRounds(); + } + + private void LoadFormData() + { + tournamentName.Text = tournament.TournamentName; + } + + private void WireUpRoundsLists() + { + roundDropDown.DataSource = null; + roundDropDown.DataSource = rounds; + + } + private void WireUpMatchupsLists() + { + MatchUpListBox.DataSource = null; + MatchUpListBox.DataSource = selectedMatchups; + MatchUpListBox.DisplayMember = "DisplayName"; + } + private void LoadRounds() + { + rounds = new List(); + + rounds.Add(1); + int currRound = 1; + foreach (List matchups in tournament.Rounds) + { + if (matchups.First().MatchupRound > currRound) + { + currRound = matchups.First().MatchupRound; + rounds.Add(currRound); + } + } + WireUpRoundsLists(); + } + + private void roundDropDown_SelectedIndexChanged(object sender, EventArgs e) + { + LoadMatchups(); + } + + + private void LoadMatchups() + { + int round = (int)roundDropDown.SelectedItem; + foreach (List matchups in tournament.Rounds) + { + if (matchups.First().MatchupRound == round) + { + selectedMatchups = matchups; + } + } + WireUpMatchupsLists(); } } }