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

187
LINES

< > BotCompany Repo | #1002666 // The Dialog Network (dev.)

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

Transpiled version (2412L) is out of date.

!752

static class Line {
  S id;
  S text;
  S type;
  new L<S> on;
  
  Source source;
}

static PersistentLog<Line> data;
static new Map<S, Line> byID;
static long nextID;

p {
  data = new PersistentLog("data");
  for (Line line : data) index(line);
  load("nextID");
}

synchronized answer {
  if "dn size"
    ret "Dialog network: " + n(data.size(), "line");
    
  if "dn add *" exceptionToUser{
    new Line line;
    line.text = m.unq(0);
    line.type = "user-added";
    add(line, new Source(s));
    ret "Yo! Your ID: " + line.id;
  }
  
  if (matchAny(litlist("dn add * on *", "dn add * to *"), s, m)) exceptionToUser {
    S ids = m.unq(1);
    new Line line;
    line.text = m.unq(0);
    line.type = "user-added";
    line.on.addAll(splitIDs(ids));
    add(line, new Source(s));
    ret "Yo! Your ID: " + line.id;
  }
  
  if "dn show *" exceptionToUser {
    S id = clearID(m.unq(0));
    Line line = byID.get(id);
    ret line == null ? format("Line * not found", id) : structure(line);
  }
  
  if "dn refs *" exceptionToUser {
    S id = clearID(m.unq(0));
    L<Line> refs = findRefsTo(id);
    ret empty(refs) ? "No references found" : formatList(refs);
  }
  
  if "dn search *" exceptionToUser {
    S query = m.unq(0);
    L<Line> refs = search(query);
    ret empty(refs) ? "No results" : formatList(refs);
  }
  
  if "dn clear yes i am sure" {
    if (master()) {
      clear();
      ret "OK";
    }
  }
  
  if "dn dump"
    ret "OK " + structure(data);
    
  if "dn copy from sister" exceptionToUser {
    O sisterBot = getBot("#1002626");
    if (sisterBot == null) ret "No sister bot (install #1002626!)";
    S url = getString(get(sisterBot, "sister"), "robotURL");
    S q = "dn dump";
    S a = loadPage(url + "?" + makePostData("q", q));
    a = htmldecode(a);
    a = dropPrefixMandatory("OK ", a);
    L<Line> newData = cast unstructure(a); // we're trusting the other server here as its URL was given to us by master
    if (empty(newData))
      ret "Empty data, did nothing";
    int l = l(data);
    clear();
    data.addAll(newData);
    for (Line line : newData) index(line);
    fixNextID();
    ret "OK, cleared " + l + " entries, added " + l(newData) + " entries.";
  }
}

static S formatList(L<Line> lines) {
  new L<S> l;
  for (Line line : lines) {
    S text = line.text;
    l.add(line.id + ": " + (text.contains("\n") ? quote(text) : text));
  }
  ret slackSnippet(fromLines(l));
}

static synchronized Line add(S s) {
  new Line line;
  line.text = s;
  add(line, new Source(""));
  ret line;
}
  

static void add(Line line, Source source) {
  line.source = source;
  line.id = newID();
  data.add(line);
  index(line);
}

static void index(Line line) {
  byID.put(line.id, line);
}

static S newID() {
  S id = "_" + ++nextID;
  save("nextID");
  ret id;
}

static L<Line> findRefsTo(S id) {
  new L<Line> l;
  for (Line line : data)
    if (line.on.contains(id))
      l.add(line);
  ret l;
}

static L<Line> search(S query) {
  new L<Line> l;
  for (Line line : data)
    if (indexOfIgnoreCase(line.text, query) >= 0)
      l.add(line);
  ret l;
}

static S clearID(S _id) {
  S id = dropPrefix("_", _id);
  if (!isInteger(id)) fail("woot bad id: " + quote(_id));
  ret "_" + id;
}

static L<S> splitIDs(S s) {
  new L<S> l;
  for (S id : s.split("[ ,]+"))
    l.add(clearID(id));
  ret l;
}

static void clear() {
  pcall { logQuoted("data.clear", loadTextFile(data.file)); }
  data.clear();
  byID.clear();
}

static synchronized L<Line> findStarters() {
  new L<Line> l;
  for (Line line : data)
    if (empty(line.on))
      l.add(line);
  ret l;
}

static void fixNextID() {
  long id = 0;
  for (Line line : data)
    id = max(id, parseLong(dropPrefix("_", line.id)));
  nextID = id+1;
  save("nextID");
}

static L<Line> getReferences(Line line) {
  ret findRefsTo(line.id);
}

// here's the magic! might as well match loosely
static Line findStarter(S s) {
  for (Line line : findStarters())
    if (match(s, line.text))
      ret line;
  ret null;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002666
Snippet name: The Dialog Network (dev.)
Eternal ID of this version: #1002666/2
Text MD5: c7cc9a6a364b61a30ffc24ba51ebc712
Author: stefan
Category: eleu / javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-23 14:45:05
Source code size: 4206 bytes / 187 lines
Pitched / IR pitched: No / No
Views / Downloads: 564 / 1164
Version history: 1 change(s)
Referenced in: [show references]