View Javadoc
1   /******************************************************************************
2    * PlayerReportPane.java - 
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.2  2010/03/03 12:19:49  inim
10   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
11   *
12   * Revision 1.1  2010/02/04 05:57:53  inim
13   * Mavenized project folder layout
14   *
15   * Revision 1.3  2010/01/23 06:16:41  dick
16   * Better RepBot parsing.
17   *
18   * Revision 1.2  2009/03/12 15:28:43  dick
19   * PlayerReportPane looks good.
20   *
21   * Revision 1.1  2009/03/04 19:05:24  dick
22   * Display the panel with what we know about a player.
23   *
24   */
25  
26  /* 
27   * This program is free software: you can redistribute it and/or modify
28   * it under the terms of the GNU General Public License as published by
29   * the Free Software Foundation, either version 3 of the License, or
30   * (at your option) any later version.
31   *
32   * This program is distributed in the hope that it will be useful,
33   * but WITHOUT ANY WARRANTY; without even the implied warranty of
34   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35   * GNU General Public License for more details.
36   *
37   * You should have received a copy of the GNU General Public License
38   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39   *
40   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
41   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
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  /** Display the panel with what we know about a player.
64   * @author Dick Balaska
65   * @since Mar 2, 2009
66   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/PlayerReportPane.java">cvs PlayerReportPane.java</a>
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;  //  @jve:decl-index=0:
72  	private	Database			db;
73  	
74  	private	JTree		tree = null;
75  	private	DefaultTreeModel	treeModel = null;
76  	private Node		topNode = new Node("Player:");  //  @jve:decl-index=0:
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  	/** Create a PlayerReportPane
98  	 */
99  	public PlayerReportPane() {
100 		super();
101 		
102 		initialize();
103 	}
104 
105 	/** I just don't want to type that long name... 
106 	 * If i could type #define Node DefaultMutableTreeNode, i would.
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 	/** Set the reference to the command dispatcher
115 	 * @param commandDispatcher Our system CommandDispatcher
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 	/** Set the Player that we will be looking up.
126 	 * @param playerName
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 	/** Receive a line from FIBS.
144 	 * @param s
145 	 * @param cookie
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);		// Scrape off '12 RepBotNG '
172 		if (s.startsWith("You have")) {
173 			this.commandDispatcher.writeSystemMessageln(SystemMessagesTextPane.ERROR,
174 					"RepBot says: " + s);
175 			return;
176 		}
177 		int i = s.indexOf('\''); 	// "Player's vouchers..."
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 	/** Not really a parse, because we know all the info
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 			//sb.append("  1-2,  13 Aug 03 02:34");
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 			//sb.append("  --" + d);
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 	 * This method initializes this
291 	 * 
292 	 */
293 	private void initialize() {
294         this.setViewportView(getTree());
295 	}
296 
297 	/**
298 	 * This method initializes jTree	
299 	 * 	
300 	 * @return javax.swing.JTree	
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 		//if (repbotNode == null)
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 }