public TeamMatchupWithMatches(teammatchup teamMatchup)
     : base(teamMatchup)
 {
     this.Matches =
         teamMatchup.matches == null ?
         null :
         teamMatchup.matches.OrderBy(x => x.matchOrder).Select(x => new MatchWebResponse(x, this.Team1.Id, this.Team2.Id));
 }
Beispiel #2
0
 public static bool HasNoTeam(this teammatchup teamMatchup)
 {
     foreach (team aTeam in teamMatchup.teams)
     {
         if (aTeam.teamName == "NO TEAM")
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #3
0
        private teammatchup CreateTeamMatchup(team team1, team team2, week week, int matchOrder)
        {
            var tm = new teammatchup {
                week = week, matchOrder = matchOrder, matchComplete = false
            };

            tm.teams.Add(team1);
            tm.teams.Add(team2);

            return(tm);
        }
Beispiel #4
0
 public static bool IsAfterNoTeam(this teammatchup teamMatchup)
 {
     // Unfortunately, the iteration order seems not by match order, so we can't break early
     foreach (teammatchup matchup in teamMatchup.week.teammatchups)
     {
         if (matchup.matchOrder < teamMatchup.matchOrder && matchup.HasNoTeam())
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #5
0
        private void DeletePreviousData(WestBlue db, teammatchup teamMatchup)
        {
            var resultsToDelete = new List <result>();

            foreach (var m in teamMatchup.matches)
            {
                resultsToDelete.AddRange(m.results);
            }

            db.results.RemoveRange(resultsToDelete);
            db.matches.RemoveRange(teamMatchup.matches);
        }
        public static TeamMatchupResponse From(teammatchup tm)
        {
            var tmr = new TeamMatchupResponse();

            tmr.Id          = tm.id;
            tmr.Mc          = tm.matchComplete;
            tmr.TeamIds     = tm.teams.Select(x => x.id).ToList();
            tmr.WId         = tm.weekId;
            tmr.Matches     = tm.matches.Select(x => MatchResponse.From(x)).ToList();
            tmr.PlayoffType = tm.playoffType;
            tmr.MatchOrder  = tm.matchOrder;
            return(tmr);
        }
Beispiel #7
0
        public async Task <HttpResponseMessage> PutMatchup(int weekId, int matchupId, TeamMatchupWithMatches teamMatchup)
        {
            if (teamMatchup.Id != matchupId)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var databaseTeamMatchup = await this.Db.teammatchups.Include(y => y.week).Include("week.year").FirstOrDefaultAsync(x => x.id == teamMatchup.Id);

            if (databaseTeamMatchup == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errors = new string[] { "Couldn't find requested teammatchup." } }));
            }

            ScoreEntry scoreEntry = new ScoreEntry(teamMatchup, databaseTeamMatchup.week);

            if (!scoreEntry.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errors = scoreEntry.Errors }));
            }

            teammatchup persistedTeamMatchup = null;

            try
            {
                persistedTeamMatchup = await scoreEntry.SaveScoresAsync(this.Db);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new { errors = new string[] { "There was an error saving scores/matches. " + e.Message } }));
            }

            try
            {
                if (scoreEntry.ShouldCalculateLeaderBoards(persistedTeamMatchup))
                {
                    var lbc = new PostScoreEntryProcessor(this.Db, persistedTeamMatchup.id);
                    lbc.Execute();
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new { errors = new string[] { "There was an error saving handicaps and leaderboards. " + e.Message } }));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        // Needed for most other team display
        public TeamProfileResult(team t, teammatchup tm)
        {
            this.PriorHandicapForOpponent = 0;
            this.PriorHandicapForPlayer   = 0;
            this.OpponentPoints           = 0;
            this.Points                  = 0;
            this.ScoreDifference         = 0;
            this.OpponentScoreDifference = 0;
            this.Score         = 0;
            this.OpponentScore = 0;

            foreach (match m in tm.matches)
            {
                foreach (result r in m.results)
                {
                    if (r.team.id == t.id)
                    {
                        this.PriorHandicapForPlayer += r.priorHandicap;
                        this.Points          += r.points;
                        this.ScoreDifference += r.ScoreDifference();
                        this.Score           += r.score;
                    }
                    else
                    {
                        this.PriorHandicapForOpponent += r.priorHandicap;
                        this.OpponentPoints           += r.points;
                        this.OpponentScoreDifference  += r.ScoreDifference();
                        this.OpponentScore            += r.score;
                    }
                }
            }

            foreach (team aTeam in tm.teams)
            {
                if (t.id != aTeam.id)
                {
                    this.OpponentName = aTeam.teamName;
                }
            }

            this.WeekIndex  = tm.week.seasonIndex;
            this.WeekDate   = tm.week.date;
            this.TeeTime    = tm.TeeTimeText();
            this.CourseName = tm.week.course.name;
            this.WasWin     = this.Points > 48;
            this.WasLoss    = this.Points < 48;
        }
        /// <summary>
        ///
        /// </summary>
        private void UpdatePlayerHandicaps(teammatchup tm)
        {
            var playerIds = new List <int>();

            foreach (var match in tm.matches)
            {
                playerIds.AddRange(match.players.Select(x => x.id));
            }

            var resultsForTeamMatchup = this.database.results.Where(x => playerIds.Contains(x.playerId) && x.year.value == tm.week.year.value).ToList();
            var resultsLookup         = resultsForTeamMatchup.ToLookup(x => x.playerId);

            var scoreResultFactory = new ScoreResultFactory(tm.week.year.value);
            var hc = new HandicapCalculator(scoreResultFactory);

            foreach (var match in tm.matches)
            {
                // Only compute handicaps for valid matches.
                if (!match.IsComplete())
                {
                    continue;
                }

                foreach (var player in match.players)
                {
                    if (!player.validPlayer)
                    {
                        continue;
                    }

                    var pyd     = player.playeryeardatas.FirstOrDefault(x => x.year.value == tm.week.year.value);
                    var results = resultsLookup[player.id].Where(x => x.IsComplete()).OrderBy(x => x.match.teammatchup.week.date);

                    if (results.Count() == 0)
                    {
                        continue;
                    }

                    // This is done to ensure that prior handicap is correct.
                    var mostRecentHandicap = hc.CalculateAndCascadeHandicaps(results, pyd.week0Score, pyd.isRookie);

                    player.currentHandicap = mostRecentHandicap.Handicap;
                    pyd.finishingHandicap  = mostRecentHandicap.Handicap;
                }
            }
        }
Beispiel #10
0
        // TODO: could probably use expression instead of function
        private static result TopX(teammatchup tm, team team, Func <result, result, result> func)
        {
            if (!tm.IsComplete())
            {
                return(null);
            }

            List <result> allResults = tm.matches.SelectMany(x => x.results).ToList();//.Where(x => x.teamId == team.id).ToList();

            if (allResults.Count == 0)
            {
                return(null);
            }

            result r = allResults.Aggregate(func);

            return(r);
        }
        public TeamMatchupWithResults(teammatchup teamMatchup)
            : base(teamMatchup)
        {
            this.Team1Results = new List <ResultWithOutcome>(4);
            this.Team2Results = new List <ResultWithOutcome>(4);

            int team1Id = this.Team1 != null ? this.Team1.Id : -1;
            int team2Id = this.Team2 != null ? this.Team2.Id : -1;

            foreach (match m in teamMatchup.matches.OrderBy(x => x.matchOrder))
            {
                var result1 = m.results.FirstOrDefault(x => x.teamId == team1Id);
                var result2 = m.results.FirstOrDefault(x => x.teamId == team2Id);

                this.Team1Results.Add(result1 == null ? null : new ResultWithOutcome(result1));
                this.Team2Results.Add(result2 == null ? null : new ResultWithOutcome(result2));
            }
        }
Beispiel #12
0
 public static string TeeTimeText(this teammatchup teamMatchup)
 {
     if (teamMatchup.matchOrder < 0)
     {
         return("n/a");
     }
     // Unfortunately, we need to fudge the tee times due to no-team matches only getting one time
     if (teamMatchup.HasNoTeam())
     {
         return(teamMatchup.matchOrder == null ? "n/a" : TeeTimes[teamMatchup.matchOrder.Value * 2]);
     }
     else if (teamMatchup.matchOrder > 0 && teamMatchup.IsAfterNoTeam())
     {
         // Do a safety check on matchOrder to not bother checking if after no team if first matchup
         return(teamMatchup.matchOrder == null ? "n/a" : TeeTimes[teamMatchup.matchOrder.Value * 2 - 1] + " (" + TeeTimes[teamMatchup.matchOrder.Value * 2] + ")");
     }
     else
     {
         return(teamMatchup.matchOrder == null ? "n/a" : TeeTimes[teamMatchup.matchOrder.Value * 2] + " (" + TeeTimes[teamMatchup.matchOrder.Value * 2 + 1] + ")");
     }
 }
Beispiel #13
0
        public static int?PointsForTeam(this teammatchup teamMatchup, int teamIndex)
        {
            if (teamMatchup.teams.Count < teamIndex + 1)
            {
                return(0);
            }

            int?points = 0;
            var t      = teamMatchup.teams.ElementAt(teamIndex);

            foreach (match m in teamMatchup.matches)
            {
                foreach (result r in m.results)
                {
                    if (r.team.id == t.id)
                    {
                        points += r.points;
                    }
                }
            }
            return(points);
        }
Beispiel #14
0
 public bool ShouldCalculateLeaderBoards(teammatchup matchup)
 {
     return(matchup.matches.SelectMany(x => x.results).Any(x => x.IsComplete()));
 }
Beispiel #15
0
 public static result TopNetDifference(this teammatchup tm, team team)
 {
     return(TopX(tm, team, (agg, next) => agg.NetScoreDifference() < next.NetScoreDifference() ? agg : next));
 }
Beispiel #16
0
        public ScheduleTeamMatchup(teammatchup tm)
        {
            int numOfTeams = tm.teams.Count;

            this.MatchOrder = tm.matchOrder;

            team team1 = numOfTeams > 0 ? tm.teams.First() : null;
            team team2 = numOfTeams > 1 ? tm.teams.Skip(1).First() : null;

            if (team1 != null)
            {
                this.Team1       = TeamResponse.From(team1);
                this.Team1Points = tm.PointsFor(team1);
                this.Team1Win    = tm.Team1Won();
            }

            if (team2 != null)
            {
                this.Team2       = TeamResponse.From(team2);
                this.Team2Points = tm.PointsFor(team2);
                this.Team2Win    = tm.Team2Won();
            }

            this.IsComplete  = tm.IsComplete();
            this.TeeTimeText = tm.TeeTimeText();
            this.Id          = tm.id;
            this.PlayoffType = tm.playoffType;

            if (this.IsComplete && team1 != null && team2 != null)
            {
                result team1PointsResult = tm.TopPoints(team1);
                //result team2PointsResult = tm.TopPoints(team2);

                this.TopPoints = team1PointsResult != null ?
                                 new MatchSummaryValue
                {
                    Player         = new PlayerWebResponse(team1PointsResult.player),
                    FormattedValue = LeaderBoardFormat.Default.FormatValue(team1PointsResult.points),
                    Value          = (double)team1PointsResult.points
                } : null;


                result topNetScore = tm.TopNetDifference(team1);

                this.TopNetScore = topNetScore != null ?
                                   new MatchSummaryValue
                {
                    Player         = new PlayerWebResponse(topNetScore.player),
                    FormattedValue = LeaderBoardFormat.Net.FormatValue(topNetScore.NetScoreDifference()),
                    Value          = (double)topNetScore.NetScoreDifference()
                } : null;

                //this.Team2TopPoints = team2PointsResult != null ?
                //    new MatchSummaryValue
                //    {
                //        Player = new PlayerWebResponse(team2PointsResult.player),
                //        FormattedValue = LeaderBoardFormat.Default.FormatValue(team2PointsResult.points),
                //        Value = (double)team2PointsResult.points
                //    } : null;
            }
        }
Beispiel #17
0
 public static result TopPoints(this teammatchup tm, team team)
 {
     return(TopX(tm, team, (agg, next) => agg.points > next.points ? agg : next));
 }
Beispiel #18
0
 public static int?PointsFor(this teammatchup tm, team team)
 {
     return(tm.matches.Select(x => x.results.FirstOrDefault(r => r.teamId == team.id)).Sum(x => x == null ? 0 : x.points));
 }
Beispiel #19
0
 public static bool IsComplete(this teammatchup tm)
 {
     return(tm.matches != null && tm.matches.Count > 0 && tm.matches.All(x => x.IsComplete()));
 }
Beispiel #20
0
 public static bool Team2Won(this teammatchup teamMatchup)
 {
     return(teamMatchup.PointsForTeam(1) > 48);
 }