From bddb1400d83fdb8bb59989207c7e9cd9dc264678 Mon Sep 17 00:00:00 2001 From: Matt Busche Date: Sun, 29 Sep 2024 21:37:18 -0500 Subject: [PATCH] clean up top scores --- src/main/java/trap/report/ReportHelper.java | 62 +++++++++------------ src/main/java/trap/report/TrapHelper.java | 50 ++++++++++++++--- 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/main/java/trap/report/ReportHelper.java b/src/main/java/trap/report/ReportHelper.java index c6cf862..2bc9b1a 100644 --- a/src/main/java/trap/report/ReportHelper.java +++ b/src/main/java/trap/report/ReportHelper.java @@ -28,6 +28,11 @@ import java.util.Map; import java.util.Objects; +import static trap.report.TrapHelper.getRoundsToCount; +import static trap.report.TrapHelper.parseInteger; +import static trap.report.TrapHelper.setStringToZero; +import static trap.report.TrapHelper.trimString; + @Service public class ReportHelper { private static final String SINGLES = "singles"; @@ -51,7 +56,7 @@ public class ReportHelper { Logger logger = LoggerFactory.getLogger(ReportHelper.class); public void generateExcelFile() throws Exception { - downloadHelper.downloadFiles(trapTypes); +// downloadHelper.downloadFiles(trapTypes); var workbook = getWorkbook(); @@ -181,26 +186,29 @@ private void populateCleanData(Sheet sheet, List allRoundScores) { private List getTeamScores(List>> teamData) { var teamScoresThatCount = new HashMap(); for (Map.Entry> total : teamData) { - var details = new TeamScore(total.getValue().getFirst().getTeam(), 0); - teamScoresThatCount.put(total.getValue().getFirst().getTeam(), details); + var team = total.getValue().getFirst().getTeam(); + teamScoresThatCount.putIfAbsent(team, new TeamScore(team, 0)); } - for (Map.Entry> total : teamData) { - var teamTotal = teamScoresThatCount.get(total.getValue().getFirst().getTeam()); - var scoresCounted = 0; - int scoresToCount = total.getValue().getFirst().getType().equals(SINGLES) || total.getValue().getFirst().getType().equals(HANDICAP) || total.getValue().getFirst().getType().equals(DOUBLES) ? 5 : 3; - ; - for (var indTotal : total.getValue()) { - int currentTotal = teamTotal.getTotal(); - teamTotal.setTotal(currentTotal + indTotal.getTotal()); - teamScoresThatCount.put(indTotal.getTeam(), teamTotal); - scoresCounted++; - if (scoresCounted >= scoresToCount) { - break; - } - } + for (Map.Entry> entry : teamData) { + // Get the first individual's team and type + IndividualTotal firstIndividual = entry.getValue().getFirst(); + TeamScore teamTotal = teamScoresThatCount.get(firstIndividual.getTeam()); + + // Determine the number of scores to count based on the type + int scoresToCount = getRoundsToCount(firstIndividual.getType()); + + // Sum the top scores up to the limit (scoresToCount) + int scoreSum = entry.getValue().stream() + .limit(scoresToCount) + .mapToInt(IndividualTotal::getTotal) + .sum(); + + // Update the team's total score + teamTotal.setTotal(teamTotal.getTotal() + scoreSum); } + return teamScoresThatCount.values().stream().sorted(Comparator.comparingInt(TeamScore::getTotal).reversed()).toList(); } @@ -345,8 +353,7 @@ private HashMap> calculateTeamScores(List Integer.compare(b.getTotal(), a.getTotal())); // Handle ties at 5th place - int scoresToCount = currentTeam.getFirst().getType().equals(SINGLES) || currentTeam.getFirst().getType().equals(HANDICAP) || currentTeam.getFirst().getType().equals(DOUBLES) ? 5 : 3; - ; + int scoresToCount = getRoundsToCount(currentTeam.getFirst().getType()); if (currentTeam.size() > scoresToCount) { int fifthScore = currentTeam.get(scoresToCount - 1).getTotal(); @@ -411,21 +418,4 @@ private void populateAllIndividualData(Workbook workbook, String sheetName, Map< sheet.setAutoFilter(CellRangeAddress.valueOf("A1:F1")); logger.info("Individual All Scores data populated in {} ms", System.currentTimeMillis() - trueStart); } - - private int parseInteger(String number) { - try { - return Integer.parseInt(number); - } catch (NumberFormatException e) { - return 0; - } - } - - private int setStringToZero(String number) { - return number.isEmpty() ? 0 : parseInteger(number); - } - - private String trimString(String s) { - return s.trim(); - } - } diff --git a/src/main/java/trap/report/TrapHelper.java b/src/main/java/trap/report/TrapHelper.java index 8acda00..91708e8 100644 --- a/src/main/java/trap/report/TrapHelper.java +++ b/src/main/java/trap/report/TrapHelper.java @@ -13,7 +13,8 @@ import java.util.Set; public class TrapHelper { - private static final Map roundCounts = determineEventsToCount(); + private static final Map eventCounts = determineEventsToCount(); + public static final Map roundCounts = determineRoundsToCount(); public Map> calculatePlayerRoundTotals(List roundScores) { Map> playerRoundTotals = new HashMap<>(); @@ -138,18 +139,51 @@ public boolean singleRound(String roundType) { } private static Map determineEventsToCount() { + var eventCounts = new HashMap(); + eventCounts.put("singles", 4); + eventCounts.put("doubles", 4); + eventCounts.put("handicap", 4); + eventCounts.put("skeet", 4); + eventCounts.put("clays", 3); + eventCounts.put("fivestand", 4); + eventCounts.put("doublesskeet", 4); + return eventCounts; + } + + public static int getEventsToCount(String type) { + return eventCounts.getOrDefault(type, 0); // Default to 0 if type not found + } + + public static String trimString(String s) { + return s.trim(); + } + + private static Map determineRoundsToCount() { var roundCounts = new HashMap(); - roundCounts.put("singles", 4); - roundCounts.put("doubles", 4); - roundCounts.put("handicap", 4); - roundCounts.put("skeet", 4); + roundCounts.put("singles", 5); + roundCounts.put("doubles", 5); + roundCounts.put("handicap", 5); + + roundCounts.put("skeet", 3); roundCounts.put("clays", 3); - roundCounts.put("fivestand", 4); - roundCounts.put("doublesskeet", 4); + roundCounts.put("fivestand", 3); + roundCounts.put("doublesskeet", 3); return roundCounts; } - public static int getEventsToCount(String type) { + public static int getRoundsToCount(String type) { return roundCounts.getOrDefault(type, 0); // Default to 0 if type not found } + + public static int parseInteger(String number) { + try { + return Integer.parseInt(number); + } catch (NumberFormatException e) { + return 0; + } + } + + public static int setStringToZero(String number) { + return number.isEmpty() ? 0 : parseInteger(number); + } }