Skip to content

Commit

Permalink
clean up top scores
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbusche committed Sep 30, 2024
1 parent ac1bc52 commit bddb140
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 44 deletions.
62 changes: 26 additions & 36 deletions src/main/java/trap/report/ReportHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();

Expand Down Expand Up @@ -181,26 +186,29 @@ private void populateCleanData(Sheet sheet, List<RoundScore> allRoundScores) {
private List<TeamScore> getTeamScores(List<Map.Entry<String, ArrayList<IndividualTotal>>> teamData) {
var teamScoresThatCount = new HashMap<String, TeamScore>();
for (Map.Entry<String, ArrayList<IndividualTotal>> 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<String, ArrayList<IndividualTotal>> 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<String, ArrayList<IndividualTotal>> 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();
}

Expand Down Expand Up @@ -345,8 +353,7 @@ private HashMap<String, ArrayList<IndividualTotal>> calculateTeamScores(List<Ind
currentTeam.sort((a, b) -> 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();

Expand Down Expand Up @@ -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();
}

}
50 changes: 42 additions & 8 deletions src/main/java/trap/report/TrapHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import java.util.Set;

public class TrapHelper {
private static final Map<String, Integer> roundCounts = determineEventsToCount();
private static final Map<String, Integer> eventCounts = determineEventsToCount();
public static final Map<String, Integer> roundCounts = determineRoundsToCount();

public Map<String, List<RoundTotal>> calculatePlayerRoundTotals(List<RoundScore> roundScores) {
Map<String, List<RoundTotal>> playerRoundTotals = new HashMap<>();
Expand Down Expand Up @@ -138,18 +139,51 @@ public boolean singleRound(String roundType) {
}

private static Map<String, Integer> determineEventsToCount() {
var eventCounts = new HashMap<String, Integer>();
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<String, Integer> determineRoundsToCount() {
var roundCounts = new HashMap<String, Integer>();
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);
}
}

0 comments on commit bddb140

Please sign in to comment.