Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

97
LINES

< > BotCompany Repo | #1000976 // Dialog 3 (sucking chat for answers)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (837L/7K/22K).

!747
!awt {

m {
  static JTextArea textArea;
  static float fontSize = 24f;
  static S fontSnippet =
    //"#1000970"
    "#1000973" // Classic Trash 1
    ;
  static S thinking = "[...]";
  static S chatID;
  
  p {
    chatID = (S) get(getJavaX(), "chatSend_id");
    print("Chat ID: " + chatID);
    
    textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setFont(loadFont());
    textArea.setMargin(new Insets(10, 10, 10, 10));
    
    textArea.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) {
          enterPressed();
        }
      }
    });
    
    makeFrame("Dialog 3", new JScrollPane(textArea));
    
    daemon {
      while (true) {
        S line = suckFromChatNoHistory();
        if (line == null) break;
        L<S> tok = javaTok(line);
        S text = unquote(tok.get(tok.size()-2));
        S guy = tok.get(1);
        S prefix = chatID + ":";
        if (text.startsWith(prefix))
          answer(text.substring(prefix.length()).trim());
      }
    }
    
    sleep(); // important for auto-logging as of now
  }

  static void enterPressed() {
    S text = textArea.getText();
    int idx = textArea.getCaretPosition();
    
    // move to end of line first
    while (idx < text.length() && text.charAt(idx) != '\n') ++idx;
    textArea.setCaretPosition(idx);
    
    //print(quote(text.substring(0, idx)));
    int lineStart = text.lastIndexOf("\n", idx-1);
    //print(lineStart + " " + idx);
    final S line = text.substring(lineStart+1, idx);
    print("?? " + line);
    type(thinking);
    thread {
      processLine(line);
    }
  }
  
  static Font loadFont() ctex {
    return Font.createFont(Font.TRUETYPE_FONT, loadLibrary(fontSnippet)).deriveFont(fontSize);
  }

  static void processLine(S line) {
    // nothing to do, just wait for answers
  }
  
  static void answer(final S s) {
    awt {
      print("!! " + s);
      S text = textArea.getText();
      int idx = text.indexOf(thinking);
      if (idx < 0) {
        print("Weird! Maybe user edited?");
        return;
      }
      int cursor = textArea.getCaretPosition();
      textArea.replaceRange(s, idx, idx+thinking.length());
      if (cursor > idx+thinking.length())
        textArea.setCaretPosition(cursor+s.length()-thinking.length());
    }
  }
  
  static void type(S bla) {
    print("!! " + bla);
    textArea.insert("\n" + bla, textArea.getCaretPosition());
  }
}

Author comment

Began life as a copy of #1000974

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000976
Snippet name: Dialog 3 (sucking chat for answers)
Eternal ID of this version: #1000976/1
Text MD5: 83c67f88d2e6c03f75bc41dc9163a8fc
Transpilation MD5: 7e40aea5f033275810f247cef5fc0293
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-13 03:33:22
Source code size: 2638 bytes / 97 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 599 / 644
Referenced in: #1000978 - Test suckFromChatNoHistory
#3000189 - Answer for stefanreich(>> t bla)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)