Advertisement
mf22

D2BotTimerRefresh.dbj

Aug 25th, 2019
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var StarterConfig = {
  2.     MinGameTime: rand(150, 180), // Minimum game length in seconds. If a game is ended too soon, the rest of the time is waited in the lobby
  3.     CreateGameDelay: rand(5, 15), // Seconds to wait before creating a new game
  4.  
  5.     SwitchKeyDelay: 0, // Seconds to wait before switching a used/banned key or after realm down
  6.  
  7.     CrashDelay: rand(60, 120), // Seconds to wait after a d2 window crash
  8.     RealmDownDelay: rand(3, 6), // Minutes to wait after getting Realm Down message
  9.     UnableToConnectDelay: rand(5, 8), // Minutes to wait after Unable To Connect message
  10.     CDKeyInUseDelay: rand(5, 8), // Minutes to wait before connecting again if CD-Key is in use. SwitchKeys overrides this!
  11.     ConnectingTimeout: rand(20, 30), // Seconds to wait before cancelling the 'Connecting...' screen
  12.     PleaseWaitTimeout: rand(10, 20), // Seconds to wait before cancelling the 'Please Wait...' screen
  13.     WaitInLineTimeout: rand(30, 60) // Seconds to wait before cancelling the 'Waiting in Line...' screen
  14. };
  15.  
  16. var RefreshAccounts = {
  17.         /* Format:
  18.             "account1/password1/realm": ["charname1", "charname2 etc"],
  19.             "account2/password2/realm": ["charnameX", "charnameY etc"],
  20.             "account3/password3/realm": ["all"]
  21.  
  22.             To log a full account, put "account/password/realm": ["all"]
  23.  
  24.             realm = useast, uswest, europe or asia
  25.  
  26.             Individual entries are separated with a comma.
  27.         */
  28.  
  29.     "account/password/realm": ["all"]
  30.  
  31. }
  32.  
  33. // No touchy!
  34. include("json2.js");
  35. include("OOG.js");
  36. include("common/misc.js");
  37.  
  38. if (!FileTools.exists("data/" + me.profile + ".json")) {
  39.     DataFile.create();
  40. }
  41.  
  42. var handle, gameInfo, connectFail, currAcc, charList,
  43.     accounts = [],
  44.     chars = [];
  45.  
  46. function save(hash, data) {
  47.     var filename = "data/secure/" + hash + ".txt";
  48.     FileTools.writeText(filename, data);
  49. }
  50.  
  51. function parseInfo() {
  52.     var i;
  53.  
  54.     for (i in RefreshAccounts) {
  55.         if (RefreshAccounts.hasOwnProperty(i) && typeof i === "string") {
  56.             accounts.push(i);
  57.             chars.push(RefreshAccounts[i]);
  58.         }
  59.     }
  60. }
  61.  
  62. function ReceiveCopyData(mode, msg) {
  63.     switch (msg) {
  64.     case "Handle":
  65.         handle = mode;
  66.  
  67.         break;
  68.     }
  69.  
  70.     switch (mode) {
  71.     case 2: // game info
  72.         print("Recieved Game Info");
  73.  
  74.         gameInfo = JSON.parse(msg);
  75.  
  76.         break;
  77.     case 4:
  78.         // Heartbeat ping
  79.         if (msg === "pingreq") {
  80.             sendCopyData(null, me.windowtitle, 4, "pingrep");
  81.         }
  82.  
  83.         break;
  84.     }
  85. }
  86.  
  87. function timeoutDelay(text, time) {
  88.     var endTime = getTickCount() + time;
  89.  
  90.     while (getTickCount() < endTime) {
  91.         D2Bot.updateStatus(text + " (" + Math.floor((endTime - getTickCount()) / 1000) + "s)");
  92.         delay(500);
  93.     }
  94. }
  95.  
  96. function locationTimeout(time, location) {
  97.     var endtime = getTickCount() + time;
  98.  
  99.     while (getLocation() === location && endtime > getTickCount()) {
  100.         delay(500);
  101.     }
  102.  
  103.     return (getLocation() !== location);
  104. }
  105.  
  106. function updateCount() {
  107.     D2Bot.updateCount();
  108.     delay(1000);
  109.     ControlAction.click(6, 264, 366, 272, 35);
  110.  
  111.     try {
  112.         login(me.profile);
  113.     } catch (e) {
  114.  
  115.     }
  116.  
  117.     delay(1000);
  118.     ControlAction.click(6, 33, 572, 128, 35);
  119. }
  120.  
  121. function countdowntimer (tick) {
  122.     return " (" + new Date(tick - getTickCount()).toISOString().slice(11, -5) + ")";
  123. }
  124.  
  125. function main() {
  126.     addEventListener('copydata', ReceiveCopyData);
  127.  
  128.     while (!handle) {
  129.         delay(100);
  130.     }
  131.  
  132.     DataFile.updateStats("handle", handle);
  133.     D2Bot.init();
  134.     load("tools/heartbeat.js");
  135.  
  136.     while (!gameInfo) {
  137.         D2Bot.requestGameInfo();
  138.         delay(500);
  139.     }
  140.  
  141.     if (gameInfo.rdBlocker) {
  142.         D2Bot.printToConsole("You must disable RD Blocker for Timer Refresh to work properly. Stopping.");
  143.         D2Bot.stop(me.profile, true);
  144.  
  145.         return;
  146.     }
  147.  
  148.     parseInfo();
  149.  
  150.     if (gameInfo.error) {
  151.         if (!!DataFile.getStats().debugInfo) {
  152.             gameInfo.crashInfo = DataFile.getStats().debugInfo;
  153.  
  154.             D2Bot.printToConsole("Crash Info: Script: " + JSON.parse(gameInfo.crashInfo).currScript + " Area: " + JSON.parse(gameInfo.crashInfo).area, 10);
  155.         }
  156.  
  157.         ControlAction.timeoutDelay("Crash Delay", StarterConfig.CrashDelay * 1e3);
  158.         D2Bot.updateRuns();
  159.     }
  160.  
  161.     DataFile.updateStats("debugInfo", JSON.stringify({currScript: "none", area: "out of game"}));
  162.  
  163.     while (true) {
  164.         locationAction(getLocation());
  165.         delay(1000);
  166.     }
  167. }
  168.  
  169. function locationAction(location) {
  170.     var i, control, string, text, currChar,
  171.         obj = {};
  172.  
  173. MainSwitch:
  174.     switch (location) {
  175.     case 0:
  176.         break;
  177.     case 1: // Lobby
  178.     case 3: // Lobby Chat
  179.         D2Bot.updateStatus("Lobby");
  180.  
  181.         // delay and show remaining lobby time
  182.         var tick,
  183.             tick = getTickCount() + rand(15000, 25000);
  184.  
  185.         D2Bot.printToConsole("  char: " + me.charname);
  186.  
  187.         while (getTickCount() - tick < 0) {
  188.             D2Bot.updateStatus("Lobby: " + countdowntimer(tick));
  189.             delay(1000);
  190.         }
  191.  
  192.         ControlAction.click(6, 693, 490, 80, 20); // Quit from Lobby
  193.         delay(rand(1500, 3000));
  194.  
  195.         break;
  196.     case 2: // Waiting In Line
  197.         D2Bot.updateStatus("Waiting...");
  198.         locationTimeout(StarterConfig.WaitInLineTimeout * 1e3, location);
  199.         ControlAction.click(6, 433, 433, 96, 32);
  200.  
  201.         break;
  202.     case 4: // Create Game
  203.         D2Bot.updateStatus("Creating Game");
  204.  
  205.         break;
  206.     case 5: // Join Game
  207.         break;
  208.     case 6: // Ladder
  209.         break;
  210.     case 7: // Channel List
  211.         break;
  212.     case 8: // Main Menu
  213.     case 9: // Login
  214.     case 18: // D2 Splash
  215.         if (!accounts.length) {
  216.             FileTools.remove("logs/CharLog.json");
  217.             FileTools.remove("data/" + me.profile + ".json");
  218.             delay(1000);
  219.             D2Bot.printToConsole("Done refreshing countdown timer!");
  220.             D2Bot.stop(me.profile, true);
  221.  
  222.             break;
  223.         }
  224.  
  225.         if (FileTools.exists("logs/CharLog.json")) {
  226.             obj = JSON.parse(FileTools.readText("logs/CharLog.json"));
  227.  
  228.             if (obj.currAcc) {
  229.                 for (i = 0; i < accounts.length; i += 1) {
  230.                     if (accounts[i].split("/")[0] === obj.currAcc) {
  231.                         accounts.splice(0, i);
  232.                         chars.splice(0, i);
  233.  
  234.                         i -= 1;
  235.  
  236.                         break;
  237.                     }
  238.                 }
  239.             }
  240.         }
  241.  
  242.         currAcc = accounts[0];
  243.         currAcc = currAcc.split("/");
  244.         charList = chars[0];
  245.         obj.currAcc = currAcc[0];
  246.  
  247.         FileTools.writeText("logs/CharLog.json", JSON.stringify(obj));
  248.         save(md5(currAcc[2].toLowerCase() + currAcc[0].toLowerCase()), currAcc[1]);
  249.         D2Bot.printToConsole("account: " + currAcc[0]);
  250.         delay(rand(500, 1500));
  251.         ControlAction.loginAccount({account: currAcc[0], password: currAcc[1], realm: currAcc[2]});
  252.         delay(rand(500, 1500));
  253.         accounts.shift(); // remove current account from the list
  254.  
  255.         break;
  256.     case 10: // Login Error
  257.         string = "";
  258.         text = ControlAction.getText(4, 199, 377, 402, 140);
  259.  
  260.         if (text) {
  261.             for (i = 0; i < text.length; i += 1) {
  262.                 string += text[i];
  263.  
  264.                 if (i !== text.length - 1) {
  265.                     string += " ";
  266.                 }
  267.             }
  268.  
  269.             switch (string) {
  270.             case getLocaleString(5207):
  271.                 D2Bot.updateStatus("Invalid Password");
  272.                 D2Bot.printToConsole("Invalid Password");
  273.  
  274.                 break;
  275.             case getLocaleString(5208):
  276.                 D2Bot.updateStatus("Invalid Account");
  277.                 D2Bot.printToConsole("Invalid Account");
  278.  
  279.                 break;
  280.             case getLocaleString(5202): // cd key intended for another product
  281.             case getLocaleString(10915): // lod key intended for another product
  282.                 D2Bot.updateStatus("Invalid CDKey");
  283.                 D2Bot.printToConsole("Invalid CDKey: " + gameInfo.mpq, 6);
  284.                 D2Bot.CDKeyDisabled();
  285.  
  286.                 if (gameInfo.switchKeys) {
  287.                     ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  288.                     D2Bot.restart(true);
  289.                 } else {
  290.                     D2Bot.stop(me.profile, true);
  291.                 }
  292.  
  293.                 break;
  294.             case getLocaleString(5199):
  295.                 D2Bot.updateStatus("Disabled CDKey");
  296.                 D2Bot.printToConsole("Disabled CDKey: " + gameInfo.mpq, 6);
  297.                 D2Bot.CDKeyDisabled();
  298.  
  299.                 if (gameInfo.switchKeys) {
  300.                     ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  301.                     D2Bot.restart(true);
  302.                 } else {
  303.                     D2Bot.stop(me.profile, true);
  304.                 }
  305.  
  306.                 break;
  307.             case getLocaleString(10913):
  308.                 D2Bot.updateStatus("Disabled LoD CDKey");
  309.                 D2Bot.printToConsole("Disabled LoD CDKey: " + gameInfo.mpq, 6);
  310.                 D2Bot.CDKeyDisabled();
  311.  
  312.                 if (gameInfo.switchKeys) {
  313.                     ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  314.                     D2Bot.restart(true);
  315.                 } else {
  316.                     D2Bot.stop(me.profile, true);
  317.                 }
  318.  
  319.                 break;
  320.             case getLocaleString(5347):
  321.                 D2Bot.updateStatus("Disconnected");
  322.                 D2Bot.printToConsole("Disconnected");
  323.                 ControlAction.click(6, 335, 412, 128, 35);
  324.  
  325.                 break MainSwitch;
  326.             default:
  327.                 D2Bot.updateStatus("Login Error");
  328.                 D2Bot.printToConsole("Login Error - " + string);
  329.  
  330.                 if (gameInfo.switchKeys) {
  331.                     ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  332.                     D2Bot.restart(true);
  333.                 } else {
  334.                     D2Bot.stop(me.profile, true);
  335.                 }
  336.  
  337.                 break;
  338.             }
  339.         }
  340.  
  341.         ControlAction.click(6, 335, 412, 128, 35);
  342.  
  343.         while (true) {
  344.             delay(1000);
  345.         }
  346.  
  347.         break;
  348.     case 11: // Unable To Connect
  349.         D2Bot.updateStatus("Unable To Connect");
  350.  
  351.         if (connectFail) {
  352.             timeoutDelay("Unable to Connect", StarterConfig.UnableToConnectDelay * 6e4);
  353.  
  354.             connectFail = false;
  355.         }
  356.  
  357.         if (!ControlAction.click(6, 335, 450, 128, 35)) {
  358.             break;
  359.         }
  360.  
  361.         connectFail = true;
  362.  
  363.         break;
  364.     case 12: // Character Select
  365.         // Single Player screen fix
  366.         if (getLocation() === 12 && !getControl(4, 626, 100, 151, 44)) {
  367.             ControlAction.click(6, 33, 572, 128, 35);
  368.  
  369.             break;
  370.         }
  371.  
  372.         if (!charList.length) {
  373.             ControlAction.click(6, 33, 572, 128, 35);
  374.  
  375.             break;
  376.         }
  377.  
  378.         if (charList[0] === "all") {
  379.             charList = ControlAction.getCharacters();
  380.         }
  381.  
  382.         if (FileTools.exists("logs/CharLog.json")) {
  383.             obj = JSON.parse(FileTools.readText("logs/CharLog.json"));
  384.  
  385.             if (obj.currChar) {
  386.                 for (i = 0; i < charList.length; i += 1) {
  387.                     if (charList[i] === obj.currChar) {
  388.                         charList.splice(0, i + 1); // Remove the previous currChar as well
  389.  
  390.                         break;
  391.                     }
  392.                 }
  393.             }
  394.         }
  395.  
  396.         // last char in acc = trigger next acc
  397.         if (!charList.length) {
  398.             accounts.shift(); // remove current account from the list
  399.             chars.shift();
  400.  
  401.             break;
  402.         }
  403.  
  404.         currChar = charList.shift();
  405.         obj.currChar = currChar;
  406.  
  407.         FileTools.writeText("logs/CharLog.json", JSON.stringify(obj));
  408.         ControlAction.loginCharacter({charName: currChar});
  409.  
  410.         break;
  411.     case 13: // Realm Down - Character Select screen
  412.         D2Bot.updateStatus("Realm Down");
  413.         delay(1000);
  414.  
  415.         if (!ControlAction.click(6, 33, 572, 128, 35)) {
  416.             break;
  417.         }
  418.  
  419.         updateCount();
  420.         timeoutDelay("Realm Down", StarterConfig.RealmDownDelay * 6e4);
  421.  
  422.         if (gameInfo.switchKeys) {
  423.             D2Bot.printToConsole("Realm Down - Changing CD-Key");
  424.             timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  425.             D2Bot.restart(true);
  426.         } else {
  427.             D2Bot.restart();
  428.         }
  429.  
  430.         break;
  431.     case 14: // Character Select / Main Menu - Disconnected
  432.         D2Bot.updateStatus("Disconnected");
  433.         delay(500);
  434.         ControlAction.click(6, 351, 337, 96, 32);
  435.         break;
  436.     case 15: // New Character
  437.         break;
  438.     case 16: // Character Select - Please Wait popup
  439.         if (!locationTimeout(StarterConfig.PleaseWaitTimeout * 1e3, location)) {
  440.             ControlAction.click(6, 351, 337, 96, 32);
  441.         }
  442.  
  443.         break;
  444.     case 17: // Lobby - Lost Connection - just click okay, since we're toast anyway
  445.         delay(1000);
  446.         ControlAction.click(6, 351, 337, 96, 32);
  447.         break;
  448.     case 19: // Login - Cdkey In Use
  449.         D2Bot.printToConsole(gameInfo.mpq + " is in use by " + ControlAction.getText(4, 158, 310, 485, 40), 6);
  450.         D2Bot.CDKeyInUse();
  451.  
  452.         if (gameInfo.switchKeys) {
  453.             ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  454.             D2Bot.restart(true);
  455.         } else {
  456.             ControlAction.click(6, 335, 450, 128, 35);
  457.             ControlAction.timeoutDelay("CD-Key in use", StarterConfig.CDKeyInUseDelay * 6e4);
  458.         }
  459.  
  460.         break;
  461.     case 20: // Single Player - Select Difficulty
  462.         break;
  463.     case 21: // Main Menu - Connecting
  464.         if (!locationTimeout(StarterConfig.ConnectingTimeout * 1e3, location)) {
  465.             ControlAction.click(6, 330, 416, 128, 35);
  466.         }
  467.  
  468.         break;
  469.     case 22: // Login - Invalid Cdkey (classic or xpac)
  470.         text = ControlAction.getText(4, 162, 270, 477, 50);
  471.         string = "";
  472.  
  473.         if (text) {
  474.             for (i = 0; i < text.length; i += 1) {
  475.                 string += text[i];
  476.  
  477.                 if (i !== text.length - 1) {
  478.                     string += " ";
  479.                 }
  480.             }
  481.         }
  482.  
  483.         switch (string) {
  484.         case getLocaleString(10914):
  485.             D2Bot.printToConsole(gameInfo.mpq + " LoD key in use by " + ControlAction.getText(4, 158, 310, 485, 40), 6);
  486.             D2Bot.CDKeyInUse();
  487.  
  488.             if (gameInfo.switchKeys) {
  489.                 ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  490.                 D2Bot.restart(true);
  491.             } else {
  492.                 ControlAction.click(6, 335, 450, 128, 35);
  493.                 ControlAction.timeoutDelay("LoD key in use", StarterConfig.CDKeyInUseDelay * 6e4);
  494.             }
  495.  
  496.             break;
  497.         default:
  498.             if (gameInfo.switchKeys) {
  499.                 D2Bot.printToConsole("Invalid CD-Key");
  500.                 ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  501.                 D2Bot.restart(true);
  502.             } else {
  503.                 ControlAction.click(6, 335, 450, 128, 35);
  504.                 ControlAction.timeoutDelay("Invalid CD-Key", StarterConfig.CDKeyInUseDelay * 6e4);
  505.             }
  506.  
  507.             break;
  508.         }
  509.  
  510.         break;
  511.     case 23: // Character Select - Connecting
  512.         if (!locationTimeout(StarterConfig.ConnectingTimeout * 1e3, location)) {
  513.             ControlAction.click(6, 33, 572, 128, 35);
  514.         }
  515.  
  516.         break;
  517.     case 24: // Server Down - not much to do but wait..
  518.         break;
  519.     case 25: // Lobby - Please Wait
  520.         if (!locationTimeout(StarterConfig.PleaseWaitTimeout * 1e3, location)) {
  521.             ControlAction.click(6, 351, 337, 96, 32);
  522.         }
  523.  
  524.         break;
  525.     case 26: // Lobby - Game Name Exists
  526.         ControlAction.click(6, 533, 469, 120, 20);
  527.  
  528.         break;
  529.     case 27: // Gateway Select
  530.         ControlAction.click(6, 436, 538, 96, 32);
  531.  
  532.         break;
  533.     case 28: // Lobby - Game Does Not Exist
  534.         ControlAction.click(6, 533, 469, 120, 20);
  535.  
  536.         break;
  537.     case 38: // Game is full
  538.         D2Bot.printToConsole("Game is full");
  539.  
  540.         break;
  541.     case 42: // Empty character screen
  542.         // TODO: see if this is needed in case 12 too
  543.         string = "";
  544.         text = ControlAction.getText(4, 45, 318, 531, 140);
  545.  
  546.         if (text) {
  547.             for (i = 0; i < text.length; i += 1) {
  548.                 string += text[i];
  549.  
  550.                 if (i !== text.length - 1) {
  551.                     string += " ";
  552.                 }
  553.             }
  554.  
  555.             if (string === getLocaleString(11161)) { // CDKey disabled from realm play
  556.                 D2Bot.updateStatus("Realm Disabled CDKey");
  557.                 D2Bot.printToConsole("Realm Disabled CDKey: " + gameInfo.mpq, 6);
  558.                 D2Bot.CDKeyDisabled();
  559.  
  560.                 if (gameInfo.switchKeys) {
  561.                     ControlAction.timeoutDelay("Key switch delay", StarterConfig.SwitchKeyDelay * 1000);
  562.                     D2Bot.restart(true);
  563.                 } else {
  564.                     D2Bot.stop(me.profile, true);
  565.                 }
  566.             }
  567.         }
  568.  
  569.         if (!locationTimeout(5000, location)) {
  570.             accounts.shift(); // remove current account from the list
  571.             chars.shift();
  572.  
  573.             ControlAction.click(6, 33, 572, 128, 35);
  574.         }
  575.  
  576.         break;
  577.     default:
  578.         if (location !== undefined) {
  579.             D2Bot.printToConsole("Unhandled location " + location);
  580.             delay(500);
  581.             D2Bot.restart();
  582.         }
  583.  
  584.         break;
  585.     }
  586. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement