1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-12-21 23:48:49 -05:00

Some refactoring done

This commit is contained in:
davpapp 2018-01-31 00:45:08 -05:00
parent eb5357335d
commit 925c8d35ca
8 changed files with 77 additions and 16 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,6 +5,7 @@ import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
@ -18,16 +19,25 @@ import java.util.regex.Pattern;
public class Cursor {
public static final int NUMBER_OF_DISTANCES = 1000;
public static final int MINIMUM_CLICK_LENGTH = 120;
public static final int MAXIMUM_CLICK_LENGTH = 240;
private Robot robot;
private Random random = new Random();
private ArrayList<ArrayList<CursorPath>> cursorPathsByDistance;
public Cursor() throws AWTException {
ArrayList<CursorPath> cursorPaths = getArrayListOfCursorPathsFromFile("/home/dpapp/GhostMouse/coordinates.txt");// read from file or something;
initializeCursorPathsByDistance();
assignCursorPathsByDistance(cursorPaths);
initializeCursorPathsByDistanceFromFile("/home/dpapp/GhostMouse/coordinates.txt");
robot = new Robot();
random = new Random();
}
private void initializeCursorPathsByDistanceFromFile(String path) {
initializeCursorPathsByDistance();
ArrayList<CursorPath> cursorPaths = getArrayListOfCursorPathsFromFile(path);
assignCursorPathsByDistance(cursorPaths);
}
private void initializeCursorPathsByDistance() {
@ -54,13 +64,40 @@ public class Cursor {
this.cursorPathsByDistance.get(cursorPath.getCursorPathDistance()).add(cursorPath);
}
private int getRandomClickLength() {
return random.nextInt(MAXIMUM_CLICK_LENGTH - MINIMUM_CLICK_LENGTH) + MINIMUM_CLICK_LENGTH;
}
public void leftClickCursor() throws InterruptedException {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength());
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
public void rightClickCursor() throws InterruptedException {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(getRandomClickLength());
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
}
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException {
moveCursorToCoordinates(goalPoint);
leftClickCursor();
}
public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException {
moveCursorToCoordinates(goalPoint);
rightClickCursor();
}
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
Point startingCursorPoint = getCurrentCursorPoint();
int distanceToMoveCursor = calculateDistanceBetweenPoints(startingCursorPoint, goalPoint);
double angleToMoveCursor = calculateThetaBetweenPoints(startingCursorPoint, goalPoint);
// TODO: check if exists
CursorPath cursorPathToFollow = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor);
//cursorPathToFollow.displayCursorPoints();
double angleToTranslatePathBy = angleToMoveCursor - cursorPathToFollow.getCursorPathTheta();
followCursorPath(startingCursorPoint, angleToTranslatePathBy, cursorPathToFollow);
}
@ -85,6 +122,7 @@ public class Cursor {
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) {
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(distanceToMoveCursor);
// TODO: Error check if path of this size exists
return cursorPathsWithSameDistance.get(new Random().nextInt(cursorPathsWithSameDistance.size()));
}
@ -99,10 +137,4 @@ public class Cursor {
public Point getCurrentCursorPoint() {
return MouseInfo.getPointerInfo().getLocation();
}
/*public void displaycursorPathsByDistance() {
for (int i = 0; i < cursorPathsByDistance.size(); i++) {
System.out.println("There are " + cursorPathsByDistance.get(i).size() + " CursorPaths of length " + i);
}
}*/
}

View File

@ -46,14 +46,12 @@ public class CursorDataFileParser {
lastCursorPoint = newCursorPoint;
}
}
else {
System.out.println("Skipping invalid REGEX: " + line);
}
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished parsing cursor data...");
return cursorPaths;
}

View File

@ -9,8 +9,14 @@ public class CursorTask extends Cursor {
public void dropAllItemsInInventory() {
for (int inventoryColumn = 0; inventoryColumn < 7; inventoryColumn++) {
for (int inventoryRow = 0; inventoryRow < 4; inventoryRow++) {
dropItem
}
}
}
public void dropItem(InventorySlot inventorySlot) {
Point inventorySlotCoordinates = inventorySlot.getCoordinates();
moveAndRightClickAtCoordinates(inventorySlotCoordinates);
moveAndLeftClickAtCoordinates();
}
}

View File

@ -18,6 +18,11 @@ class CursorTest {
@Test
void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException {
initialize();
testMoveCursorToCoordinates();
testRightClickCursor();
}
void testMoveCursorToCoordinates() throws InterruptedException {
Point a = new Point(0, 0);
Point b = new Point(150, 250);
Point c = new Point(375, 190);
@ -35,6 +40,26 @@ class CursorTest {
testMoveCursorToCoordinates(c, f);
testMoveCursorToCoordinates(f, b);
testMoveCursorToCoordinates(b, a);
testMoveCursorToCoordinates(a, g);
}
void testRightClickCursor() throws InterruptedException {
Point a = new Point(375, 600);
Point b = new Point(952, 603);
Point c = new Point(1025, 133);
Point d = new Point(543, 582);
testMoveAndRightClickCursor(a, b);
testMoveAndRightClickCursor(b, c);
testMoveAndRightClickCursor(c, d);
testMoveAndRightClickCursor(d, a);
}
void testMoveAndRightClickCursor(Point a, Point b) throws InterruptedException {
cursor.robotMouseMove(a);
cursor.moveAndRightClickAtCoordinates(b);
Point point = cursor.getCurrentCursorPoint();
verifyCursorIsInCorrectPlace(point, b);
// Way to verify that context menu is open?
}
void testMoveCursorToCoordinates(Point a, Point b) throws InterruptedException {