[52] | 1 | package geniusweb.protocol.session.shaop;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.LinkedList;
|
---|
| 6 | import java.util.List;
|
---|
| 7 | import java.util.stream.Collectors;
|
---|
| 8 |
|
---|
| 9 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 10 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 11 |
|
---|
| 12 | import geniusweb.deadline.Deadline;
|
---|
| 13 | import geniusweb.protocol.session.SessionProtocol;
|
---|
| 14 | import geniusweb.protocol.session.SessionSettings;
|
---|
| 15 | import geniusweb.protocol.session.TeamInfo;
|
---|
| 16 | import geniusweb.references.PartyWithProfile;
|
---|
| 17 | import tudelft.utilities.logging.Reporter;
|
---|
| 18 |
|
---|
| 19 | public class SHAOPSettings implements SessionSettings {
|
---|
| 20 | private static final int SHAOP = 0;
|
---|
| 21 | private static final int COB = 1;
|
---|
| 22 | private final List<TeamInfo> participants;
|
---|
| 23 | private final Deadline deadline;
|
---|
| 24 | private final transient List<PartyWithProfile> allParties = new LinkedList<>();
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * @param teams the {@link TeamInfo}s each containing the shaop parties
|
---|
| 28 | * and their partner cob, in that order. There must be at
|
---|
| 29 | * least 2 {@link TeamInfo}s to run the SHAOP protocol. But
|
---|
| 30 | * SHAOP can be initialized with less, for use in
|
---|
| 31 | * TournamentSettings.
|
---|
| 32 | * @param deadline the deadline settings for the session
|
---|
| 33 | */
|
---|
| 34 | @JsonCreator
|
---|
| 35 | public SHAOPSettings(@JsonProperty("participants") List<TeamInfo> teams,
|
---|
| 36 | @JsonProperty("deadline") Deadline deadline) {
|
---|
| 37 | if (teams == null)
|
---|
| 38 | throw new IllegalArgumentException("participants must not be null");
|
---|
| 39 | if (deadline == null)
|
---|
| 40 | throw new IllegalArgumentException("deadline must not be null");
|
---|
| 41 | this.participants = teams;
|
---|
| 42 | this.deadline = deadline;
|
---|
| 43 | // notice, order here is crucial because of way #isShaopParty works.
|
---|
| 44 | for (TeamInfo team : teams) {
|
---|
| 45 | if (team.getSize() != 2)
|
---|
| 46 | throw new IllegalArgumentException(
|
---|
| 47 | "Team must be size 2 but found " + team);
|
---|
| 48 | allParties.add(team.getParties().get(SHAOP));
|
---|
| 49 | allParties.add(team.getParties().get(COB));
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
| 54 | public Double getMaxRunTime() {
|
---|
| 55 | return deadline.getDuration() / 1000d;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public SessionProtocol getProtocol(Reporter logger) {
|
---|
| 60 | return new SHAOP(new SHAOPState(this), logger);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
| 64 | public List<TeamInfo> getTeams() {
|
---|
| 65 | // cast needed...
|
---|
| 66 | return new ArrayList<TeamInfo>(participants);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * @return the deadline for this negotiation
|
---|
| 71 | */
|
---|
| 72 | public Deadline getDeadline() {
|
---|
| 73 | return deadline;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | *
|
---|
| 78 | * @return list of all parties, the even parties (0=first element in list)
|
---|
| 79 | * being the SHAOP party and the next in the list the partner COB
|
---|
| 80 | * party.
|
---|
| 81 | */
|
---|
| 82 | @Override
|
---|
| 83 | public List<PartyWithProfile> getAllParties() {
|
---|
| 84 | return participants.stream().map(team -> team.getParties())
|
---|
| 85 | .flatMap(Collection::stream).collect(Collectors.toList());
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public SessionSettings with(TeamInfo team) {
|
---|
| 90 | List<TeamInfo> newparts = new LinkedList<>(participants);
|
---|
| 91 | newparts.add(team);
|
---|
| 92 | return new SHAOPSettings(newparts, deadline);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Override
|
---|
| 96 | public Integer getTeamSize() {
|
---|
| 97 | return 2;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Override
|
---|
| 101 | public int hashCode() {
|
---|
| 102 | final int prime = 31;
|
---|
| 103 | int result = 1;
|
---|
| 104 | result = prime * result
|
---|
| 105 | + ((deadline == null) ? 0 : deadline.hashCode());
|
---|
| 106 | result = prime * result
|
---|
| 107 | + ((participants == null) ? 0 : participants.hashCode());
|
---|
| 108 | return result;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | public boolean equals(Object obj) {
|
---|
| 113 | if (this == obj)
|
---|
| 114 | return true;
|
---|
| 115 | if (obj == null)
|
---|
| 116 | return false;
|
---|
| 117 | if (getClass() != obj.getClass())
|
---|
| 118 | return false;
|
---|
| 119 | SHAOPSettings other = (SHAOPSettings) obj;
|
---|
| 120 | if (deadline == null) {
|
---|
| 121 | if (other.deadline != null)
|
---|
| 122 | return false;
|
---|
| 123 | } else if (!deadline.equals(other.deadline))
|
---|
| 124 | return false;
|
---|
| 125 | if (participants == null) {
|
---|
| 126 | if (other.participants != null)
|
---|
| 127 | return false;
|
---|
| 128 | } else if (!participants.equals(other.participants))
|
---|
| 129 | return false;
|
---|
| 130 | return true;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | @Override
|
---|
| 134 | public String toString() {
|
---|
| 135 | return "SHAOPSettings" + participants + "," + deadline + "]";
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | }
|
---|