Uses 911K of libraries. Click here for Pure Java version (20991L/110K).
1 | !7 |
2 | |
3 | concept ScriptClass > Named {} |
4 | |
5 | concept BotStatus { |
6 | bool isPublic; // defaulting to false, user can say "go public" |
7 | } |
8 | |
9 | cmodule ChatBotFrontend > DynChatBotFrontend { |
10 | switchable bool keepLoaded; |
11 | Set<S> scriptRefs = syncSet(); |
12 | transient L<virtual CruddieScript> scripts; |
13 | transient CRUD<ScriptClass> scriptsCRUD; |
14 | |
15 | transient S[] exportedFunctions = litstringarray('setBotName, 'getBotName, 'makeBotPublic, 'isBotPublic, 'chatLog_userMessagesOnly); |
16 | |
17 | // TODO |
18 | bool isLegitScriptID(S scriptID) { |
19 | new Matches m; |
20 | ret startsWithOneOf(scriptID, m, |
21 | "#1027663/SomeCruddieScripts/", |
22 | "#1027704/SomeCruddieScripts/", |
23 | ) |
24 | && isIdentifier(m.rest()); |
25 | } |
26 | |
27 | start { |
28 | scriptsCRUD = new CRUD(cc, ScriptClass); |
29 | thread "Load Scripts" { |
30 | while (dm_booting()) sleepSeconds(0.1); |
31 | loadScripts(); |
32 | } |
33 | dm_registerAs chatBotFrontend(); |
34 | } |
35 | |
36 | afterVisualize { |
37 | addTab(tabs, "Scripts", scriptsCRUD.visualize()); |
38 | } |
39 | |
40 | void loadScripts enter { |
41 | new L<virtual CruddieScript> scripts; |
42 | for (S ref : (LS) collect name(list(ScriptClass))) pcall { |
43 | LS l = splitAtSlash(ref); |
44 | if (l(l) != 3) continue with print("Can't load script: " + ref); |
45 | S mod = dm_require(first(l) + "/" + second(l)); |
46 | Class c = dm_getClassInModuleRealm(mod, "main$" + last(l)); |
47 | if (c == null) continue with print("Couldn't find class: " + ref); |
48 | O o = nu(c); |
49 | forwardSwappableFunctionsToObject(o, module(), exportedFunctions); |
50 | setOpt(o, deleteMe := r { removeScript(ref) }); |
51 | scripts.add(o); |
52 | print("Got script: " + o); |
53 | } |
54 | this.scripts = scripts; |
55 | } |
56 | |
57 | @Override |
58 | S answer_other(S s) null { |
59 | new Matches m; |
60 | if "clear scripts" { |
61 | deleteConcepts(cc, ScriptClass); |
62 | ret "OK"; |
63 | } |
64 | if "add script *" { |
65 | if (!isLegitScriptID($1)) ret "Not a legit script ID"; |
66 | addScripts(ll($1)); |
67 | ret "OK"; |
68 | } |
69 | if "list scripts" |
70 | ret "[don't say]" + or2(lines(collect name(list(ScriptClass))), "No scripts loaded"); |
71 | } |
72 | |
73 | @Override |
74 | S answer_other_lowPrio(S s) null { |
75 | fOr (virtual CruddieScript script : scripts) pcall { |
76 | try S a = (S) call(script, 'answer, s); |
77 | } |
78 | } |
79 | |
80 | @Override |
81 | O[] popDownButtonParams() { |
82 | ret objectArrayPlus(super.popDownButtonParams(), |
83 | "Load scripts", rThread loadScripts); |
84 | } |
85 | |
86 | // API |
87 | |
88 | void addScripts(LS scriptIDs) { |
89 | bool change; |
90 | fOr (S id : scriptIDs) |
91 | if (uniq_trueIfNew(cc, ScriptClass, name := addPrefix("#", id))) |
92 | set change; |
93 | if (change) loadScripts(); |
94 | } |
95 | |
96 | void addScript(S moduleLibID, S shortClassName) { |
97 | if (uniq_trueIfNew(cc, ScriptClass, name := addPrefix("#", moduleLibID) + "/" + assertIdentifier(shortClassName))) |
98 | loadScripts(); |
99 | } |
100 | |
101 | void removeScript(S scriptID) { |
102 | deleteConceptsWhere(cc, ScriptClass, name := scriptID); |
103 | } |
104 | |
105 | // timeout is in seconds |
106 | bool shouldStayLoaded(double timeout) { |
107 | ret keepLoaded // user wants this module to stay loaded |
108 | || nempty(currentActivities) // module is doing something rn |
109 | || elapsedSeconds_sysTime(lastAccessed) <= timeout; // used recently |
110 | } |
111 | |
112 | BotStatus botStatus() { |
113 | ret uniq(cc, BotStatus); |
114 | } |
115 | |
116 | void makeBotPublic(bool isPublic) { |
117 | cset(botStatus(), +isPublic); |
118 | } |
119 | |
120 | bool isBotPublic() { |
121 | ret botStatus().isPublic; |
122 | } |
123 | |
124 | swappable L/*S*/ chatLog_userMessagesOnly() { null; } |
125 | } |
Began life as a copy of #1027665
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027675 |
Snippet name: | Cruddie Individual Frontend v4 |
Eternal ID of this version: | #1027675/29 |
Text MD5: | b65f76afe62a2c00bd2bda87c87d866b |
Transpilation MD5: | fc545c4ced39dfd9675c4d2bc82e8944 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-07-05 18:39:58 |
Source code size: | 3528 bytes / 125 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 290 / 149651 |
Version history: | 28 change(s) |
Referenced in: | [show references] |