示例#1
0
        public void DeleteFrame(SnookerFrameScore frame)
        {
            int frameNumber = FrameScores.IndexOf(frame) + 1;

            if (frameNumber <= 0)
            {
                return;
            }

            // remove breaks
            List <SnookerBreak> breaksToDelete = this.GetBreaksForFrame(frameNumber);

            foreach (var b in breaksToDelete)
            {
                this.YourBreaks.Remove(b);
                this.OpponentBreaks.Remove(b);
            }
            foreach (var b in this.YourBreaks)
            {
                if (b.FrameNumber > frameNumber)
                {
                    b.FrameNumber -= 1;
                }
            }
            foreach (var b in this.OpponentBreaks)
            {
                if (b.FrameNumber > frameNumber)
                {
                    b.FrameNumber -= 1;
                }
            }

            // remove the frame
            FrameScores.Remove(frame);
        }
示例#2
0
        public SnookerFrameScore Clone()
        {
            SnookerFrameScore newObj = new SnookerFrameScore();

            newObj.A = this.A;
            newObj.B = this.B;
            newObj.ballsOnTable.lowestColor  = this.ballsOnTable.lowestColor;
            newObj.ballsOnTable.numberOfReds = this.ballsOnTable.numberOfReds;

            return(newObj);
        }
示例#3
0
        public void PostToScore(Score score)
        {
            this.CalculateMatchScoreFromFrameScores();

            score.ScoreID              = ID;
            score.AthleteAID           = YourAthleteID;
            score.AthleteBID           = OpponentAthleteID;
            score.VenueID              = VenueID;
            score.Date                 = Date;
            score.IsUnfinished         = IsUnfinished;
            score.Type1                = (int)TableSize;
            score.PointsA              = MatchScoreA;
            score.PointsB              = MatchScoreB;
            score.AthleteBConfirmation = (int)this.OpponentConfirmation;

            for (int i = 1; i <= 10; ++i)
            {
                var frameScore = new SnookerFrameScore();
                if (HasFrameScores && FrameScores.Count >= i)
                {
                    frameScore = FrameScores[i - 1];
                }
                score.UpdateInnerPoints(i, frameScore.A, frameScore.B);
            }

            // breaks
            try
            {
                List <SnookerBreakDataCompressed> breaks = new List <SnookerBreakDataCompressed>();
                if (this.YourBreaks != null)
                {
                    foreach (var b in this.YourBreaks)
                    {
                        breaks.Add(new SnookerBreakDataCompressed()
                        {
                            AthleteID = this.YourAthleteID,
                            Date      = b.Date,
                            Points    = b.Points,
                            Number    = b.NumberOfBalls,
                            Frame     = b.FrameNumber,
                            Balls     = b.Balls.ToList(),
                            IsFoul    = b.IsFoul
                        });
                    }
                }
                if (this.OpponentBreaks != null)
                {
                    foreach (var b in this.OpponentBreaks)
                    {
                        breaks.Add(new SnookerBreakDataCompressed()
                        {
                            AthleteID = this.OpponentAthleteID,
                            Date      = b.Date,
                            Points    = b.Points,
                            Number    = b.NumberOfBalls,
                            Frame     = b.FrameNumber,
                            Balls     = b.Balls.ToList(),
                            IsFoul    = b.IsFoul
                        });
                    }
                }
                score.ExtraData = Newtonsoft.Json.JsonConvert.SerializeObject(breaks);
            }
            catch (Exception)
            {
                score.ExtraData = null;
            }
        }
示例#4
0
        public SnookerFrameAnalysis Analyze(SnookerMatchScore match, int frameNumber)
        {
            SnookerFrameAnalysis analysis = new SnookerFrameAnalysis();

            analysis.Status = SnookerFrameAnalysisEnum.Issues;
            analysis.Issue  = "";

            // all breaks
            List <SnookerBreak> breaks = match.GetBreaksForFrame(frameNumber).OrderBy(i => i.Date).ToList();

            // do the frame scores total the breaks?
            if (match.FrameScores == null || frameNumber < match.FrameScores.Count)
            {
                analysis.Status = SnookerFrameAnalysisEnum.NotFullyTracked;
                analysis.Issue  = "No frame scores";
                return(analysis);
            }
            SnookerFrameScore frameScore = match.FrameScores[frameNumber - 1];
            int pointsA = breaks.Where(i => i.AthleteID == match.YourAthleteID).Sum(i => i.Points);
            int pointsB = breaks.Where(i => i.AthleteID == match.OpponentAthleteID).Sum(i => i.Points);

            if (frameScore.A != pointsA || frameScore.B != pointsB)
            {
                analysis.Status = SnookerFrameAnalysisEnum.NotFullyTracked;
                analysis.Issue  = "Frame score edited";
                return(analysis);
            }

            // score difference
            int scoreDifference = System.Math.Abs(frameScore.A - frameScore.B);

            analysis.ScoreDifference = scoreDifference;

            /// go thru the breaks
            ///
            int numberOfRedsLeft        = 15;
            int numberOfFouls           = 0;
            int lastColoredPocketedBall = 1;

            foreach (var b in breaks)
            {
                int index = breaks.IndexOf(b) + 1;

                if (b.NumberOfBalls < 1)
                {
                    analysis.Issue = "Empty break #" + index;
                    return(analysis);
                }

                if (b.NumberOfBalls == 1 && b.Points >= 4 && b.Points <= 7)
                {
                    numberOfFouls++;
                    continue;
                }

                bool expectingColoredAfterRed = false;
                for (int i = 0; i < b.Balls.Count; ++i)
                {
                    int ball = b.Balls[i];
                    if (numberOfRedsLeft > 0 || expectingColoredAfterRed)
                    {
                        // red, colored, red, colored

                        if (expectingColoredAfterRed == true && ball == 1)
                        {
                            if (numberOfRedsLeft <= 0)
                            {
                                analysis.Issue = "Invalid break #" + index;
                                return(analysis);
                            }

                            // several reds at the same time (happens when more than one red pocketed at the same time)
                            numberOfRedsLeft--;
                            continue;
                        }

                        if (expectingColoredAfterRed)
                        {
                            if (ball < 2 || ball > 7)
                            {
                                analysis.Issue = "Invalid break #" + index;
                                return(analysis);
                            }
                            expectingColoredAfterRed = false;
                        }
                        else
                        {
                            if (ball != 1)
                            {
                                analysis.Issue = "Invalid break #" + index;
                                return(analysis);
                            }

                            numberOfRedsLeft--;
                            expectingColoredAfterRed = true;
                        }
                    }
                    else
                    {
                        // last colored

                        if (ball < 2 || ball > 7)
                        {
                            analysis.Issue = "Invalid break #" + index;
                            return(analysis);
                        }

                        if (ball > lastColoredPocketedBall + 1 || ball == lastColoredPocketedBall)
                        {
                            numberOfFouls++;
                            continue;
                        }

                        if (ball != lastColoredPocketedBall + 1)
                        {
                            analysis.Issue = "Invalid break #" + index;
                            return(analysis);
                        }

                        lastColoredPocketedBall = ball;

                        if (ball == 7)
                        {
                            bool ok = false;
                            if (i == b.Balls.Count - 1)
                            {
                                ok = true;
                            }
                            if (i == b.Balls.Count - 2 && b.Balls[i + 1] == 7)
                            {
                                ok = true;
                            }
                            if (ok == false)
                            {
                                analysis.Issue = "Breaks after last ball potted";
                                return(analysis);
                            }
                        }
                    }
                }
            }

            /// Calculate number of points left
            ///
            int numberOfLastColoredBallPointsLeft = 0; // maximum=2+3+4+5+6+7=27

            for (int ball = 7; ball >= 2; --ball)
            {
                if (ball <= lastColoredPocketedBall)
                {
                    break;
                }
                numberOfLastColoredBallPointsLeft += ball;
            }
            int numberOfPointsLeft = numberOfLastColoredBallPointsLeft;

            numberOfPointsLeft += numberOfRedsLeft * (1 + 7);

            /// Calculate snookers required
            ///
            int snookersRequired = 0;

            if (numberOfPointsLeft < scoreDifference)
            {
                int snookerPoints = 4;
                if (lastColoredPocketedBall > 4)
                {
                    snookerPoints = lastColoredPocketedBall;
                }
                int extraPoints = scoreDifference - numberOfPointsLeft;
                snookersRequired = extraPoints / snookerPoints;
                if (extraPoints % snookerPoints > 0)
                {
                    snookersRequired++;
                }
            }

            analysis.Status             = SnookerFrameAnalysisEnum.FullyTracked;
            analysis.Issue              = "";
            analysis.NumberOfBreaks     = breaks.Count - numberOfFouls;
            analysis.NumberOfFouls      = numberOfFouls;
            analysis.NumberOfRedsLeft   = numberOfRedsLeft;
            analysis.NumberOfPointsLeft = numberOfPointsLeft;
            analysis.SnookersRequired   = snookersRequired;
            return(analysis);
        }