coinwalk

luabot

Aug 21st, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 68.76 KB | None | 0 0
  1. /* a simple bot for 999dice to run dicebot style lua scripts
  2. Copyright (C) 2020 Elele <echo "ZWxlbGVAc2VjbWFpbC5wcm8K" | base64 -d>
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17.  
  18. #include <curl/curl.h>
  19. #include <fcntl.h>
  20. #include <inttypes.h>
  21. #include <lauxlib.h>
  22. #include <lua.h>
  23. #include <lualib.h>
  24. #include <ncurses.h>
  25. #include <openssl/sha.h>
  26. #include <signal.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/stat.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <termios.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37.  
  38. bool alarmOn = 1;
  39. char autowd[200]="";
  40. int64_t balance = 0; // balance in Satoshi
  41. double basebet = 0; // used for stop point calc
  42. int64_t BasePayIn=0;
  43. struct timeval begin, end, now;
  44. int betcount = 0; // Bets done in this session
  45. int BetCount=0; // this is for server replies on autobet, not to confuse with betcount
  46. int64_t BetId = 0; // from server reply
  47. WINDOW *betregion; // ncurses region for bet output
  48. int64_t before=0;
  49. double bmax = 0;
  50. double bnow = 0;
  51. char bnowfile[12];
  52. char botname[40];
  53. int64_t bprofit = 0;
  54. double bstart = -1; // start balance;
  55. double chance = 5.0;
  56. int color = 0;
  57. int color2 = 3;
  58. CURL *curl;
  59. char currency[5] = "doge";
  60. double currentprofit = 0; // profit of last bet
  61. int currentstreak = 0; // if positive, winning streak, if negative, losing streak
  62. char cwd[PATH_MAX];
  63. char dirname[25];
  64. int exitOnMax = 0;
  65. int exitOnMismatch = 0;
  66. char exitReason[80] = "";
  67. int64_t gain;
  68. double globalmax = 0;
  69. double globalmin;
  70. double globalprofit = 0;
  71. double goal = 0; // when to count a run as success
  72. double goalDelta=0;
  73. int hardstop=0;
  74. int height, width;
  75. int heightavail;
  76. int high;
  77. int High = 0;
  78. double IncreaseOnLosePercent=0.0;
  79. double IncreaseOnWinPercent=0.0;
  80. double infoval = 0.0;
  81. char kbd = ' ';
  82. int keeplogfile=0;
  83. lua_State *L;
  84. lua_State *Lman;
  85. int lastmax = 0;
  86. int maxincrcount=0;
  87. int lastmaxmax = 0;
  88. int laststreak = 0;
  89. FILE *logfile;
  90. int losecount = 0;
  91. int low;
  92. int Low = 0;
  93. int luaopen=0;
  94. int manualSession=0;
  95. int manualActive=0;
  96. int manualline=0;
  97. int64_t maxbalance = 0;
  98. int maxbets = 0;
  99. int MaxBets=0; // for PlaceAutomatedBets (max.200)
  100. int maxcount = 0;
  101. int64_t maxdiff = 0;
  102. int64_t MaxPayIn = 0;
  103. int64_t AutoMaxPayIn = 0;
  104. int maxstreak = 0;
  105. int64_t minbalance = 9999999999999999;
  106. int minstreak = 0;
  107. WINDOW *msgregion;
  108. double nextbet = 0.00001;
  109. int nobetdisplay = 0;
  110. int nostoploss = 0;
  111. bool OnStopCmd=1;
  112. bool overrideOnStopCmd=0;
  113. bool overrideAutowd=0;
  114. char OnStopCommand[256];
  115. int64_t PayIn = 0;
  116. int64_t PayOut = 0;
  117. double percentualStop=0;
  118. int64_t profitensurance = 0;
  119. double previousbet = 0;
  120. double sessionprofit = 0; // session profit
  121. int recovery=0;
  122. char request[300];
  123. bool ResetOnLose=0;
  124. bool ResetOnLoseMaxBet=0;
  125. bool ResetOnWin=0;
  126. int secret = 0;
  127. char serverseed[65];
  128. char nextserverseed[65]="";
  129. int32_t clientseed;
  130. char sessioncookie[33] = "00000000000000000000000000000000";
  131. int show = 1000000;
  132. int64_t StartingBalance;
  133. int64_t StartingPayIn=0;
  134. WINDOW *statregion;
  135. int64_t stopamount = 200000000;
  136. int stopbetting = 0;
  137. int stopifwin=0; // stop if win in interactive mode
  138. int64_t stoploss = 0;
  139. int64_t StopMinBalance=0;
  140. int64_t StopMaxBalance=0;
  141. int StopOnLoseMaxBet=0;
  142. int seconds;
  143. int streaks[300];
  144. int streamprefix=0; // for bnow file (and feed driveGnuPlots.pl)
  145. struct termios term, term_orig;
  146. char termtitle[80] = "";
  147. int64_t TmpMaxPayIn = 0;
  148. int turnlimit = 0;
  149. char version[15] = "luabot/0.3";
  150. int64_t wagered = 0;
  151. int win;
  152. int wincount = 0;
  153. int WinCount=0; // for AutoBet requests
  154. int Bet (double betsize);
  155. int msg(lua_State *L);
  156. void updateStatus();
  157. // for curl
  158. struct MemoryStruct {
  159. char *memory;
  160. size_t size;
  161. };
  162.  
  163. static size_t
  164. WriteMemoryCallback (void *contents, size_t size, size_t nmemb, void *userp) {
  165. size_t realsize = size * nmemb;
  166. struct MemoryStruct *mem = (struct MemoryStruct *) userp;
  167.  
  168. mem->memory = realloc (mem->memory, mem->size + realsize + 1);
  169. if (mem->memory == NULL) {
  170. /* out of memory! */
  171. printf ("not enough memory (realloc returned NULL)\n");
  172. return 0;
  173. }
  174.  
  175. memcpy (&(mem->memory[mem->size]), contents, realsize);
  176. mem->size += realsize;
  177. mem->memory[mem->size] = 0;
  178.  
  179. return realsize;
  180. }
  181.  
  182. // make lua errors exit program
  183. void bailout (lua_State * L, char *msg, int code) {
  184. mvprintw(height,0,"\n");
  185. endwin ();
  186. fprintf (stderr, "\nFATAL ERROR:\n %s: %s\n\n", msg, lua_tostring (L, -1));
  187. lua_close (L);
  188. exit (code);
  189. }
  190.  
  191. // the stop() function for Lua scripts
  192. int stoplua (lua_State * L) {
  193. bailout(L, "\nstop() called\n",5);
  194. return 1; // not reached
  195. }
  196.  
  197. void verifySeed(char *serverseed, char *hash) {
  198. char binaryseed[32];
  199. char binaryhash[32];
  200. unsigned char hashcalc[32];
  201. char hashstr[65];
  202. SHA256_CTX ctx;
  203.  
  204. char * spos = serverseed;
  205. for (int i=0;i<32;i++) {
  206. sscanf(spos,"%2hhx",&binaryseed[i]);
  207. spos+=2;
  208. }
  209. spos = hash;
  210. for (int i=0;i<32;i++) {
  211. sscanf(spos,"%2hhx",&binaryhash[i]);
  212. spos+=2;
  213. }
  214. SHA256_Init(&ctx);
  215. SHA256_Update(&ctx, binaryseed, 32);
  216. SHA256_Final(hashcalc, &ctx);
  217.  
  218. sprintf(hashstr,"%02x",hashcalc[0]); // convert hashresult back to str
  219. for (int p=1;p<32;p++) {
  220. char tmpstr[3];
  221. sprintf(tmpstr,"%02x",hashcalc[p]);
  222. strcat(hashstr,tmpstr);
  223. }
  224.  
  225. if (strcmp(hashstr,hash)) {
  226. wprintw(msgregion,"BetId %"PRIi64": server seed sha256sum mismatch?\n",BetId);
  227. wprintw(msgregion," expected: %s\n",hash);
  228. wprintw(msgregion,"calculated: %s\n",hashstr);
  229. wprintw(msgregion,"Maybe there is another bot running with the same SessionCookie?\n");
  230. wrefresh(msgregion);
  231. if (exitOnMismatch==1) bailout(L, "server seed hash mismatch in BetId %"PRIi64,BetId);
  232. }
  233. }
  234. int Withdraw(lua_State * L){
  235. int64_t wdamount = (int64_t) (lua_tonumber(L,1)*100000000);
  236. if (strlen(autowd)>0) {
  237. CURLcode res;
  238. struct MemoryStruct chunk;
  239. chunk.memory = malloc (1);
  240. chunk.size = 0;
  241. if (curl) {
  242. sprintf (request,"a=Withdraw&s=%s&Amount=%lu&Address=%s&Currency=%s",sessioncookie,wdamount,autowd,currency);
  243. curl_easy_setopt (curl, CURLOPT_URL,"https://www.999dice.com/api/web.aspx");
  244. curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request);
  245. curl_easy_setopt (curl, CURLOPT_USERAGENT, version);
  246. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10L);
  247. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  248. curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);
  249. res = curl_easy_perform (curl);
  250.  
  251. if (res != CURLE_OK) {
  252. // this is a critical error so better bailout
  253. fprintf (stderr, "curl_easy_perform() failed: %s\n",
  254. curl_easy_strerror (res));
  255. endwin();
  256. exit(9);
  257. } else {
  258. // chunk.memory has result
  259. // and it looks like {"Pending":1234567890}
  260. if (strstr (chunk.memory, "Pending")) {
  261. wattron (msgregion, COLOR_PAIR(5));
  262. wprintw (msgregion, "%12i: ", betcount);
  263. wattroff (msgregion, COLOR_PAIR(5));
  264. wprintw(msgregion,"Withdraw to %s: %s\n",autowd,chunk.memory);
  265. balance-=wdamount;
  266. maxbalance=balance;
  267. lua_pushnumber (L, (double) (balance / 1e8));
  268. lua_setglobal (L, "balance");
  269. logfile = fopen (bnowfile, "a+");
  270. fprintf (logfile, "%i:%.8f\n", streamprefix, (double) balance / 1e8);
  271. fclose (logfile);
  272. wrefresh(msgregion);
  273. } else {
  274. wattron (msgregion, COLOR_PAIR(5));
  275. wprintw (msgregion, "%12i: ", betcount);
  276. wattroff (msgregion, COLOR_PAIR(5));
  277. printf("Withdraw failed. Server replied: %s\n",chunk.memory);
  278. endwin();
  279. exit(9);
  280. }
  281. }
  282. }
  283. } else {
  284. wprintw(msgregion,"withdraw() failed - no address given\n");
  285. wrefresh(msgregion);
  286. }
  287. return(1);
  288. }
  289.  
  290.  
  291. int BetResult(char *serverSeed, int clientseed, int betNumber) {
  292. char clientstr[9];
  293. char betnumstr[9];
  294. unsigned char binaryseed[40];
  295. unsigned char hash[64];
  296. int result;
  297. int i;
  298. SHA512_CTX ctx;
  299.  
  300. sprintf(clientstr,"%08x",clientseed);
  301. sprintf(betnumstr,"%08x",betNumber);
  302.  
  303. char * spos = serverSeed;
  304. for (i=0;i<32;i++) {
  305. sscanf(spos,"%2hhx",&binaryseed[i]);
  306. spos+=2;
  307. }
  308.  
  309. spos = clientstr;
  310. for (i=0;i<4;i++) {
  311. sscanf(spos,"%2hhx",&binaryseed[32+i]);
  312. spos+=2;
  313. }
  314.  
  315. spos = betnumstr;
  316. for (i=0;i<4;i++) {
  317. sscanf(spos,"%2hhx",&binaryseed[36+i]);
  318. spos+=2;
  319. }
  320.  
  321. SHA512_Init(&ctx);
  322. SHA512_Update(&ctx, binaryseed, 40); // ServerSeed [32] + clientseed [4] + Betnumber [4]
  323. SHA512_Final(hash, &ctx);
  324.  
  325. SHA512_Init(&ctx);
  326. SHA512_Update(&ctx, hash, 64); // second SHA2-512 hash
  327. SHA512_Final(hash, &ctx);
  328. while (1) {
  329. int pos=0;
  330. for (pos=0;pos<=61;pos+=3) {
  331. result=(hash[pos]<<16) | (hash[pos+1]<<8) | (hash[pos+2]);
  332. if (result<16000000) {
  333. return result%1000000;
  334. }
  335. }
  336. SHA512_Init(&ctx);
  337. SHA512_Update(&ctx, hash, 64);
  338. SHA512_Final(hash, &ctx);
  339. }
  340. }
  341.  
  342.  
  343.  
  344. int getBalanceFromFile () {
  345. FILE *fd;
  346. sprintf(bnowfile,"./bnow.%s",currency);
  347. static const long max_len = 55 + 1;
  348. char buff[max_len + 1];
  349. if ((fd = fopen (bnowfile, "rb")) != NULL) {
  350. fseek (fd, -max_len, SEEK_END);
  351. fread (buff, max_len - 1, 1, fd);
  352. fclose (fd);
  353. buff[max_len - 1] = '\0';
  354. char *last_newline = strrchr (buff, '\n');
  355. char *last_space = strrchr (buff, '.');
  356. char *last_line = last_newline + 1;
  357. char *last_balance = last_line+2;
  358. balance = strtod (last_balance, &last_space - 1) * 1e8;
  359. }
  360. return 0;
  361. }
  362.  
  363. // GetBalance from API or bnow file if that recently changed
  364. int getBalance() {
  365. struct stat attrib;
  366. int mtime;
  367. char *token;
  368. CURLcode res;
  369. struct MemoryStruct chunk;
  370.  
  371. sprintf(bnowfile,"./bnow.%s",currency);
  372. stat(bnowfile,&attrib);
  373. mtime=attrib.st_mtime;
  374. gettimeofday(&now,(struct timezone *)0);
  375. if ((mtime+120)<now.tv_sec) {
  376. // if bnow file is older than 2 minutes we use API GetBalance to have a reliable value
  377. chunk.memory = malloc (1); /* will be grown as needed by the realloc above */
  378. chunk.size = 0; /* no data at this point */
  379. if (curl) {
  380. sprintf (request,"a=GetBalance&s=%s&Currency=%s",sessioncookie,currency);
  381. curl_easy_setopt (curl, CURLOPT_URL,"https://www.999dice.com/api/web.aspx");
  382. curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request);
  383. curl_easy_setopt (curl, CURLOPT_USERAGENT, version);
  384. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10L);
  385. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  386. curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);
  387. res = curl_easy_perform (curl);
  388. while (res == CURLE_OPERATION_TIMEDOUT) {
  389. // timeout - just try again we might have connection problem only
  390. wprintw(msgregion,"Timeout?! Retrying.\nThis might cause checksum error in case former bet had been processed,\nas we didn't receive result and new server seed hash yet.\nConsider to stop betting for a while until lags are fixed.\n");
  391. refresh();
  392. sprintf (request,"a=GetBalance&s=%s&Currency=%s",sessioncookie,currency);
  393. curl_easy_setopt (curl, CURLOPT_URL,"https://www.999dice.com/api/web.aspx");
  394. curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request);
  395. curl_easy_setopt (curl, CURLOPT_USERAGENT, version);
  396. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20L);
  397. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  398. curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);
  399. res = curl_easy_perform (curl);
  400. }
  401.  
  402. if (res != CURLE_OK) {
  403. // this is a critical error so better bailout
  404. fprintf (stderr, "curl_easy_perform() failed: %s\n",
  405. curl_easy_strerror (res));
  406. endwin();
  407. exit(9);
  408. } else {
  409. // chunk.memory has result
  410. // and it looks like {"Balance":1234567890}
  411. if (strstr (chunk.memory, "Balance")) {
  412. token = strtok (chunk.memory, ":");
  413. token = strtok (NULL, "}");
  414. balance=strtoll(token,NULL,10);
  415. } else {
  416. printf("GetBalance failed. Server replied: %s\n",chunk.memory);
  417. endwin();
  418. exit(9);
  419. }
  420. }
  421. }
  422. } else getBalanceFromFile(); // bnow recently changed so we rely on its content
  423. return 0;
  424. }
  425.  
  426. // when receiving signal USR1 exit on max
  427. void signalHandlerUSR1 () {
  428. exitOnMax = 1;
  429. }
  430.  
  431. void betlog(char *line) {
  432. struct stat st;
  433. logfile = fopen ("./betlog", "a+");
  434. fprintf (logfile, "%s\n", line);
  435. fclose (logfile);
  436. stat("./betlog",&st);
  437. if ((st.st_size>10485760) && (keeplogfile==0)) { // dont grow betlog forever
  438. rename("./betlog","./betlog.old");
  439. }
  440. }
  441.  
  442.  
  443. void keypress (char *f) {
  444. lua_pushnumber (Lman, (double) balance/1e8);
  445. lua_setglobal (Lman, "balance");
  446.  
  447. lua_pushnumber (Lman, (double) maxbalance/1e8);
  448. lua_setglobal (Lman, "maxbalance");
  449.  
  450. lua_pushnumber (Lman, secret);
  451. lua_setglobal (Lman, "secret");
  452.  
  453. lua_pushboolean (Lman, win);
  454. lua_setglobal (Lman, "win");
  455.  
  456. lua_pushnumber (Lman, sessionprofit);
  457. lua_setglobal (Lman, "profit");
  458.  
  459. lua_pushnumber (Lman, currentprofit);
  460. lua_setglobal (Lman, "currentprofit");
  461.  
  462. lua_pushnumber (Lman, currentstreak);
  463. lua_setglobal (Lman, "currentstreak");
  464.  
  465. lua_pushnumber (Lman, nextbet);
  466. lua_setglobal (Lman, "previousbet");
  467.  
  468. lua_pushnumber (Lman, betcount);
  469. lua_setglobal (Lman, "bets");
  470.  
  471. lua_pushnumber (Lman, wincount);
  472. lua_setglobal (Lman, "wins");
  473.  
  474. lua_pushnumber (Lman, losecount);
  475. lua_setglobal (Lman, "losses");
  476.  
  477. lua_pushnumber (Lman, chance);
  478. lua_setglobal (Lman, "chance");
  479.  
  480. lua_pushnumber (Lman, nextbet);
  481. lua_setglobal (Lman, "nextbet");
  482.  
  483. lua_pushnumber (Lman, basebet);
  484. lua_setglobal (Lman, "basebet");
  485.  
  486. lua_pushcfunction (Lman, msg);
  487. lua_setglobal(Lman, "print");
  488.  
  489. lua_getglobal (Lman, f);
  490. if (lua_pcall (Lman, 0, 0, 0))
  491. bailout (Lman, "lua_pcall() failed",1);
  492.  
  493. lua_getglobal (Lman, "nextbet");
  494. nextbet = lua_tonumber (Lman, -1);
  495.  
  496. lua_getglobal (Lman, "basebet");
  497. basebet = lua_tonumber (Lman, -1);
  498.  
  499. lua_getglobal (Lman, "info");
  500. infoval = lua_tonumber (Lman, -1);
  501.  
  502. lua_getglobal (Lman, "chance");
  503. chance = lua_tonumber (Lman, -1);
  504.  
  505. lua_settop (Lman, 0);
  506. }
  507.  
  508. int manualMode() {
  509. // in manual mode most keys are defined via manual.lua
  510. // if luabot is not called with -m to enter manual betting
  511. // then this function will exit as soon as a new max balance
  512. // is reached (i.e. the loss of the script that run in to stop point
  513. // is recovered)
  514. int cred;
  515. int localcount;
  516. manualActive=1;
  517. Lman = luaL_newstate ();
  518. luaL_openlibs (Lman);
  519. before=balance;
  520. if (manualSession==1) {
  521. if (strcmp(currency,"doge")==0) nextbet=0.01;
  522. if (strcmp(currency,"ltc")==0) nextbet=0.0001;
  523. basebet=nextbet;
  524. } else {
  525. recovery=1;
  526. }
  527. updateStatus();
  528. if (luaL_loadfile (Lman, "manual.lua"))
  529. bailout (Lman, "luaL_loadfile() failed",1);
  530. if (lua_pcall (Lman, 0, 0, 0))
  531. bailout (Lman, "lua_pcall() failed",1);
  532.  
  533. while ((kbd != 'Q') && (kbd != 'D') && (kbd!='X')) {
  534. if ((exitOnMax==1) && (balance>=maxbalance)) {
  535. kbd='Q';
  536. }
  537. attron(COLOR_PAIR(5));
  538. if (chance>=10.0) mvprintw (manualline,30,"%.2f%%",chance);
  539. else mvprintw (manualline,30," %.2f%%",chance);
  540. mvprintw (manualline,43,"%15.8f",nextbet);
  541. if (balance>before) mvprintw (manualline,69,"+%.8f",(float) (balance-before)/1e8);
  542. else mvprintw (manualline,69,"%.8f",(float) (balance-before)/1e8);
  543. attroff(COLOR_PAIR(5));
  544. refresh();
  545. timeout (5);
  546. lrand48 ();
  547. kbd = getch ();
  548. srand48 (time (NULL) + 327980);
  549. switch (kbd) {
  550. case 'l':
  551. attron(COLOR_PAIR(5));
  552. if (chance>=10.0) mvprintw (manualline,30,"%.2f%%",chance);
  553. else mvprintw (manualline,30," %.2f%%",chance);
  554. mvprintw (manualline,43,"%15.8f",nextbet);
  555. attroff(COLOR_PAIR(5));
  556. refresh();
  557. Bet(nextbet);
  558. break;
  559. case 'L':
  560. move(manualline,0);
  561. clrtoeol();
  562. refresh();
  563. cred=9;
  564. while ((cred==9) || (currentstreak%9!=0)) {
  565. Bet(nextbet);
  566. if (balance==maxbalance) cred=5;
  567. if (PayOut==0) cred--;
  568. else cred=9;
  569. if ((recovery) && (balance==maxbalance)) { cred=0 ; currentstreak=-9; }
  570. }
  571. break;
  572. case 'j': nextbet=(float) (maxbalance-balance)/6000000000; break;
  573. case 'p':
  574. move(manualline,0);
  575. clrtoeol();
  576. before=balance;
  577. refresh();
  578. cred=100/chance*20;
  579. localcount=0;
  580. while (cred>0) {
  581. Bet(nextbet);
  582. if (PayOut>0) {
  583. localcount++;
  584. if ((recovery) && (balance==maxbalance)) cred=0;
  585. }
  586. cred--;
  587. if ((balance==maxbalance) && (nextbet>basebet) ) {
  588. cred=0;
  589. nextbet=(float) balance/640000000000;
  590. basebet=nextbet;
  591. }
  592. }
  593. attron(COLOR_PAIR(5));
  594. if (localcount==1) mvprintw (manualline,20,"1 win ");
  595. else mvprintw (manualline,20,"%i wins ",localcount);
  596. attroff(COLOR_PAIR(5));
  597. if ((localcount<19) && (balance<maxbalance)) nextbet*=2;
  598. break;
  599. case 'O':
  600. move(manualline,0);
  601. clrtoeol();
  602. refresh();
  603. cred=15;
  604. while ((cred==15) || (currentstreak%15!=0)) {
  605. Bet(nextbet);
  606. if (balance==maxbalance) cred=5;
  607. if (PayOut==0) cred--;
  608. else cred=9;
  609. if ((recovery) && (balance==maxbalance)) currentstreak=-15;
  610. }
  611. break;
  612. case 'H':
  613. move(manualline,0);
  614. clrtoeol();
  615. refresh();
  616. cred=5;
  617. while ((cred==5) || (currentstreak%5!=0)) {
  618. Bet(nextbet);
  619. if (balance==maxbalance) cred=5;
  620. if (PayOut==0) cred--;
  621. else cred=9;
  622. if ((recovery) && (balance==maxbalance)) { cred=0 ; currentstreak=-5; }
  623. }
  624. break;
  625. case 'r': // up to 15 rolls but stop when win
  626. move(manualline,0);
  627. clrtoeol();
  628. refresh();
  629. cred=15;
  630. while (cred>0) {
  631. Bet(nextbet);
  632. if (PayOut>0) cred=0;
  633. else cred--;
  634. }
  635. break;
  636. case 'y': // up to 6 rolls but stop when win
  637. move(manualline,0);
  638. clrtoeol();
  639. refresh();
  640. cred=6;
  641. while (cred>0) {
  642. Bet(nextbet);
  643. if (PayOut>0) cred=0;
  644. else cred--;
  645. }
  646. break;
  647. case 'e': // up to 3 bets until win
  648. move(manualline,0);
  649. clrtoeol();
  650. refresh();
  651. cred=3;
  652. while (cred>0) {
  653. Bet(nextbet);
  654. if (PayOut>0) cred=0;
  655. else cred--;
  656. }
  657. break;
  658. case 'q':
  659. move(manualline,0);
  660. clrtoeol();
  661. refresh();
  662. cred=50;
  663. while (cred>0) {
  664. Bet(nextbet);
  665. if (PayOut>0) {
  666. cred=0;
  667. if (balance==maxbalance) nextbet=basebet;
  668. } else cred--;
  669. }
  670. if (balance<maxbalance) nextbet*=2;
  671. break;
  672. // lots of keys to be configured in Lua code
  673. case '1': keypress("key_1"); break;
  674. case '2': keypress("key_2"); break;
  675. case '3': keypress("key_3"); break;
  676. case '4': keypress("key_4"); break;
  677. case '5': keypress("key_5"); break;
  678. case '6': keypress("key_6"); break;
  679. case '7': keypress("key_7"); break;
  680. case '8': keypress("key_8"); break;
  681. case '9': keypress("key_9"); break;
  682. case '0': keypress("key_0"); break;
  683.  
  684. // case 'q': keypress("key_w"); break;
  685. case 'w': keypress("key_w"); break;
  686. // case 'e': keypress("key_w"); break;
  687. // case 'r': keypress("key_w"); break;
  688. case 't': keypress("key_t"); break;
  689. // case 'y': keypress("key_y"); break;
  690. case 'u': keypress("key_u"); break;
  691. case 'i': keypress("key_i"); break;
  692. case 'o': keypress("key_o"); break;
  693. // case 'p': keypress("key_p"); break;
  694. case '[': keypress("key_bracketleft"); break;
  695. case ']': keypress("key_bracketright"); break;
  696.  
  697. case 'a': keypress("key_a"); break;
  698. case 'A': keypress("key_A"); break;
  699. case 's': keypress("key_s"); break;
  700. case 'd': keypress("key_d"); break;
  701. case 'f': keypress("key_f"); break;
  702. case 'g': keypress("key_g"); break;
  703. case 'h': keypress("key_h"); break;
  704. // case 'j': keypress("key_j"); break;
  705. case 'k': keypress("key_k"); break;
  706. // case 'l': keypress("key_l"); break;
  707. case ';': keypress("key_semicolon"); break;
  708. case 39: keypress("key_apostrophe"); break;
  709. case '#': keypress("key_numbersign"); break;
  710. case 'z': keypress("key_z"); break;
  711. case 'x': keypress("key_x"); break;
  712. case 'c': keypress("key_c"); break;
  713. case 'v': keypress("key_v"); break;
  714. case 'b': keypress("key_b"); break;
  715. case 'n': keypress("key_n"); break;
  716. case 'm': keypress("key_m"); break;
  717. case ',': keypress("key_comma"); break;
  718. case '.': keypress("key_dot"); break;
  719. case '/': keypress("key_slash"); break;
  720. }
  721. }
  722. manualActive=0;
  723. return(1);
  724. }
  725. int InteractiveMode() {
  726. // ask back to user what to do next
  727. if ((OnStopCmd==1) && (alarmOn==1)) { // run OnStopCmd once
  728. system(OnStopCommand);
  729. alarmOn=0;
  730. }
  731. goal=(double) maxbalance/1e8;
  732. timeout (5);
  733. char ekey = ' ';
  734. while ((ekey != 'q') &&
  735. (ekey != 'c') &&
  736. (ekey != 'm') &&
  737. (ekey != '+') &&
  738. (ekey != 'w')) {
  739. lrand48 ();
  740. ekey = getch ();
  741. srand48 (time (NULL) + 327980);
  742. if (hardstop==1) ekey='q';
  743. // ekey='q';
  744. getBalanceFromFile();
  745. if (balance > maxbalance) {
  746. ekey = 'q';
  747. }
  748. }
  749. if (ekey == 'q') {
  750. kbd = 'Q';
  751. return 0;
  752. }
  753. if (ekey == 'm') {
  754. manualMode();
  755. return 0;
  756. }
  757. if (ekey == 'w') stopifwin=1;
  758. if (ekey == 'c') stopifwin=0;
  759.  
  760. if (ekey == '+') {
  761. stopamount = stopamount*110/100;
  762. }
  763. return(0);
  764. }
  765.  
  766.  
  767. int Bet(double betsize) {
  768. int c;
  769. double oncein;
  770. char *token;
  771. CURLcode res;
  772. struct MemoryStruct chunk;
  773. PayIn = (int64_t) (betsize * 1e8);
  774. if (PayIn > MaxPayIn)
  775. MaxPayIn = PayIn;
  776. if (PayIn > TmpMaxPayIn)
  777. TmpMaxPayIn = PayIn;
  778. if (nostoploss == 0) {
  779. if ((balance > (bstart * 2)) && (bstart > 0)) {
  780. stoploss = 1;
  781. attron (A_BOLD);
  782. attron (COLOR_PAIR (3));
  783. mvprintw (1, 29, "STOP2 @");
  784. attroff (COLOR_PAIR (3));
  785. attroff (A_BOLD);
  786. }
  787.  
  788. if ((maxdiff > 2000 * basebet*1e8) && (balance > (bstart + 2 * maxdiff))
  789. && (bstart > 0)) {
  790. stoploss = 1;
  791. attron (A_BOLD);
  792. attron (COLOR_PAIR (3));
  793. mvprintw (1, 29, "STOP3 @");
  794. attroff (COLOR_PAIR (3));
  795. attroff (A_BOLD);
  796. }
  797. if ((maxbalance - 2000 * basebet*1e8 > bstart) && (bstart > 0)) {
  798. stoploss = 1;
  799. attron (A_BOLD);
  800. attron (COLOR_PAIR (3));
  801. mvprintw (1, 29, "STOP4 @");
  802. attroff (COLOR_PAIR (3));
  803. attroff (A_BOLD);
  804. }
  805. if ((profitensurance>0) && (maxbalance-profitensurance>bstart) && (bstart>0)) {
  806. stoploss = 1;
  807. attron (A_BOLD);
  808. attron (COLOR_PAIR (3));
  809. mvprintw (1, 29, "STOP5 @");
  810. attroff (COLOR_PAIR (3));
  811. attroff (A_BOLD);
  812. }
  813. }
  814.  
  815. if (manualActive==0) {
  816. if (((double) balance / 1e8 >= goal) && (goal > 0)) {
  817. sprintf(exitReason, "GOAL REACHED %.8f >= %.8f",(double) (balance) / 1e8, goal);
  818. kbd='Q';
  819. return 0;
  820. }
  821. if ((balance - PayIn) < ((bstart + maxbalance) / 2) && (stoploss == 1)) {
  822. sprintf (exitReason, "STOP LOSS %.8f < %.8f",
  823. (double) (balance - PayIn) / 1e8,
  824. (double) (balance + maxbalance) / 200000000);
  825. kbd = 'Q';
  826. return 0;
  827. }
  828. if (((balance - PayIn < maxbalance - stopamount) && (stopamount > 0)
  829. && (maxbalance > 0)) || stopifwin==1) {
  830. // kbd='D';
  831. // return 0;
  832. wattron(betregion,COLOR_PAIR(5));
  833. wprintw (betregion,"\nNext bet: %.8f - now at %i%% of maxbalance. Press ", betsize, balance * 100 / maxbalance);
  834. wattroff(betregion,COLOR_PAIR(5));
  835. wattron(betregion,COLOR_PAIR(2));
  836. attron (A_BOLD);
  837. wprintw (betregion,"c");
  838. attroff (A_BOLD);
  839. wattroff(betregion,COLOR_PAIR(2));
  840. wattron(betregion,COLOR_PAIR(5));
  841. wprintw (betregion,"ontinue / ");
  842. wattroff(betregion,COLOR_PAIR(5));
  843. wattron(betregion,COLOR_PAIR(2));
  844. attron (A_BOLD);
  845. wprintw (betregion,"q");
  846. attroff (A_BOLD);
  847. wattroff(betregion,COLOR_PAIR(2));
  848. wattron(betregion,COLOR_PAIR(5));
  849. wprintw (betregion,"uit / ");
  850. wattroff(betregion,COLOR_PAIR(5));
  851. wattron(betregion,COLOR_PAIR(2));
  852. attron (A_BOLD);
  853. wprintw (betregion,"m");
  854. attroff (A_BOLD);
  855. wattroff(betregion,COLOR_PAIR(2));
  856. wattron(betregion,COLOR_PAIR(5));
  857. wprintw (betregion,"anual\n");
  858. wattroff(betregion,COLOR_PAIR(5));
  859. exitOnMax = 1;
  860. wrefresh (betregion);
  861. if (balance < bstart || stopifwin==1) {
  862. InteractiveMode();
  863. if (balance>bstart) {
  864. kbd = 'Q';
  865. return 0;
  866. }
  867. } else { // still in profit
  868. kbd = 'Q';
  869. return 0;
  870. }
  871. }
  872. } // not manual
  873. wagered += PayIn;
  874. chunk.memory = malloc (1); /* will be grown as needed by the realloc above */
  875. chunk.size = 0; /* no data at this point */
  876. if (curl) {
  877. c = (int) (chance * 10000 - 1);
  878. sprintf (request,
  879. "a=PlaceBet&s=%s&PayIn=%"PRIi64"&Low=%i&High=%i&clientseed=%"PRIi32"&Currency=%s&ProtocolVersion=2",
  880. sessioncookie, PayIn, Low, High, clientseed, currency);
  881. curl_easy_setopt (curl, CURLOPT_URL,
  882. "https://www.999dice.com/api/web.aspx");
  883. curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request);
  884. curl_easy_setopt (curl, CURLOPT_USERAGENT, version);
  885. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20L);
  886. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  887. curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);
  888. res = curl_easy_perform (curl);
  889. for (int i=15;i<43;i++) request[i]='x';
  890. betlog(request);
  891.  
  892. if (res != CURLE_OK) {
  893. fprintf (stderr, "curl_easy_perform() failed: %s\n",
  894. curl_easy_strerror (res));
  895. endwin();
  896. exit(9);
  897. }
  898. else {
  899. // chunk.memory has result
  900. // looks like this:
  901. // {"BetId":129898866219,"PayOut":24047912075,"Secret":862172,"StartingBalance":578920384883,"ServerSeed":"3463f9c5d513a429f95d0d05c2b588f8d1edc88b81e5c9d263c045d6f97e6859","Next":"4de685c66dc78356c85ed82f6663075edee13c855ebd5827d42c1456b3c33062"}
  902. betlog(chunk.memory);
  903.  
  904. if (strstr (chunk.memory, "BetId")) { // → bet successful
  905. token = strtok (chunk.memory, ":"); // BetId
  906. token = strtok (NULL, ",");
  907. BetId = strtoll (token, NULL, 10);
  908.  
  909. token = strtok (NULL, ":"); // PayOut
  910. token = strtok (NULL, ",");
  911. PayOut = strtoll (token, NULL, 10);
  912.  
  913. token = strtok (NULL, ":"); // Secret
  914. token = strtok (NULL, ",");
  915. secret = strtoll (token, NULL, 10);
  916.  
  917. token = strtok (NULL, ":"); // StartingBalance
  918. token = strtok (NULL, ",");
  919. StartingBalance = strtoll (token, NULL, 10);
  920. if (bstart == -1)
  921. bstart = StartingBalance;
  922.  
  923. token = strtok (NULL, ":"); // ServerSeed
  924. token = strtok (NULL, ",");
  925. strncpy(serverseed,token+1,64);
  926. if (strlen(nextserverseed)>0) verifySeed(serverseed,nextserverseed);
  927.  
  928. token = strtok (NULL, ":"); // Next
  929. token = strtok (NULL, "}");
  930. strncpy(nextserverseed,token+1,64);
  931.  
  932. // verify secret
  933. int bres=BetResult(serverseed,clientseed,0);
  934. if (bres!=secret) {
  935. wprintw(msgregion,"result calculation mismatch:\n");
  936. wprintw(msgregion,"BetId: %"PRIi64", expected: %i but server presents %i\n",BetId,bres,secret);
  937. // this shouldn't happen at all. let's exit
  938. bailout(L, "result mismatch in BetId %"PRIi64"?",BetId);
  939. wrefresh(msgregion);
  940. }
  941.  
  942. balance = StartingBalance - PayIn + PayOut;
  943. bprofit = PayOut - PayIn;
  944. if (PayOut > 0) { // win
  945. color = 1;
  946. color2 = 1;
  947. win = 1;
  948. currentprofit = PayOut - PayIn;
  949. if (currentstreak < 0) {
  950. laststreak = currentstreak;
  951. currentstreak = 1;
  952. }
  953. else {
  954. currentstreak++;
  955. oncein=1;
  956. for (int o=0;o<currentstreak;o++) oncein*=100/chance;
  957. oncein+=0.9;
  958. if ((oncein>=8000)) {
  959. if ((nobetdisplay==0) || (manualActive==1)) {
  960. wattron (msgregion,COLOR_PAIR(5));
  961. wprintw (msgregion, "%12i: %.8f %.8f streak %3i (once in %lu)\n", betcount,
  962. (double) balance / 1e8,
  963. (double) gain / 1e8, currentstreak,
  964. (long long) oncein);
  965. wattroff (msgregion,COLOR_PAIR(5));
  966. wrefresh (msgregion);
  967. }
  968. }
  969. }
  970. wincount++;
  971. }
  972. else {
  973. color2 = 3;
  974. win = 0;
  975. if (currentstreak > 0) {
  976. laststreak = currentstreak;
  977. currentstreak = -1;
  978. }
  979. else {
  980. currentstreak--;
  981. oncein=1;
  982. for (int o=0;o>currentstreak;o--) oncein*=100/(100-chance);
  983. oncein+=0.9;
  984. if ((oncein>=8000)) {
  985. if ((nobetdisplay==0) || (manualActive==1)) {
  986. wattron (msgregion,COLOR_PAIR(5));
  987. wprintw (msgregion, "%12i: %.8f %.8f streak %3i (once in %lu)\n", betcount,
  988. (double) balance / 1e8,
  989. (double) gain / 1e8, currentstreak,
  990. (long long) oncein);
  991. wattroff (msgregion,COLOR_PAIR(5));
  992. wrefresh (msgregion);
  993. }
  994. }
  995. }
  996. losecount++;
  997.  
  998. }
  999. if (currentstreak < minstreak)
  1000. minstreak = currentstreak;
  1001. if (currentstreak > maxstreak)
  1002. maxstreak = currentstreak;
  1003. betcount++;
  1004. if (balance >= maxbalance) {
  1005. gain = balance - maxbalance;
  1006. maxbalance = balance;
  1007. if (percentualStop>0) stopamount=balance*percentualStop/100;
  1008. maxbets = betcount - lastmax;
  1009. maxincrcount++;
  1010. wattron (betregion, A_BOLD);
  1011. /* if (nobetdisplay == 0) {
  1012. wprintw (msgregion, "%i %.8f %.8f %3i\n", ++maxcount,
  1013. (double) balance / 1e8,
  1014. (double) gain / 1e8, laststreak);
  1015. wrefresh (msgregion);
  1016. } */
  1017. TmpMaxPayIn = 0;
  1018. lastmax = betcount;
  1019. if ((exitOnMax)
  1020. || ((turnlimit > 0) && (betcount > turnlimit))) {
  1021. strcpy (exitReason, "EXIT on MAX requested");
  1022. kbd = 'Q';
  1023. }
  1024.  
  1025. }
  1026. if (balance < minbalance)
  1027. minbalance = balance;
  1028. if ((maxbalance - balance) > maxdiff)
  1029. maxdiff = maxbalance - balance;
  1030. if ((nobetdisplay==0) || (manualActive==1)) {
  1031. color = balance*100/maxbalance;
  1032. if (color<86) color=86;
  1033. wattron (betregion, COLOR_PAIR (color2));
  1034. if (balance<maxbalance) {
  1035. wprintw (betregion, "%4i %4i ",abs(currentstreak),betcount-lastmax);
  1036. wattron (betregion, COLOR_PAIR (color));
  1037. wprintw (betregion, "%18.8f ",(double) balance / 1e8);
  1038. wattron (betregion, COLOR_PAIR (color2));
  1039. wprintw(betregion, "%5.2f%% %20.8f %20.8f \n",chance,betsize,(double) bprofit / 1e8);
  1040. } else
  1041. wprintw (betregion, "%4i %4i %18.8f %5.2f%% %20.8f %20.8f +%.8f\n",
  1042. abs(currentstreak),betcount-lastmax,
  1043. (double) balance / 1e8, chance, betsize,
  1044. (double) bprofit / 1e8,
  1045. (double) gain/1e8);
  1046. wattroff (betregion, COLOR_PAIR (color));
  1047. wattroff (betregion, A_BOLD);
  1048. wrefresh (betregion);
  1049. }
  1050. updateStatus();
  1051. logfile = fopen (bnowfile, "a+");
  1052. if (infoval>0.0)
  1053. fprintf (logfile, "%i:%.8f %.8f\n", streamprefix, (double) balance / 1e8, infoval);
  1054. else
  1055. fprintf (logfile, "%i:%.8f\n", streamprefix, (double) balance / 1e8);
  1056.  
  1057. fclose (logfile);
  1058. // printf("Balance: %.8f %.2f%% bet: %.8f profit: %.8f won %i of %i (%.2f%%) lastmax:%i diff: %.8f\n",(double) balance/1e8,chance,betsize,profit/1e8,wincount,betcount,(double) wincount/betcount*100,betcount-lastmax,(double) (balance-maxbalance)/1e8);
  1059.  
  1060. }
  1061.  
  1062. else { // something went wrong
  1063. endwin();
  1064. for (int n=0;n<height;n++) printf("\n");
  1065. printf("\033[1m\033[31mERROR?!\n\033[33mRequest sent: \033[37m%s\n\033[33mServer answered: \033[36m%s\033[0m\n",
  1066. request, chunk.memory);
  1067. kbd = 'D'; // quit
  1068. }
  1069. }
  1070. printf ("\033[0m");
  1071. return 1;
  1072. }
  1073. return 1;
  1074. }
  1075.  
  1076.  
  1077.  
  1078. int autobets() {
  1079. int c;
  1080. char *token;
  1081. char option[50];
  1082. int secret;
  1083. CURLcode res;
  1084. struct MemoryStruct chunk;
  1085. chunk.memory = malloc (1); /* will be grown as needed by the realloc above */
  1086. chunk.size = 0; /* no data at this point */
  1087. if (BasePayIn > MaxPayIn)
  1088. MaxPayIn = BasePayIn;
  1089. attron(COLOR_PAIR(5));
  1090. mvprintw (7, 18,"balance chance #wins BasePayIn profit");
  1091. attroff(COLOR_PAIR(5));
  1092. if (curl) {
  1093. if (((balance-BasePayIn)>(maxbalance-stopamount)) && ((balance-BasePayIn)>StopMinBalance)) {
  1094. c = (int) (chance * 10000 - 1);
  1095. sprintf (request,"a=PlaceAutomatedBets&s=%s",sessioncookie);
  1096. sprintf (option,"&BasePayIn=%"PRIi64,BasePayIn);
  1097. strcat(request,option);
  1098. sprintf (option,"&Low=%i&High=%i",Low, High);
  1099. strcat(request,option);
  1100. sprintf (option,"&MaxBets=%i",MaxBets);
  1101. strcat(request,option);
  1102. if ((StopMinBalance>0) && (StopMinBalance>maxbalance-stopamount))
  1103. sprintf (option,"&StopMinBalance=%"PRIi64,StopMinBalance);
  1104. else sprintf (option,"&StopMinBalance=%"PRIi64,maxbalance-stopamount);
  1105. strcat(request,option);
  1106. if (StopMaxBalance>0) {
  1107. sprintf (option,"&StopMaxBalance=%"PRIi64,StopMaxBalance);
  1108. strcat(request,option);
  1109. }
  1110.  
  1111. if (ResetOnWin) strcat(request,"&ResetOnWin=true");
  1112. if (ResetOnLose) strcat(request,"&ResetOnLose=true");
  1113.  
  1114. if (IncreaseOnWinPercent>0) {
  1115. sprintf(option,"&IncreaseOnWinPercent=%.6f",IncreaseOnWinPercent);
  1116. strcat(request,option);
  1117. }
  1118. if (IncreaseOnLosePercent>0) {
  1119. sprintf(option,"&IncreaseOnLosePercent=%.6f",IncreaseOnLosePercent);
  1120. strcat(request,option);
  1121. }
  1122.  
  1123. if (AutoMaxPayIn>0) {
  1124. sprintf(option,"&MaxPayIn=%"PRIi64,AutoMaxPayIn);
  1125. strcat(request,option);
  1126. }
  1127.  
  1128. if (ResetOnLoseMaxBet) strcat(request,"&ResetOnLoseMaxBet=true");
  1129. if (StopOnLoseMaxBet) strcat(request,"&StopOnLoseMaxBet=true");
  1130.  
  1131. if (StartingPayIn>0) {
  1132. sprintf(option,"&StartingPayIn=%"PRIi64,StartingPayIn);
  1133. strcat(request,option);
  1134. }
  1135. sprintf (option,"&clientseed=%"PRIi32,clientseed);
  1136. strcat(request,option);
  1137.  
  1138. sprintf (option,"&Currency=%s",currency);
  1139. strcat(request,option);
  1140.  
  1141. strcat(request,"&Compact=true&ProtocolVersion=2");
  1142.  
  1143. curl_easy_setopt (curl, CURLOPT_URL,"https://www.999dice.com/api/web.aspx");
  1144. curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request);
  1145. curl_easy_setopt (curl, CURLOPT_USERAGENT, version);
  1146. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20L);
  1147. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  1148. curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);
  1149.  
  1150. res = curl_easy_perform (curl);
  1151. if (res != CURLE_OK) {
  1152. fprintf (stderr, "curl_easy_perform() failed: %s\n",
  1153. curl_easy_strerror (res));
  1154. endwin();
  1155. exit(9);
  1156. } else { // Got a server reply - chunk.memory has result
  1157. /*
  1158. {"BetId":129889029664,"BetCount":24,"Seed":"9950CEDA4D53E15DC6B292D8C7EBD9D0D7C5EAA21D9C635EEB8A2E2147E42708","PayIn":-2410400,"PayOut":2609800,"StartingBalance":571793585947,"Next":"5332805df62fbbda96eb7b45294e1547e19532cc086a3608c5c9b6d9d08b3eb2"}
  1159. */
  1160. for (int i=25;i<53;i++) request[i]='x';
  1161. betlog(request);
  1162. betlog(chunk.memory);
  1163.  
  1164. if (strstr (chunk.memory, "BetId")) { // → bet successful
  1165. token = strtok (chunk.memory, ":"); // BetId
  1166. token = strtok (NULL, ",");
  1167. BetId = strtoll (token, NULL, 10);
  1168.  
  1169. token = strtok (NULL, ":"); // BetCount
  1170. token = strtok (NULL, ",");
  1171. BetCount = strtoll (token, NULL, 10);
  1172. betcount+=BetCount;
  1173.  
  1174. token = strtok (NULL, ":"); // ServerSeed
  1175. token = strtok (NULL, ",");
  1176. strncpy(serverseed,token+1,64);
  1177. if (strlen(nextserverseed)>0) verifySeed(serverseed,nextserverseed);
  1178.  
  1179. token = strtok (NULL, ":"); // PayIn
  1180. token = strtok (NULL, ",");
  1181. PayIn = -strtoll (token, NULL, 10);
  1182. wagered+=PayIn;
  1183.  
  1184. token = strtok (NULL, ":"); // PayOut
  1185. token = strtok (NULL, ",");
  1186. PayOut = strtoll (token, NULL, 10);
  1187.  
  1188. token = strtok (NULL, ":"); // StartingBalance
  1189. token = strtok (NULL, ",");
  1190. StartingBalance = strtoll (token, NULL, 10);
  1191.  
  1192. token = strtok (NULL, ":"); // Next
  1193. token = strtok (NULL, ",");
  1194. strncpy(nextserverseed,token+1,64);
  1195.  
  1196. // Calculate the number of wins in this request
  1197. WinCount=0;
  1198. for (int w=0;w<BetCount;w++) {
  1199. secret=BetResult(serverseed,clientseed,w);
  1200. if ((secret>=Low) && (secret<=High)) WinCount++;
  1201. }
  1202. wincount+=WinCount;
  1203.  
  1204. balance = StartingBalance - PayIn + PayOut;
  1205. bprofit = PayOut - PayIn;
  1206. if (PayOut>PayIn) { // win
  1207. color = 1;
  1208. color2 = 1;
  1209. win = 1;
  1210. currentprofit = PayOut - PayIn;
  1211. if (currentstreak < 0) {
  1212. laststreak = currentstreak;
  1213. currentstreak = 1;
  1214. }
  1215. else {
  1216. currentstreak++;
  1217. }
  1218. } else {
  1219. color2 = 3;
  1220. win = 0;
  1221. currentstreak = -1;
  1222. }
  1223. if (balance >= maxbalance) {
  1224. gain = balance - maxbalance;
  1225. maxbalance = balance;
  1226. maxbets = betcount - lastmax;
  1227. maxincrcount++;
  1228. wattron (betregion, A_BOLD);
  1229. TmpMaxPayIn = 0;
  1230. lastmax = betcount;
  1231. if ((exitOnMax)
  1232. || ((turnlimit > 0) && (betcount > turnlimit))) {
  1233. strcpy (exitReason, "EXIT on MAX requested");
  1234. kbd = 'Q';
  1235. }
  1236.  
  1237. }
  1238. if (balance < minbalance)
  1239. minbalance = balance;
  1240. if ((maxbalance - balance) > maxdiff)
  1241. maxdiff = maxbalance - balance;
  1242. if ((nobetdisplay == 0) || (manualActive==1)) {
  1243. color = balance*100/maxbalance;
  1244. if (color<86) color=86;
  1245. wattron (betregion, COLOR_PAIR (color2));
  1246. if (balance<maxbalance) {
  1247. wprintw (betregion, " %6i ",betcount-lastmax);
  1248. wattron (betregion, COLOR_PAIR (color));
  1249. wprintw (betregion, "%18.8f ",(double) balance / 1e8);
  1250. wattron (betregion, COLOR_PAIR (color2));
  1251. wprintw(betregion, "%5.2f%% %3i/%3i %20.8f %20.8f \n",chance,WinCount,BetCount,(double) BasePayIn/1e8,(double) bprofit / 1e8);
  1252. } else
  1253. wprintw (betregion, " %6i %18.8f %5.2f%% %3i/%3i %20.8f %20.8f +%.8f\n",
  1254. betcount-lastmax,
  1255. (double) balance / 1e8, chance, WinCount, BetCount,(double) BasePayIn/1e8,
  1256. (double) bprofit / 1e8,
  1257. (double) gain/1e8);
  1258. wattroff (betregion, COLOR_PAIR (color));
  1259. wattroff (betregion, A_BOLD);
  1260. wrefresh (betregion);
  1261. }
  1262. updateStatus();
  1263. logfile = fopen (bnowfile, "a+");
  1264. if (infoval>0) fprintf (logfile, "%i:%.8f %.8f\n", streamprefix, (double) balance / 1e8,infoval);
  1265. else fprintf (logfile, "%i:%.8f\n", streamprefix, (double) balance / 1e8);
  1266. fclose (logfile);
  1267. } else {
  1268. // we didn't get a bet Id, so we got an error
  1269. endwin ();
  1270. fprintf (stderr, "\nFATAL ERROR:\n %s\n\n", chunk.memory);
  1271. exit (9);
  1272. }
  1273. }
  1274. } else {
  1275. if (sessionprofit>0) {
  1276. kbd='Q';
  1277. return 1;
  1278. }
  1279. wattron(betregion,COLOR_PAIR(5));
  1280. wprintw (betregion,"\nNext BasePayIn: %.8f - now at %i%% of maxbalance. Press ", (double) BasePayIn/1e8, balance * 100 / maxbalance);
  1281. wattroff(betregion,COLOR_PAIR(5));
  1282. wattron(betregion,COLOR_PAIR(2));
  1283. attron (A_BOLD);
  1284. wprintw (betregion,"+");
  1285. attroff (A_BOLD);
  1286. wattroff(betregion,COLOR_PAIR(2));
  1287. wattron(betregion,COLOR_PAIR(5));
  1288. wprintw (betregion," to increase risk balance / ");
  1289. wattroff(betregion,COLOR_PAIR(5));
  1290. wattron(betregion,COLOR_PAIR(2));
  1291. attron (A_BOLD);
  1292. wprintw (betregion,"q");
  1293. attroff (A_BOLD);
  1294. wattroff(betregion,COLOR_PAIR(2));
  1295. wattron(betregion,COLOR_PAIR(5));
  1296. wprintw (betregion,"uit / ");
  1297. wattroff(betregion,COLOR_PAIR(5));
  1298. wattron(betregion,COLOR_PAIR(2));
  1299. attron (A_BOLD);
  1300. wprintw (betregion,"m");
  1301. attroff (A_BOLD);
  1302. wattroff(betregion,COLOR_PAIR(2));
  1303. wattron(betregion,COLOR_PAIR(5));
  1304. wprintw (betregion,"anual\n");
  1305. wattroff(betregion,COLOR_PAIR(5));
  1306. exitOnMax = 1;
  1307. wrefresh (betregion);
  1308.  
  1309. InteractiveMode();
  1310. return 0;
  1311. /* if ((sessionprofit>0) || (hardstop==1)) {
  1312. endwin();
  1313. lua_close(L);
  1314. exit(7);
  1315. } else {
  1316. wprintw (msgregion, "%12i: ", betcount);
  1317. wattron (msgregion, COLOR_PAIR (2));
  1318. wprintw (msgregion, "bottom stop. entered manual mode.\n", lua_tostring(L,1));
  1319. wattroff (msgregion, COLOR_PAIR (2));
  1320. wrefresh (msgregion);
  1321. return 2;
  1322. } */
  1323. }
  1324. } // if (curl)
  1325. return 1;
  1326. }
  1327.  
  1328. // function for msg and print
  1329. int msg(lua_State *L) {
  1330. wattron (msgregion, COLOR_PAIR(5));
  1331. wprintw (msgregion, "%12i: ", betcount);
  1332. wattroff (msgregion, COLOR_PAIR(5));
  1333. wattron (msgregion, COLOR_PAIR (2));
  1334. wprintw (msgregion, " %s\n", lua_tostring(L,1));
  1335. wattroff (msgregion, COLOR_PAIR (2));
  1336. wrefresh (msgregion);
  1337. return 0;
  1338. }
  1339.  
  1340. // Output the status in upper part of the terminal
  1341. void updateStatus() {
  1342. attron (COLOR_PAIR (2));
  1343. mvprintw (0, 0, " minimum: %18.8f", (double) minbalance / 1e8);
  1344. // if (exitOnMax) mvprintw (0, 60, "EXIT on MAX %i",exitOnMaxLine);
  1345. if (stopamount==maxbalance) {
  1346. mvprintw (1, 0, " current: %18.8f ", (double) balance / 1e8);
  1347. } else {
  1348. if (strcmp(currency,"doge"))
  1349. mvprintw (1, 0, " current: %18.8f (%.8f) ", (double) balance / 1e8, (double) (maxbalance-stopamount)/100000000);
  1350. else
  1351. mvprintw (1, 0, " current: %18.8f (%i) ", (double) balance / 1e8, (maxbalance-stopamount)/100000000);
  1352. }
  1353. mvprintw (2, 0, " diff: %18.8f (max. %.8f) ",
  1354. (double) (maxbalance - balance) / 1e8,
  1355. (double) maxdiff / 1e8);
  1356. mvprintw (3, 0, " maximum: %18.8f", (double) maxbalance / 1e8);
  1357. if (goal>0) mvprintw (3,29,"(goal: %.8f) ",goal);
  1358. mvprintw (4, 0, " profit: %18.8f (%.3f%%) ",
  1359. (double) (balance - bstart) / 1e8,
  1360. (double) balance*100/bstart-100);
  1361. if (MaxBets>0) mvprintw (5, 0, "MaxBasePayIn: %14.8f", (double) MaxPayIn / 1e8);
  1362. else mvprintw (5, 0, "MaxPayIn: %18.8f", (double) MaxPayIn / 1e8);
  1363.  
  1364. mvprintw (6, 0, " wagered: %18.8f (ref. %.8f) ",
  1365. (double) wagered / 1e8,
  1366. (double) wagered / 1e8 / 2000);
  1367. attroff (COLOR_PAIR(2));
  1368. attron (COLOR_PAIR(5));
  1369. gettimeofday(&end,(struct timezone *)0);
  1370. seconds = end.tv_sec - begin.tv_sec;
  1371. int minutes = seconds/60;
  1372. int hours = minutes/60;
  1373. minutes=minutes%60;
  1374. seconds=seconds%60;
  1375. mvprintw (2, 60, "won %i of %i bets (%.2f%%) %i:%02i:%02i ", wincount, betcount,
  1376. (double) wincount / betcount * 100,hours,minutes,seconds);
  1377. if ((betcount - lastmax) > lastmaxmax)
  1378. lastmaxmax = betcount - lastmax;
  1379. mvprintw (3, 60, "last max: %4i (%i) (%.0f avg) %.2f%% ", betcount - lastmax,
  1380. lastmaxmax, (double) betcount/maxincrcount, (double) balance * 100 / maxbalance);
  1381. mvprintw (4, 60, "streak: %4i min %i max %i) ",
  1382. currentstreak, minstreak, maxstreak);
  1383. mvprintw (5, 60, "info: %8f ", infoval);
  1384.  
  1385. if (stoploss == 1)
  1386. mvprintw (1, 39, "%.8f", (double) (bstart + maxbalance) / 200000000);
  1387. attroff(COLOR_PAIR(5));
  1388. refresh ();
  1389. }
  1390.  
  1391. void login(){
  1392. char username[50];
  1393. char password[100];
  1394. char totp[10];
  1395. char *token;
  1396. char currency[5]="btc";
  1397. char *ptr;
  1398. double balance;
  1399. char filename[10];
  1400. FILE *fd;
  1401. CURLcode res;
  1402. struct MemoryStruct chunk;
  1403.  
  1404. echo();
  1405. curs_set(1);
  1406. wattron(betregion,COLOR_PAIR(5));
  1407. wprintw(betregion,"\nWelcome!\nPlease login to 999dice.com\n\n");
  1408. wprintw(betregion,"Note: No login data is saved to your disk. What IS SAVED to your disk\n");
  1409. wprintw(betregion,"is a SessionCookie to file account.data in your current working directory,\n");
  1410. wprintw(betregion,"If you want the bot to logout just delete the file account.data\n\n");
  1411. wprintw(betregion,"\nUsername: ");
  1412. wattroff(betregion,COLOR_PAIR(5));
  1413. wrefresh(betregion);
  1414. wgetnstr(betregion,username,49);
  1415. wattron(betregion,COLOR_PAIR(5));
  1416. wprintw(betregion,"Password: ");
  1417. wattroff(betregion,COLOR_PAIR(5));
  1418. wrefresh(betregion);
  1419. noecho();
  1420. wgetnstr (betregion,password,49);
  1421. wattron(betregion,COLOR_PAIR(5));
  1422. wprintw(betregion,"TOTP (2FA) [Leave empty if you don't use 2FA]: ");
  1423. wattroff(betregion,COLOR_PAIR(5));
  1424. wrefresh(betregion);
  1425. echo();
  1426. wgetnstr(betregion,totp,9);
  1427. curs_set(0);
  1428. chunk.memory = malloc (1);
  1429. chunk.size = 0;
  1430. if (curl) {
  1431. if (strlen(totp)==0)
  1432. sprintf (request,"a=Login&Key=f2268504b4a04d5e9e75f6cc199a4763&Username=%s&Password=%s",username,password);
  1433. else
  1434. sprintf (request,"a=Login&Key=f2268504b4a04d5e9e75f6cc199a4763&Username=%s&Password=%s&Totp=%s",username,password,totp);
  1435. curl_easy_setopt (curl, CURLOPT_URL,"https://www.999dice.com/api/web.aspx");
  1436. curl_easy_setopt (curl, CURLOPT_POSTFIELDS, request);
  1437. curl_easy_setopt (curl, CURLOPT_USERAGENT, version);
  1438. curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20L);
  1439. curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  1440. curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk);
  1441. res = curl_easy_perform (curl);
  1442. if (res != CURLE_OK) {
  1443. fprintf (stderr, "curl_easy_perform() failed: %s\n",
  1444. curl_easy_strerror (res));
  1445. endwin();
  1446. exit(9);
  1447. } else {
  1448. // chunk.memory has result
  1449. // and it looks like
  1450. // {"SessionCookie":"12345678901234567890123456789012","MaxBetBatchSize":200,"clientseed":0,"ReferredById":null,"BetCount":117690,"BetPayIn":-3075313,"BetPayOut":2994301,"BetWinCount":41129,"AccountId":1485915,"Balance":0,"Email":null,"EmergencyAddress":null,"DepositAddress":"1LLFFt47uviu14S9VAd1KSMej9E45iqtzM","Doge":{"BetCount":359274,"BetPayIn":-434555444618186,"BetPayOut":420227517351790,"BetWinCount":25353,"Balance":33102001998,"DepositAddress":"DS9auodJVYpXmvuB4ipTB7J9x4zpoToqE8"},"LTC":{"BetCount":20748,"BetPayIn":-12053652057,"BetPayOut":11664803985,"BetWinCount":3936,"Balance":0,"DepositAddress":null},"ETH":{"BetCount":20,"BetPayIn":0,"BetPayOut":0,"BetWinCount":3,"Balance":0,"DepositAddress":null}}
  1451. if (strstr (chunk.memory, "SessionCookie")) {
  1452. token = strtok (chunk.memory, "\"");
  1453. while (token != NULL) {
  1454. if (strstr(token,"SessionCookie")) {
  1455. token = strtok (NULL, "\"");
  1456. token = strtok (NULL, "\"");
  1457. if ((fd=fopen("account.data","w+")) != NULL) {
  1458. fprintf(fd,"SessionCookie=\"%s\"\n",token);
  1459. fclose(fd);
  1460. }
  1461. }
  1462. if (strstr(token,"DepositAddress")) {
  1463. token = strtok (NULL, "\"");
  1464. token = strtok (NULL, "\"");
  1465. if ((fd=fopen("account.data","a+")) != NULL) {
  1466. fprintf(fd,"%sAddress=\"%s\"\n",currency,token);
  1467. fclose(fd);
  1468. }
  1469. }
  1470. if (strstr(token,"MaxBetBatchSize")) {
  1471. token = strtok (NULL, ",");
  1472. if ((fd=fopen("account.data","a+")) != NULL) {
  1473. fprintf(fd,"MaxBetBatchSize=%s\n",token+1);
  1474. fclose(fd);
  1475. }
  1476. }
  1477. if (strstr(token,"AccountId")) {
  1478. token = strtok (NULL, ",");
  1479. if ((fd=fopen("account.data","a+")) != NULL) {
  1480. fprintf(fd,"AccountId=%s\n",token+1);
  1481. fclose(fd);
  1482. }
  1483. }
  1484. if (strstr(token,"Balance")) {
  1485. token = strtok (NULL, ",");
  1486. balance=strtod(token+1,&ptr);
  1487. sprintf(filename,"bnow.%s",currency);
  1488. if ((fd=fopen(filename,"a+")) != NULL) {
  1489. fprintf(fd,"%i:%.8f\n",streamprefix,balance/1e8);
  1490. fclose(fd);
  1491. }
  1492. }
  1493. if (strstr(token,"Doge")) strcpy(currency,"doge");
  1494. if (strstr(token,"LTC")) strcpy(currency,"ltc");
  1495. if (strstr(token,"ETH")) strcpy(currency,"eth");
  1496. token = strtok (NULL, "\"");
  1497. }
  1498. token = strtok (chunk.memory, ":");
  1499. token = strtok (NULL, "\"");
  1500. endwin();
  1501. if (strlen(totp)!=0) {
  1502. if ((fd=fopen("account.data","a+")) != NULL) {
  1503. fprintf(fd,"TOTP=yes\n");
  1504. fclose(fd);
  1505. }
  1506. }
  1507. printf ("\nLogin successful. If you ever get \"Invalid Session\" from server, delete the file »account.data« to relogin.\n" );
  1508. exit(0);
  1509. } else {
  1510. endwin();
  1511. printf("Login didn't work. Server returned:\n%s\n",chunk.memory);
  1512. exit(1);
  1513. }
  1514. }
  1515. }
  1516. }
  1517.  
  1518. int securitycheck() {
  1519. FILE *fp;
  1520. int line_num = 1;
  1521. char line[512];
  1522.  
  1523. if((fp = fopen(botname, "r")) == NULL) return(-1);
  1524. while(fgets(line, 512, fp) != NULL) {
  1525. if((strstr(line, "os.execute")) != NULL) {
  1526. printf("WARNING! This lua-script contain an os.execute() call on line: %d\n", line_num);
  1527. printf("\n%s\n", line);
  1528. printf("\nThis is a potential security risk. Please examine the line closely\n");
  1529. printf("Press CTRL-c to exit or wait 15 seconds\n");
  1530. sleep(15);
  1531. }
  1532. line_num++;
  1533. }
  1534. fclose(fp);
  1535. return(0);
  1536. }
  1537.  
  1538.  
  1539. int main (int argc, char *argv[]) {
  1540. gettimeofday(&begin,(struct timezone *)0);
  1541. int opt;
  1542. srand48 (time (NULL) + 327980);
  1543. getcwd (cwd, sizeof (cwd));
  1544. char *base = strrchr(cwd, '/');
  1545. strncpy(dirname,base+1,25);
  1546. printf ("\ek%s\e\\", cwd);
  1547. // install USR1 handler → exit on next max balance
  1548. signal (10, signalHandlerUSR1);
  1549.  
  1550. // cmdline parsing
  1551. if (argc < 2) {
  1552. printf("\nluabot is a fast 999dice bot capable to handle DiceBot style lua files\n\n");
  1553. printf("Copyright (C) 2020 Elele <elele@secmail.pro>\n");
  1554. printf("This program comes with ABSOLUTELY NO WARRANTY\n");
  1555. printf("This is free software, and you are welcome to redistribute it\n");
  1556. printf("under conditions of GPL3 or later. See file LICENSE for details.\n");
  1557. printf("\n");
  1558. printf(" call it as: luabot [options] <name_of_your_lua_file>\n");
  1559. printf("\noptions:\n");
  1560. printf(" -c <currency> # set currency, to btc, doge (default), ltc or eth\n");
  1561. printf(" -H <amount> # enter interactive mode after losing <amount>\n");
  1562. printf(" -h <percent> # enter interactive mode after losing <percent>%%\n");
  1563. printf(" -O \"command\" # override OnStopCommand from account.data\n");
  1564. printf(" -P <amount> # set an amount of proft that should enable profit ensurance\n");
  1565. printf(" -n # disable stop on losing half profit after winning 2000*basebet\n");
  1566. printf(" -g <amount> # stop when this balance has been reached\n");
  1567. printf(" -G <amount> # stop when this profit has been reached\n");
  1568. printf(" -t <turns> # after running for <turns> bets, exit on reaching max. balance\n");
  1569. printf(" -k # keep betlog file (no matter how large it grows)\n");
  1570. printf(" -S # exit instead of interactive mode when run into stoppoint\n");
  1571. printf(" -m # start a manual session\n");
  1572. printf(" -X # exit when detecting as server seed hash mismatch\n");
  1573. printf(" -r # set betcount (useful for stats when resuming a session\n");
  1574. printf(" -w # set wincount (useful for stats when resuming a session\n");
  1575. printf(" -p # set a streamprefix for bnow file (useful with driveGnuPlots.pl)\n");
  1576. printf("\n\nSetting a bottom stop with -h or -H is mandatory, if you don't want luabot\n");
  1577. printf("to handle stop points (or going interactive) at all use luabot -h 100 -n \n");
  1578. printf("\nGood Luck!\n\n");
  1579. return 1;
  1580. }
  1581.  
  1582. while ((opt = getopt(argc, argv, ":s:c:h:H:SkmnNr:w:t:g:G:Xp:O:P:A:")) != -1) {
  1583. switch (opt) {
  1584. case 's': strncpy(sessioncookie,optarg,32);
  1585. break;
  1586. case 'c': strncpy(currency,optarg,4);
  1587. break;
  1588. case 'H': stopamount=atol(optarg)*1e8;
  1589. break;
  1590. case 'h': percentualStop=atof(optarg);
  1591. break;
  1592. case 'k': keeplogfile=1;
  1593. break;
  1594. case 'S': hardstop=1; alarmOn=0;
  1595. break;
  1596. case 'm': manualSession=1;
  1597. break;
  1598. case 'n': nostoploss=1;
  1599. break;
  1600. case 'N': nobetdisplay=1;
  1601. break;
  1602. case 'r': betcount=atoi(optarg);
  1603. break;
  1604. case 'w': wincount=atoi(optarg);
  1605. break;
  1606. case 't': turnlimit=atoi(optarg);
  1607. break;
  1608. case 'g': goal=atof(optarg);
  1609. break;
  1610. case 'G': goalDelta=atof(optarg);
  1611. break;
  1612. case 'X': exitOnMismatch=1;
  1613. break;
  1614. case 'p': streamprefix=atoi(optarg);
  1615. break;
  1616. case 'O': overrideOnStopCmd=1;
  1617. strncpy(OnStopCommand,optarg,255);
  1618. OnStopCmd=1;
  1619. break;
  1620. case 'A': overrideAutowd=1;
  1621. strncpy(autowd,optarg,199);
  1622. break;
  1623. case 'P': profitensurance=(long long) (atof(optarg)*1e8);
  1624. break;
  1625. case '?': printf("unknown option: %c\n",optopt);
  1626. break;
  1627. }
  1628. }
  1629. // last parameter is bot name
  1630. for(; optind < argc; optind++){
  1631. strncpy(botname, argv[optind],39);
  1632. }
  1633.  
  1634. // check for os.execute()
  1635. securitycheck();
  1636.  
  1637. // INIT CURL
  1638. curl_global_init (CURL_GLOBAL_ALL);
  1639. curl = curl_easy_init ();
  1640.  
  1641. /* init ncurses */
  1642. initscr ();
  1643. curs_set (0);
  1644. cbreak ();
  1645. noecho ();
  1646. nodelay (stdscr, TRUE);
  1647. start_color ();
  1648. getmaxyx (stdscr, height, width);
  1649. heightavail=height-8;
  1650. if ((heightavail%2==0)) {
  1651. betregion = newwin (heightavail/2+1,width, 8, 0);
  1652. msgregion = newwin (heightavail/2-1,width, heightavail/2+10,0);
  1653. } else {
  1654. betregion = newwin (heightavail/2+1,width, 8, 0);
  1655. msgregion = newwin (heightavail/2,width, heightavail/2+10,0);
  1656. }
  1657. manualline = heightavail/2+8;
  1658. scrollok (betregion, TRUE);
  1659. scrollok (msgregion, TRUE);
  1660. use_default_colors();
  1661. init_pair (1, COLOR_GREEN, -1);
  1662. init_pair (2, COLOR_YELLOW, -1);
  1663. init_pair (3, COLOR_RED, -1);
  1664. init_pair (4, COLOR_CYAN, -1);
  1665. init_pair (5, COLOR_WHITE, -1);
  1666. init_pair (100, COLOR_GREEN, -1); // the following pairs are for color gradient
  1667. init_pair (99, 48, -1); // on 256 color terminal. TODO: check if colors availble
  1668. init_pair (98, 82, -1);
  1669. init_pair (97,118, -1);
  1670. init_pair (96,154, -1);
  1671. init_pair (95,190, -1);
  1672. init_pair (94,226, -1);
  1673. init_pair (93,220, -1);
  1674. init_pair (92,214, -1);
  1675. init_pair (91,208, -1);
  1676. init_pair (90,202, -1);
  1677. init_pair (89,196, -1);
  1678. init_pair (88,160, -1);
  1679. init_pair (87,124, -1);
  1680. init_pair (86, 88, -1);
  1681.  
  1682. if ((nobetdisplay == 0) || (manualActive==1)) {
  1683. attron(COLOR_PAIR(5));
  1684. mvprintw (7, 18, "balance chance betsize profit");
  1685. attroff(COLOR_PAIR(5));
  1686. }
  1687. attron(COLOR_PAIR(5));
  1688. mvprintw (0, 60, botname);
  1689. mvprintw (1, 60, dirname);
  1690. attroff(COLOR_PAIR(5));
  1691.  
  1692. // read account.data or call login
  1693. L = luaL_newstate ();
  1694. luaL_openlibs (L);
  1695. if (luaL_loadfile (L, "account.data")) {
  1696. // we couldn't open account.data so we need to make a new one
  1697. login();
  1698. exit(0);
  1699. }
  1700. if (lua_pcall (L, 0, 0, 0))
  1701. bailout (L, "lua_pcall() failed",1);
  1702. lua_getglobal (L, "SessionCookie");
  1703. strncpy(sessioncookie,lua_tostring(L,-1),32);
  1704. if (overrideOnStopCmd==0) {
  1705. lua_getglobal (L, "OnStopCommand");
  1706. if (lua_tostring(L,-1) != NULL) {
  1707. strncpy(OnStopCommand,lua_tostring(L,-1),255);
  1708. OnStopCmd=1;
  1709. }
  1710. }
  1711. if (overrideAutowd==0) {
  1712. lua_getglobal (L, "WithdrawAddress");
  1713. if (lua_tostring(L,-1) != NULL) {
  1714. strncpy(autowd,lua_tostring(L,-1),199);
  1715. }
  1716. }
  1717. lua_close(L);
  1718.  
  1719. /* get balance from file or api before init botscript */
  1720. getBalance();
  1721. maxbalance=balance;
  1722. minbalance=balance;
  1723. bstart=balance;
  1724. if (goalDelta>0) goal=((double) balance / 1e8)+goalDelta;
  1725. if (percentualStop>=100) percentualStop=100;
  1726. if (percentualStop>0) stopamount=balance*percentualStop/100;
  1727. if (manualSession==1) manualMode();
  1728. else {
  1729. /* init Lua */
  1730. L = luaL_newstate ();
  1731. luaL_openlibs (L);
  1732. if (luaL_loadfile (L, botname))
  1733. bailout (L, "luaL_loadfile() failed",1);
  1734. luaopen=1;
  1735. lua_pushnumber (L, (double) (balance / 1e8));
  1736. lua_setglobal (L, "balance");
  1737. lua_pushnumber (L, MaxBets);
  1738. lua_setglobal (L, "MaxBets");
  1739. lua_pushstring (L, currency);
  1740. lua_setglobal (L, "currency");
  1741. if (lua_pcall (L, 0, 0, 0))
  1742. bailout (L, "lua_pcall() failed",1);
  1743. lua_getglobal (L, "MaxBets");
  1744. MaxBets = lua_tonumber(L, -1);
  1745. if (MaxBets==0) {
  1746. // for single bets
  1747. lua_getglobal (L, "chance");
  1748. chance = lua_tonumber (L, -1);
  1749. lua_getglobal (L, "Low");
  1750. Low = lua_tonumber (L, -1);
  1751. lua_getglobal (L, "High");
  1752. High = lua_tonumber (L, -1);
  1753. lua_getglobal (L, "clientseed");
  1754. clientseed = lua_tonumber (L, -1);
  1755. lua_getglobal (L, "nextbet");
  1756. nextbet = lua_tonumber (L, -1);
  1757. basebet = nextbet;
  1758. lua_pushcfunction (L, msg);
  1759. lua_setglobal(L, "print");
  1760. lua_pushcfunction (L, Withdraw);
  1761. lua_setglobal(L, "withdraw");
  1762. lua_pushcfunction (L, stoplua);
  1763. lua_setglobal (L, "stop");
  1764. lua_pushnumber (L, bnow);
  1765. lua_setglobal (L, "balance");
  1766. } else { // for PlaceAutomatedBets
  1767. lua_pushcfunction (L, msg);
  1768. lua_setglobal(L, "print");
  1769. lua_pushcfunction (L, stoplua);
  1770. lua_setglobal (L, "stop");
  1771. lua_pushcfunction (L, Withdraw);
  1772. lua_setglobal(L, "withdraw");
  1773. lua_getglobal (L, "BasePayIn");
  1774. BasePayIn=(int64_t) (lua_tonumber(L,-1)*1e8);
  1775. lua_getglobal (L, "chance");
  1776. chance = lua_tonumber (L, -1);
  1777. lua_getglobal (L, "Low");
  1778. Low = lua_tonumber (L, -1);
  1779. lua_getglobal (L, "High");
  1780. High = lua_tonumber (L, -1);
  1781. lua_getglobal (L, "clientseed");
  1782. clientseed = lua_tonumber (L, -1);
  1783. lua_getglobal (L, "ResetOnWin");
  1784. ResetOnWin= lua_toboolean (L, -1);
  1785. lua_getglobal (L, "ResetOnLose");
  1786. ResetOnLose= lua_toboolean (L, -1);
  1787. lua_getglobal (L, "IncreaseOnWinPercent");
  1788. IncreaseOnWinPercent=lua_tonumber(L, -1);
  1789. lua_getglobal (L, "IncreaseOnLosePercent");
  1790. IncreaseOnLosePercent=lua_tonumber(L, -1);
  1791. lua_getglobal (L, "MaxPayIn");
  1792. AutoMaxPayIn=(int64_t) (lua_tonumber(L, -1)*100000000);
  1793. lua_getglobal (L, "ResetOnLoseMaxBet");
  1794. ResetOnLoseMaxBet=lua_toboolean(L,-1);
  1795. lua_getglobal (L, "StopOnLoseMaxBet");
  1796. StopOnLoseMaxBet=lua_toboolean(L,-1);
  1797. lua_getglobal (L, "StopMaxBalance");
  1798. StopMaxBalance=(int64_t) (lua_tonumber(L,-1)*100000000);
  1799. lua_getglobal (L, "StopMinBalance");
  1800. StopMinBalance=(int64_t) (lua_tonumber(L,-1)*100000000);
  1801. lua_getglobal (L, "StartingPayIn");
  1802. StartingPayIn=(int64_t) (lua_tonumber(L,-1)*100000000);
  1803. lua_getglobal (L, "info");
  1804. infoval=lua_tonumber(L,-1);
  1805. }
  1806.  
  1807.  
  1808. // USER INTERFACE
  1809. while ((kbd != 'Q') && (kbd != 'D')) {
  1810. timeout (0);
  1811. kbd = getch ();
  1812. switch (kbd) {
  1813. case 'h':
  1814. stopamount = stopamount*110/100;
  1815. stopbetting = 0;
  1816. break;
  1817. case 'c':
  1818. stopbetting = 0;
  1819. break;
  1820. case 'p':
  1821. stopbetting = 1;
  1822. break;
  1823. case 's':
  1824. stoploss = 1;
  1825. attron (A_BOLD);
  1826. attron (COLOR_PAIR (3));
  1827. mvprintw (1, 29, "STOP5 @");
  1828. attroff (COLOR_PAIR (3));
  1829. attroff (A_BOLD);
  1830. break;
  1831. case 'x':
  1832. attron (A_BOLD);
  1833. attron (COLOR_PAIR (4));
  1834. mvprintw (0, 60, "EXIT on MAX balance");
  1835. attroff (COLOR_PAIR (4));
  1836. attroff (A_BOLD);
  1837. exitOnMax = 1;
  1838. break;
  1839. }
  1840. if (((double) balance / 1e8 > goal) && (goal > 0)) {
  1841. exitOnMax = 1;
  1842. }
  1843. if (stopbetting == 0) { // betting loop
  1844. // is this single or autobet?
  1845. if (MaxBets<2) Bet(nextbet);
  1846. else {
  1847. int rv=autobets();
  1848. if (rv==2) {
  1849. nextbet=(double) BasePayIn/1e8;
  1850. MaxBets=0;
  1851. exitOnMax=1;
  1852. manualMode();
  1853. }
  1854. }
  1855.  
  1856. sessionprofit = balance - bstart;
  1857. // https://steemit.com/dicebot/@seuntjie/dicebot-programmer-mode-tutorial-1-1-variables
  1858. if (MaxBets>0) {
  1859. lua_pushnumber (L, WinCount); // with autobets set WinCount to the number of wins of last betrow
  1860. lua_setglobal(L, "WinCount");
  1861. lua_pushnumber (L, BetCount); // set BetCount to the number of wins of last betrow
  1862. lua_setglobal(L, "BetCount");
  1863. }
  1864.  
  1865. lua_pushnumber (L, (double) balance / 100000000);
  1866. lua_setglobal (L, "balance");
  1867.  
  1868. lua_pushnumber (L, secret);
  1869. lua_setglobal (L, "secret");
  1870.  
  1871. lua_pushboolean (L, win);
  1872. lua_setglobal (L, "win");
  1873.  
  1874. lua_pushnumber (L, sessionprofit/1e8);
  1875. lua_setglobal (L, "profit");
  1876.  
  1877. lua_pushnumber (L, currentprofit);
  1878. lua_setglobal (L, "currentprofit");
  1879.  
  1880. lua_pushnumber (L, currentstreak);
  1881. lua_setglobal (L, "currentstreak");
  1882.  
  1883. lua_pushnumber (L, nextbet);
  1884. lua_setglobal (L, "previousbet");
  1885.  
  1886. lua_pushnumber (L, betcount);
  1887. lua_setglobal (L, "bets");
  1888.  
  1889. lua_pushnumber (L, wincount);
  1890. lua_setglobal (L, "wins");
  1891.  
  1892. lua_pushnumber (L, losecount);
  1893. lua_setglobal (L, "losses");
  1894.  
  1895. // chance: to be set by lua script
  1896. // bethigh: not supported
  1897. // lastBet: not supported
  1898. // currencies: not supported
  1899. // currency: not supported
  1900. // enablesrc: not supported
  1901. // enablezz: not supported
  1902.  
  1903. lua_getglobal (L, "dobet");
  1904. if (lua_pcall (L, 0, 0, 0))
  1905. bailout (L, "lua_pcall() failed",1); // run dobet()
  1906. lua_getglobal (L, "MaxBets");
  1907. MaxBets = lua_tonumber(L, -1);
  1908. if (MaxBets==0) {
  1909. // for single bets
  1910. lua_getglobal (L, "chance");
  1911. chance = lua_tonumber (L, -1);
  1912. lua_getglobal (L, "Low");
  1913. Low = lua_tonumber (L, -1);
  1914. lua_getglobal (L, "High");
  1915. High = lua_tonumber (L, -1);
  1916. lua_getglobal (L, "clientseed");
  1917. clientseed = lua_tonumber (L, -1);
  1918. lua_getglobal (L, "nextbet");
  1919. nextbet = lua_tonumber (L, -1);
  1920. basebet = nextbet;
  1921. lua_getglobal (L, "info");
  1922. infoval = (double) lua_tonumber (L, -1);
  1923. lua_pushcfunction (L, msg);
  1924. lua_setglobal(L, "print");
  1925. lua_pushcfunction (L, stoplua);
  1926. lua_setglobal (L, "stop");
  1927. lua_pushnumber (L, bnow);
  1928. lua_setglobal (L, "balance");
  1929. } else { // for PlaceAutomatedBets
  1930. lua_getglobal (L, "BasePayIn");
  1931. BasePayIn=(int64_t) (lua_tonumber(L,-1)*1e8);
  1932. lua_getglobal (L, "chance");
  1933. chance = lua_tonumber (L, -1);
  1934. lua_getglobal (L, "Low");
  1935. Low = lua_tonumber (L, -1);
  1936. lua_getglobal (L, "High");
  1937. High = lua_tonumber (L, -1);
  1938. lua_getglobal (L, "clientseed");
  1939. clientseed = lua_tonumber (L, -1);
  1940. lua_getglobal (L, "ResetOnWin");
  1941. ResetOnWin= lua_toboolean (L, -1);
  1942. lua_getglobal (L, "ResetOnLose");
  1943. ResetOnLose= lua_toboolean (L, -1);
  1944. lua_getglobal (L, "IncreaseOnWinPercent");
  1945. IncreaseOnWinPercent=lua_tonumber(L, -1);
  1946. lua_getglobal (L, "IncreaseOnLosePercent");
  1947. IncreaseOnLosePercent=lua_tonumber(L, -1);
  1948. lua_getglobal (L, "MaxPayIn");
  1949. AutoMaxPayIn=(int64_t) (lua_tonumber(L, -1)*1e8);
  1950. lua_getglobal (L, "ResetOnLoseMaxBet");
  1951. ResetOnLoseMaxBet=lua_toboolean(L,-1);
  1952. lua_getglobal (L, "StopOnLoseMaxBet");
  1953. StopOnLoseMaxBet=lua_toboolean(L,-1);
  1954. lua_getglobal (L, "StopMaxBalance");
  1955. StopMaxBalance=(int64_t) (lua_tonumber(L,-1)*100000000);
  1956. lua_getglobal (L, "StopMinBalance");
  1957. StopMinBalance=(int64_t) (lua_tonumber(L,-1)*100000000);
  1958. lua_getglobal (L, "StartingPayIn");
  1959. StartingPayIn=(int64_t) (lua_tonumber(L,-1)*100000000);
  1960. lua_getglobal (L, "info");
  1961. infoval = (double) lua_tonumber (L, -1);
  1962. }
  1963. lua_settop (L, 0);
  1964. }
  1965. }
  1966. }
  1967. curl_easy_cleanup (curl);
  1968. curl_global_cleanup ();
  1969.  
  1970. if (luaopen) lua_close (L); // not a manual session only
  1971. endwin ();
  1972.  
  1973. logfile = fopen ("luabot.log", "a+");
  1974. fprintf (logfile, "%lu ", (unsigned long) time (NULL));
  1975. fprintf (logfile, "won %i of %i bets (%.2f%%) at %.2f%% chance, ", wincount,
  1976. betcount, (double) wincount / betcount * 100, chance);
  1977. fprintf (logfile, "Balance: %.8f %s, ", (double) balance / 1e8, currency);
  1978. fprintf (logfile, "%.8f refPayment, ", (double) (wagered / 1e8 / 2000));
  1979. fprintf (logfile, " bmax: %.8f ", (double) maxbalance / 100000000);
  1980. fprintf (logfile, " maxdiff: %.8f ", (double) maxdiff / 100000000);
  1981. fprintf (logfile, " lastmaxmax: %i ", lastmaxmax);
  1982. fprintf (logfile, " MaxPayIn: %.8f ", (double) MaxPayIn / 1e8);
  1983. fprintf (logfile, " minstreak: %i, maxstreak: %i, ", minstreak, maxstreak);
  1984. fprintf (logfile, ", %s, ", botname);
  1985. fprintf (logfile, "%s \n", exitReason);
  1986.  
  1987. fclose (logfile);
  1988. seconds = end.tv_sec - begin.tv_sec;
  1989. printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\033[1m\n\nluabot exit: %s\n", exitReason);
  1990. printf ("\nBalance: %.8f %s\n\033[0m", (double) balance / 1e8,
  1991. currency);
  1992. printf ("%8i bets done in %i seconds (%.2f bets/sec) ", betcount,seconds,(double) betcount/seconds);
  1993. printf ("\n ref payment: %.8f\n", (double) (wagered / 1e8 / 2000));
  1994. printf (" bmax: %.8f ", (double) maxbalance / 1e8);
  1995. printf (" MaxPayIn: %.8f\n", (double) MaxPayIn / 1e8);
  1996. if (kbd == 'D')
  1997. exit (9);
  1998. exit (0);
  1999. }
  2000.  
Add Comment
Please, Sign In to add comment