Beispiel #1
0
        public static void Refresh()
        {
            if (DateTime.Now.Subtract(UpdateTime).TotalMinutes < 1.0)
            {
                return;                                                        // Hand records updated recently, so don't bother
            }
            UpdateTime = DateTime.Now;

            HandRecordsList.Clear();
            using (OdbcConnection connection = new OdbcConnection(AppData.DBConnectionString))
            {
                string         SQLString = $"SELECT Section, Board, NorthSpades, NorthHearts, NorthDiamonds, NorthClubs, EastSpades, EastHearts, EastDiamonds, EastClubs, SouthSpades, SouthHearts, SouthDiamonds, SouthClubs, WestSpades, WestHearts, WestDiamonds, WestClubs FROM HandRecord";
                OdbcCommand    cmd       = new OdbcCommand(SQLString, connection);
                OdbcDataReader reader    = null;
                connection.Open();
                try
                {
                    ODBCRetryHelper.ODBCRetry(() =>
                    {
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            HandRecord handRecord = new HandRecord
                            {
                                SectionID     = reader.GetInt16(0),
                                BoardNumber   = reader.GetInt16(1),
                                NorthSpades   = reader.GetString(2),
                                NorthHearts   = reader.GetString(3),
                                NorthDiamonds = reader.GetString(4),
                                NorthClubs    = reader.GetString(5),
                                EastSpades    = reader.GetString(6),
                                EastHearts    = reader.GetString(7),
                                EastDiamonds  = reader.GetString(8),
                                EastClubs     = reader.GetString(9),
                                SouthSpades   = reader.GetString(10),
                                SouthHearts   = reader.GetString(11),
                                SouthDiamonds = reader.GetString(12),
                                SouthClubs    = reader.GetString(13),
                                WestSpades    = reader.GetString(14),
                                WestHearts    = reader.GetString(15),
                                WestDiamonds  = reader.GetString(16),
                                WestClubs     = reader.GetString(17)
                            };
                            handRecord.Dealer = Utilities.GetDealerForBoard(handRecord.BoardNumber);
                            HandRecordsList.Add(handRecord);
                        }
                    });
                }
                catch (OdbcException e)
                {
                    if (e.Errors.Count == 1 && e.Errors[0].SQLState == "42S02")  // HandRecord table does not exist
                    {
                        return;
                    }
                    else
                    {
                        throw (e);
                    }
                }
                finally
                {
                    reader.Close();
                    cmd.Dispose();
                }

                SQLString = $"SELECT Section, Board, NorthSpades, NorthHearts, NorthDiamonds, NorthClubs, NorthNotrump, EastSpades, EastHearts, EastDiamonds, EastClubs, EastNotrump, SouthSpades, SouthHearts, SouthDiamonds, SouthClubs, SouthNotrump, WestSpades, WestHearts, WestDiamonds, WestClubs, WestNoTrump, NorthHcp, EastHcp, SouthHcp, WestHcp FROM HandEvaluation";
                cmd       = new OdbcCommand(SQLString, connection);
                try
                {
                    ODBCRetryHelper.ODBCRetry(() =>
                    {
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            HandRecord handRecord = HandRecordsList.Find(x => x.SectionID == reader.GetInt16(0) && x.BoardNumber == reader.GetInt16(1));
                            if (handRecord != null)
                            {
                                if (reader.GetInt16(2) > 6)
                                {
                                    handRecord.EvalNorthSpades = (reader.GetInt16(2) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalNorthSpades = "";
                                }
                                if (reader.GetInt16(3) > 6)
                                {
                                    handRecord.EvalNorthHearts = (reader.GetInt16(3) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalNorthHearts = "";
                                }
                                if (reader.GetInt16(4) > 6)
                                {
                                    handRecord.EvalNorthDiamonds = (reader.GetInt16(4) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalNorthDiamonds = "";
                                }
                                if (reader.GetInt16(5) > 6)
                                {
                                    handRecord.EvalNorthClubs = (reader.GetInt16(5) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalNorthClubs = "";
                                }
                                if (reader.GetInt16(6) > 6)
                                {
                                    handRecord.EvalNorthNT = (reader.GetInt16(6) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalNorthNT = "";
                                }
                                if (reader.GetInt16(7) > 6)
                                {
                                    handRecord.EvalEastSpades = (reader.GetInt16(7) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalEastSpades = "";
                                }
                                if (reader.GetInt16(8) > 6)
                                {
                                    handRecord.EvalEastHearts = (reader.GetInt16(8) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalEastHearts = "";
                                }
                                if (reader.GetInt16(9) > 6)
                                {
                                    handRecord.EvalEastDiamonds = (reader.GetInt16(9) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalEastDiamonds = "";
                                }
                                if (reader.GetInt16(10) > 6)
                                {
                                    handRecord.EvalEastClubs = (reader.GetInt16(10) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalEastClubs = "";
                                }
                                if (reader.GetInt16(11) > 6)
                                {
                                    handRecord.EvalEastNT = (reader.GetInt16(11) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalEastNT = "";
                                }
                                if (reader.GetInt16(12) > 6)
                                {
                                    handRecord.EvalSouthSpades = (reader.GetInt16(12) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalSouthSpades = "";
                                }
                                if (reader.GetInt16(13) > 6)
                                {
                                    handRecord.EvalSouthHearts = (reader.GetInt16(13) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalSouthHearts = "";
                                }
                                if (reader.GetInt16(14) > 6)
                                {
                                    handRecord.EvalSouthDiamonds = (reader.GetInt16(14) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalSouthDiamonds = "";
                                }
                                if (reader.GetInt16(15) > 6)
                                {
                                    handRecord.EvalSouthClubs = (reader.GetInt16(15) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalSouthClubs = "";
                                }
                                if (reader.GetInt16(16) > 6)
                                {
                                    handRecord.EvalSouthNT = (reader.GetInt16(16) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalSouthNT = "";
                                }
                                if (reader.GetInt16(17) > 6)
                                {
                                    handRecord.EvalWestSpades = (reader.GetInt16(17) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalWestSpades = "";
                                }
                                if (reader.GetInt16(18) > 6)
                                {
                                    handRecord.EvalWestHearts = (reader.GetInt16(18) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalWestHearts = "";
                                }
                                if (reader.GetInt16(19) > 6)
                                {
                                    handRecord.EvalWestDiamonds = (reader.GetInt16(19) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalWestDiamonds = "";
                                }
                                if (reader.GetInt16(20) > 6)
                                {
                                    handRecord.EvalWestClubs = (reader.GetInt16(20) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalWestClubs = "";
                                }
                                if (reader.GetInt16(21) > 6)
                                {
                                    handRecord.EvalWestNT = (reader.GetInt16(21) - 6).ToString();
                                }
                                else
                                {
                                    handRecord.EvalWestNT = "";
                                }

                                handRecord.HCPNorth = reader.GetInt16(22).ToString();
                                handRecord.HCPEast  = reader.GetInt16(23).ToString();
                                handRecord.HCPSouth = reader.GetInt16(24).ToString();
                                handRecord.HCPWest  = reader.GetInt16(25).ToString();
                            }
                        }
                    });
                }
                catch (OdbcException e)
                {
                    if (e.Errors.Count > 1 || e.Errors[0].SQLState != "42S02")  // Error other than HandEvaluation table does not exist
                    {
                        throw (e);
                    }
                }
                finally
                {
                    reader.Close();
                    cmd.Dispose();
                }
            }
            return;
        }
Beispiel #2
0
        // Validate the lead card against the hand record
        public static bool ValidateLead(TableStatus tableStatus, string card)
        {
            if (HandRecords.HandRecordsList.Count == 0)
            {
                return(true);                                           // No hand records to validate against
            }
            if (card == "SKIP")
            {
                return(true);                   // Lead card entry has been skipped, so no validation
            }
            HandRecord handRecord = HandRecords.HandRecordsList.Find(x => x.SectionID == tableStatus.SectionID && x.BoardNumber == tableStatus.ResultData.BoardNumber);

            if (handRecord == null)     // Can't find matching hand record, so try default SectionID = 1
            {
                handRecord = HandRecords.HandRecordsList.Find(x => x.SectionID == 1 && x.BoardNumber == tableStatus.ResultData.BoardNumber);
                if (handRecord == null)
                {
                    return(true);                       // Still no match, so no validation possible
                }
            }

            string cardSuit  = card.Substring(0, 1);
            string cardValue = card.Substring(1, 1);

            if (cardValue == "1")    // Account for different representations of '10'
            {
                cardValue = "T";
            }

            switch (tableStatus.ResultData.DeclarerNSEW)
            {
            case "N":
                switch (cardSuit)
                {
                case "S":
                    if (handRecord.EastSpades.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "H":
                    if (handRecord.EastHearts.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "D":
                    if (handRecord.EastDiamonds.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "C":
                    if (handRecord.EastClubs.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;
                }
                break;

            case "S":
                switch (cardSuit)
                {
                case "S":
                    if (handRecord.WestSpades.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "H":
                    if (handRecord.WestHearts.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "D":
                    if (handRecord.WestDiamonds.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "C":
                    if (handRecord.WestClubs.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;
                }
                break;

            case "E":
                switch (cardSuit)
                {
                case "S":
                    if (handRecord.SouthSpades.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "H":
                    if (handRecord.SouthHearts.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "D":
                    if (handRecord.SouthDiamonds.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "C":
                    if (handRecord.SouthClubs.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;
                }
                break;

            case "W":
                switch (cardSuit)
                {
                case "S":
                    if (handRecord.NorthSpades.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "H":
                    if (handRecord.NorthHearts.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "D":
                    if (handRecord.NorthDiamonds.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;

                case "C":
                    if (handRecord.NorthClubs.Contains(cardValue))
                    {
                        return(true);
                    }
                    break;
                }
                break;
            }
            return(false);
        }
Beispiel #3
0
        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;
                    }
                }
            }
        }