Browse Source

Scaling working, but finding the closestPath needs to be fixed

master
davpapp 4 years ago
parent
commit
ee0efadb37
10 changed files with 99558 additions and 50 deletions
  1. +44
    -21
      src/Cursor.java
  2. +1
    -6
      src/CursorPath.java
  3. +25
    -8
      src/CursorPathTest.java
  4. +41
    -15
      src/CursorTest.java
  5. BIN
      target/classes/Cursor.class
  6. BIN
      target/classes/CursorPath.class
  7. BIN
      target/classes/CursorPathTest.class
  8. BIN
      target/classes/CursorTask.class
  9. BIN
      target/classes/CursorTest.class
  10. +99447
    -0
      testfiles/cursorPathTest.txt

+ 44
- 21
src/Cursor.java View File

@ -87,31 +87,31 @@ public class Cursor {
Thread.sleep(getRandomClickLength());
}
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException {
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws Exception {
moveCursorToCoordinates(goalPoint);
leftClickCursor();
}
public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException {
public void moveAndRightClickAtCoordinates(Point goalPoint) throws Exception {
moveCursorToCoordinates(goalPoint);
rightClickCursor();
}
public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException {
public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint);
leftClickCursor();
return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
}
public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException {
public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint);
rightClickCursor();
return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
}
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
public void moveCursorToCoordinates(Point goalPoint) throws Exception {
Point startingPoint = getCurrentCursorPoint();
int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint);
double angleToRotateCursorPathTo = getThetaBetweenPoints(startingPoint, goalPoint);
@ -147,33 +147,56 @@ public class Cursor {
robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y);
}
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) {
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) throws Exception {
int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor);
double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor);
System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor);
System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor + " from scaling by " + scaleToFactorBy);
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor);
int indexOfRandomPathToFollow = random.nextInt(cursorPathsWithSameDistance.size());
//CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(indexOfRandomPathToFollow).getScaledCopyOfCursorPath(scaleToFactorBy);
//System.out.println("Chose the following path: ");
//scaledCursorPath.displayCursorPoints();
return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath;
CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())).getScaledCopyOfCursorPath(scaleToFactorBy);
return scaledCursorPath;
//return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath;
}
private int findNearestPathLengthThatExists(int distanceToMoveCursor) {
int offset = 1;
public int findNearestPathLengthThatExists(int distanceToMoveCursor) throws Exception {
int offset = 0;
boolean reachedMinimumLimit = false;
boolean reachedMaximumLimit = false;
while (cursorPathsByDistance.get(distanceToMoveCursor + offset).size() == 0) {
if (offset > 0) {
offset = -(offset + 1);
// Go up
if (distanceToMoveCursor + Math.abs(offset) + 1 >= NUMBER_OF_DISTANCES) {
reachedMaximumLimit = true;
}
if (distanceToMoveCursor - Math.abs(offset) - 1 < 0) {
reachedMinimumLimit = true;
}
if (reachedMaximumLimit && reachedMinimumLimit) {
throw new Exception("No paths exist.");
}
if (offset < 0) {
if (!reachedMaximumLimit) {
offset = -offset + 1;
}
else {
offset--;
}
}
else {
offset = -offset + 1;
if (!reachedMinimumLimit) {
offset = -offset - 1;
}
else {
offset++;
}
}
}
return distanceToMoveCursor + offset;
}
private double getScaleToFactorBy(int newDistanceToMoveCursor, int distanceToMoveCursor) {
return (1.0 * newDistanceToMoveCursor / distanceToMoveCursor);
return (1.0 * distanceToMoveCursor / newDistanceToMoveCursor);
}
@ -187,13 +210,13 @@ public class Cursor {
}
public void displayCursorPaths() {
for (int i = 0; i < 1000; i++) {
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i);
}
System.out.println("--------------");
for (int i = 0; i < cursorPathsByDistance.get(1).size(); i++) {
/*for (int i = 0; i < cursorPathsByDistance.get(1).size(); i++) {
cursorPathsByDistance.get(1).get(i).displayCursorPoints();
}
}*/
//cursorPathsByDistance.get(0).get(0).displayCursorPoints();
}
}

+ 1
- 6
src/CursorPath.java View File

@ -34,7 +34,6 @@ public class CursorPath {
return normalizedDelayCursorPoints;
}
// util
private ArrayList<CursorPoint> getTranslatedListOfCursorPoints(ArrayList<CursorPoint> cursorPoints, CursorPoint cursorPointToTranslateBy) {
ArrayList<CursorPoint> translatedCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : cursorPoints) {
@ -43,7 +42,6 @@ public class CursorPath {
return translatedCursorPath;
}
//util
private ArrayList<CursorPoint> getNormalizedDelayListOfCursorPoints(ArrayList<CursorPoint> cursorPoints) {
ArrayList<CursorPoint> normalizedDelayCursorPoints = new ArrayList<CursorPoint>();
for (int i = 0; i < cursorPoints.size() - 1; i++) {
@ -55,7 +53,6 @@ public class CursorPath {
return normalizedDelayCursorPoints;
}
//util
public CursorPath getScaledCopyOfCursorPath(double factorToScaleBy) {
ArrayList<CursorPoint> scaledCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : this.cursorPoints) {
@ -64,11 +61,9 @@ public class CursorPath {
return new CursorPath(scaledCursorPath, true);
}
//util
public CursorPath getRotatedCopyOfCursorPath(double angleToRotateTo) {
ArrayList<CursorPoint> rotatedCursorPath = new ArrayList<CursorPoint>();
double angleToRotateBy = this.theta - angleToRotateTo;
System.out.println("Rotating points by: " + angleToRotateBy);
for (CursorPoint cursorPoint : this.cursorPoints) {
rotatedCursorPath.add(cursorPoint.getCursorPointRotatedBy(angleToRotateBy));
}
@ -110,7 +105,7 @@ public class CursorPath {
}
private boolean isCursorPathDistanceReasonable() {
return (this.distance > 0 && this.distance < 1000);
return (this.distance > 0 && this.distance < 2200);
}
private boolean isCursorPathNumPointsReasonable() {


+ 25
- 8
src/CursorPathTest.java View File

@ -14,7 +14,7 @@ class CursorPathTest {
initializeCursorPath();
for (CursorPath cursorPath : cursorPaths) {
cursorPath.displayCursorPoints();
//cursorPath.displayCursorPoints();
testCursorPathStartsAtOrigin(cursorPath);
testEndingCursorPointInCursorPathHasZeroDelay(cursorPath);
@ -23,9 +23,23 @@ class CursorPathTest {
testCursorPathRotation(cursorPath, Math.PI);
testCursorPathRotation(cursorPath, 7 / 4 * Math.PI);
//testCursorPathRotation(cursorPath, 10.32184);
//testCursorPathRotation(cursorPath, 0.01372);
//testCursorPathRotation(cursorPath, -0.0001238);
//testCursorPathRotation(cursorPath, 0);
testCursorPathScaling(cursorPath, 1.15);
testCursorPathScaling(cursorPath, 0.48324);
testCursorPathScaling(cursorPath, 0.9999);
testCursorPathScaling(cursorPath, 1.8431838);
testCursorPathScaling(cursorPath, 2.10004);
testCursorPathScaling(cursorPath, 1.15);
testCursorPathScaling(cursorPath, 1.001010101);
testCursorPathScaling(cursorPath, 1.523521);
testCursorPathScaling(cursorPath, 1.12366);
testCursorPathScaling(cursorPath, 0.974324);
testCursorPathScaling(cursorPath, 0.72134);
testCopyTime(cursorPath);
//testDelays(cursorPath);
}
}
@ -66,15 +80,18 @@ class CursorPathTest {
void testCursorPathRotation(CursorPath cursorPath, double angleToRotateTo) {
CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(angleToRotateTo);
assertEquals(angleToRotateTo, rotatedCursorPath.getCursorPathTheta());
assertEquals(angleToRotateTo % Math.PI, ((rotatedCursorPath.getCursorPathTheta() % Math.PI) + Math.PI) % Math.PI, 0.01);
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), rotatedCursorPoints.size());
assertEquals(cursorPath.getStartingCursorPoint().x, rotatedCursorPath.getStartingCursorPoint().x);
assertEquals(cursorPath.getStartingCursorPoint().y, rotatedCursorPath.getStartingCursorPoint().y);
}
void testCopyTime(CursorPath cursorPath) {
void testDelays(CursorPath cursorPath) {
CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(2.3 / 9.0 * Math.PI);
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints();
@ -90,9 +107,9 @@ class CursorPathTest {
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> scaledCursorPoints = scaledCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), scaledCursorPoints.size());
}
void assertInRangeByTolerance(double expected, double actual, double tolerance) {
assertTrue((actual <= (expected + tolerance)) && (actual >= (expected - tolerance)));
assertEquals(cursorPath.getStartingCursorPoint().x, scaledCursorPath.getStartingCursorPoint().x);
assertEquals(cursorPath.getStartingCursorPoint().y, scaledCursorPath.getStartingCursorPoint().y);
assertEquals(cursorPath.getCursorPathDistance() * factorToScaleBy, scaledCursorPath.getCursorPathDistance(), 3);
}
}

+ 41
- 15
src/CursorTest.java View File

@ -16,15 +16,23 @@ class CursorTest {
}
@Test
void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException {
void testMoveCursorToCoordinatesHelper() throws Exception {
initialize();
cursor.displayCursorPaths();
//cursor.displayCursorPaths();
testThetaBetweenPoints();
testFindNearestPathLengthThatExists();
testMoveCursorToCoordinates();
//testRightClickCursor();
}
void testFindNearestPathLengthThatExists() throws Exception {
for (int i = 0; i < 2203; i++) {
int closestLength = cursor.findNearestPathLengthThatExists(i);
System.out.println("Closest path to length " + i + " is " + closestLength);
}
}
void testThetaBetweenPoints() {
Point a = new Point(0, 0);
Point b = new Point(10, 0);
@ -36,7 +44,7 @@ class CursorTest {
assertEquals(degreesToRadians(45), cursor.getThetaBetweenPoints(b, d));
}
void testMoveCursorToCoordinates() throws InterruptedException {
void testMoveCursorToCoordinates() throws Exception {
Point a = new Point(0, 0);
Point b = new Point(150, 250);
Point c = new Point(375, 190);
@ -44,6 +52,18 @@ class CursorTest {
Point e = new Point(952, 603);
Point f = new Point(1025, 133);
Point g = new Point(543, 582);
Point h = new Point(1643, 582);
Point i = new Point(0, 582);
Point j = new Point(954, 123);
Point k = new Point(954, 960);
Point l = new Point(384, 654);
Point m = new Point(84, 43);
Point n = new Point(84, 43);
Point o = new Point(12, 842);
Point p = new Point(1600, 514);
Point q = new Point(1599, 513);
Point r = new Point(1600, 515);
Point s = new Point(1602, 513);
testMoveCursorToCoordinates(a, b);
testMoveCursorToCoordinates(b, c);
testMoveCursorToCoordinates(c, d);
@ -55,6 +75,19 @@ class CursorTest {
testMoveCursorToCoordinates(f, b);
testMoveCursorToCoordinates(b, a);
testMoveCursorToCoordinates(a, g);
testMoveCursorToCoordinates(g, h);
testMoveCursorToCoordinates(h, i);
testMoveCursorToCoordinates(i, j);
testMoveCursorToCoordinates(j, k);
testMoveCursorToCoordinates(k, l);
testMoveCursorToCoordinates(l, m);
testMoveCursorToCoordinates(m, n);
testMoveCursorToCoordinates(n, o);
testMoveCursorToCoordinates(o, p);
testMoveCursorToCoordinates(p, q);
testMoveCursorToCoordinates(q, r);
testMoveCursorToCoordinates(r, s);
testMoveCursorToCoordinates(s, a);
}
/*void testRightClickCursor() throws InterruptedException {
@ -77,27 +110,20 @@ class CursorTest {
// Way to verify that context menu is open?
}*/
void testMoveCursorToCoordinates(Point a, Point b) throws InterruptedException {
void testMoveCursorToCoordinates(Point a, Point b) throws Exception {
cursor.robotMouseMove(a);
cursor.moveCursorToCoordinates(b);
Point point = cursor.getCurrentCursorPoint();
System.out.println("Cursor ended up on " + point.x + "," + point.y);
//System.out.println("Cursor ended up on " + point.x + "," + point.y);
verifyCursorIsInCorrectPlace(point, b);
Thread.sleep(500);
Thread.sleep(250);
}
void verifyCursorIsInCorrectPlace(Point actualPoint, Point expectedPoint) {
assertInRangeByAbsoluteValue(actualPoint.x, expectedPoint.x, cursorTolerance);
assertInRangeByAbsoluteValue(actualPoint.y, expectedPoint.y, cursorTolerance);
assertEquals(actualPoint.x, expectedPoint.x, cursorTolerance);
assertEquals(actualPoint.y, expectedPoint.y, cursorTolerance);
}
void assertInRangeByPercentage(double valueToTest, double expectation, double tolerancePercentage) {
assertTrue((valueToTest <= (expectation * (1 + tolerancePercentage))) && (valueToTest >= (expectation * (1 - tolerancePercentage))));
}
void assertInRangeByAbsoluteValue(double valueToTest, double expectation, double toleranceAbsolute) {
assertTrue((valueToTest <= (expectation + toleranceAbsolute)) && (valueToTest >= (expectation * (1 - toleranceAbsolute))));
}
private double degreesToRadians(double degrees) {
return degrees * Math.PI / 180.0;


BIN
target/classes/Cursor.class View File


BIN
target/classes/CursorPath.class View File


BIN
target/classes/CursorPathTest.class View File


BIN
target/classes/CursorTask.class View File


BIN
target/classes/CursorTest.class View File


+ 99447
- 0
testfiles/cursorPathTest.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save