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

105
LINES

< > BotCompany Repo | #1001384 // Master Bot (developing)

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

Libraryless. Click here for Pure Java version (1806L/12K/42K).

!747
!pcall {

m {
  p {
    makeAndroid3("Master Bot.");
    update();
  }
  
  static class Bot {
    int port;
    S helloString;
    S botID;
    L<S> questions;
  }
  
  static new L<Bot> bots;
  
  static synchronized S answer(S s, L<S> history) {
    new Matches m;
    
    if ("!update".equalsIgnoreCase(s)) {
      update();
      ret "OK. " + stats();
    }
    
    if (match3("search *", s, m)) {
      //search(s, 3); // TODO
    }
    
    ret search(s);
  }
  
  static void update() {
    L<ProgramScan.Program> list = quickBotScan();
    new L<Bot> bots;
    for (ProgramScan.Program p : list) pcall {
      new Bot bot;
      bot.port = p.port;
      bot.helloString = p.helloString;
      bot.botID = getProgramIDOfBot(bot.port);
      bot.questions = getSupportedQuestions(bot.botID);
      print(bot.questions.size() + " question(s) found in bot " + quote(bot.helloString));
      bots.add(bot);
    }
    synchronized(main.class) {
      main.bots = bots;
    }
    //print("Bots found: " + structure(bots));
    print(stats());
  }
  
  static synchronized S stats(){
    ret bots.size() + " bots, " + countQuestions() + " questions.";
  }
  
  static synchronized int countQuestions() {
    int n = 0;
    for (Bot bot : bots)
      n += bot.questions.size();
    return n;
  }
  
  static S getProgramIDOfBot(int port) {
    S answer = sendToLocalBot(port, "what is your program id");
    if (!isSnippetID(answer))
      fail("huch: " + answer);
    return formatSnippetID(answer);
  }
  
  static class BQ {
    Bot bot;
    S question;
    
    *(Bot *bot, S *question) {}
    *() {}
  }
  
  static S search(S input) {
    new HashMap<BQ, Long> scores;
    for (Bot bot : bots) {
      for (S question : bot.questions) {
        long score = scoreQuestion(input, question);
        scores.put(new BQ(bot, question), score);
      }
    }
    BQ bq = getHighest(scores);
    return bq == null ? "No match." : format3("Best match: * *", bq.bot.botID, bq.question);
  }
  
  static long scoreQuestion(S input, S question) {
    return commonPrefix(input.toUpperCase(), question.toUpperCase()).length();
  }
  
  static BQ getHighest(Map<BQ, Long> map) {
    BQ best = null;
    long bestScore = 0;
    for (Map.Entry<BQ, Long> entry : map.entrySet())
      if (best == null || entry.getValue() > bestScore) {
        best = entry.getKey();
        bestScore = entry.getValue();
      }
    return best;
  }
}

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: #1001384
Snippet name: Master Bot (developing)
Eternal ID of this version: #1001384/1
Text MD5: cac20f3e327aee5d1da3e19887f267af
Transpilation MD5: 7a480602b9fb2deeb3de66cac6d166e9
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-13 21:42:25
Source code size: 2538 bytes / 105 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 526 / 530
Referenced in: [show references]