thufir

connection, parser -- no game data

Sep 16th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. running the program, output:
  2.  
  3. 482 636 [n,s,w,e]say confusing the hell out of bob.
  4. Sep 16, 2013 8:06:07 AM player.GameData <init>
  5. INFO: bob
  6. Sep 16, 2013 8:06:07 AM player.TelnetParser ifs
  7. INFO: new data object       bob
  8.  
  9. 482 636 [n,s,w,e]
  10.  
  11.  
  12.  
  13. TelnetParser seems to create a GameData object, but that object never ?seems? to make it to the Observer.
  14.  
  15.  
  16. package player;
  17.  
  18. import java.util.Observable;
  19. import java.util.logging.Logger;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22.  
  23. public class TelnetParser extends Observable {
  24.  
  25.     private static Logger log = Logger.getLogger(TelnetParser.class.getName());
  26.     private String string = null;
  27.  
  28.     public TelnetParser() {
  29.     }
  30.  
  31.     private void stripAnsiColors() {
  32.         Pattern regex = Pattern.compile("\\e\\[[0-9;]*m");
  33.         Matcher regexMatcher = regex.matcher(string);
  34.         string = regexMatcher.replaceAll(""); // *3 ??
  35.     }
  36.  
  37.     public void parse(String string) {
  38.         this.string = string;
  39.         ifs();
  40.     }
  41.  
  42.     //       [\w]+(?=\.)
  43.     private void ifs() {
  44.         log.fine("checking..");
  45.         if (string.contains("confusing the hell out of")) {
  46.             Pattern pattern = Pattern.compile("[\\w]+(?=\\.)");  //(\w+)\.
  47.             Matcher matcher = pattern.matcher(string);
  48.             String enemy = null;
  49.             GameData data = null;
  50.             while (matcher.find()) {
  51.                 enemy = matcher.group();
  52.             }
  53.             data = new GameData.Builder().enemy(enemy).build();
  54.             log.info("new data object\t\t" + data.getEnemy());
  55.             setChanged();
  56.             notifyObservers(data);
  57.  
  58.         } else if (string.contains("Enter 3-letter city code:")) {
  59.             log.fine("found enter city code");
  60.         } else {
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. package telnet;
  69.  
  70. import java.io.FileNotFoundException;
  71. import java.io.IOException;
  72. import java.io.OutputStream;
  73. import java.net.InetAddress;
  74. import java.net.SocketException;
  75. import java.util.Deque;
  76. import java.util.Observable;
  77. import java.util.Observer;
  78. import java.util.Properties;
  79. import java.util.logging.Logger;
  80. import org.apache.commons.net.telnet.TelnetClient;
  81. import player.GameAction;
  82. import player.GameData;
  83. import player.TelnetParser;
  84.  
  85. public class TelnetConnection implements Observer {
  86.  
  87.     private static Logger log = Logger.getLogger(TelnetConnection.class.getName());
  88.     private TelnetClient telnetClient = new TelnetClient();
  89.     private InputOutput inputOutput = new InputOutput();
  90.     private TelnetParser parser = new TelnetParser();
  91.     private Logic logic = new Logic();
  92.  
  93.     public TelnetConnection() {
  94.         try {
  95.             init();
  96.         } catch (SocketException ex) {
  97.         } catch (FileNotFoundException ex) {
  98.         } catch (IOException ex) {
  99.         }
  100.     }
  101.  
  102.     private void init() throws SocketException, FileNotFoundException, IOException {
  103.         Properties props = PropertiesReader.getProps();
  104.         InetAddress host = InetAddress.getByName(props.getProperty("host"));
  105.         int port = Integer.parseInt(props.getProperty("port"));
  106.         telnetClient.connect(host, port);
  107.         inputOutput.readWriteParse(telnetClient.getInputStream(), telnetClient.getOutputStream());
  108.         inputOutput.addObserver(this);
  109.     }
  110.  
  111.     private void sendAction(GameAction action) throws IOException {
  112.         log.info(action.toString());
  113.         byte[] actionBytes = action.getAction().getBytes();
  114.         OutputStream outputStream = telnetClient.getOutputStream();
  115.         outputStream.write(actionBytes);
  116.         outputStream.write(13);
  117.         outputStream.write(10);
  118.         outputStream.flush();
  119.     }
  120.  
  121.     private void sendActions(Deque<GameAction> gameActions) {
  122.         while (!gameActions.isEmpty()) {
  123.             GameAction action = gameActions.remove();
  124.             try {
  125.                 sendAction(action);
  126.             } catch (IOException ex) {
  127.             }
  128.         }
  129.     }
  130.  
  131.     @Override
  132.     public void update(Observable o, Object arg) {
  133.         GameData data = null;
  134.         String line = null;
  135.         if (o instanceof InputOutput) {
  136.             line = arg.toString();
  137.             parser.parse(line);
  138.         } else if (o instanceof TelnetParser) {
  139.             log.info("trying..");
  140.             data = (GameData) arg;
  141.             log.info("hmm, never get here");
  142.             Deque<GameAction> gameActions = logic.getActions(data);
  143.             sendActions(gameActions);
  144.         }
  145.     }
  146.  
  147.     public static void main(String[] args) {
  148.         new TelnetConnection();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment