import java.lang.Math; // headers MUST be above the first class
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.imageio.ImageIO;
import java.io.*;
import java.time.LocalTime;
public class SignZero{
private static Robot bot;
final static String CNT_FILE_NAME = "ZeroSignCounter.txt";
final static int WAIT_INTERVAL = 500;
final static int MAX_WAIT = 40; // In halves of a second
final static Rectangle SCREEN = new Rectangle(0, 0, 1920, 1080);
final static int KEY_INTERVAL = 20;
final static int CLICK_OFFSET = 5;
final static int DROPDOWN_DELAY = 200;
static int counter = 0;
static int skips = 0;
enum Error {
NO_ERR,
NOT_OPENED,
NOT_CLOSED,
NO_PANEL
}
static Error lastErr = Error.NO_ERR;
public static void main(String args[]) throws IOException, AWTException{
main();
BufferedReader reader = new BufferedReader(new FileReader(CNT_FILE_NAME));
String line;
int lastCount = 0;
if((line = reader.readLine()) != null){
lastCount = Integer.parseInt(line);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(CNT_FILE_NAME));
writer.write(""+(lastCount+counter));
writer.close();
}
private static void main() throws IOException, AWTException{
BufferedImage editImg = getImg("edit"),
edit2Img = getImg("edit2"),
signBtnImg = getImg("sign_button"),
acceptImg = getImg("accept"),
tabsImg = getImg("tabs"),
succImg = getImg("success"),
assignFldImg = getImg("assign_field"),
businessDetailsImg = getImg("business_details"),
noEditImg = getImg("no_edit"),
ntnImg = getImg("netanya"),
detailsTabImg = getImg("details"),
cmdrFldImg = getImg("commander"),
fullListImg = getImg("full_list"),
infoTabTitleImg = getImg("causes"),
funcPanelImg = getImg("func_panel"),
popupTitleImg = getImg("edit_popup_title");
bot = new Robot();
while(true){
Point p, signP;
// Checking if ready
log("Readying for next line", 5);
if((p = waitForImg(funcPanelImg, 10)) == null){
if((p = imgLoc(popupTitleImg)) != null){
if(lastErr != Error.NOT_CLOSED){
closeWin();
error("Somehow the windows didn't close. Counter might be wrong.");
lastErr = Error.NOT_CLOSED;
bot.delay(4000);
continue;
}else{
if(lastErr == Error.NO_PANEL){
lastErr = Error.NO_PANEL;
bot.delay(4000);
continue;
}else{
error("Window didn't close... Giving up.");
return;
}
}
}
error("Can't find panel. Something went wrong.");
return;
}
// Wait for edit button
log("Waiting for edit btn", 5);
p = waitForImg(editImg, edit2Img);
if(p == null){
if(waitForImg(tabsImg) != null){
closeWin();
continue;
}
notFoundErr("edit button");
return;
}
log("Found edit", 3);
aimAndClick(p);
// Wait for loading
log("Waiting until loaded", 5);
p = waitForImg(businessDetailsImg);
if(p == null){
if(lastErr == Error.NOT_CLOSED || lastErr == Error.NOT_OPENED){
notFoundErr("loaded details");
return;
}
// Make sure the win has opened
if(imgLoc(funcPanelImg) == null){
if((p = imgLoc(popupTitleImg)) != null){
closeWin();
error("Somehow the windows didn't close. Counter might be wrong.");
lastErr = Error.NOT_CLOSED;
continue;
}
}else{ // You can still see the panel. Try again.
lastErr = Error.NOT_OPENED;
continue;
}
}
// Validate assignment
log("Make sure a station is assigned", 5);
p = waitForImg(assignFldImg, 2);
if(p == null){
if(imgLoc(noEditImg) != null){
closeWin();
skips++;
continue;
}
}else{
aimAndClick(p);
if((p = waitForImg(ntnImg, 5)) == null){
notFoundErr("dropdown ntn option");
return;
}
bot.delay(DROPDOWN_DELAY);
aimAndClick(p);
}
// Go to more info tab
log("Going to info tab", 5);
p = waitForImg(detailsTabImg, 2);
if(p == null){
notFoundErr("more info tab");
return;
}
aimAndClick(p);
// Wait for loading
log("Wait until info is loaded", 5);
if(waitForImg(infoTabTitleImg) == null){
notFoundErr("loaded info in info tab");
return;
}
// Check commander
log("Checking commander", 5);
p = imgLoc(cmdrFldImg);
if(p == null){
log("Commander already set", 3);
}else{
aimAndClick(p);
p = waitForImg(fullListImg, 2);
if(p == null){
key(KeyEvent.VK_DOWN);
key(KeyEvent.VK_ENTER);
}else{
key(KeyEvent.VK_T);
key(KeyEvent.VK_K);
key(KeyEvent.VK_H);
key(KeyEvent.VK_V);
key(KeyEvent.VK_U);
space();
key(KeyEvent.VK_R);
key(KeyEvent.VK_J);
bot.delay(KEY_INTERVAL);
key(KeyEvent.VK_ENTER);
}
bot.delay(DROPDOWN_DELAY);
}
{ // Try signing
log("Looking for sign btn",5);
p = waitForImg(signBtnImg);
if(p == null){
skips++;
continue;
}
log("Click sign", 5);
aimAndClick(p);
// Accept Signing
log("Accept signing", 5);
p = waitForImg(acceptImg);
if(p == null){
notFoundErr("accept button");
return;
}
space();
// Check for success msg
log("Wait for success", 5);
p = waitForImg(succImg, 7);
if(p == null){
notFoundErr("success");
if(waitForImg(acceptImg, 4) == null){
notFoundErr("accept button");
return;
}
space();
continue;
}
counter++;
space();
}
}
}
private static void notFoundErr(String name){
log("Error. Can't find "+name, 1);
}
private static void error(String str){
log("Error. "+str, 1);
}
private static void aimAndClick(Point p){
aim((int) p.getX()+CLICK_OFFSET, (int) p.getY()+CLICK_OFFSET);
bot.delay(KEY_INTERVAL);
click();
}
private static BufferedImage getImg(String name) throws IOException{
return ImageIO.read(new File("Assets\\"+name+".bmp"));
}
private static void key(int keyCode){
bot.keyPress(keyCode);
bot.delay(KEY_INTERVAL);
bot.keyRelease(keyCode);
}
private static void space(){
bot.keyPress(KeyEvent.VK_SPACE);
bot.delay(KEY_INTERVAL);
bot.keyRelease(KeyEvent.VK_SPACE);
}
private static void click(){
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
}
private static void aim(int x, int y){
bot.mouseMove(x,y);
}
private static void assignStation(){
// TODO: Assign missing stuff
}
static Point waitForImg(BufferedImage img){
return waitForImg(img, 0);
}
static Point waitForImg(BufferedImage img, BufferedImage img2){
return waitForImg(img, img2, 0);
}
private static Point waitForImg(BufferedImage img, BufferedImage img2, int sec) {
int times = sec*2;
if(sec == 0){
times = MAX_WAIT;
}
while((times--) > 0){
bot.delay(WAIT_INTERVAL);
Point p;
// If found then return loc
if((p = imgLoc(img)) != null || (p = imgLoc(img2)) != null){
log("", 5);
return p;
}
logil(".", 5);
}
log("", 5);
return null;
}
private static Point waitForImg(BufferedImage img, int sec) {
int times = sec*2;
if(sec == 0){
times = MAX_WAIT;
}
while((times--) > 0){
bot.delay(WAIT_INTERVAL);
Point p;
// If found then return loc
if((p = imgLoc(img)) != null){
log("", 5);
return p;
}
logil(".", 5);
}
log("", 5);
return null;
}
private static Point imgLoc(BufferedImage img){
BufferedImage current = bot.createScreenCapture(SCREEN);
for (int x = 0; x < SCREEN.getWidth()-img.getWidth(); x++)
for (int y = 0; y < SCREEN.getHeight()-img.getHeight(); y++){
boolean match = true;
for (int x2 = 0; x2 < img.getWidth() && match; x2++){
for (int y2 = 0; y2 < img.getHeight() && match; y2++){
if (img.getRGB(x2, y2) != current.getRGB(x+x2, y+y2)){
match = false;
}
}
}
if (match){
log("Found on: ("+x+","+y+")", 10);
return new Point(x,y);
}
}
return null;
}
private static void log(String str, int lvl){
log(str);
}
private static void logil(String str, int lvl){
logil(str);
}
private static void log(String str){
LocalTime localTime = LocalTime.now();
logil("["+localTime+"]: "+str+"\n");
}
private static void logil(String str){
System.out.print(str);
}
private static void closeWin(){
bot.keyPress(KeyEvent.VK_ALT);
bot.keyPress(KeyEvent.VK_F4);
bot.delay(20);
bot.keyRelease(KeyEvent.VK_ALT);
bot.keyRelease(KeyEvent.VK_F4);
}
}
Comments