source: exampleparties/humangui/src/main/java/geniusweb/exampleparties/humangui/BiddingInfo.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: 4.3 KB
Line 
1package geniusweb.exampleparties.humangui;
2
3import java.io.IOException;
4import java.util.Collections;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.logging.Level;
8
9import javax.websocket.DeploymentException;
10
11import geniusweb.actions.Action;
12import geniusweb.actions.Offer;
13import geniusweb.actions.PartyId;
14import geniusweb.connection.ConnectionEnd;
15import geniusweb.inform.Inform;
16import geniusweb.inform.Settings;
17import geniusweb.issuevalue.Bid;
18import geniusweb.profile.Profile;
19import geniusweb.profileconnection.ProfileInterface;
20import geniusweb.progress.Progress;
21import geniusweb.progress.ProgressRounds;
22import tudelft.utilities.listener.DefaultListenable;
23import tudelft.utilities.logging.Reporter;
24
25/**
26 * Contains the current state of the user's negotiation. mutable. Assumes SAOP
27 * protocol.
28 * <p>
29 * Listeners receive the following objects:
30 * <ul>
31 * <li>{@link Action} if user did an action (place offer, end nego, etc)
32 * <li>{@link Bid} if user changed his prepared bid (but did not yet offer it)
33 * <li>{@link Profile} if the profile was changed
34 * <li>{@link Boolean} if the isMyTurn value changed.
35 * </ul>
36 */
37public class BiddingInfo extends DefaultListenable<Object> {
38 private final PartyId me;
39 private Offer lastReceivedOffer = null;
40 private Progress progress = null;
41 private Reporter reporter;
42
43 // profile is the latest one received.
44 private Profile profile = null;
45
46 // same as profile.getDomain but forces order on the issues
47 // unmodifyable (but we can replace the whole list)
48 private List<String> issues = Collections.emptyList();
49 private Bid currentBid = new Bid(Collections.emptyMap());
50 private boolean isMyTurn = false;
51 private ConnectionEnd<Inform, Action> connection;
52
53 /**
54 *
55 * @param settings the session {@link Settings}
56 * @param connection the connection to the protocol.
57 * @param reporter the {@link Reporter} where we can log issues.
58 * @param profileint the {@link ProfileInterface}
59 * @throws IOException if we can not reach the protocol server
60 * @throws DeploymentException if we can not reach the protocol server
61 */
62 public BiddingInfo(Settings settings,
63 ConnectionEnd<Inform, Action> connection, Reporter reporter,
64 ProfileInterface profileint)
65 throws IOException, DeploymentException {
66 this.connection = connection;
67 this.me = settings.getID();
68 this.progress = settings.getProgress();
69 this.reporter = reporter;
70
71 profileint.addListener(prof -> updateProfile(prof));
72 // this call blocks till we have the profile.
73 updateProfile(profileint.getProfile());
74 }
75
76 /**
77 *
78 * @return the profile currently used.
79 */
80 public Profile getProfile() {
81 return profile;
82 }
83
84 /**
85 * Changes the model to use the given new profile.
86 *
87 * @param newprof a new profile
88 */
89 private void updateProfile(Profile newprof) {
90 this.profile = newprof;
91 this.issues = Collections.unmodifiableList(
92 new LinkedList<String>(newprof.getDomain().getIssues()));
93 notifyListeners(newprof);
94 }
95
96 /**
97 * executes an action and sets isMyTurn to false.
98 *
99 * @param action the action to execute.
100 */
101 public void doAction(Action action) {
102 if (!isMyTurn) {
103 reporter.log(Level.SEVERE, "User did action but it's not his turn");
104 return;
105 }
106 try {
107 connection.send(action);
108 } catch (IOException e) {
109 reporter.log(Level.SEVERE, "Can't send action", e);
110 return;
111 }
112 if (progress instanceof ProgressRounds) {
113 progress = ((ProgressRounds) progress).advance();
114 }
115 setMyTurn(false);
116 // notifyListeners(action);
117 }
118
119 /**
120 * @param offer offer received from some other party
121 */
122 public void receivedOffer(Offer offer) {
123 lastReceivedOffer = offer;
124 notifyListeners(offer);
125 }
126
127 /**
128 *
129 * @return the last received offer, or null if no offer received yet.
130 */
131 public Offer getReceivedOffer() {
132 return lastReceivedOffer;
133 }
134
135 public List<String> getIssues() {
136 return issues;
137 }
138
139 /**
140 *
141 * @return current bid. Initially is an empty bid.
142 */
143 public Bid getCurrentBid() {
144 return currentBid;
145 }
146
147 public void setCurrentBid(Bid bid) {
148 if (bid == null)
149 throw new IllegalArgumentException();
150 if (!bid.equals(currentBid)) {
151 currentBid = bid;
152 notifyListeners(bid);
153 }
154 }
155
156 public PartyId getMyId() {
157 return me;
158 }
159
160 public void setMyTurn(boolean b) {
161 if (isMyTurn != b) {
162 isMyTurn = b;
163 notifyListeners(b);
164 }
165 }
166
167}
Note: See TracBrowser for help on using the repository browser.