package design.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonTypeName; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @JsonTypeName("scramble") public class ScrambleLeague extends League { private List participants; private Map totalTeamScores; @JsonCreator private ScrambleLeague(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List participants, List schedule) { super(id, name, registrationDate, startDate, endDate, owner, schedule); this.participants = participants; this.totalTeamScores = new HashMap<>(); participants.forEach(t -> totalTeamScores.putIfAbsent(t, 0)); } public ScrambleLeague(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) { super(name, registrationDate, startDate, endDate, owner); this.participants = new ArrayList<>(); this.totalTeamScores = new HashMap<>(); } public boolean addParticipants(Team t) { boolean added = participants.add(t); if(added) totalTeamScores.putIfAbsent(t, 0); return added; } public boolean removeParticipants(Team t) { totalTeamScores.remove(t); return participants.remove(t); } public Team[] getParticipants() { return participants.toArray(Team[]::new); } public Team locateTeam(Golfer golfer) { for (Team participant : participants) { if (List.of(participant.getMembers()).contains(golfer)) { return participant; } } return null; } @Override public String getType() { return "scramble"; } @Override public void recordPlay(Golfer player, Match match, Round round){ if(!isPlayable()) return; Team team = player.getTeam(); if (team == null) return; int index = participants.indexOf(team); team.addMemberRound(player, round.getTotalSwings()); match.addRound(round); match.addRoundFor(index, round); } @Override public void finalizeLeague(){ markCompleted(); for(Team team : participants){ int score = team.computeTotalBestScore(); totalTeamScores.put(team, score); } participants.sort(Comparator.comparingInt(totalTeamScores::get)); } public Map getTotalTeamScores(){ return Collections.unmodifiableMap(totalTeamScores); } @Override public Map getResults() { return getTotalTeamScores(); } // @Override // public void finalizeMatch(Match match) { // getCompletedMatches().add(match); // for(int i = 0; i < participants.size(); i++){ // Team team = participants.get(i); // Round round = match.getRoundFor(i); // if(round != null){ // for(Golfer member : team.getMembers()){ // member.addRound(round); // } // } // } // } }