001    /*
002     * UnrealBot.java
003     *
004     * Created on February 19, 2002, 12:38 AM
005     */
006    
007    package edu.tamu.gamebots.humanbot;
008    
009    import edu.isi.gamebots.client.*;
010    import edu.tamu.gamebots.ui.*;
011    import java.util.*;
012    
013    /**
014     * <p>
015     * A user interface that allows users to view the messages sent to the bot and to
016     * and send messages (actions) to the server.
017     * </p>
018     * @author rtr7684
019     */
020    public class HumanBot extends edu.isi.gamebots.client.Bot{
021      
022      UnrealMessageTextArea textArea;
023      UnrealMessageTextArea asyncMessageArea;
024      CurrentBotStatePanel botState = null;
025      /**
026       * <p>
027       * The table that will hold the parsed visual infromation
028       * </p>
029       */  
030      protected javax.swing.JTable table = null;
031      /**
032       * <p>
033       * Flag to update the table during the next synchronous block
034       * </p>
035       */  
036      protected boolean updateTable = true;
037      
038      /**
039       * <p>
040       * Creates a new instance of HumanBot
041       * </p>
042       */
043      public HumanBot() {
044        super();
045        textArea = new UnrealMessageTextArea();
046        
047      }
048      
049      /**
050       * <p>
051       * Creates a new HumanBot.
052       * </p>
053       * @param syncArea The TextPanel that will hold the synchronous messages
054       * @param asyncArea The TextPanel that will hold the asynchronous messages
055       */  
056      public HumanBot(UnrealMessageTextArea syncArea, UnrealMessageTextArea asyncArea){
057        super();
058        textArea = syncArea;
059        asyncMessageArea = asyncArea;
060      }
061      
062      /**
063       * <p>
064       * Sets the CurrentBotStatePanel for this bot. This shows the current stats for the
065       * bot
066       * </p>
067       * @param statePanel The CurrentBotStatePanel for this bot
068       */  
069      public void setCurrentBotStatePanel(CurrentBotStatePanel statePanel){
070        botState = statePanel;
071      }
072      
073      /**
074       * <p>
075       * Sets the UnrealMessageTextArea for the bot where the synchronous messages will
076       * be displayed.
077       * </p>
078       * @param syncArea The UnrealMessageTextArea for the bot where the synchronous messages will be displayed.
079       */  
080      public void setSyncMessagesTextArea(UnrealMessageTextArea syncArea){
081        textArea = syncArea;
082      }
083      
084      /**
085       * <p>
086       * Sets the UnrealMessageTextArea for the bot where the asynchronous messages will
087       * be displayed.
088       * </p>
089       * @param asyncArea The UnrealMessageTextArea for the bot where the asynchronous messages will be displayed.
090       */  
091      public void setASyncMessagesTextArea(UnrealMessageTextArea asyncArea){
092        asyncMessageArea = asyncArea;
093      }
094      
095      /**
096       * <p>
097       * Set the table that will hold the parsed visual node information from sync messages.
098       * </p>
099       * @param msgTable <CODE>JTable</CODE> that holds the information.
100       */  
101      public void addJTableForMessages(javax.swing.JTable msgTable){
102        table = msgTable;
103      }
104      
105      /**
106       * <p>
107       * If it is an NFO message, initializes the bot with the world. Otherwise, it prints the message to the async message text panel.
108       * </p>
109       * @param message The ASynchronous message to handle.
110       */   
111      protected void receivedAsyncMessage(Message message) {
112        // If you get an NFO message initialize
113        if( message.getType().equals( INFO ) ) {
114          // Init
115          //  Should check to make sure it is only the first...
116          Properties props = new Properties();
117          props.setProperty( client.PLAYER_NAME, getName() );
118          int team = getInitialTeam();
119          if( team != TEAM_ANY )
120            props.setProperty( client.PLAYER_TEAM, Integer.toString(team) );
121          client.sendMessage( client.INIT, props );
122        }
123        
124        // Add the message to the message area
125        asyncMessageArea.addMessage(message);
126      }
127      
128      /**
129       * <p>
130       * Flag the table to be updated on the next Synch. message block.
131       * </p>
132       */  
133      protected void setUpdateTable(){
134        updateTable = true;
135      }
136      
137      /**
138       * <p>
139       * Returns a {@link #edu.tamu.gamebots.humanbot.HumanBotPanel BotPanel} for display. Called by {@link #edu.isi.gamebots.client.BotRunnerApp BotRunnerApp}
140       * </p>
141       * @return The <CODE>HumanBotPanel</CODE> of this bot
142       */  
143      public javax.swing.JComponent getView(){
144        return new HumanBotPanel(this);
145      }
146      
147      // Event Handlers
148       /**
149        * <p>
150        * Sends a message to the interface saying that the bot was connected. This is an event handling method in Bot.
151        * </p>
152        */   
153      protected void connected() {
154        super.connected();    
155        log.logNote( "Connected... ("+new Date()+")" );
156      }
157      
158      /**
159       * <p>
160       * Sends a message to the interface saying that the bot was disconnected. This is an event handling method in Bot.
161       * </p>
162       */  
163      protected void disconnected() {
164        log.logNote( "Disconnected... ("+new Date()+")" );
165      }
166      
167      /**
168       * <p>
169       * Updates the interface when a new sync. message is received.
170       * </p>
171       * @param message The <CODE>Message</CODE> object to handle
172       */  
173      protected void receivedSyncMessage(MessageBlock message) {
174        // update JTable
175        if(table != null && updateTable){
176          table.setModel(new SyncMsgTableModel(message));
177          updateTable = false;
178        }
179        
180        // update text areal with all messages
181        Iterator it = message.getMessages();
182        while(it.hasNext()){
183          Message next = (Message)it.next();
184          textArea.addMessage(next);
185          if(next.getType().equalsIgnoreCase(SELF)) botState.update(next);
186        }
187      }
188      
189      /**
190       * <p>
191       * Sends all messages sent to the server to the debug log. If using
192       * BotRunnerApp, then this is sent to the lower half of the split pane
193       * in the display
194       * </p>
195       */
196      public void init() {
197        client.setLog(log);
198        client.setLogOutput(true);
199      }
200      
201    }