Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

149
LINES

< > BotCompany Repo | #1004810 // Concepts / UserLine / Question / UserAnswer / Answer

JavaX fragment (include)

1  
!include #1004681 // Concepts
2  
3  
// Anyone's line in a chat (should be called ChatLine I guess)
4  
sclass UserLine extends Concept {
5  
  ELPost post;
6  
  
7  
  *() {}
8  
  *(ELPost *post) { _doneLoading(); }
9  
  
10  
  void _doneLoading() {
11  
    userLines.put(postToID(post), this);
12  
  }
13  
  
14  
  S text() { ret trim(post.text); }
15  
  S s() { ret text(); }
16  
}
17  
18  
// A question posed by this program
19  
sclass Question extends Concept {
20  
  new Ref<UserLine> inResponseTo;
21  
  new Ref<UserLine> userLine;
22  
  
23  
  *() {}
24  
  *(UserLine inResponseTo) {
25  
    this.inResponseTo.set(inResponseTo);
26  
  }
27  
}
28  
29  
// User's answer to one of our questions
30  
sclass UserAnswer extends Concept {
31  
  new Ref<Question> inResponseTo;
32  
  new Ref<UserLine> userLine;
33  
}
34  
35  
// An answer given by this program
36  
sclass Answer extends Concept {
37  
  new Ref<UserLine> userLine;
38  
}
39  
40  
static new Map<S, UserLine> userLines;
41  
42  
static File eventLogFile;
43  
static L<ELPost> posts;
44  
45  
static File lastFile;
46  
static long lastLength;
47  
static int chatIdx;
48  
49  
static void scanChatIfNecessary {
50  
  eventLogFile = latestEventLogFile();
51  
  long l = eventLogFile.length();
52  
  bool sameFile;
53  
  if (!((sameFile = sameFile(eventLogFile, lastFile)) && l == lastLength)) {
54  
    lastFile = eventLogFile;
55  
    lastLength = l;
56  
    posts = scanEventLogForPosts(eventLogFile);
57  
    psl(posts);
58  
    if (!sameFile) chatIdx = 0;
59  
    scanChat();
60  
    chatIdx = l(posts);
61  
  }
62  
}
63  
64  
static void scanChat {
65  
  // scan over all new posts in chat
66  
  
67  
  for (int i = chatIdx; i < l(posts); i++) {
68  
    ELPost p = posts.get(i);
69  
    parsePossibleAnswer(i);
70  
    processNewUserLine(getUserLine(p));
71  
  }
72  
}
73  
    
74  
static void parsePossibleAnswer(int chatIdx) {
75  
  ELPost p = posts.get(chatIdx);
76  
  S s = trim(p.text);
77  
  if (startsWith(s, "@")) {
78  
    s = substring(s, 1);
79  
    int i = min(xIndexOf(s, ' '), xIndexOf(s, ':'));
80  
    S n = substring(s, 0, i);
81  
    if (!isInteger(n)) ret;
82  
    S answer = trimDropPrefixTrim(":", substring(s, i));
83  
    int questionIdx = chatIdx-parseInt(n);
84  
    registerUserAnswer(chatIdx, questionIdx, answer);
85  
  } else if (findBackRef(getUserLine(p), Question.class) == null) {
86  
    registerUserAnswer(chatIdx, chatIdx-1, s);
87  
  }
88  
}
89  
90  
static void registerUserAnswer(int chatIdx, int questionIdx, S answer) {
91  
  ELPost p = posts.get(chatIdx);
92  
  ELPost q = get(posts, questionIdx);
93  
  if (q == null)
94  
    print("Didn't find question to answer " + quote(answer));
95  
  else {
96  
    print("Possible answer: " + q.text + " => " + quote(answer));
97  
    UserLine ulQ = getUserLine(q);
98  
    Question qu = findBackRefOfType(ulQ, Question.class);
99  
    if (qu == null)
100  
      print("  Wasn't our question");
101  
    else {
102  
      UserLine ul = getUserLine(p);
103  
      UserAnswer ua = findBackRefOfType(ul, UserAnswer.class);
104  
      if (ua != null)
105  
        print("User answer already registered.");
106  
      else {
107  
        ua = new UserAnswer();
108  
        ua.userLine.set(ul);
109  
        ua.inResponseTo.set(qu);
110  
        print("  Registered user answer as " + ua.id);
111  
      }
112  
    }
113  
  }
114  
}
115  
116  
static S postToID(ELPost p) {
117  
  ret p.chatTime + ": " + p.text;
118  
}
119  
120  
static UserLine getUserLine(ELPost p) {
121  
  UserLine ul = userLines.get(postToID(p));
122  
  if (ul == null)
123  
    ul = new UserLine(p); // automatically adds to userLines index
124  
  ret ul;
125  
}
126  
127  
static UserLine postToChat(S text) {
128  
  S chatTime = matchOK(sendToLocalBot("Chat.", "please post * to chat file *",
129  
    text, f2s(eventLogFile)));
130  
  if (chatTime == null) null;
131  
  new ELPost post;
132  
  post.chatTime = chatTime;
133  
  post.text = text;
134  
  ret getUserLine(post);
135  
}
136  
137  
static void action {
138  
  while licensed {
139  
    pcall {
140  
      scanChatIfNecessary();
141  
    }
142  
    
143  
    pcall {
144  
      dbActions();
145  
    }
146  
    
147  
    sleepSeconds(1);
148  
  }
149  
}

Author comment

Began life as a copy of #1004778

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: #1004810
Snippet name: Concepts / UserLine / Question / UserAnswer / Answer
Eternal ID of this version: #1004810/1
Text MD5: 0bb08dbfb9b6c12ba879d92fe61c546e
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-01 22:46:22
Source code size: 3749 bytes / 149 lines
Pitched / IR pitched: No / No
Views / Downloads: 577 / 967
Referenced in: [show references]