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)

!include #1004681 // Concepts

// Anyone's line in a chat (should be called ChatLine I guess)
sclass UserLine extends Concept {
  ELPost post;
  
  *() {}
  *(ELPost *post) { _doneLoading(); }
  
  void _doneLoading() {
    userLines.put(postToID(post), this);
  }
  
  S text() { ret trim(post.text); }
  S s() { ret text(); }
}

// A question posed by this program
sclass Question extends Concept {
  new Ref<UserLine> inResponseTo;
  new Ref<UserLine> userLine;
  
  *() {}
  *(UserLine inResponseTo) {
    this.inResponseTo.set(inResponseTo);
  }
}

// User's answer to one of our questions
sclass UserAnswer extends Concept {
  new Ref<Question> inResponseTo;
  new Ref<UserLine> userLine;
}

// An answer given by this program
sclass Answer extends Concept {
  new Ref<UserLine> userLine;
}

static new Map<S, UserLine> userLines;

static File eventLogFile;
static L<ELPost> posts;

static File lastFile;
static long lastLength;
static int chatIdx;

static void scanChatIfNecessary {
  eventLogFile = latestEventLogFile();
  long l = eventLogFile.length();
  bool sameFile;
  if (!((sameFile = sameFile(eventLogFile, lastFile)) && l == lastLength)) {
    lastFile = eventLogFile;
    lastLength = l;
    posts = scanEventLogForPosts(eventLogFile);
    psl(posts);
    if (!sameFile) chatIdx = 0;
    scanChat();
    chatIdx = l(posts);
  }
}

static void scanChat {
  // scan over all new posts in chat
  
  for (int i = chatIdx; i < l(posts); i++) {
    ELPost p = posts.get(i);
    parsePossibleAnswer(i);
    processNewUserLine(getUserLine(p));
  }
}
    
static void parsePossibleAnswer(int chatIdx) {
  ELPost p = posts.get(chatIdx);
  S s = trim(p.text);
  if (startsWith(s, "@")) {
    s = substring(s, 1);
    int i = min(xIndexOf(s, ' '), xIndexOf(s, ':'));
    S n = substring(s, 0, i);
    if (!isInteger(n)) ret;
    S answer = trimDropPrefixTrim(":", substring(s, i));
    int questionIdx = chatIdx-parseInt(n);
    registerUserAnswer(chatIdx, questionIdx, answer);
  } else if (findBackRef(getUserLine(p), Question.class) == null) {
    registerUserAnswer(chatIdx, chatIdx-1, s);
  }
}

static void registerUserAnswer(int chatIdx, int questionIdx, S answer) {
  ELPost p = posts.get(chatIdx);
  ELPost q = get(posts, questionIdx);
  if (q == null)
    print("Didn't find question to answer " + quote(answer));
  else {
    print("Possible answer: " + q.text + " => " + quote(answer));
    UserLine ulQ = getUserLine(q);
    Question qu = findBackRefOfType(ulQ, Question.class);
    if (qu == null)
      print("  Wasn't our question");
    else {
      UserLine ul = getUserLine(p);
      UserAnswer ua = findBackRefOfType(ul, UserAnswer.class);
      if (ua != null)
        print("User answer already registered.");
      else {
        ua = new UserAnswer();
        ua.userLine.set(ul);
        ua.inResponseTo.set(qu);
        print("  Registered user answer as " + ua.id);
      }
    }
  }
}

static S postToID(ELPost p) {
  ret p.chatTime + ": " + p.text;
}

static UserLine getUserLine(ELPost p) {
  UserLine ul = userLines.get(postToID(p));
  if (ul == null)
    ul = new UserLine(p); // automatically adds to userLines index
  ret ul;
}

static UserLine postToChat(S text) {
  S chatTime = matchOK(sendToLocalBot("Chat.", "please post * to chat file *",
    text, f2s(eventLogFile)));
  if (chatTime == null) null;
  new ELPost post;
  post.chatTime = chatTime;
  post.text = text;
  ret getUserLine(post);
}

static void action {
  while licensed {
    pcall {
      scanChatIfNecessary();
    }
    
    pcall {
      dbActions();
    }
    
    sleepSeconds(1);
  }
}

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: 574 / 962
Referenced in: [show references]