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

Last change on this file since 11 was 11, checked in by bart, 4 years ago

Tournament overview table, few bug fixes

File size: 4.8 KB
Line 
1package geniusweb.protocol.session.shaop;
2
3import java.util.Collections;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.Map;
7
8import com.fasterxml.jackson.annotation.JsonCreator;
9
10import geniusweb.actions.Action;
11import geniusweb.actions.PartyId;
12import geniusweb.progress.Progress;
13import geniusweb.protocol.ProtocolException;
14import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
15import geniusweb.protocol.session.SessionSettings;
16import geniusweb.protocol.session.SessionState;
17import geniusweb.references.PartyWithProfile;
18
19/**
20 * The state without all the "with" functioality to make this more readable.
21 * immutable. json-serializable.
22 */
23public abstract class BareSHAOPState implements SessionState {
24 protected final List<Action> actions;
25
26 protected final transient ProtocolToPartyConnections connections;
27
28 protected final Progress progress;
29
30 protected final SHAOPSettings settings;
31
32 protected final ProtocolException error;
33
34 protected final int teamNr;
35
36 protected final Map<PartyId, Integer> partyNumbers;
37
38 protected final Map<PartyId, Double> totalSpent;
39
40 /**
41 *
42 * @param actions value for actions done so far. null equals to empty
43 * list
44 * @param conns the currently existing connections. Can be empty/null.
45 * Each connection represents another party. Normally the
46 * connections are in the order SHAOP1,COB1,SHAOP2,COB2,..
47 * so 2 parties for each team. The protocol should check
48 * this.
49 * @param progr the {@link Progress} that governs this session. Can be
50 * null if session did not yet start.
51 * @param settings the settings used for the session
52 * @param e the exception that occured, usually null. All errors
53 * occuring due to faulty {@link Action}s translate to
54 * {@link ProtocolException}s. All errors in our own code
55 * are bugs (not ProtocolExceptions) and should result in
56 * a throw.
57 * @param teamNr the teamnr (0,1,2..) that has the turn. 2* gives index
58 * {@link #connections} and settings
59 * @param partyNrs a map for each known PartyId to a number. The number is
60 * the index in both {@link SHAOPSettings#getTeams()} and
61 * in {@link #connections}. null is empyt map.
62 * @param totalSpent total accumulated elicitation costs so far. only SHAOP
63 * parties accumulate costs. null = empty map
64 */
65 @JsonCreator
66 public BareSHAOPState(List<Action> actions,
67 ProtocolToPartyConnections conns, Progress progr,
68 SHAOPSettings settings, ProtocolException e, int teamNr,
69 Map<PartyId, Integer> partyNrs, Map<PartyId, Double> spent) {
70 if (conns == null) {
71 this.connections = new ProtocolToPartyConnections(
72 Collections.emptyList());
73 } else {
74 this.connections = conns;
75 }
76 if (actions == null) {
77 this.actions = new LinkedList<>();
78 } else {
79 this.actions = actions;
80 }
81 if (partyNrs == null) {
82 this.partyNumbers = Collections.emptyMap();
83 } else {
84 this.partyNumbers = partyNrs;
85 }
86 if (spent == null) {
87 this.totalSpent = Collections.emptyMap();
88 } else {
89 this.totalSpent = spent;
90 }
91
92 if (!connections.allunique()) {
93 throw new IllegalArgumentException(
94 "There can not be multiple connections for a party:"
95 + conns);
96 }
97
98 this.progress = progr;
99 this.settings = settings;
100 this.error = e;
101 this.teamNr = teamNr;
102 }
103
104 public ProtocolToPartyConnections getConnections() {
105 return connections;
106 }
107
108 /**
109 *
110 * @return party ID of current team leader (SHAOP party)
111 */
112 public PartyId getCurrentTeam() {
113 return connections.get(2 * teamNr).getParty();
114 }
115
116 /**
117 * @param id the {@link PartyId}
118 * @return the PartyWithProfile associated with that id.
119 */
120 public PartyWithProfile getPartyProfile(PartyId id) {
121 return settings.getAllParties().get(partyNumbers.get(id));
122 }
123
124 /**
125 *
126 * @return the index of each party
127 */
128 public Map<PartyId, Integer> getPartyNumbers() {
129 return Collections.unmodifiableMap(partyNumbers);
130 }
131
132 /**
133 *
134 * @return unmodifyable list of actions done so far.
135 */
136 @Override
137 public List<Action> getActions() {
138 return Collections.unmodifiableList(actions);
139 }
140
141 @Override
142 public Progress getProgress() {
143 return progress;
144 }
145
146 @Override
147 public SessionSettings getSettings() {
148 return settings;
149 }
150
151 @Override
152 public boolean isFinal(long currentTimeMs) {
153 return error != null
154 || (progress != null && progress.isPastDeadline(currentTimeMs));
155 }
156
157 @Override
158 public ProtocolException getError() {
159 return error;
160 }
161
162 @Override
163 public String toString() {
164 return this.getClass().getSimpleName() + "[" + actions + ","
165 + connections + "," + progress + "," + settings + "," + error
166 + "]";
167 }
168
169}
Note: See TracBrowser for help on using the repository browser.