summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/ScrambleLeague.java
blob: f5d17eed428c82e7eab5b13dd6a6302779cc5c78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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<Team> participants;
    private Map<Team, Integer> totalTeamScores;

    @JsonCreator
    private ScrambleLeague(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Team> participants, List<Match> 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 strokes = round.getTotalSwings();
        team.addMemberRound(player, strokes);
        match.addRound(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<Team, Integer> getTotalTeamScores(){
        return Collections.unmodifiableMap(totalTeamScores);
    }

    @Override
    public Map<Team, Integer> 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);
                }
            }
        }
    }
}