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