Libraryless. Click here for Pure Java version (1806L/12K/42K).
1 | !747 |
2 | !pcall { |
3 | |
4 | m { |
5 | p { |
6 | makeAndroid3("Master Bot."); |
7 | update(); |
8 | } |
9 | |
10 | static class Bot { |
11 | int port; |
12 | S helloString; |
13 | S botID; |
14 | L<S> questions; |
15 | } |
16 | |
17 | static new L<Bot> bots; |
18 | |
19 | static synchronized S answer(S s, L<S> history) { |
20 | new Matches m; |
21 | |
22 | if ("!update".equalsIgnoreCase(s)) { |
23 | update(); |
24 | ret "OK. " + stats(); |
25 | } |
26 | |
27 | if (match3("search *", s, m)) { |
28 | //search(s, 3); // TODO |
29 | } |
30 | |
31 | ret search(s); |
32 | } |
33 | |
34 | static void update() { |
35 | L<ProgramScan.Program> list = quickBotScan(); |
36 | new L<Bot> bots; |
37 | for (ProgramScan.Program p : list) pcall { |
38 | new Bot bot; |
39 | bot.port = p.port; |
40 | bot.helloString = p.helloString; |
41 | bot.botID = getProgramIDOfBot(bot.port); |
42 | bot.questions = getSupportedQuestions(bot.botID); |
43 | print(bot.questions.size() + " question(s) found in bot " + quote(bot.helloString)); |
44 | bots.add(bot); |
45 | } |
46 | synchronized(main.class) { |
47 | main.bots = bots; |
48 | } |
49 | //print("Bots found: " + structure(bots)); |
50 | print(stats()); |
51 | } |
52 | |
53 | static synchronized S stats(){ |
54 | ret bots.size() + " bots, " + countQuestions() + " questions."; |
55 | } |
56 | |
57 | static synchronized int countQuestions() { |
58 | int n = 0; |
59 | for (Bot bot : bots) |
60 | n += bot.questions.size(); |
61 | return n; |
62 | } |
63 | |
64 | static S getProgramIDOfBot(int port) { |
65 | S answer = sendToLocalBot(port, "what is your program id"); |
66 | if (!isSnippetID(answer)) |
67 | fail("huch: " + answer); |
68 | return formatSnippetID(answer); |
69 | } |
70 | |
71 | static class BQ { |
72 | Bot bot; |
73 | S question; |
74 | |
75 | *(Bot *bot, S *question) {} |
76 | *() {} |
77 | } |
78 | |
79 | static S search(S input) { |
80 | new HashMap<BQ, Long> scores; |
81 | for (Bot bot : bots) { |
82 | for (S question : bot.questions) { |
83 | long score = scoreQuestion(input, question); |
84 | scores.put(new BQ(bot, question), score); |
85 | } |
86 | } |
87 | BQ bq = getHighest(scores); |
88 | return bq == null ? "No match." : format3("Best match: * *", bq.bot.botID, bq.question); |
89 | } |
90 | |
91 | static long scoreQuestion(S input, S question) { |
92 | return commonPrefix(input.toUpperCase(), question.toUpperCase()).length(); |
93 | } |
94 | |
95 | static BQ getHighest(Map<BQ, Long> map) { |
96 | BQ best = null; |
97 | long bestScore = 0; |
98 | for (Map.Entry<BQ, Long> entry : map.entrySet()) |
99 | if (best == null || entry.getValue() > bestScore) { |
100 | best = entry.getKey(); |
101 | bestScore = entry.getValue(); |
102 | } |
103 | return best; |
104 | } |
105 | } |
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
No comments. add comment
Snippet ID: | #1001384 |
Snippet name: | Master Bot (developing) |
Eternal ID of this version: | #1001384/1 |
Text MD5: | cac20f3e327aee5d1da3e19887f267af |
Transpilation MD5: | 7a480602b9fb2deeb3de66cac6d166e9 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-10-13 21:42:25 |
Source code size: | 2538 bytes / 105 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 597 / 623 |
Referenced in: | [show references] |