From f17c3f208995053a3594e1c5cb23be2ba48c474f Mon Sep 17 00:00:00 2001 From: emkael Date: Wed, 21 Mar 2018 17:05:16 +0100 Subject: Complete redesign of names loading mechanism. Fixes #24 --- Aktywator/Bws.cs | 219 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 148 insertions(+), 71 deletions(-) (limited to 'Aktywator/Bws.cs') diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index e589418..815e643 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -27,6 +27,13 @@ namespace Aktywator public bool analysis = false; } + class PairPosition + { + public int pairNo; + public int table; + public string position; + } + public Bws(string filename, MainForm main) { this._filename = filename; @@ -590,101 +597,171 @@ namespace Aktywator throw new InvalidCastException("Unable to read numeric value from BWS field"); } - public void syncNames(Tournament tournament, bool interactive, string startRounds, string section, DataGridView grid) + public void syncNames(Tournament tournament, bool interactive, string section, DataGridView grid) { int count = 0, countNew = 0, SKOK_STOLOW = Convert.ToInt32(main.numTeamsTableOffset.Value); - OleDbDataReader d; - startRounds = startRounds.Trim(); - string fromRound = sql.selectOne("SELECT min(`Round`) FROM RoundData WHERE NSPair>0"); - string sectionCondition = ""; - if (!("*".Equals(section))) + OleDbDataReader roundsReader = sql.select("SELECT `Section`, MIN(`Round`) FROM RoundData WHERE LowBoard > 0 GROUP BY `Section`;"); + Dictionary firstRounds = new Dictionary(); + while (roundsReader.Read()) + { + firstRounds.Add(this.getBWSNumber(roundsReader, 0), this.getBWSNumber(roundsReader, 1)); + } + roundsReader.Close(); + if (firstRounds.Count == 0) + { + MessageBox.Show("W BWSie nie ma danych rund!", "Brak danych", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + StringBuilder pairsQuery = new StringBuilder("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE ("); + List roundQueries = new List(); + foreach (KeyValuePair firstRound in firstRounds) + { + StringBuilder roundQuery = new StringBuilder("(`Round` = "); + roundQuery.Append(firstRound.Value); + roundQuery.Append(" AND `Section` = "); + roundQuery.Append(firstRound.Key); + roundQuery.Append(")"); + roundQueries.Add(roundQuery.ToString()); + } + pairsQuery.Append(String.Join(" OR ", roundQueries.ToArray())); + pairsQuery.Append(")"); + if (tournament.type == Tournament.TYPE_TEAMY) { - section = this.sectorLetterToNumber(section).ToString(); - sectionCondition = " AND `Section` = " + section; + pairsQuery.Append(" AND `Table` <= "); + pairsQuery.Append(SKOK_STOLOW); } - if (tournament.type != Tournament.TYPE_TEAMY) + pairsQuery.Append(";"); + + OleDbDataReader d; + d = sql.select(pairsQuery.ToString()); + + Dictionary> sectionPairs = new Dictionary>(); + Dictionary> pairs = new Dictionary>(); + while (d.Read()) { - if (tournament.type == Tournament.TYPE_PARY && startRounds.Length > 0) + int sectionNumber = this.getBWSNumber(d, 0); + int tableNumber = this.getBWSNumber(d, 1); + int nsPairNumber = this.getBWSNumber(d, 2); + int ewPairNumber = this.getBWSNumber(d, 3); + if (!sectionPairs.ContainsKey(sectionNumber)) { - d = sql.select("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE NSPair>0 AND `Round` in (" + startRounds + ")" + sectionCondition); + sectionPairs.Add(sectionNumber, new List()); } - else + sectionPairs[sectionNumber].Add(nsPairNumber); + sectionPairs[sectionNumber].Add(ewPairNumber); + if (!pairs.ContainsKey(sectionNumber)) { - d = sql.select("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE `Round`=" + fromRound + sectionCondition); + pairs.Add(sectionNumber, new List()); } + pairs[sectionNumber].Add(new PairPosition { pairNo = nsPairNumber, position = "NS", table = tableNumber }); + pairs[sectionNumber].Add(new PairPosition { pairNo = ewPairNumber, position = "EW", table = tableNumber }); } - else - { - d = sql.select("SELECT `Section`, `Table`, NSPair, EWPair FROM RoundData WHERE `Round`=" + fromRound + " AND `Table`<=" + SKOK_STOLOW + sectionCondition); - } + d.Close(); - try - { - Dictionary> names = tournament.getBWSNames(grid); + Dictionary> names = tournament.getBWSNames(grid); - while (d.Read()) + Dictionary> usedSections = new Dictionary>(); + List extraPairs = new List(); + foreach (KeyValuePair> pair in names) + { + bool foundInBWS = false; + foreach (KeyValuePair> pairsInSection in sectionPairs) { - string pairSection = "*".Equals(section) ? this.getBWSNumber(d, 0).ToString() : section; - string table = this.getBWSNumber(d, 1).ToString(); - int ns = this.getBWSNumber(d, 2); - int ew = this.getBWSNumber(d, 3); - - try + if (pairsInSection.Value.Contains(pair.Key)) { - if (!names.ContainsKey(ns)) + if (!usedSections.ContainsKey(pairsInSection.Key)) { - throw new KeyNotFoundException(ns.ToString()); - } - countNew += updateName(pairSection, table, "N", names[ns][0]); - countNew += updateName(pairSection, table, "S", names[ns][1]); - count += 2; - if (tournament.type == Tournament.TYPE_TEAMY) - { - countNew += updateName(pairSection, (int.Parse(table) + SKOK_STOLOW).ToString(), "E", - names.ContainsKey(ns + TeamNamesSettings.OpenClosedDiff) ? names[ns + TeamNamesSettings.OpenClosedDiff][0] : names[ns][0]); - countNew += updateName(pairSection, (int.Parse(table) + SKOK_STOLOW).ToString(), "W", - names.ContainsKey(ns + TeamNamesSettings.OpenClosedDiff) ? names[ns + TeamNamesSettings.OpenClosedDiff][1] : names[ns][1]); - count += 2; + usedSections.Add(pairsInSection.Key, new List()); } + usedSections[pairsInSection.Key].Add(pair.Key); + foundInBWS = true; } - catch (KeyNotFoundException keyE) - { - if (interactive) - { - DialogResult dr = MessageBox.Show("W bws-ie jest para/team (" + keyE.Message + ")" - + ", który nie istnieje w wybranym turnieju." - + "Może to nie ten turniej?" + "\n\n" + "Kontynuować wczytywanie?", - "Zły turniej", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); - if (dr == DialogResult.No) break; - } - } - try + } + if (!foundInBWS) { + extraPairs.Add(pair.Key); + } + } + + if (interactive) { + List warnings = new List(); + foreach (KeyValuePair> sectionData in sectionPairs) + { + if (this.sectorNumberToLetter(sectionData.Key).Equals(section) || "*".Equals(section)) { - if (!names.ContainsKey(ew)) + if (!usedSections.ContainsKey(sectionData.Key)) { - throw new KeyNotFoundException(ew.ToString()); + warnings.Add(" - w turnieju nie ma par dla sektora " + this.sectorNumberToLetter(sectionData.Key)); } - countNew += updateName(pairSection, table, "E", names[ew][0]); - countNew += updateName(pairSection, table, "W", names[ew][1]); - count += 2; - if (tournament.type == Tournament.TYPE_TEAMY) + else { - countNew += updateName(pairSection, (int.Parse(table) + SKOK_STOLOW).ToString(), "N", - names.ContainsKey(ns + TeamNamesSettings.OpenClosedDiff) ? names[ew + TeamNamesSettings.OpenClosedDiff][0] : names[ew][0]); - countNew += updateName(pairSection, (int.Parse(table) + SKOK_STOLOW).ToString(), "S", - names.ContainsKey(ns + TeamNamesSettings.OpenClosedDiff) ? names[ew + TeamNamesSettings.OpenClosedDiff][1] : names[ew][1]); - count += 2; + List missingPairs = new List(); + foreach (int pair in sectionData.Value) + { + if (!usedSections[sectionData.Key].Contains(pair)) + { + missingPairs.Add(pair); + } + } + if (missingPairs.Count > 0) + { + StringBuilder warning = new StringBuilder(" - w sektorze "); + warning.Append(this.sectorNumberToLetter(sectionData.Key)); + warning.Append(" brakuje "); + warning.Append(missingPairs.Count); + warning.Append(" par:"); + foreach (int pair in missingPairs) + { + warning.Append(' '); + warning.Append(pair); + } + warnings.Add(warning.ToString()); + } } } - catch (KeyNotFoundException keyE) + } + if (extraPairs.Count > 0) + { + StringBuilder warning = new StringBuilder(" - w BWS nie ma w ogóle "); + warning.Append(extraPairs.Count); + warning.Append(" par:"); + foreach (int pair in extraPairs) { - if (interactive) - { - DialogResult dr = MessageBox.Show("W bws-ie jest para/team (" + keyE.Message + ")" - + ", który nie istnieje w wybranym turnieju." - + "Może to nie ten turniej?" + "\n\n" + "Kontynuować wczytywanie?", - "Zły turniej", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); - if (dr == DialogResult.No) break; + warning.Append(' '); + warning.Append(pair); + } + warnings.Add(warning.ToString()); + } + + if (warnings.Count > 0) + { + DialogResult warningDialog = MessageBox.Show("Wykryto potencjalne problemy z wczytaniem nazwisk: \n\n" + String.Join("\n", warnings.ToArray()) + "\n\nCzy chcesz wczytać nazwiska mimo wszystko?", "Problemy z nazwiskami", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); + if (warningDialog == DialogResult.No) + { + return; + } + } + } + + try + { + int sectionNumber = 0; + if (!"*".Equals(section)) { + sectionNumber = this.sectorLetterToNumber(section); + } + char[] seatMapping = { 'N', 'S', 'E', 'W' }; + foreach (KeyValuePair> sections in usedSections) { + if ("*".Equals(section) || sectionNumber == sections.Key) { + foreach (int pairNumber in sections.Value) { + PairPosition pair = pairs[sections.Key].Find(delegate(PairPosition p) { return p.pairNo == pairNumber; }); + for (int i = 0; i < names[pair.pairNo].Count; i++) { + countNew += this.updateName(sections.Key.ToString(), pair.table.ToString(), pair.position[i].ToString(), names[pair.pairNo][i]); + if (tournament.type == Tournament.TYPE_TEAMY) + { + char otherTableSeat = seatMapping[(Array.IndexOf(seatMapping, pair.position[i]) + 2) % 4]; + countNew += this.updateName(sections.Key.ToString(), (pair.table + SKOK_STOLOW).ToString(), otherTableSeat.ToString(), names[pair.pairNo][i]); + } + } + count += names[pair.pairNo].Count * ((tournament.type == Tournament.TYPE_TEAMY) ? 2 : 1); } } } -- cgit v1.2.3 From d9a9ced98db04759739eebb6b07361670f097eaa Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 13 Apr 2018 16:51:14 +0200 Subject: Some of the initialization procedures for BWS might need a valid reference to Bws() --- Aktywator/Bws.cs | 4 ++++ Aktywator/MainForm.cs | 1 + 2 files changed, 5 insertions(+) (limited to 'Aktywator/Bws.cs') diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index 815e643..1af237f 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -39,6 +39,10 @@ namespace Aktywator this._filename = filename; sql = new Sql(filename); this.main = main; + } + + public void init() + { string[] sections = this.getSections().Split(','); this.displaySectionBoardsInfo(sections); } diff --git a/Aktywator/MainForm.cs b/Aktywator/MainForm.cs index 1e418fd..511db11 100644 --- a/Aktywator/MainForm.cs +++ b/Aktywator/MainForm.cs @@ -65,6 +65,7 @@ namespace Aktywator } bws = new Bws(filename, this); + bws.init(); bws.convert(); labelFilename.Text = filename; -- cgit v1.2.3 From 7b2f2c9e74a0e00f1fb12cba23fb218e470998eb Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 13 Apr 2018 16:55:18 +0200 Subject: Section group setting saved in a uniform way --- Aktywator/Bws.cs | 10 +--------- Aktywator/Setting.cs | 8 ++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'Aktywator/Bws.cs') diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index 1af237f..5695a00 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -552,15 +552,7 @@ namespace Aktywator Setting.save("BM2NameSource", "2", this, errors, section); Setting.save("BM2PINcode", "'" + main.xPINcode.Text + "'", this, errors, section); Setting.save("BM2ResultsOverview", main.xResultsOverview.SelectedIndex.ToString(), this, errors, section); - if (main.xGroupSections.Checked) - { - sql.query("UPDATE Tables SET `Group` = 1;"); - } - else - { - sql.query("UPDATE Tables SET `Group` = `Section`;"); - } - + Setting.saveSectionGroups(this.sql, main.xGroupSections.Checked); this.loadSettings(); } diff --git a/Aktywator/Setting.cs b/Aktywator/Setting.cs index 613065f..bde6fd1 100644 --- a/Aktywator/Setting.cs +++ b/Aktywator/Setting.cs @@ -170,5 +170,13 @@ namespace Aktywator { } } + + public static void saveSectionGroups(Sql sql, bool value) + { + StringBuilder sb = new StringBuilder("UPDATE Tables SET `Group` = "); + sb.Append(value ? "1" : "`Section`"); + sb.Append(";"); + sql.query(sb.ToString()); + } } } -- cgit v1.2.3 From bc1eb7582b69794bc519a086a306bcf9e9bd6a9c Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 13 Apr 2018 16:56:44 +0200 Subject: Detecting different hand records between sections --- Aktywator/Bws.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Aktywator/Bws.cs') diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index 5695a00..19a2463 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -841,6 +841,7 @@ namespace Aktywator { sql.query("DELETE FROM HandRecord WHERE `Section` = " + section); sql.query("DELETE FROM HandEvaluation WHERE `Section` = " + section); + this._differentRecordsDetected = false; } public void clearHandRecords() @@ -922,6 +923,30 @@ namespace Aktywator return count; } + private bool _differentRecordsDetected = false; + private bool _differentRecordsInSections = false; + + public bool detectDifferentRecordsInSections() + { + if (!this._differentRecordsDetected) + { + this._differentRecordsInSections = false; + OleDbDataReader sections = sql.select("SELECT DISTINCT COUNT(`Section`) FROM HandRecord GROUP BY Board, NorthSpades, NorthHearts, NorthDiamonds, NorthClubs, EastSpades, EastHearts, EastDiamonds, EastClubs, SouthSpades, SouthHearts, SouthDiamonds, SouthClubs"); + while (sections.Read()) + { + int boardSections = this.getBWSNumber(sections, 0); + int bwsSections = main.gwSections.Rows.Count; + if (boardSections != bwsSections) + { + this._differentRecordsInSections = true; + break; + } + } + this._differentRecordsDetected = true; + } + return this._differentRecordsInSections; + } + internal string getMySQLDatabaseForSection() { try -- cgit v1.2.3 From 252f77a86f9d70800e7cc9aa58de2557fba5fdaa Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 13 Apr 2018 17:05:46 +0200 Subject: Forcing section group settings and a warning if different records are uploaded to sections Fixes #42 --- Aktywator/Bws.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'Aktywator/Bws.cs') diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index 19a2463..15219f9 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -119,6 +119,16 @@ namespace Aktywator this.setHandRecordInfo(board.Key); } } + main.xShowResults_CheckedChanged(null, EventArgs.Empty); + if (this.detectDifferentRecordsInSections()) + { + if (main.xGroupSections.Checked) + { + MessageBox.Show(MainForm.differentRecordsInSections, "Ustawienia grupowania zapisów w sektorach", MessageBoxButtons.OK, MessageBoxIcon.Warning); + main.xGroupSections.Checked = false; + } + Setting.saveSectionGroups(this.sql, main.xGroupSections.Checked); + } } private void setHandRecordInfo(int board, string section = null, List layout = null, bool analysis = false) @@ -514,7 +524,7 @@ namespace Aktywator public void sectionGroupWarning() { main.lGroupSectionsWarning.Visible = false; - if (main.xShowResults.Checked) + if (main.xShowResults.Checked || this.detectDifferentRecordsInSections()) { main.lGroupSectionsWarning.Visible = true; } -- cgit v1.2.3 From 914aacc61a1297786dc8c59a092674b023bf5180 Mon Sep 17 00:00:00 2001 From: emkael Date: Sun, 15 Apr 2018 11:16:38 +0200 Subject: BWS structure for hand records needs to be checked and created earlier on. --- Aktywator/Bws.cs | 45 ++++++++++++++++++++++----------------- Aktywator/Resources/BuildDate.txt | 2 +- 2 files changed, 27 insertions(+), 20 deletions(-) (limited to 'Aktywator/Bws.cs') diff --git a/Aktywator/Bws.cs b/Aktywator/Bws.cs index 15219f9..9121eb1 100644 --- a/Aktywator/Bws.cs +++ b/Aktywator/Bws.cs @@ -390,13 +390,14 @@ namespace Aktywator { } + this._ensureHandRecordStructure(); + try { - sql.query("CREATE TABLE HandRecord (`Section` integer, `Board` integer, " - + "NorthSpades text(13),NorthHearts text(13),NorthDiamonds text(13),NorthClubs text(13)," - + "EastSpades text(13),EastHearts text(13),EastDiamonds text(13),EastClubs text(13)," - + "SouthSpades text(13),SouthHearts text(13),SouthDiamonds text(13),SouthClubs text(13)," - + "WestSpades text(13),WestHearts text(13),WestDiamonds text(13),WestClubs text(13)" + sql.query("CREATE TABLE PlayData (" + + "`ID` autoincrement, `Section` integer, `Table` integer, `Round` integer, `Board` integer," + + "`Counter` integer, `Direction` text(2), `Card` text(10), `DateLog` datetime," + + "`TimeLog` datetime, `Erased` bit" + ");"); } catch (OleDbException) @@ -404,23 +405,26 @@ namespace Aktywator } try { - sql.query("CREATE TABLE HandEvaluation (`Section` integer, `Board` integer, " - + "NorthSpades integer,NorthHearts integer,NorthDiamonds integer,NorthClubs integer,NorthNotrump integer," - + "EastSpades integer,EastHearts integer,EastDiamonds integer,EastClubs integer,EastNotrump integer," - + "SouthSpades integer,SouthHearts integer,SouthDiamonds integer,SouthClubs integer,SouthNotrump integer," - + "WestSpades integer,WestHearts integer,WestDiamonds integer,WestClubs integer,WestNotrump integer," - + "NorthHcp integer,EastHcp integer,SouthHcp integer,WestHcp integer" + sql.query("CREATE TABLE BiddingData (" + + "`ID` autoincrement, `Section` integer, `Table` integer, `Round` integer, `Board` integer," + + "`Counter` integer, `Direction` text(2), `Bid` text(10), `DateLog` datetime," + + "`TimeLog` datetime, `Erased` bit" + ");"); } catch (OleDbException) { } + } + + private void _ensureHandRecordStructure() + { try { - sql.query("CREATE TABLE PlayData (" - + "`ID` autoincrement, `Section` integer, `Table` integer, `Round` integer, `Board` integer," - + "`Counter` integer, `Direction` text(2), `Card` text(10), `DateLog` datetime," - + "`TimeLog` datetime, `Erased` bit" + sql.query("CREATE TABLE HandRecord (`Section` integer, `Board` integer, " + + "NorthSpades text(13),NorthHearts text(13),NorthDiamonds text(13),NorthClubs text(13)," + + "EastSpades text(13),EastHearts text(13),EastDiamonds text(13),EastClubs text(13)," + + "SouthSpades text(13),SouthHearts text(13),SouthDiamonds text(13),SouthClubs text(13)," + + "WestSpades text(13),WestHearts text(13),WestDiamonds text(13),WestClubs text(13)" + ");"); } catch (OleDbException) @@ -428,10 +432,12 @@ namespace Aktywator } try { - sql.query("CREATE TABLE BiddingData (" - + "`ID` autoincrement, `Section` integer, `Table` integer, `Round` integer, `Board` integer," - + "`Counter` integer, `Direction` text(2), `Bid` text(10), `DateLog` datetime," - + "`TimeLog` datetime, `Erased` bit" + sql.query("CREATE TABLE HandEvaluation (`Section` integer, `Board` integer, " + + "NorthSpades integer,NorthHearts integer,NorthDiamonds integer,NorthClubs integer,NorthNotrump integer," + + "EastSpades integer,EastHearts integer,EastDiamonds integer,EastClubs integer,EastNotrump integer," + + "SouthSpades integer,SouthHearts integer,SouthDiamonds integer,SouthClubs integer,SouthNotrump integer," + + "WestSpades integer,WestHearts integer,WestDiamonds integer,WestClubs integer,WestNotrump integer," + + "NorthHcp integer,EastHcp integer,SouthHcp integer,WestHcp integer" + ");"); } catch (OleDbException) @@ -938,6 +944,7 @@ namespace Aktywator public bool detectDifferentRecordsInSections() { + this._ensureHandRecordStructure(); if (!this._differentRecordsDetected) { this._differentRecordsInSections = false; diff --git a/Aktywator/Resources/BuildDate.txt b/Aktywator/Resources/BuildDate.txt index 777043e..c370c3b 100644 --- a/Aktywator/Resources/BuildDate.txt +++ b/Aktywator/Resources/BuildDate.txt @@ -1 +1 @@ -2018-04-13 +2018-04-15 -- cgit v1.2.3