public RankingList(int tabletDeviceNumber) { TabletDeviceNumber = tabletDeviceNumber; TabletDeviceStatus tabletDeviceStatus = AppData.TabletDeviceStatusList[tabletDeviceNumber]; RoundNumber = tabletDeviceStatus.RoundNumber; // Set player numbers to highlight appropriate rows of ranking list if (AppData.SectionsList.Find(x => x.SectionID == tabletDeviceStatus.SectionID).TabletDevicesPerTable == 1) { TableStatus tableStatus = AppData.TableStatusList.Find(x => x.SectionID == tabletDeviceStatus.SectionID && x.TableNumber == tabletDeviceStatus.TableNumber); NumberNorth = tableStatus.RoundData.NumberNorth; NumberEast = tableStatus.RoundData.NumberEast; NumberSouth = tableStatus.RoundData.NumberSouth; NumberWest = tableStatus.RoundData.NumberWest; } else // More than one tablet device per table { // Only need to highlight one row entry, so use NumberNorth as proxy NumberNorth = tabletDeviceStatus.PairNumber; } using (OdbcConnection connection = new OdbcConnection(AppData.DBConnectionString)) { connection.Open(); string SQLString = $"SELECT Orientation, Number, Score, Rank FROM Results WHERE Section={tabletDeviceStatus.SectionID}"; OdbcCommand cmd = new OdbcCommand(SQLString, connection); OdbcDataReader reader1 = null; try { ODBCRetryHelper.ODBCRetry(() => { reader1 = cmd.ExecuteReader(); while (reader1.Read()) { Ranking ranking = new Ranking { Orientation = reader1.GetString(0), PairNo = reader1.GetInt32(1), Score = reader1.GetString(2), Rank = reader1.GetString(3) }; ranking.ScoreDecimal = Convert.ToDouble(ranking.Score); Add(ranking); } }); } catch (OdbcException e) { if (e.Errors.Count > 1 || e.Errors[0].SQLState != "42S02") // Any error other than results table doesn't exist { throw (e); } } finally { reader1.Close(); cmd.Dispose(); } if (Count == 0) // Results table either doesn't exist or contains no entries, so try to calculate rankings { if (AppData.IsIndividual) { InsertRange(0, CalculateIndividualRankingFromReceivedData(tabletDeviceStatus.SectionID)); } else { InsertRange(0, CalculateRankingFromReceivedData(tabletDeviceStatus.SectionID)); } } // Make sure that ranking list is sorted into presentation order Sort((x, y) => { int sortValue = y.Orientation.CompareTo(x.Orientation); // N's first then E's if (sortValue == 0) { sortValue = y.ScoreDecimal.CompareTo(x.ScoreDecimal); } if (sortValue == 0) { sortValue = x.PairNo.CompareTo(y.PairNo); } return(sortValue); }); } }
// Database read constructor public Round(TableStatus tableStatus) { using (OdbcConnection connection = new OdbcConnection(AppData.DBConnectionString)) { connection.Open(); if (AppData.IsIndividual) { string SQLString = $"SELECT NSPair, EWPair, South, West, LowBoard, HighBoard FROM RoundData WHERE Section={tableStatus.SectionID} AND Table={tableStatus.TableNumber} AND Round={tableStatus.RoundNumber}"; OdbcCommand cmd = new OdbcCommand(SQLString, connection); OdbcDataReader reader = null; try { ODBCRetryHelper.ODBCRetry(() => { reader = cmd.ExecuteReader(); if (reader.Read()) { NumberNorth = reader.GetInt32(0); NumberEast = reader.GetInt32(1); NumberSouth = reader.GetInt32(2); NumberWest = reader.GetInt32(3); LowBoard = reader.GetInt32(4); HighBoard = reader.GetInt32(5); } }); } finally { reader.Close(); cmd.Dispose(); } } else // Not individual { string SQLString = $"SELECT NSPair, EWPair, LowBoard, HighBoard FROM RoundData WHERE Section={tableStatus.SectionID} AND Table={tableStatus.TableNumber} AND Round={tableStatus.RoundNumber}"; OdbcCommand cmd = new OdbcCommand(SQLString, connection); OdbcDataReader reader = null; try { ODBCRetryHelper.ODBCRetry(() => { reader = cmd.ExecuteReader(); if (reader.Read()) { NumberNorth = NumberSouth = reader.GetInt32(0); NumberEast = NumberWest = reader.GetInt32(1); LowBoard = reader.GetInt32(2); HighBoard = reader.GetInt32(3); } }); } finally { reader.Close(); cmd.Dispose(); } } } // Check for use of missing pair in Section table and set player numbers to 0 if necessary int missingPair = AppData.SectionsList.Find(x => x.SectionID == tableStatus.SectionID).MissingPair; if (NumberNorth == missingPair) { NumberNorth = NumberSouth = 0; } if (NumberEast == missingPair) { NumberEast = NumberWest = 0; } return; }
private static string GetNameFromPlayerNumbersTableIndividual(OdbcConnection conn, TableStatus tableStatus, int playerNo) { if (playerNo == 0) { return(""); } string number = ""; string name = ""; DateTime latestTimeLog = new DateTime(2010, 1, 1); string SQLString = $"SELECT Number, Name, Round, TimeLog FROM PlayerNumbers WHERE Section={tableStatus.SectionID} AND TabScorePairNo={playerNo}"; OdbcCommand cmd = new OdbcCommand(SQLString, conn); OdbcDataReader reader = null; try { ODBCRetryHelper.ODBCRetry(() => { reader = cmd.ExecuteReader(); while (reader.Read()) { try { int readerRoundNumber = reader.GetInt32(2); DateTime timeLog; if (reader.IsDBNull(3)) { timeLog = new DateTime(2010, 1, 1); } else { timeLog = reader.GetDateTime(3); } if (readerRoundNumber <= tableStatus.RoundNumber && timeLog >= latestTimeLog) { number = reader.GetString(0); name = reader.GetString(1); latestTimeLog = timeLog; } } catch { } // Record found, but format cannot be parsed } }); } finally { cmd.Dispose(); reader.Close(); } return(FormatName(name, number)); }
private static string GetNameFromPlayerNumbersTable(OdbcConnection conn, TableStatus tableStatus, int pairNo, string dir) { if (pairNo == 0) { return(""); } string number = ""; string name = ""; DateTime latestTimeLog = new DateTime(2010, 1, 1); // First look for entries in the same direction string SQLString = $"SELECT Number, Name, Round, TimeLog FROM PlayerNumbers WHERE Section={tableStatus.SectionID} AND TabScorePairNo={pairNo} AND Direction='{dir}'"; OdbcCommand cmd = new OdbcCommand(SQLString, conn); OdbcDataReader reader = null; try { ODBCRetryHelper.ODBCRetry(() => { reader = cmd.ExecuteReader(); while (reader.Read()) { try { int readerRoundNumber = reader.GetInt32(2); DateTime timeLog; if (reader.IsDBNull(3)) { timeLog = new DateTime(2010, 1, 1); } else { timeLog = reader.GetDateTime(3); } if (readerRoundNumber <= tableStatus.RoundNumber && timeLog >= latestTimeLog) { number = reader.GetString(0); name = reader.GetString(1); latestTimeLog = timeLog; } } catch { } // Record found, but format cannot be parsed } }); } finally { reader.Close(); } if (AppData.SectionsList.Find(x => x.SectionID == tableStatus.SectionID).Winners == 1) // If a one-winner pairs movement, we also need to check the other direction { string otherDir; switch (dir) { case "N": otherDir = "E"; break; case "S": otherDir = "W"; break; case "E": otherDir = "N"; break; case "W": otherDir = "S"; break; default: otherDir = ""; break; } SQLString = $"SELECT Number, Name, Round, TimeLog FROM PlayerNumbers WHERE Section={tableStatus.SectionID} AND TabScorePairNo={pairNo} AND Direction='{otherDir}'"; cmd = new OdbcCommand(SQLString, conn); try { ODBCRetryHelper.ODBCRetry(() => { reader = cmd.ExecuteReader(); while (reader.Read()) { try { int readerRoundNumber = reader.GetInt32(2); DateTime timeLog; if (reader.IsDBNull(3)) { timeLog = new DateTime(2010, 1, 1); } else { timeLog = reader.GetDateTime(3); } if (readerRoundNumber <= tableStatus.RoundNumber && timeLog >= latestTimeLog) { number = reader.GetString(0); name = reader.GetString(1); latestTimeLog = timeLog; } } catch { } // Record found, but format cannot be parsed } }); } finally { reader.Close(); } } cmd.Dispose(); return(FormatName(name, number)); }
public MovesList(int tabletDeviceNumber, TableStatus tableStatus, int newRoundNumber, int tableNotReadyNumber) { TabletDeviceNumber = tabletDeviceNumber; TabletDeviceStatus tabletDeviceStatus = AppData.TabletDeviceStatusList[tabletDeviceNumber]; Direction = tabletDeviceStatus.Direction; NewRoundNumber = newRoundNumber; TableNotReadyNumber = tableNotReadyNumber; Section section = AppData.SectionsList.Find(x => x.SectionID == tabletDeviceStatus.SectionID); TabletDevicesPerTable = section.TabletDevicesPerTable; int missingPair = section.MissingPair; RoundsList roundsList = new RoundsList(tabletDeviceStatus.SectionID, newRoundNumber); if (TabletDevicesPerTable == 1) // tableStatus cannot be null { if (AppData.IsIndividual) { if (tableStatus.RoundData.NumberNorth != 0) { Add(roundsList.GetMove(tableStatus.TableNumber, tableStatus.RoundData.NumberNorth, Direction.North)); } if (tableStatus.RoundData.NumberSouth != 0) { Add(roundsList.GetMove(tableStatus.TableNumber, tableStatus.RoundData.NumberSouth, Direction.South)); } if (tableStatus.RoundData.NumberEast != 0) { Add(roundsList.GetMove(tableStatus.TableNumber, tableStatus.RoundData.NumberEast, Direction.East)); } if (tableStatus.RoundData.NumberWest != 0) { Add(roundsList.GetMove(tableStatus.TableNumber, tableStatus.RoundData.NumberWest, Direction.West)); } } else // Not individual { if (tableStatus.RoundData.NumberNorth != 0 && tableStatus.RoundData.NumberNorth != missingPair) { Add(roundsList.GetMove(tableStatus.TableNumber, tableStatus.RoundData.NumberNorth, Direction.North)); } if (tableStatus.RoundData.NumberEast != 0 && tableStatus.RoundData.NumberEast != missingPair) { Add(roundsList.GetMove(tableStatus.TableNumber, tableStatus.RoundData.NumberEast, Direction.East)); } } } else // TabletDevicesPerTable > 1, so only need move for single player/pair. tableStatus could be null (at phantom table), so use tabletDeviceStatus { Add(roundsList.GetMove(tabletDeviceStatus.TableNumber, tabletDeviceStatus.PairNumber, Direction)); } BoardsNewTable = -999; if (tableStatus != null) // tableStatus==null => phantom table, so no boards to worry about { // Show boards move only to North (or North/South) unless missing, in which case only show to East (or East/West) LowBoard = tableStatus.RoundData.LowBoard; HighBoard = tableStatus.RoundData.HighBoard; if (Direction == Direction.North || ((tableStatus.RoundData.NumberNorth == 0 || tableStatus.RoundData.NumberNorth == missingPair) && Direction == Direction.East)) { BoardsNewTable = roundsList.GetBoardsNewTableNumber(tableStatus.TableNumber, tableStatus.RoundData.LowBoard); } } }
public Traveller(int tabletDeviceNumber, TableStatus tableStatus) { TabletDeviceNumber = tabletDeviceNumber; BoardNumber = tableStatus.ResultData.BoardNumber; NumberNorth = tableStatus.RoundData.NumberNorth; using (OdbcConnection connection = new OdbcConnection(AppData.DBConnectionString)) { connection.Open(); string SQLString; OdbcCommand cmd = null; OdbcDataReader reader = null; try { if (AppData.IsIndividual) { SQLString = $"SELECT PairNS, PairEW, South, West, [NS/EW], Contract, LeadCard, Result FROM ReceivedData WHERE Section={tableStatus.SectionID} AND Board={BoardNumber}"; } else { SQLString = $"SELECT PairNS, PairEW, [NS/EW], Contract, LeadCard, Result FROM ReceivedData WHERE Section={tableStatus.SectionID} AND Board={BoardNumber}"; } cmd = new OdbcCommand(SQLString, connection); ODBCRetryHelper.ODBCRetry(() => { reader = cmd.ExecuteReader(); while (reader.Read()) { Result result = null; if (AppData.IsIndividual) { result = new Result() { BoardNumber = BoardNumber, NumberNorth = reader.GetInt32(0), NumberEast = reader.GetInt32(1), NumberSouth = reader.GetInt32(2), NumberWest = reader.GetInt32(3), DeclarerNSEW = reader.GetString(4), Contract = reader.GetString(5), LeadCard = reader.GetString(6), TricksTakenSymbol = reader.GetString(7) }; } else { result = new Result() { BoardNumber = BoardNumber, NumberNorth = reader.GetInt32(0), NumberEast = reader.GetInt32(1), DeclarerNSEW = reader.GetString(2), Contract = reader.GetString(3), LeadCard = reader.GetString(4), TricksTakenSymbol = reader.GetString(5) }; } if (result.Contract.Length > 2) // Testing for unplayed boards and corrupt ReceivedData table { result.CalculateScore(); Add(result); if (result.NumberNorth == NumberNorth) // Get score for current result for calculating percentages { currentScore = result.Score; } } } }); } finally { reader.Close(); cmd.Dispose(); } }; // Sort traveller and calculate percentage for current result Sort((x, y) => y.Score.CompareTo(x.Score)); if (Settings.ShowPercentage) { if (Count == 1) { PercentageNS = 50; } else { int currentMP = 2 * FindAll((x) => x.Score < currentScore).Count + FindAll((x) => x.Score == currentScore).Count - 1; PercentageNS = Convert.ToInt32(50.0 * currentMP / (Count - 1)); } } else { PercentageNS = -1; // Don't show percentage } // Determine if there is a hand record to view HandRecord = false; if (Settings.ShowHandRecord && HandRecords.HandRecordsList.Count > 0) { HandRecord handRecord = HandRecords.HandRecordsList.Find(x => x.SectionID == tableStatus.SectionID && x.BoardNumber == BoardNumber); if (handRecord != null) { HandRecord = true; } else // Can't find matching hand record, so try default SectionID = 1 { handRecord = HandRecords.HandRecordsList.Find(x => x.SectionID == 1 && x.BoardNumber == BoardNumber); if (handRecord != null) { HandRecord = true; } } } }