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