<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WestBlueGolfLeagueWeb.Models.Entities;

namespace WestBlueGolfLeagueWeb.Models.Extensions
{
    public static class TeamExtensions
    {
        public static IEnumerable<result> AllResultsForYear(this team team, year year, week week = null)
        {
            var seasonIndex = week == null ? 50 : week.seasonIndex;

            return team.results.Where(x => x.year.value == year.value && x.match.teammatchup.week.seasonIndex < seasonIndex && x.IsComplete());
        }

        public static double AverageHandicapForYear(this team team, year year)
        {
            // Get all results for year
            var resultsForYear = team.AllResultsForYear(year);

            // Get all players for these results
            var playersWhichPlayedForYear = resultsForYear.Select(x => x.player).Where(x => x.validPlayer).GroupBy(x => x.id).Select(x => x.First());

            // get year data for all players
            List<playeryeardata> yds = new List<playeryeardata>();

            foreach (var player in playersWhichPlayedForYear)
            {
                var yd = player.playeryeardatas.FirstOrDefault(y => y.year.id == year.id);

                if (yd == null) { continue; }

                yds.Add(yd);
            }

            var handicapSum = yds.Sum(x => x.finishingHandicap);

            if (playersWhichPlayedForYear.Count() == 0) return 0.0;

            return (double)handicapSum / (double)playersWhichPlayedForYear.Count();
        }

        public static IEnumerable<player> AllPlayersForYear(this team team, year year)
        {
            var allPlayersForYear = team.playeryeardatas.Where(x => x.year.id == year.id).ToList();

            return allPlayersForYear.Where(x => x.player.validPlayer).Select(x => x.player);
        }

        public static int TotalPointsForYear(this team team, year year)
        {
            var allResults = team.AllResultsForYear(year);

            int total = 0;

            foreach (var result in allResults)
            {
                total += result.points.Value;
            }

            return total;
        }

        public static int TotalPointsForFirstHalf(this team team, year year)
        {
            var allResults = team.AllResultsForYear(year, year.weeks.ElementAt(8));

            int total = 0;

            foreach (var result in allResults)
            {
                total += result.points.Value;
            }

            return total;
        }

        public static int TotalPointsForSecondHalf(this team team, year year)
        {
            var allResults = team.AllResultsForYear(year);

            int total = 0;

            foreach (var result in allResults)
            {
                if (result.match.teammatchup.week.seasonIndex > 8)
                {
                    total += result.points.Value;
                }
            }

            return total;
        }

        public static double AverageOpponentScoreForYear(this team team, year year)
        {
            var results = team.AllResultsForYear(year);

            if (results == null || results.Count() == 0) return 0;

            double totalScore = 0;
            double opponentCount = 0;
<commit_msg>Fixing bug with first half leaderboard<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WestBlueGolfLeagueWeb.Models.Entities;

namespace WestBlueGolfLeagueWeb.Models.Extensions
{
    public static class TeamExtensions
    {
        public static IEnumerable<result> AllResultsForYear(this team team, year year, week week = null)
        {
            var seasonIndex = week == null ? 50 : week.seasonIndex;

            return team.results.Where(x => x.year.value == year.value && x.match.teammatchup.week.seasonIndex < seasonIndex && x.IsComplete());
        }

        public static double AverageHandicapForYear(this team team, year year)
        {
            // Get all results for year
            var resultsForYear = team.AllResultsForYear(year);

            // Get all players for these results
            var playersWhichPlayedForYear = resultsForYear.Select(x => x.player).Where(x => x.validPlayer).GroupBy(x => x.id).Select(x => x.First());

            // get year data for all players
            List<playeryeardata> yds = new List<playeryeardata>();

            foreach (var player in playersWhichPlayedForYear)
            {
                var yd = player.playeryeardatas.FirstOrDefault(y => y.year.id == year.id);

                if (yd == null) { continue; }

                yds.Add(yd);
            }

            var handicapSum = yds.Sum(x => x.finishingHandicap);

            if (playersWhichPlayedForYear.Count() == 0) return 0.0;

            return (double)handicapSum / (double)playersWhichPlayedForYear.Count();
        }

        public static IEnumerable<player> AllPlayersForYear(this team team, year year)
        {
            var allPlayersForYear = team.playeryeardatas.Where(x => x.year.id == year.id).ToList();

            return allPlayersForYear.Where(x => x.player.validPlayer).Select(x => x.player);
        }

        public static int TotalPointsForYear(this team team, year year)
        {
            var allResults = team.AllResultsForYear(year);

            int total = 0;

            foreach (var result in allResults)
            {
                total += result.points.Value;
            }

            return total;
        }

        public static int TotalPointsForFirstHalf(this team team, year year)
        {
            var allResults = team.AllResultsForYear(year);

            int total = 0;

            foreach (var result in allResults)
            {
                if (result.match.teammatchup.week.seasonIndex <= 8)
                {
                    total += result.points.Value;
                }
            }

            return total;
        }

        public static int TotalPointsForSecondHalf(this team team, year year)
        {
            var allResults = team.AllResultsForYear(year);

            int total = 0;

            foreach (var result in allResults)
            {
                if (result.match.teammatchup.week.seasonIndex > 8)
                {
                    total += result.points.Value;
                }
            }

            return total;
        }

        public static double AverageOpponentScoreForYear(this team team, year year)
        {
            var results = team.AllResultsForYear(year);

            if (results == null || results.Count() == 0) return 0;

            double totalScore = 0;
            double opponentCount = 0;

{'fraction_non_alphanumeric': 0.09189429928741093, 'fraction_numerical': 0.004156769596199525, 'mean_word_length': 1.8012474012474013, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3439592', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1750, 'n_words': 447}