source: protocol/src/main/java/geniusweb/protocol/session/shaop/BareSHAOPState.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 7.2 KB
Line 
1package geniusweb.protocol.session.shaop;
2
3import java.util.Arrays;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Map;
9import java.util.stream.Collectors;
10
11import com.fasterxml.jackson.annotation.JsonCreator;
12
13import geniusweb.actions.Action;
14import geniusweb.actions.PartyId;
15import geniusweb.progress.Progress;
16import geniusweb.protocol.ProtocolException;
17import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
18import geniusweb.protocol.session.SessionResult;
19import geniusweb.protocol.session.SessionSettings;
20import geniusweb.protocol.session.SessionState;
21import geniusweb.references.PartyWithProfile;
22
23/**
24 * The state without all the "with" functioality to make this more readable.
25 * immutable. json-serializable.
26 */
27public abstract class BareSHAOPState implements SessionState {
28
29 protected final List<Action> actions;
30
31 protected final transient ProtocolToPartyConnections connections;
32
33 protected final Progress progress;
34
35 protected final SHAOPSettings settings;
36
37 protected final ProtocolException error;
38
39 protected final int teamNr;
40
41 protected final Map<PartyId, Integer> partyNumbers;
42
43 protected final Map<PartyId, Double> totalSpent;
44
45 /**
46 *
47 * @param actions value for actions done so far. null equals to empty list
48 * @param conns the currently existing connections. Can be empty/null.
49 * Each connection represents another party. Normally the
50 * connections are in the order SHAOP1,COB1,SHAOP2,COB2,..
51 * so 2 parties for each team. The protocol should check
52 * this.
53 * @param progr the {@link Progress} that governs this session. Can be
54 * null if session did not yet start.
55 * @param settings the settings used for the session
56 * @param e the exception that occured, usually null. All errors
57 * occuring due to faulty {@link Action}s translate to
58 * {@link ProtocolException}s. All errors in our own code
59 * are bugs (not ProtocolExceptions) and should result in a
60 * throw.
61 * @param teamNr the teamnr (0,1,2..) that has the turn. 2* gives index
62 * {@link #connections} and settings
63 * @param partyNrs a map for each known PartyId to a number. The number is
64 * the index in both {@link SHAOPSettings#getTeams()} and in
65 * {@link #connections}. null is empyt map.
66 * @param spent total accumulated elicitation costs so far for each
67 * party. only SHAOP parties accumulate costs. null = empty
68 * map
69 */
70 @JsonCreator
71 public BareSHAOPState(List<Action> actions,
72 ProtocolToPartyConnections conns, Progress progr,
73 SHAOPSettings settings, ProtocolException e, int teamNr,
74 Map<PartyId, Integer> partyNrs, Map<PartyId, Double> spent) {
75 if (conns == null) {
76 this.connections = new ProtocolToPartyConnections(
77 Collections.emptyList());
78 } else {
79 this.connections = conns;
80 }
81 if (actions == null) {
82 this.actions = new LinkedList<>();
83 } else {
84 this.actions = actions;
85 }
86 if (partyNrs == null) {
87 this.partyNumbers = Collections.emptyMap();
88 } else {
89 this.partyNumbers = partyNrs;
90 }
91 if (spent == null) {
92 this.totalSpent = Collections.emptyMap();
93 } else {
94 this.totalSpent = spent;
95 }
96
97 if (!connections.allunique()) {
98 throw new IllegalArgumentException(
99 "There can not be multiple connections for a party:"
100 + conns);
101 }
102
103 this.progress = progr;
104 this.settings = settings;
105 this.error = e;
106 this.teamNr = teamNr;
107 }
108
109 public ProtocolToPartyConnections getConnections() {
110 return connections;
111 }
112
113 /**
114 *
115 * @return party ID of current team leader (SHAOP party)
116 */
117 public PartyId getCurrentTeam() {
118 return connections.get(2 * teamNr).getParty();
119 }
120
121 /**
122 * @param id the {@link PartyId}
123 * @return the PartyWithProfile associated with that id.
124 */
125 public PartyWithProfile getPartyProfile(PartyId id) {
126 return settings.getAllParties().get(partyNumbers.get(id));
127 }
128
129 /**
130 *
131 * @return the index of each party
132 */
133 public Map<PartyId, Integer> getPartyNumbers() {
134 return Collections.unmodifiableMap(partyNumbers);
135 }
136
137 /**
138 *
139 * @return unmodifyable list of actions done so far.
140 */
141 @Override
142 public List<Action> getActions() {
143 return Collections.unmodifiableList(actions);
144 }
145
146 @Override
147 public Progress getProgress() {
148 return progress;
149 }
150
151 @Override
152 public SessionSettings getSettings() {
153 return settings;
154 }
155
156 @Override
157 public boolean isFinal(long currentTimeMs) {
158 return error != null
159 || (progress != null && progress.isPastDeadline(currentTimeMs));
160 }
161
162 public ProtocolException getError() {
163 return error;
164 }
165
166 @Override
167 public String toString() {
168 return this.getClass().getSimpleName() + "[" + actions + ","
169 + connections + "," + progress + "," + settings + "," + error
170 + "]";
171 }
172
173 @Override
174 public List<SessionResult> getResults() {
175 Map<PartyId, Double> penalties = new HashMap<>();
176 for (PartyId party : partyNumbers.keySet()) {
177 Double spent = totalSpent.get(party);
178 if (spent == null) {
179 spent = 0d;
180 } else {
181 spent = Math.max(0, Math.min(1.0, spent));
182 }
183 penalties.put(party, spent);
184 }
185 Map<PartyId, PartyWithProfile> allparties = partyNumbers.keySet()
186 .stream().collect(Collectors.toMap(pid -> pid, pid -> settings
187 .getAllParties().get(partyNumbers.get(pid))));
188 return Arrays.asList(new SessionResult(allparties, getAgreements(),
189 penalties, getError()));
190
191 }
192
193 @Override
194 public int hashCode() {
195 final int prime = 31;
196 int result = 1;
197 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
198 result = prime * result + ((error == null) ? 0 : error.hashCode());
199 result = prime * result
200 + ((partyNumbers == null) ? 0 : partyNumbers.hashCode());
201 result = prime * result
202 + ((progress == null) ? 0 : progress.hashCode());
203 result = prime * result
204 + ((settings == null) ? 0 : settings.hashCode());
205 result = prime * result + teamNr;
206 result = prime * result
207 + ((totalSpent == null) ? 0 : totalSpent.hashCode());
208 return result;
209 }
210
211 @Override
212 public boolean equals(Object obj) {
213 if (this == obj)
214 return true;
215 if (obj == null)
216 return false;
217 if (getClass() != obj.getClass())
218 return false;
219 BareSHAOPState other = (BareSHAOPState) obj;
220 if (actions == null) {
221 if (other.actions != null)
222 return false;
223 } else if (!actions.equals(other.actions))
224 return false;
225 if (error == null) {
226 if (other.error != null)
227 return false;
228 } else if (!error.equals(other.error))
229 return false;
230 if (partyNumbers == null) {
231 if (other.partyNumbers != null)
232 return false;
233 } else if (!partyNumbers.equals(other.partyNumbers))
234 return false;
235 if (progress == null) {
236 if (other.progress != null)
237 return false;
238 } else if (!progress.equals(other.progress))
239 return false;
240 if (settings == null) {
241 if (other.settings != null)
242 return false;
243 } else if (!settings.equals(other.settings))
244 return false;
245 if (teamNr != other.teamNr)
246 return false;
247 if (totalSpent == null) {
248 if (other.totalSpent != null)
249 return false;
250 } else if (!totalSpent.equals(other.totalSpent))
251 return false;
252 return true;
253 }
254
255}
Note: See TracBrowser for help on using the repository browser.