1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
package com.buckosoft.fibs.BuckoFIBS.gui; |
45 | |
|
46 | |
import java.text.SimpleDateFormat; |
47 | |
import java.util.List; |
48 | |
|
49 | |
import javax.swing.JScrollPane; |
50 | |
import javax.swing.JTree; |
51 | |
import javax.swing.tree.DefaultMutableTreeNode; |
52 | |
import javax.swing.tree.DefaultTreeModel; |
53 | |
import javax.swing.tree.TreeNode; |
54 | |
import javax.swing.tree.TreePath; |
55 | |
import javax.swing.tree.TreeSelectionModel; |
56 | |
|
57 | |
import com.buckosoft.fibs.BuckoFIBS.CommandDispatcher; |
58 | |
import com.buckosoft.fibs.BuckoFIBS.db.Database; |
59 | |
import com.buckosoft.fibs.domain.FinishedMatch; |
60 | |
import com.buckosoft.fibs.domain.Player; |
61 | |
import com.buckosoft.fibs.net.FIBSMessages; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public class PlayerReportPane extends JScrollPane implements FIBSMessages { |
69 | |
private final static boolean DEBUG = true; |
70 | |
private static final long serialVersionUID = 1L; |
71 | |
private CommandDispatcher commandDispatcher; |
72 | |
private Database db; |
73 | |
|
74 | 0 | private JTree tree = null; |
75 | 0 | private DefaultTreeModel treeModel = null; |
76 | 0 | private Node topNode = new Node("Player:"); |
77 | 0 | private Node repbotNode = null; |
78 | 0 | private Node whoisNode = null; |
79 | 0 | private Node recordNode = null; |
80 | 0 | private Node savedNode = null; |
81 | 0 | private TreePath pathToRepbot = null; |
82 | 0 | private TreePath pathToWhois = null; |
83 | 0 | private TreePath pathToRecord = null; |
84 | |
|
85 | 0 | SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yy - HH:mm"); |
86 | |
|
87 | |
private String playerName; |
88 | |
|
89 | 0 | private String[] commands = { |
90 | |
"whois", |
91 | |
"show savedcount", |
92 | |
"Tell repbot ask", |
93 | |
"Tell repbot list" |
94 | |
}; |
95 | 0 | private int nextCommand = 0; |
96 | |
|
97 | |
|
98 | |
|
99 | |
public PlayerReportPane() { |
100 | 0 | super(); |
101 | |
|
102 | 0 | initialize(); |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
class Node extends DefaultMutableTreeNode { |
109 | |
private static final long serialVersionUID = 1L; |
110 | 0 | Node() { super(); } |
111 | 0 | Node(Object o) { super(o); } |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void setCommandDispatcher(CommandDispatcher commandDispatcher) { |
118 | 0 | this.commandDispatcher = commandDispatcher; |
119 | 0 | } |
120 | |
|
121 | |
public void setDatabase(Database database) { |
122 | 0 | this.db = database; |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void setPlayer(String playerName) { |
129 | 0 | this.playerName = playerName; |
130 | 0 | topNode.setUserObject("Player: " + playerName); |
131 | 0 | topNode.removeAllChildren(); |
132 | 0 | this.repbotNode.removeAllChildren(); |
133 | 0 | this.whoisNode.removeAllChildren(); |
134 | 0 | this.recordNode = null; |
135 | 0 | pathToRecord = null; |
136 | 0 | createNodes(topNode); |
137 | 0 | updateGui(); |
138 | 0 | nextCommand = 0; |
139 | 0 | sendNextCommand(); |
140 | 0 | parseRecord(); |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void receiveLine(String s, int cookie) { |
148 | 0 | System.out.println("PlayerReport..line " + s); |
149 | |
|
150 | 0 | if (s.startsWith("12 RepBotNG ")) |
151 | 0 | parseRepbot(s); |
152 | 0 | else if (cookie == FIBS_HasSavedGames || cookie == FIBS_HasNoSavedGames) |
153 | 0 | parseSavedGames(s); |
154 | |
else |
155 | 0 | parseWhois(s, cookie); |
156 | 0 | } |
157 | |
|
158 | |
private void parseSavedGames(String s) { |
159 | 0 | int i = s.indexOf(' '); |
160 | 0 | s = s.substring(i+4); |
161 | 0 | if (savedNode == null) |
162 | 0 | savedNode = new Node(s); |
163 | |
else |
164 | 0 | savedNode.setUserObject(s); |
165 | 0 | topNode.add(savedNode); |
166 | 0 | sendNextCommand(); |
167 | 0 | } |
168 | |
|
169 | |
private void parseRepbot(String s) { |
170 | 0 | System.out.println("parseRepbot '" + s + "'"); |
171 | 0 | s = s.substring(12); |
172 | 0 | if (s.startsWith("You have")) { |
173 | 0 | this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR, |
174 | |
"RepBot says: " + s); |
175 | 0 | return; |
176 | |
} |
177 | 0 | int i = s.indexOf('\''); |
178 | 0 | if (i != -1) { |
179 | 0 | String name = s.substring(0, i); |
180 | 0 | if (!name.equals(this.playerName)) { |
181 | 0 | String t = "Repbot.. NOT player: '" + s + "'"; |
182 | |
if (DEBUG) |
183 | 0 | System.out.println(t); |
184 | 0 | this.commandDispatcher.writeSystemMessageln(t); |
185 | 0 | return; |
186 | |
} |
187 | 0 | s = s.substring(i+3); |
188 | 0 | if (s.startsWith("reputation")) { |
189 | 0 | parseRepbotReputation(s); |
190 | 0 | sendNextCommand(); |
191 | |
} else |
192 | 0 | parseRepbotOther(s); |
193 | 0 | return; |
194 | |
} |
195 | 0 | if (s.startsWith("User ")) { |
196 | 0 | parseRepbotReputation(s); |
197 | 0 | return; |
198 | |
} |
199 | 0 | if (this.playerName != null && s.startsWith(this.playerName)) { |
200 | 0 | s = s.substring(this.playerName.length() + 1); |
201 | 0 | parseRepbotOther(s); |
202 | 0 | return; |
203 | |
} |
204 | |
if (DEBUG) |
205 | 0 | System.out.println("Repbot.. busted line: '" + s + "'"); |
206 | 0 | this.commandDispatcher.writeSystemMessageln("Repbot: " + s); |
207 | 0 | } |
208 | |
|
209 | |
private void parseRepbotReputation(String s) { |
210 | 0 | repbotNode.setUserObject("RepBot " + s); |
211 | 0 | updateGui(); |
212 | 0 | } |
213 | |
|
214 | |
private void parseRepbotOther(String s) { |
215 | |
if (DEBUG) |
216 | 0 | System.out.println("Repot other: = '" + s + "'"); |
217 | 0 | repbotNode.add(new Node(s)); |
218 | 0 | updateGui(); |
219 | |
|
220 | 0 | } |
221 | |
|
222 | |
private void parseWhois(String s, int cookie) { |
223 | 0 | if (cookie == FIBS_PlayerInfoStart) |
224 | 0 | whoisNode.setUserObject("whois " + s); |
225 | |
else |
226 | 0 | whoisNode.add(new Node(s)); |
227 | 0 | updateGui(); |
228 | 0 | if (cookie == FIBS_NoEmail || cookie == FIBS_EmailAddress) |
229 | 0 | sendNextCommand(); |
230 | 0 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
private void parseRecord() { |
235 | 0 | Player p = this.db.getPlayer(this.playerName); |
236 | 0 | if (p == null) |
237 | 0 | return; |
238 | 0 | List<FinishedMatch> matches = db.getFinishedMatches(p.getId()); |
239 | 0 | if (matches.size() == 0) |
240 | 0 | return; |
241 | 0 | if (recordNode == null) |
242 | 0 | recordNode = new Node(); |
243 | |
else |
244 | 0 | recordNode.removeAllChildren(); |
245 | 0 | int w = 0; |
246 | 0 | int l = 0; |
247 | 0 | for (FinishedMatch fm : matches) { |
248 | 0 | StringBuffer sb = new StringBuffer(); |
249 | 0 | if (fm.getYourScore() > fm.getOpponentScore()) |
250 | 0 | w++; |
251 | |
else |
252 | 0 | l++; |
253 | 0 | sb.append("" + fm.getYourScore()); |
254 | 0 | sb.append("-"); |
255 | 0 | sb.append("" + fm.getOpponentScore()); |
256 | |
|
257 | 0 | sb.append(";"); |
258 | 0 | sb.append("" + fm.getMatchPoints()); |
259 | 0 | sb.append(", "); |
260 | 0 | sb.append(sdf.format(fm.getDate())); |
261 | 0 | int d = fm.getDuration(); |
262 | 0 | int m = d/60; |
263 | 0 | int s = d-m*60; |
264 | 0 | sb.append(" t=" + m + ":" + s); |
265 | |
|
266 | |
|
267 | 0 | recordNode.add(new Node(sb.toString())); |
268 | 0 | } |
269 | 0 | recordNode.setUserObject("" + w + "-" + l); |
270 | 0 | this.topNode.add(recordNode); |
271 | 0 | this.pathToRecord = new TreePath(treeModel.getPathToRoot(this.recordNode)); |
272 | 0 | } |
273 | |
|
274 | |
private void sendNextCommand() { |
275 | 0 | if (nextCommand >= commands.length) |
276 | 0 | return; |
277 | 0 | String s = commands[nextCommand++] + " " + this.playerName; |
278 | 0 | this.commandDispatcher.dispatch(CommandDispatcher.Command.SEND_COMMAND, s); |
279 | 0 | } |
280 | |
|
281 | |
private void updateGui() { |
282 | 0 | treeModel.reload(); |
283 | 0 | if (pathToRecord != null) |
284 | 0 | tree.expandPath(pathToRecord); |
285 | 0 | tree.expandPath(pathToRepbot); |
286 | 0 | tree.expandPath(pathToWhois); |
287 | 0 | } |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
private void initialize() { |
294 | 0 | this.setViewportView(getTree()); |
295 | 0 | } |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
private JTree getTree() { |
303 | 0 | if (tree == null) { |
304 | 0 | treeModel = new DefaultTreeModel(topNode); |
305 | 0 | createNodes(topNode); |
306 | 0 | tree = new JTree(treeModel); |
307 | 0 | tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
308 | |
} |
309 | 0 | return tree; |
310 | |
} |
311 | |
|
312 | |
private void createNodes(DefaultMutableTreeNode top) { |
313 | |
|
314 | 0 | repbotNode = new Node("RepBot:"); |
315 | 0 | top.add(repbotNode); |
316 | 0 | if (whoisNode == null) |
317 | 0 | whoisNode = new Node("whois:"); |
318 | 0 | top.add(whoisNode); |
319 | 0 | TreeNode[] tn = treeModel.getPathToRoot(this.repbotNode); |
320 | 0 | pathToRepbot = new TreePath(tn); |
321 | 0 | this.pathToWhois = new TreePath(treeModel.getPathToRoot(this.whoisNode)); |
322 | |
|
323 | 0 | } |
324 | |
} |