Advertisement
gur111

Zero Sign v2.0

Jul 9th, 2018
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.00 KB | None | 0 0
  1. import java.lang.Math; // headers MUST be above the first class
  2. import java.awt.AWTException;
  3. import java.awt.Robot;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.InputEvent;
  6. import java.awt.image.BufferedImage;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import java.awt.Toolkit;
  10. import javax.imageio.ImageIO;
  11. import java.io.*;
  12. import java.time.LocalTime;
  13.  
  14.  
  15. public class SignZero{
  16.     private static Robot bot;
  17.     final static String CNT_FILE_NAME = "ZeroSignCounter.txt";
  18.     final static int WAIT_INTERVAL = 500;
  19.     final static int MAX_WAIT = 40; // In halves of a second
  20.     final static Rectangle SCREEN = new Rectangle(0, 0, 1920, 1080);
  21.     final static int KEY_INTERVAL = 20;
  22.     final static int CLICK_OFFSET = 5;
  23.     final static int DROPDOWN_DELAY = 200;
  24.     static int counter = 0;
  25.     static int skips = 0;
  26.     enum Error {
  27.         NO_ERR,
  28.         NOT_OPENED,
  29.         NOT_CLOSED,
  30.         NO_PANEL
  31.     }
  32.     static Error lastErr = Error.NO_ERR;
  33.    
  34.    
  35.     public static void main(String args[]) throws IOException, AWTException{
  36.         main();
  37.         BufferedReader reader = new BufferedReader(new FileReader(CNT_FILE_NAME));
  38.         String line;
  39.         int lastCount = 0;
  40.         if((line = reader.readLine()) != null){
  41.             lastCount = Integer.parseInt(line);
  42.         }
  43.         reader.close();
  44.         BufferedWriter writer = new BufferedWriter(new FileWriter(CNT_FILE_NAME));
  45.         writer.write(""+(lastCount+counter));
  46.         writer.close();
  47.        
  48.        
  49.     }
  50.    
  51.     private static void main() throws IOException, AWTException{
  52.        
  53.         BufferedImage editImg = getImg("edit"),
  54.                         edit2Img = getImg("edit2"),
  55.                         signBtnImg = getImg("sign_button"),
  56.                         acceptImg = getImg("accept"),
  57.                         tabsImg = getImg("tabs"),
  58.                         succImg = getImg("success"),
  59.                         assignFldImg = getImg("assign_field"),
  60.                         businessDetailsImg = getImg("business_details"),
  61.                         noEditImg = getImg("no_edit"),
  62.                         ntnImg = getImg("netanya"),
  63.                         detailsTabImg = getImg("details"),
  64.                         cmdrFldImg = getImg("commander"),
  65.                         fullListImg = getImg("full_list"),
  66.                         infoTabTitleImg = getImg("causes"),
  67.                         funcPanelImg = getImg("func_panel"),
  68.                         popupTitleImg = getImg("edit_popup_title");
  69.        
  70.         bot = new Robot();
  71.         while(true){
  72.             Point p, signP;
  73.             // Checking if ready
  74.             log("Readying for next line", 5);
  75.             if((p = waitForImg(funcPanelImg, 10)) == null){
  76.                 if((p = imgLoc(popupTitleImg)) != null){
  77.                     if(lastErr != Error.NOT_CLOSED){
  78.                         closeWin();
  79.                         error("Somehow the windows didn't close. Counter might be wrong.");
  80.                         lastErr = Error.NOT_CLOSED;
  81.                         bot.delay(4000);
  82.                         continue;
  83.                     }else{
  84.                         if(lastErr == Error.NO_PANEL){
  85.                             lastErr = Error.NO_PANEL;
  86.                             bot.delay(4000);
  87.                             continue;
  88.                         }else{
  89.                             error("Window didn't close... Giving up.");
  90.                             return;
  91.                         }
  92.                     }
  93.                 }
  94.                 error("Can't find panel. Something went wrong.");
  95.                 return;
  96.             }
  97.            
  98.             // Wait for edit button
  99.             log("Waiting for edit btn", 5);
  100.             p = waitForImg(editImg, edit2Img);
  101.             if(p == null){
  102.                 if(waitForImg(tabsImg) != null){
  103.                     closeWin();
  104.                     continue;
  105.                 }
  106.                 notFoundErr("edit button");
  107.                 return;
  108.             }
  109.             log("Found edit", 3);
  110.             aimAndClick(p);
  111.            
  112.             // Wait for loading
  113.             log("Waiting until loaded", 5);
  114.             p = waitForImg(businessDetailsImg);
  115.             if(p == null){
  116.                 if(lastErr == Error.NOT_CLOSED || lastErr == Error.NOT_OPENED){
  117.                     notFoundErr("loaded details");
  118.                     return;
  119.                 }
  120.                 // Make sure the win has opened
  121.                 if(imgLoc(funcPanelImg) == null){
  122.                     if((p = imgLoc(popupTitleImg)) != null){
  123.                         closeWin();
  124.                         error("Somehow the windows didn't close. Counter might be wrong.");
  125.                         lastErr = Error.NOT_CLOSED;
  126.                         continue;
  127.                     }
  128.                 }else{ // You can still see the panel. Try again.
  129.                     lastErr = Error.NOT_OPENED;
  130.                     continue;
  131.                 }
  132.             }
  133.            
  134.             // Validate assignment
  135.             log("Make sure a station is assigned", 5);
  136.             p = waitForImg(assignFldImg, 2);
  137.             if(p == null){
  138.                 if(imgLoc(noEditImg) != null){
  139.                     closeWin();
  140.                     skips++;
  141.                     continue;
  142.                 }
  143.             }else{
  144.                 aimAndClick(p);
  145.                 if((p = waitForImg(ntnImg, 5)) == null){
  146.                     notFoundErr("dropdown ntn option");
  147.                     return;
  148.                 }
  149.                 bot.delay(DROPDOWN_DELAY);
  150.                 aimAndClick(p);
  151.             }
  152.            
  153.             // Go to more info tab
  154.             log("Going to info tab", 5);
  155.             p = waitForImg(detailsTabImg, 2);
  156.             if(p == null){
  157.                 notFoundErr("more info tab");
  158.                 return;
  159.             }
  160.             aimAndClick(p);
  161.            
  162.             // Wait for loading
  163.             log("Wait until info is loaded", 5);
  164.             if(waitForImg(infoTabTitleImg) == null){
  165.                 notFoundErr("loaded info in info tab");
  166.                 return;
  167.             }
  168.            
  169.             // Check commander
  170.             log("Checking commander", 5);
  171.             p = imgLoc(cmdrFldImg);
  172.             if(p == null){
  173.                 log("Commander already set", 3);
  174.             }else{
  175.                 aimAndClick(p);
  176.                 p = waitForImg(fullListImg, 2);
  177.                 if(p == null){
  178.                     key(KeyEvent.VK_DOWN);
  179.                     key(KeyEvent.VK_ENTER);
  180.                 }else{    
  181.                     key(KeyEvent.VK_T);
  182.                     key(KeyEvent.VK_K);
  183.                     key(KeyEvent.VK_H);
  184.                     key(KeyEvent.VK_V);
  185.                     key(KeyEvent.VK_U);
  186.                     space();
  187.                     key(KeyEvent.VK_R);
  188.                     key(KeyEvent.VK_J);
  189.                     bot.delay(KEY_INTERVAL);
  190.                     key(KeyEvent.VK_ENTER);
  191.                 }
  192.                 bot.delay(DROPDOWN_DELAY);
  193.             }
  194.            
  195.            
  196.             { // Try signing
  197.                 log("Looking for sign btn",5);
  198.                 p = waitForImg(signBtnImg);
  199.                 if(p == null){
  200.                     skips++;
  201.                     continue;
  202.                 }
  203.                 log("Click sign", 5);
  204.                 aimAndClick(p);
  205.                
  206.                 // Accept Signing
  207.                 log("Accept signing", 5);
  208.                 p = waitForImg(acceptImg);
  209.                 if(p == null){
  210.                     notFoundErr("accept button");
  211.                     return;
  212.                 }
  213.                 space();
  214.                
  215.                 // Check for success msg
  216.                 log("Wait for success", 5);
  217.                 p = waitForImg(succImg, 7);
  218.                 if(p == null){
  219.                     notFoundErr("success");
  220.                     if(waitForImg(acceptImg, 4) == null){
  221.                         notFoundErr("accept button");
  222.                         return;
  223.                     }
  224.                     space();
  225.                     continue;
  226.                 }
  227.                 counter++;
  228.                 space();
  229.             }
  230.         }
  231.     }
  232.    
  233.     private static void notFoundErr(String name){
  234.         log("Error. Can't find "+name, 1);
  235.     }
  236.    
  237.     private static void error(String str){
  238.         log("Error. "+str, 1);
  239.     }
  240.    
  241.     private static void aimAndClick(Point p){
  242.         aim((int) p.getX()+CLICK_OFFSET, (int) p.getY()+CLICK_OFFSET);
  243.         bot.delay(KEY_INTERVAL);
  244.         click();
  245.     }
  246.    
  247.     private static BufferedImage getImg(String name) throws IOException{
  248.         return ImageIO.read(new File("Assets\\"+name+".bmp"));
  249.        
  250.     }
  251.    
  252.     private static void key(int keyCode){
  253.         bot.keyPress(keyCode);
  254.         bot.delay(KEY_INTERVAL);
  255.         bot.keyRelease(keyCode);
  256.     }
  257.    
  258.     private static void space(){
  259.         bot.keyPress(KeyEvent.VK_SPACE);
  260.         bot.delay(KEY_INTERVAL);
  261.         bot.keyRelease(KeyEvent.VK_SPACE);
  262.     }
  263.    
  264.     private static void click(){
  265.         bot.mousePress(InputEvent.BUTTON1_MASK);
  266.         bot.mouseRelease(InputEvent.BUTTON1_MASK);
  267.     }
  268.    
  269.     private static void aim(int x, int y){
  270.         bot.mouseMove(x,y);
  271.     }
  272.    
  273.     private static void assignStation(){
  274.         // TODO: Assign missing stuff
  275.     }
  276.    
  277.     static Point waitForImg(BufferedImage img){
  278.         return waitForImg(img, 0);
  279.     }
  280.  
  281.     static Point waitForImg(BufferedImage img, BufferedImage img2){
  282.         return waitForImg(img, img2, 0);
  283.     }
  284.    
  285.     private static Point waitForImg(BufferedImage img, BufferedImage img2, int sec) {
  286.         int times = sec*2;
  287.         if(sec == 0){
  288.             times = MAX_WAIT;
  289.         }
  290.         while((times--) > 0){
  291.             bot.delay(WAIT_INTERVAL);
  292.             Point p;
  293.             // If found then return loc
  294.             if((p = imgLoc(img)) != null || (p = imgLoc(img2)) != null){
  295.               log("", 5);
  296.               return p;
  297.             }
  298.             logil(".", 5);
  299.         }
  300.         log("", 5);
  301.         return null;
  302.     }
  303.  
  304.  
  305.     private static Point waitForImg(BufferedImage img, int sec) {
  306.         int times = sec*2;
  307.         if(sec == 0){
  308.             times = MAX_WAIT;
  309.         }
  310.         while((times--) > 0){
  311.             bot.delay(WAIT_INTERVAL);
  312.             Point p;
  313.             // If found then return loc
  314.             if((p = imgLoc(img)) != null){
  315.               log("", 5);
  316.               return p;
  317.             }
  318.             logil(".", 5);
  319.         }
  320.         log("", 5);
  321.         return null;
  322.        
  323.     }
  324.    
  325.     private static Point imgLoc(BufferedImage img){
  326.        
  327.         BufferedImage current = bot.createScreenCapture(SCREEN);
  328.        
  329.         for (int x = 0; x < SCREEN.getWidth()-img.getWidth(); x++)
  330.             for (int y = 0; y < SCREEN.getHeight()-img.getHeight(); y++){
  331.                 boolean match = true;
  332.                 for (int x2 = 0; x2 < img.getWidth() && match; x2++){
  333.                     for (int y2 = 0; y2 < img.getHeight() && match; y2++){
  334.                         if (img.getRGB(x2, y2) != current.getRGB(x+x2, y+y2)){
  335.                             match = false;
  336.                         }
  337.                     }
  338.                 }
  339.                 if (match){
  340.                     log("Found on: ("+x+","+y+")", 10);
  341.                     return new Point(x,y);
  342.                 }
  343.             }
  344.         return null;
  345.        
  346.     }
  347.    
  348.     private static void log(String str, int lvl){
  349.         log(str);
  350.     }
  351.    
  352.     private static void logil(String str, int lvl){
  353.         logil(str);
  354.     }
  355.    
  356.     private static void log(String str){
  357.         LocalTime localTime = LocalTime.now();
  358.         logil("["+localTime+"]: "+str+"\n");
  359.     }
  360.    
  361.     private static void logil(String str){
  362.         System.out.print(str);
  363.     }
  364.    
  365.    
  366.     private static void closeWin(){
  367.         bot.keyPress(KeyEvent.VK_ALT);
  368.         bot.keyPress(KeyEvent.VK_F4);
  369.         bot.delay(20);
  370.         bot.keyRelease(KeyEvent.VK_ALT);
  371.         bot.keyRelease(KeyEvent.VK_F4);
  372.   }
  373.    
  374.    
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement