<commit_before>package ch.squix.extraleague.model.ranking.tasks;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import ch.squix.extraleague.model.match.Match;
import ch.squix.extraleague.model.match.MatchUtil;
import ch.squix.extraleague.model.match.Matches;
import ch.squix.extraleague.model.match.PlayerMatchResult;
import ch.squix.extraleague.model.ranking.PlayerRanking;
import ch.squix.extraleague.model.ranking.badge.BadgeEnum;


public class TightMatchesTask implements RankingTask {
	
	private static final Logger log = Logger.getLogger(TightMatchesTask.class.getName());

    @Override
    public void rankMatches(Map<String, PlayerRanking> playerRankingMap, Matches matches) {
    	List<PlayerRanking> playerRankings = new ArrayList<>();
        for (String player : matches.getPlayers()) {
        	PlayerRanking ranking = playerRankingMap.get(player);
        	int numberOfTightlyLostMatches = 0;
        	int numberOfTightlyWonMatches = 0;
        	int numberOfMatches = 0;
        	for (Match match : matches.getMatchesByPlayer(player)) {
        		PlayerMatchResult playerMatch = MatchUtil.getPlayerMatchResult(match, player);
        		Integer goalsGot = playerMatch.getGoalsGot();
				Integer goalsMade = playerMatch.getGoalsMade();
				if (goalsGot == 5 && goalsMade == 4) {
        			numberOfTightlyLostMatches++;
        		}
        		if (goalsMade == 5 && goalsGot == 4) {
        			numberOfTightlyWonMatches++;
        		}
        		numberOfMatches++;
        	}
        	if (numberOfMatches >= 8) {
	        	playerRankings.add(ranking);
	        	Double tightlyLostRate = 1d * numberOfTightlyLostMatches / numberOfMatches;
	        	ranking.setTightlyLostRate(tightlyLostRate);
	        	Double tightlyWonRate = 1d * numberOfTightlyWonMatches / numberOfMatches;
	        	ranking.setTightlyWonRate(tightlyWonRate);
        	}

        }
        if (playerRankings.size() > 0) {
            Collections.sort(playerRankings, new TightlyLostComparator());
            PlayerRanking maxTightlyLostPlayer = playerRankings.get(0);
            maxTightlyLostPlayer.getBadges().add(BadgeEnum.BundleOfNerves.name());
            log.info("Bundle of nerves: " + maxTightlyLostPlayer.getPlayer() + " with " + maxTightlyLostPlayer.getTightlyLostRate());
    <commit_msg>Implemented suggestion from issue #14 about calculation of tightly lost/won matches<commit_after>package ch.squix.extraleague.model.ranking.tasks;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import ch.squix.extraleague.model.match.Match;
import ch.squix.extraleague.model.match.MatchUtil;
import ch.squix.extraleague.model.match.Matches;
import ch.squix.extraleague.model.match.PlayerMatchResult;
import ch.squix.extraleague.model.ranking.PlayerRanking;
import ch.squix.extraleague.model.ranking.badge.BadgeEnum;


public class TightMatchesTask implements RankingTask {
	
	private static final Logger log = Logger.getLogger(TightMatchesTask.class.getName());

    @Override
    public void rankMatches(Map<String, PlayerRanking> playerRankingMap, Matches matches) {
    	List<PlayerRanking> playerRankings = new ArrayList<>();
        for (String player : matches.getPlayers()) {
        	PlayerRanking ranking = playerRankingMap.get(player);
        	int numberOfTightlyLostMatches = 0;
        	int numberOfTightlyWonMatches = 0;
        	int numberOfMatches = 0;
        	for (Match match : matches.getMatchesByPlayer(player)) {
        		PlayerMatchResult playerMatch = MatchUtil.getPlayerMatchResult(match, player);
        		Integer goalsGot = playerMatch.getGoalsGot();
				Integer goalsMade = playerMatch.getGoalsMade();
				if (goalsGot == 5 && goalsMade == 4) {
        			numberOfTightlyLostMatches++;
        		}
        		if (goalsMade == 5 && goalsGot == 4) {
        			numberOfTightlyWonMatches++;
        		}
        		numberOfMatches++;
        	}
        	if (numberOfMatches >= 8) {
	        	playerRankings.add(ranking);
	        	Double tightlyLostRate = 1d * numberOfTightlyLostMatches / (numberOfTightlyLostMatches + numberOfTightlyWonMatches);
	        	ranking.setTightlyLostRate(tightlyLostRate);
	        	Double tightlyWonRate = 1d * numberOfTightlyWonMatches / (numberOfTightlyLostMatches + numberOfTightlyWonMatches);
	        	ranking.setTightlyWonRate(tightlyWonRate);
        	}

        }
        if (playerRankings.size() > 0) {
            Collections.sort(playerRankings, new TightlyLostComparator());
            PlayerRanking maxTightlyLostPlayer = playerRankings.get(0);
            maxTightlyLostPlayer.getBadges().add(BadgeEnum.BundleOfNerves.name());
            log.info("Bundle of nerves: " + maxTightlyLostPlayer.getPlayer() + " with " + maxTightlyLostPlayer.getTightlyLostRate());
    
{'fraction_non_alphanumeric': 0.09717741935483871, 'fraction_numerical': 0.005241935483870968, 'mean_word_length': 3.429464285714286, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '6963557', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1538, 'n_words': 260}