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.

1  
!752
2  
3  
static class Line {
4  
  S id;
5  
  S text;
6  
  S type;
7  
  new L<S> on;
8  
  
9  
  Source source;
10  
}
11  
12  
static PersistentLog<Line> data;
13  
static new Map<S, Line> byID;
14  
static long nextID;
15  
16  
p {
17  
  data = new PersistentLog("data");
18  
  for (Line line : data) index(line);
19  
  load("nextID");
20  
}
21  
22  
synchronized answer {
23  
  if "dn size"
24  
    ret "Dialog network: " + n(data.size(), "line");
25  
    
26  
  if "dn add *" exceptionToUser{
27  
    new Line line;
28  
    line.text = m.unq(0);
29  
    line.type = "user-added";
30  
    add(line, new Source(s));
31  
    ret "Yo! Your ID: " + line.id;
32  
  }
33  
  
34  
  if (matchAny(litlist("dn add * on *", "dn add * to *"), s, m)) exceptionToUser {
35  
    S ids = m.unq(1);
36  
    new Line line;
37  
    line.text = m.unq(0);
38  
    line.type = "user-added";
39  
    line.on.addAll(splitIDs(ids));
40  
    add(line, new Source(s));
41  
    ret "Yo! Your ID: " + line.id;
42  
  }
43  
  
44  
  if "dn show *" exceptionToUser {
45  
    S id = clearID(m.unq(0));
46  
    Line line = byID.get(id);
47  
    ret line == null ? format("Line * not found", id) : structure(line);
48  
  }
49  
  
50  
  if "dn refs *" exceptionToUser {
51  
    S id = clearID(m.unq(0));
52  
    L<Line> refs = findRefsTo(id);
53  
    ret empty(refs) ? "No references found" : formatList(refs);
54  
  }
55  
  
56  
  if "dn search *" exceptionToUser {
57  
    S query = m.unq(0);
58  
    L<Line> refs = search(query);
59  
    ret empty(refs) ? "No results" : formatList(refs);
60  
  }
61  
  
62  
  if "dn clear yes i am sure" {
63  
    if (master()) {
64  
      clear();
65  
      ret "OK";
66  
    }
67  
  }
68  
  
69  
  if "dn dump"
70  
    ret "OK " + structure(data);
71  
    
72  
  if "dn copy from sister" exceptionToUser {
73  
    O sisterBot = getBot("#1002626");
74  
    if (sisterBot == null) ret "No sister bot (install #1002626!)";
75  
    S url = getString(get(sisterBot, "sister"), "robotURL");
76  
    S q = "dn dump";
77  
    S a = loadPage(url + "?" + makePostData("q", q));
78  
    a = htmldecode(a);
79  
    a = dropPrefixMandatory("OK ", a);
80  
    L<Line> newData = cast unstructure(a); // we're trusting the other server here as its URL was given to us by master
81  
    if (empty(newData))
82  
      ret "Empty data, did nothing";
83  
    int l = l(data);
84  
    clear();
85  
    data.addAll(newData);
86  
    for (Line line : newData) index(line);
87  
    fixNextID();
88  
    ret "OK, cleared " + l + " entries, added " + l(newData) + " entries.";
89  
  }
90  
}
91  
92  
static S formatList(L<Line> lines) {
93  
  new L<S> l;
94  
  for (Line line : lines) {
95  
    S text = line.text;
96  
    l.add(line.id + ": " + (text.contains("\n") ? quote(text) : text));
97  
  }
98  
  ret slackSnippet(fromLines(l));
99  
}
100  
101  
static synchronized Line add(S s) {
102  
  new Line line;
103  
  line.text = s;
104  
  add(line, new Source(""));
105  
  ret line;
106  
}
107  
  
108  
109  
static void add(Line line, Source source) {
110  
  line.source = source;
111  
  line.id = newID();
112  
  data.add(line);
113  
  index(line);
114  
}
115  
116  
static void index(Line line) {
117  
  byID.put(line.id, line);
118  
}
119  
120  
static S newID() {
121  
  S id = "_" + ++nextID;
122  
  save("nextID");
123  
  ret id;
124  
}
125  
126  
static L<Line> findRefsTo(S id) {
127  
  new L<Line> l;
128  
  for (Line line : data)
129  
    if (line.on.contains(id))
130  
      l.add(line);
131  
  ret l;
132  
}
133  
134  
static L<Line> search(S query) {
135  
  new L<Line> l;
136  
  for (Line line : data)
137  
    if (indexOfIgnoreCase(line.text, query) >= 0)
138  
      l.add(line);
139  
  ret l;
140  
}
141  
142  
static S clearID(S _id) {
143  
  S id = dropPrefix("_", _id);
144  
  if (!isInteger(id)) fail("woot bad id: " + quote(_id));
145  
  ret "_" + id;
146  
}
147  
148  
static L<S> splitIDs(S s) {
149  
  new L<S> l;
150  
  for (S id : s.split("[ ,]+"))
151  
    l.add(clearID(id));
152  
  ret l;
153  
}
154  
155  
static void clear() {
156  
  pcall { logQuoted("data.clear", loadTextFile(data.file)); }
157  
  data.clear();
158  
  byID.clear();
159  
}
160  
161  
static synchronized L<Line> findStarters() {
162  
  new L<Line> l;
163  
  for (Line line : data)
164  
    if (empty(line.on))
165  
      l.add(line);
166  
  ret l;
167  
}
168  
169  
static void fixNextID() {
170  
  long id = 0;
171  
  for (Line line : data)
172  
    id = max(id, parseLong(dropPrefix("_", line.id)));
173  
  nextID = id+1;
174  
  save("nextID");
175  
}
176  
177  
static L<Line> getReferences(Line line) {
178  
  ret findRefsTo(line.id);
179  
}
180  
181  
// here's the magic! might as well match loosely
182  
static Line findStarter(S s) {
183  
  for (Line line : findStarters())
184  
    if (match(s, line.text))
185  
      ret line;
186  
  ret null;
187  
}

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: 567 / 1170
Version history: 1 change(s)
Referenced in: [show references]