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

119
LINES

< > BotCompany Repo | #1001348 // Identity Manager (LIVE)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1915L/13K/42K).

!747

m {
  static new Map<S, State> states;
  static int nMaxHistory = 2;
  
  static class State {
    S id;
    S machineID;
    S dataID;
    long since;
    S status;
    State previous;
  }
  
  p {
    readLocally("states");
    makeAndroid3("Identity Manager.");
  }
  
  static synchronized S answer(S s, L<S> history) {
    new Matches m;
    
    if (match3("* is now on machine * with data *", s, m)) {
      S programID = formatSnippetID(unquote(m.m[0]));
      S machineID = unquote(m.m[1]);
      S dataID = formatSnippetID(unquote(m.m[2]));
      new State state;
      state.since = now();
      state.machineID = machineID;
      state.dataID = dataID;
      state.status = "residing";
      setState(programID, state);
      return "OK";
    }
    
    if (match3("* is now in state *", s, m)) {
      S programID = formatSnippetID(unquote(m.m[0]));
      new State state;
      state.since = now();
      State last = states.get(programID);
      if (last != null) {
        state.machineID = last.machineID;
        state.dataID = last.dataID;
      }
      state.status = unquote(m.m[1]);
      setState(programID, state);
      return "OK";
    }
    
    if (match3("* is now in state * with data * to machine *", s, m)) {
      S programID = formatSnippetID(unquote(m.m[0]));
      new State state;
      state.since = now();
      State last = states.get(programID);
      state.machineID = unquote(m.m[3]);
      state.status = unquote(m.m[1]);
      state.dataID = formatSnippetID(unquote(m.m[2]));
      setState(programID, state);
      return "OK";
    }
    
    if (match3("get state of *", s, m))
      return structure(states.get(formatSnippetID(unquote(m.m[0]))));
      
    if (match3("what machine is * supposed to be on ", s, m)) {
      State state = states.get(formatSnippetID(unquote(m.m[0])));
      ret state == null ? "Sorry, no information on bot" : format3("this one: *", state.machineID);
    }
      
    if (match3("list programs", s, m))
      return structure(states.keySet());
      
    if (match3("who is moving to *?", s, m)) {
      S machineID = unquote(m.m[0]);
      return structure(ids(forStateAndMachine("moving", machineID)));
    }
    
    if (match3("who is moving to * (full state)?", s, m)) {
      S machineID = unquote(m.m[0]);
      return structure(forStateAndMachine("moving", machineID));
    }
    
    ret standardQuery(s, "states");
  }
  
  static L<State> forStateAndMachine(S status, S machineID) {
    new L<State> l;
    for (State state : states.values()) {
      if (eq(state.status, status) && eq(state.machineID, machineID))
        l.add(state);
    }
    ret l;
  }
    
  static L<S> ids(L<State> states) {
    new L<S> ids;
    for (State state : states)
      ids.add(state.id);
    ret ids;
  }
  
  static void clearHistory(State state) {
    for (int n = 0; n < nMaxHistory; n++) {
      state = state.previous;
      if (state == null)
        return;
    }
    state.previous = null;
  }

  static synchronized void setState(S programID, State state) {
    state.id = programID;
    state.previous = states.get(programID);
    clearHistory(state);
    states.put(programID, state);
    saveLocally("states");
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001348
Snippet name: Identity Manager (LIVE)
Eternal ID of this version: #1001348/1
Text MD5: 4aff58d7c01663ee5a7be77de8c38485
Transpilation MD5: 93eb0d5594f484081d126565b0e79adf
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-11-13 18:06:53
Source code size: 3330 bytes / 119 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 779 / 1073
Referenced in: [show references]