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

59
LINES

< > BotCompany Repo | #1001117 // Online bot!

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

Libraryless. Click here for Pure Java version (503L/5K/14K).

1  
// The online bot LOVES to be online.
2  
3  
!747
4  
5  
m {
6  
  // -1, 0, +1
7  
  static new AtomicInteger online;
8  
  static new AtomicLong nextCheck;
9  
  static S lastProblem;
10  
  
11  
  p {
12  
    makeAndroid("Online bot! " + getStatus(), stringfunc { answer(s) });
13  
14  
    while (true) {
15  
      check();
16  
      long seconds = online.get() > 0 ? 60 : 2;
17  
      nextCheck.set(now() + seconds*1000);
18  
      sleep(Math.max(nextCheck.get()-now(), 0));
19  
    }
20  
  }
21  
  
22  
  // We should add a timeout I guess? Although, if actually offline,
23  
  // it doesn't block, so it's probably ok unless the net or the
24  
  // server is currently very slow.
25  
  static synchronized void check() {
26  
    int oldValue = online.get();
27  
    Throwable problem = checkIfHttpServerIsOnline("tinybrain.de");
28  
    int value = problem == null ? 1 : -1;
29  
    if (problem == null)
30  
      lastProblem = null;
31  
    else if (!problem.toString().equals(lastProblem)) {
32  
      print(problem);
33  
      lastProblem = problem.toString();
34  
    }
35  
    online.set(value);
36  
    if (value != oldValue)
37  
      print(getStatus());
38  
  }
39  
  
40  
  static synchronized S answer(S s) {
41  
    if (match3("are we online?", s))
42  
      return getStatus();
43  
    if (match3("check again", s)) {
44  
      print("Checking...");
45  
      check();
46  
      return getStatus();
47  
    }
48  
    return null;
49  
  }
50  
  
51  
  static S getStatus() {
52  
    int i = online.get();
53  
    long checkSeconds = (nextCheck.get()-now())/1000;
54  
    S checkTime = checkSeconds >= 0 ? "(Next check in " + checkSeconds + " seconds.)" : "";
55  
    if (i < 0) return "We are: OFFLINE. " + checkTime;
56  
    else if (i > 0) return "We are: ONLINE. " + checkTime;
57  
    else return "Online status check in progress.";
58  
  }
59  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001117
Snippet name: Online bot!
Eternal ID of this version: #1001117/1
Text MD5: 345af20f18ef9a29714afcf60f9ebcdb
Transpilation MD5: 6ed9e505e8bd101cef2e87e00c42966f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-22 19:33:40
Source code size: 1697 bytes / 59 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 705 / 765
Referenced in: [show references]