Coverage Report - com.buckosoft.fibs.BuckoFIBS.gui.PlayerReportPane
 
Classes in this File Line Coverage Branch Coverage Complexity
PlayerReportPane
0%
0/152
0%
0/46
2.889
PlayerReportPane$Node
0%
0/2
N/A
2.889
 
 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  0
         private        JTree                tree = null;
 75  0
         private        DefaultTreeModel        treeModel = null;
 76  0
         private Node                topNode = new Node("Player:");  //  @jve:decl-index=0:
 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  
         /** Create a PlayerReportPane
 98  
          */
 99  
         public PlayerReportPane() {
 100  0
                 super();
 101  
                 
 102  0
                 initialize();
 103  0
         }
 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  0
                 Node() { super(); }
 111  0
                 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  0
                 this.commandDispatcher = commandDispatcher;
 119  0
         }
 120  
 
 121  
         public void setDatabase(Database database) {
 122  0
                 this.db = database;
 123  0
         }
 124  
 
 125  
         /** Set the Player that we will be looking up.
 126  
          * @param playerName
 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  
         /** Receive a line from FIBS.
 144  
          * @param s
 145  
          * @param cookie
 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);                // Scrape off '12 RepBotNG '
 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('\'');         // "Player's vouchers..."
 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  
         /** Not really a parse, because we know all the info
 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  
                         //sb.append("  1-2,  13 Aug 03 02:34");
 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  
                         //sb.append("  --" + d);
 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  
          * This method initializes this
 291  
          * 
 292  
          */
 293  
         private void initialize() {
 294  0
         this.setViewportView(getTree());
 295  0
         }
 296  
 
 297  
         /**
 298  
          * This method initializes jTree        
 299  
          *         
 300  
          * @return javax.swing.JTree        
 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  
                 //if (repbotNode == null)
 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  
 }