Libraryless. Click here for Pure Java version (2185L/15K/47K).
1 | !752 |
2 | |
3 | static L<S> thescript = toLinesFullTrim([[ |
4 | match("Hi Eleutheria"); |
5 | say("Hi there!"); |
6 | match("Hi Eleutheria"); |
7 | say("You have said that before :)"); |
8 | ]]); |
9 | |
10 | static class PerUser { |
11 | S dialogID; |
12 | long position; |
13 | } |
14 | |
15 | static new Map<S, PerUser> perUserMap; |
16 | static new ThreadLocal<PerUser> puCurrent; |
17 | |
18 | static O mainBot; |
19 | |
20 | p { |
21 | load("perUserMap"); |
22 | makeBot("A Dialog Bot"); |
23 | printList(thescript); |
24 | } |
25 | |
26 | // does not increase the index |
27 | static FC parseFunctionCall() { |
28 | ret parseFunctionCall(currentScriptLine()); |
29 | } |
30 | |
31 | static S currentScriptLine() { |
32 | int idx = (int) (u().position % l(thescript)); |
33 | ret thescript.get(idx); |
34 | } |
35 | |
36 | synchronized answer { |
37 | S dialogID = cast call(mainBot, "getDialogID"); |
38 | if (dialogID == null) ret null; // need a dialog id now |
39 | PerUser pu = perUserMap.get(dialogID); |
40 | if (pu == null) { |
41 | pu = new PerUser; |
42 | pu.dialogID = dialogID; |
43 | printFormat("Created new dialog: *", dialogID); |
44 | perUserMap.put(dialogID, pu); |
45 | } |
46 | puCurrent.set(pu); |
47 | |
48 | int safety = 0; |
49 | while (safety++ < 1000) try { |
50 | FC fc = parseFunctionCall(); |
51 | S f = fc.f; |
52 | if (eq(f, "match")) { |
53 | S pat = getString(fc.args, 0); |
54 | if (!match(pat, s, m)) { |
55 | softFail("I only understand: *", pat); |
56 | null; |
57 | } |
58 | nextPosition(); // only after match succeeds, otherwise stay at that point |
59 | } else if (eq(f, "say")) { |
60 | nextPosition(); |
61 | S answer = getString(fc.args, 0); |
62 | ret answer; |
63 | } else |
64 | throw fail("Unknown function in script: *", fc.f); |
65 | } catch (Throwable e) { |
66 | printFormat("Was parsing: *", currentScriptLine()); |
67 | throw asRuntimeException(e); |
68 | } |
69 | fail("hard limit reached - 1000 script lines at once - possible endless loop?"); |
70 | } |
71 | |
72 | // a parsed function call |
73 | static class FC { |
74 | int idx; // index of next token |
75 | S f; |
76 | new L args; |
77 | } |
78 | |
79 | static FC parseFunctionCall(S s) { |
80 | L<S> tok = javaTok(s); |
81 | new FC fc; |
82 | fc.f = assertIsIdentifier(tok.get(1)); |
83 | int i = 5; |
84 | assertEquals("(", tok.get(3)); |
85 | while (i < l(tok) && !eq(tok.get(i), ")")) { |
86 | fc.args.add(unquote(tok.get(i))); |
87 | i += 2; |
88 | if (tok.get(i).equals(",")) // otherwise it's kinda mangled, eh. well! we just keep on parsing... |
89 | i += 2; |
90 | } |
91 | if (eq(tok.get(i), ")")) // we even allow a missing closing bracket! |
92 | i += 2; |
93 | fc.idx = i; // save index so some other parser can continue parsing |
94 | ret fc; |
95 | } |
96 | |
97 | static void nextPosition() { |
98 | ++u().position; |
99 | save("perUserMap"); |
100 | print("Position in script now: " + u().position % l(thescript)); |
101 | } |
102 | |
103 | // get per-user data for current thread |
104 | static PerUser u() { |
105 | ret puCurrent.get(); |
106 | } |
Began life as a copy of #1002205
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002210 |
Snippet name: | 1st Level Dialog Engine with example script, multi-user, WORKS |
Eternal ID of this version: | #1002210/1 |
Text MD5: | 09188794f9f70b487f9919646daf9c48 |
Transpilation MD5: | 4618ccc1994bf0a20709314b2d003825 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-12-27 18:46:52 |
Source code size: | 2684 bytes / 106 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 670 / 698 |
Referenced in: | [show references] |