1 | package geniusweb.exampleparties.humangui;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.logging.Level;
|
---|
8 |
|
---|
9 | import javax.websocket.DeploymentException;
|
---|
10 |
|
---|
11 | import geniusweb.actions.Action;
|
---|
12 | import geniusweb.actions.Offer;
|
---|
13 | import geniusweb.actions.PartyId;
|
---|
14 | import geniusweb.connection.ConnectionEnd;
|
---|
15 | import geniusweb.inform.Inform;
|
---|
16 | import geniusweb.inform.Settings;
|
---|
17 | import geniusweb.issuevalue.Bid;
|
---|
18 | import geniusweb.profile.Profile;
|
---|
19 | import geniusweb.profileconnection.ProfileInterface;
|
---|
20 | import geniusweb.progress.Progress;
|
---|
21 | import geniusweb.progress.ProgressRounds;
|
---|
22 | import tudelft.utilities.listener.DefaultListenable;
|
---|
23 | import 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 | */
|
---|
37 | public 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 | }
|
---|