Libraryless. Click here for Pure Java version (2009L/13K/45K).
1 | !747 |
2 | !pcall { |
3 | !named thread |
4 | |
5 | m { |
6 | static new Map<S, S> cache; // "122" (or "t123") to md5 |
7 | static long lastUpdate; |
8 | static long updateInterval = 5000; |
9 | static long versionCount, lastAutoCheck; |
10 | |
11 | p { |
12 | readLocally("cache lastUpdate"); |
13 | Android3 android = new Android3("Boss Bot."); |
14 | android.startPort = 4990; |
15 | makeAndroid3(android); |
16 | |
17 | updateLoop(); |
18 | } |
19 | |
20 | static void updateLoop() { |
21 | thread "update loop" { |
22 | while (true) { |
23 | checkVersionCount(); |
24 | sleep(updateInterval); |
25 | } |
26 | } |
27 | } |
28 | |
29 | static synchronized S answer(S s) { |
30 | new Matches m; |
31 | |
32 | if (match3("get cache size", s)) |
33 | return cache.size() + " entries"; |
34 | |
35 | if (match3("when was your last update", s)) |
36 | return "" + lastUpdate; |
37 | |
38 | if (match3("when was your last auto-check", s)) |
39 | return "" + lastAutoCheck; |
40 | |
41 | if (match3("please update", s)) { |
42 | update(); |
43 | return "OK, done."; |
44 | } |
45 | |
46 | if (match3("please clear cache", s)) { |
47 | update(); |
48 | // TODO: delete files |
49 | return "OK, done."; |
50 | } |
51 | |
52 | if (match3("what is your update interval?", s)) |
53 | return updateInterval + " ms plus time of check"; |
54 | |
55 | if (match3("is auto-update on?", s)) |
56 | return "Yes."; |
57 | |
58 | if (match3("get md5 of *", s, m)) { |
59 | S md5 = cache.get("" + parseSnippetID(unquote(m.m[0]))); |
60 | return md5 != null ? format3("MD5: *", md5) : "Snippet not found."; |
61 | } |
62 | |
63 | if (match3("get text of *", s, m) || match3("get text for *", s, m)) { |
64 | S snippetID = "" + parseSnippetID(unquote(m.m[0])); |
65 | S md5 = cache.get(snippetID); |
66 | if (md5 == null) ret "Snippet not found."; |
67 | S text = ""; |
68 | if (!md5.equals("-")) |
69 | text = loadText(snippetID, md5); |
70 | ret format3("Text: *", text); |
71 | } |
72 | |
73 | if (match3("get transpilation of *", s, m) || match3("get transpilation for *", s, m)) { |
74 | S snippetID = "t" + parseSnippetID(unquote(m.m[0])); |
75 | S md5 = cache.get(snippetID); |
76 | if (md5 == null) ret "Transpilation not found."; |
77 | S text = ""; |
78 | if (!md5.equals("-")) { |
79 | text = loadText(snippetID, md5); |
80 | if (!eq(md5(text.substring(text.indexOf('\n')+1)), md5)) |
81 | print("Warning: MD5 mismatch for " + snippetID); |
82 | } |
83 | ret format3("Text: *", text); |
84 | } |
85 | |
86 | ret null; |
87 | } |
88 | |
89 | static synchronized void checkVersionCount() { |
90 | pcall { |
91 | lastAutoCheck = now(); |
92 | long newCount = Long.parseLong(loadPageSilently("http://tinybrain.de:8080/tb-int/versioncount.php")); |
93 | if (newCount != versionCount) { |
94 | versionCount = newCount; |
95 | print("Version count updated to " + versionCount + ", getting new md5s."); |
96 | update(); |
97 | } |
98 | } |
99 | } |
100 | |
101 | static synchronized void update() { |
102 | S s = loadPage("http://tinybrain.de:8080/tb-int/all-md5s.php?withtranspiled=1"); |
103 | print("[" + s.length() + " chars loaded]"); |
104 | cache.clear(); |
105 | for (S line : toLinesFullTrim(s)) { |
106 | S[] tok = line.split(":"); |
107 | if (tok.length != 2) |
108 | fail("huch: " + structure(tok)); |
109 | S snippetID = tok[0]; |
110 | S md5 = tok[1]; |
111 | cache.put(snippetID, md5); |
112 | } |
113 | saveLocally("cache"); |
114 | lastUpdate = now(); |
115 | saveLocally("lastUpdate"); |
116 | } |
117 | |
118 | static File getTextFile(S snippetID, S md5) { |
119 | return new File(programDir(), snippetID + "-" + md5 + ".txt"); |
120 | } |
121 | |
122 | static synchronized S loadText(S snippetID, S md5) { |
123 | File f = getTextFile(snippetID, md5); |
124 | if (f.exists()) return loadTextFile(f); |
125 | S text = loadTextFromSite(snippetID, md5); |
126 | saveTextFile(f, text); |
127 | return text; |
128 | } |
129 | |
130 | static S loadTextFromSite(S snippetID, S md5) { |
131 | S url; |
132 | if (snippetID.startsWith("t")) |
133 | url = "http://tinybrain.de:8080/tb-int/get-transpiled.php?raw=1&withlibs=1&id=" + snippetID.substring(1); |
134 | else |
135 | url = "http://tinybrain.de:8080/tb/get-version-raw.php?id=" + parseSnippetID(snippetID) + "&md5=" + urlencode(md5); |
136 | |
137 | // TODO: try a few times :) |
138 | return loadPage(url); |
139 | } |
140 | } |
Began life as a copy of #1001369
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
ID | Author/Program | Comment | Date |
---|---|---|---|
1126 | stefan | TODO: bug with translations not found... | 2015-10-13 21:05:37 |
Snippet ID: | #1001383 |
Snippet name: | Boss Bot v2 (with continuous update, developing) |
Eternal ID of this version: | #1001383/1 |
Text MD5: | 75a8a20cc067d8e8cbb97a8f250fc5a9 |
Transpilation MD5: | 0d01cd8bc8c1b6c0e17ac64a533209f0 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-11-11 18:45:35 |
Source code size: | 4138 bytes / 140 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 1069 / 1126 |
Referenced in: | [show references] |