Ejemplo n.º 1
0
        public HoleResult GetHoleResult(Hole hole)
        {
            HoleResult result = new HoleResult();

            if (hole.Shots != null && hole.Shots.Any())
            {
                result.IsEmpty = false;
                //Get the last shot
                var shot = hole.Shots.FirstOrDefault(s => s.ShotMade == true || s.ShotType.Id == 3);

                //If it was made, see if it was a push, steal
                if (shot != null && shot.ShotMade == true)
                {
                    result.Shot = shot;
                    result.LastPlayer = shot.Player;

                    //If a push
                    switch (shot.ShotType.Id)
                    {
                        case 1 :
                            result.FirstPlayer = null;
                            result.Points = hole.Par;
                            break;
                        case 3 :
                            var pushedShot = hole.Shots.FirstOrDefault(s => s.Attempts == shot.Attempts && s.Id != shot.Id && s.ShotMade == true);
                            result.FirstPlayer = pushedShot.Player;
                            break;
                        case 4 :
                            var stolenShot = hole.Shots.LastOrDefault(s => s.Attempts > shot.Attempts && s.ShotMade == true);
                            result.FirstPlayer = stolenShot.Player;
                            result.Points = hole.Par;
                            break;
                        //Try and find the players who pushed the hole
                        case 5 :
                            var lastPushedShot  = hole.Shots.LastOrDefault(s => s.ShotMade == true && s.Attempts > shot.Attempts);
                            var firstPushedShot = hole.Shots.LastOrDefault(s => s.ShotMade == true && s.Attempts == lastPushedShot.Attempts && s.Id != lastPushedShot.Id);
                            result.FirstPlayer = new Player() { Initials = lastPushedShot.Player.Initials + "/" + firstPushedShot.Player.Initials };
                            result.Points = hole.Par;

                            break;
                        default :
                            result.FirstPlayer = null;
                            break;
                    }

                    if (result.Points > 0)
                    {
                        //loop through previous holes adding up score until last winning shot.
                        for (int i = hole.Id - 1; i > 0; i--)
                        {
                            var previousHole = Holes.FirstOrDefault(h => h.Id == i);
                            //Check for push
                            if ((previousHole.Shots.Count(s => s.ShotMade == true && s.ShotType.Id == 3) > 0) || previousHole.Shots.Count(s => s.ShotMade == true) == 0)
                            {
                                result.Points += previousHole.Par;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return result;
        }
Ejemplo n.º 2
0
        public HoleResult GetHoleResult(Hole hole)
        {
            HoleResult result = new HoleResult();

            if (hole.Shots != null && hole.Shots.Any())
            {
                result.IsEmpty = false;
                //Get the last shot
                var shot = hole.Shots.FirstOrDefault(s => s.ShotMade == true || s.ShotType.Id == 3);

                //If it was made, see if it was a push, steal
                if (shot != null && shot.ShotMade == true)
                {
                    result.Shot       = shot;
                    result.LastPlayer = shot.Player;

                    //If a push
                    switch (shot.ShotType.Id)
                    {
                    case 1:
                        result.FirstPlayer = null;
                        result.Points      = hole.Par;
                        break;

                    case 3:
                        var pushedShot = hole.Shots.FirstOrDefault(s => s.Attempts == shot.Attempts && s.Id != shot.Id && s.ShotMade == true);
                        result.FirstPlayer = pushedShot.Player;
                        break;

                    case 4:
                        var stolenShot = hole.Shots.LastOrDefault(s => s.Attempts > shot.Attempts && s.ShotMade == true);
                        result.FirstPlayer = stolenShot.Player;
                        result.Points      = hole.Par;
                        break;

                    //Try and find the players who pushed the hole
                    case 5:
                        var lastPushedShot  = hole.Shots.LastOrDefault(s => s.ShotMade == true && s.Attempts > shot.Attempts);
                        var firstPushedShot = hole.Shots.LastOrDefault(s => s.ShotMade == true && s.Attempts == lastPushedShot.Attempts && s.Id != lastPushedShot.Id);
                        result.FirstPlayer = new Player()
                        {
                            Initials = lastPushedShot.Player.Initials + "/" + firstPushedShot.Player.Initials
                        };
                        result.Points = hole.Par;

                        break;

                    default:
                        result.FirstPlayer = null;
                        break;
                    }

                    if (result.Points > 0)
                    {
                        //loop through previous holes adding up score until last winning shot.
                        for (int i = hole.Id - 1; i > 0; i--)
                        {
                            var previousHole = Holes.FirstOrDefault(h => h.Id == i);
                            //Check for push
                            if ((previousHole.Shots.Count(s => s.ShotMade == true && s.ShotType.Id == 3) > 0) || previousHole.Shots.Count(s => s.ShotMade == true) == 0)
                            {
                                result.Points += previousHole.Par;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return(result);
        }