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

52
LINES

< > BotCompany Repo | #1011976 // smartBotsChat_onLine

JavaX fragment (include)

// We're also using some of stefansChat_onLine's variables
please include function stefansChat_onLine.

static double smartBotsChat_onLine_safetyDelay = 1; // seconds
static double smartBotsChat_onLine_shortSafetyDelay = 0.1;
static double smartBotsChat_onLine_errorDelay = 10;
static int smartBotsChat_onLine_lookback = 0;
static int smartBotsChat_onLine_firstRead = -1;
static new ThreadLocal smartBotsChat_onLine_onHistoryRead;

static void smartBotsChat_onLine(int lookback, O f) {
  smartBotsChat_onLine_lookback = lookback;
  smartBotsChat_onLine(f);
}

static void smartBotsChat_onLine(fO f) {
  fO hr = getAndClearThreadLocal(smartBotsChat_onLine_onHistoryRead);
  thread "Smart Bot's Chat" {
    int a = smartBotsChat_onLine_firstRead = max(0, smartBotsChat_n() - smartBotsChat_onLine_lookback);
    bool first = true;
    while licensed { pcall {
      S s = loadPageWithTimeout("http://botcompany.de/1011975/raw/incremental?json=1&a=" + a, 120*1000);
      if (empty(trim(s))) {
        sleepSeconds(smartBotsChat_onLine_safetyDelay);
        //print("Re-polling.");
        continue;
      }
      try {
        Map map = safeUnstructMap(s);
        int n = getInt(map, 'n);
        L msgs = cast get(map, 'msgs);
        for (O msg : msgs)
          try {
            stefansChat_onLine_fullParams.set(mapPlus((Map) msg, chatName := "Smart Bot's Chat"));
            pcallF(f, getString(msg, 'text));
          } finally {
            stefansChat_onLine_fullParams.set(null);
          }
        sleepSeconds(smartBotsChat_onLine_shortSafetyDelay);
        a = n;
      } catch e {
        printException(e);
        sleepSeconds(smartBotsChat_onLine_errorDelay);
      }
      
      if (first) {
        pcallF(hr);
        first = false;
      }
    }} // end pcall, while licensed
  }
}

Author comment

Began life as a copy of #1009193

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1011976
Snippet name: smartBotsChat_onLine
Eternal ID of this version: #1011976/12
Text MD5: a0acbeb5f0318f2573df06ee6feb3e99
Author: stefan
Category: javax / chat
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-02 16:33:32
Source code size: 1854 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 471 / 525
Version history: 11 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)