Refactored code

This commit is contained in:
davpapp 2018-01-30 04:14:17 -05:00
parent 6dbd4fcbc8
commit 9b0539b53c
22 changed files with 231 additions and 378 deletions

BIN
bin/Cursor.class Normal file

Binary file not shown.

Binary file not shown.

BIN
bin/CursorPath.class Normal file

Binary file not shown.

BIN
bin/CursorPoint.class Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

58
src/Cursor.java Normal file
View File

@ -0,0 +1,58 @@
/* Reads a file of coordinates
*/
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Cursor {
public static final int NUMBER_OF_DISTANCES = 600;
private ArrayList<ArrayList<CursorPath>> cursorPathsByDistance;
public Cursor() {
ArrayList<CursorPath> cursorPaths = getArrayListOfCursorPathsFromFile("/home/dpapp/GhostMouse/coordinates.txt");// read from file or something;
initializeCursorPathsByDistance();
assignCursorPathsByDistance(cursorPaths);
}
private void initializeCursorPathsByDistance() {
this.cursorPathsByDistance = new ArrayList<ArrayList<CursorPath>>();
for (int i = 0; i < NUMBER_OF_DISTANCES; i++) {
this.cursorPathsByDistance.add(new ArrayList<CursorPath>());
}
}
private ArrayList<CursorPath> getArrayListOfCursorPathsFromFile(String path) {
CursorDataFileParser cursorDataFileParser = new CursorDataFileParser(path);
return cursorDataFileParser.getArrayListOfCursorPathsFromFile();
}
private void assignCursorPathsByDistance(ArrayList<CursorPath> cursorPaths) {
for (CursorPath cursorPath : cursorPaths) {
if (cursorPath.isCursorPathReasonable()) {
addCursorPathToCursorPathsByDistance(cursorPath);
}
}
}
private void addCursorPathToCursorPathsByDistance(CursorPath cursorPath) {
this.cursorPathsByDistance.get(cursorPath.getCursorPathDistance()).add(cursorPath);
}
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

@ -0,0 +1,73 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CursorDataFileParser {
String pathToCursorData;
Pattern regexPatternToFilterInvalidLines;
public CursorDataFileParser(String pathToCursorData) {
this.pathToCursorData = pathToCursorData;
this.regexPatternToFilterInvalidLines = Pattern.compile("[0-9]*,[0-9]*,[0-9]*$");
}
public ArrayList<CursorPath> getArrayListOfCursorPathsFromFile() {
ArrayList<CursorPath> cursorPaths = new ArrayList<CursorPath>();
try {
File file = new File(this.pathToCursorData);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
CursorPoint lastCursorPoint = new CursorPoint(0, 0, 0);
int numberOfRepeats = 0;
ArrayList<CursorPoint> currentCursorPath = new ArrayList<CursorPoint>();
currentCursorPath.add(lastCursorPoint);
while ((line = bufferedReader.readLine()) != null) {
if (lineMatchesPattern(line)) {
CursorPoint newCursorPoint = getCursorPointFromLine(line);
if (cursorPointsHaveEqualCoordinates(newCursorPoint, lastCursorPoint)) {
numberOfRepeats++;
if (numberOfRepeats == 20) {
CursorPath newCursorPath = new CursorPath(currentCursorPath);
cursorPaths.add(newCursorPath);
currentCursorPath.clear();
}
}
else {
numberOfRepeats = 0;
currentCursorPath.add(newCursorPoint);
}
lastCursorPoint = newCursorPoint;
}
else {
System.out.println("Skipping invalid REGEX: " + line);
}
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return cursorPaths;
}
private boolean lineMatchesPattern(String line) {
Matcher regexMatcher = this.regexPatternToFilterInvalidLines.matcher(line);
return regexMatcher.find();
}
private CursorPoint getCursorPointFromLine(String line) {
String[] parts = line.split(Pattern.quote(","));
return new CursorPoint(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
private boolean cursorPointsHaveEqualCoordinates(CursorPoint a, CursorPoint b) {
return (a.x == b.x && a.y == b.y);
}
}

82
src/CursorPath.java Normal file
View File

@ -0,0 +1,82 @@
/*
* Represents each mouse path as an ArrayList of points.
*/
import java.util.ArrayList;
public class CursorPath {
private ArrayList<CursorPoint> pathCursorPoints;
private int pathNumPoints;
private int pathDistance;
private int pathTimespanMilliseconds;
public CursorPath(ArrayList<CursorPoint> cursorPoints)
{
this.pathCursorPoints = deepCopyCursorPoints(cursorPoints);
this.pathNumPoints = cursorPoints.size();
this.pathDistance = calculateCursorPathDistance();
this.pathTimespanMilliseconds = calculateCursorPathTimespan();
}
private ArrayList<CursorPoint> deepCopyCursorPoints(ArrayList<CursorPoint> cursorPoints) {
ArrayList<CursorPoint> cursorPointsCopy = new ArrayList<CursorPoint>(cursorPoints.size());
for (CursorPoint cursorPoint : cursorPoints) {
CursorPoint cursorPointCopy = new CursorPoint(cursorPoint.x, cursorPoint.y, cursorPoint.time);
cursorPointsCopy.add(cursorPointCopy);
}
return cursorPointsCopy;
}
private int calculateCursorPathTimespan() {
return getEndingCursorPoint().time - getStartingCursorPoint().time;
}
private int calculateCursorPathDistance() {
return (int) calculateDistanceBetweenCursorPoints(getStartingCursorPoint(), getEndingCursorPoint());
}
private CursorPoint getStartingCursorPoint() {
return pathCursorPoints.get(0);
}
private CursorPoint getEndingCursorPoint() {
return pathCursorPoints.get(pathNumPoints - 1);
}
private double calculateDistanceBetweenCursorPoints(CursorPoint a, CursorPoint b) {
return Math.hypot(a.x - b.x, a.y - b.y);
}
public boolean isCursorPathReasonable() {
return isCursorPathTimespanReasonable() && isCursorPathDistanceReasonable() &&
isCursorPathNumPointsReasonable();
}
private boolean isCursorPathTimespanReasonable() {
return (this.pathTimespanMilliseconds > 100 && this.pathTimespanMilliseconds < 400);
}
private boolean isCursorPathDistanceReasonable() {
return (this.pathDistance > 5 && this.pathDistance < 600);
}
private boolean isCursorPathNumPointsReasonable() {
return (this.pathNumPoints > 5 && this.pathNumPoints < 50);
}
public ArrayList<CursorPoint> getCursorPathPoints() {
return pathCursorPoints;
}
public int getCursorPathDistance() {
return pathDistance;
}
public void displayCursorPoints() {
for (CursorPoint p : pathCursorPoints) {
System.out.println("(" + p.x + ", " + p.y + "), " + p.time);
}
System.out.println("Length:" + pathNumPoints + ", Timespan:" + pathTimespanMilliseconds);
}
}

12
src/CursorPoint.java Normal file
View File

@ -0,0 +1,12 @@
public class CursorPoint {
public int x;
public int y;
public int time;
public CursorPoint(int x, int y, int time) {
this.x = x;
this.y = y;
this.time = time;
}
}

View File

@ -1,162 +0,0 @@
/* Reads a file of coordinates
*/
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Mouse {
private ArrayList<ArrayList<ArrayList<MousePath>>> grid;
int granularity;
//private ArrayList<MousePath> mousePaths;
private int windowWidth;
private int windowHeight;
PointerInfo pointer;
public Mouse(String path, int windowWidth, int windowHeight) {
this.windowWidth = windowWidth;
this.windowHeight = windowHeight;
granularity = 10;
// TODO: Is there another way to get the pointer location??
pointer = MouseInfo.getPointerInfo();
grid = new ArrayList<ArrayList<ArrayList<MousePath>>>();
for (int i = 0; i < 2 * (windowWidth / granularity) + 1; i++) {
grid.add(new ArrayList<ArrayList<MousePath>>());
for (int j = 0; j < 2 * (windowHeight / granularity) + 1; j++) {
grid.get(i).add(new ArrayList<MousePath>());
}
}
System.out.println("Grid size: " + grid.size() + "x" + grid.get(0).size());
ArrayList<MousePath> mousePaths = readFile(path);
assignPathsToGrid(mousePaths);
}
/**
* Moves mouse in a human-like way by finding a similar path from the past
* and slightly transforming it.
*
* @param endingX X coordinate of the ending location.
* @param endingY Y coordinate of the ending location.
*/
public void moveMouse(int endingX, int endingY) {
int[] mouseLoc = getMouseLocation();
int deltaX = endingX - mouseLoc[0];
int deltaY = endingY - mouseLoc[1];
int[] gridIndex = getGridIndex(deltaX, deltaY);
// Fetch from map
}
public int[] getMouseLocation() {
Point startingPoint = pointer.getLocation();
int x = (int) startingPoint.getX();
int y = (int) startingPoint.getY();
int loc[] = {x, y};
return loc;
}
public int[] getGridIndex(int deltaX, int deltaY) {
int offsetX = windowWidth / granularity;
int offsetY = windowHeight / granularity;
int[] gridIndex = {deltaX / granularity + offsetX, deltaY / granularity + offsetY};
return gridIndex;
}
public void assignPathsToGrid(ArrayList<MousePath> mousePaths) {
for (MousePath mousePath : mousePaths) {
int deltaX = mousePath.getDeltaX();
int deltaY = mousePath.getDeltaY();
int[] gridIndex = getGridIndex(deltaX, deltaY);
//System.out.println(deltaX + "," + deltaY);
//System.out.println("index: " + gridIndex[0] + "," + gridIndex[1]);
grid.get(gridIndex[0]).get(gridIndex[1]).add(mousePath);
}
}
public ArrayList<MousePath> readFile(String path) {
ArrayList<MousePath> mousePaths = new ArrayList<MousePath>();
try {
File file = new File(path);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
Pattern linePattern = Pattern.compile("[0-9]*,[0-9]*,[0-9]*$");
String line;
MousePoint lastPoint = new MousePoint(0, 0, 0);
int numberOfRepeats = 0;
ArrayList<MousePoint> currentPath = new ArrayList<MousePoint>();
currentPath.add(lastPoint);
while ((line = bufferedReader.readLine()) != null) {
if (!isLineValid(line, linePattern)) {
System.out.println(line + " does not match regex -- SKIPPING");
continue;
}
MousePoint point = getPointFromLine(line);
if (!point.isValid()) {
continue;
}
if (point.isSameLocation(lastPoint)) {
numberOfRepeats++;
if (numberOfRepeats == 20) {
if (currentPath.size() < 5) {
continue;
}
MousePath newPath = new MousePath(currentPath);
mousePaths.add(newPath); // Deep copies
currentPath.clear();
}
}
else {
numberOfRepeats = 0;
currentPath.add(point);
}
lastPoint = point;
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return mousePaths;
}
private boolean isLineValid(String line, Pattern linePattern) {
Matcher matcher = linePattern.matcher(line);
if (matcher.find()) {
return true;
}
return false;
}
private MousePoint getPointFromLine(String line) {
String[] parts = line.split(Pattern.quote(","));
return new MousePoint(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
public void displayPaths() {
for (int i = 0; i < 2 * (windowWidth / granularity) + 1; i++) {
for (int j = 0; j < 2 * (windowHeight / granularity) + 1; j++) {
if (grid.get(i).get(j).size() > 0) {
System.out.println("(" + i + "," + j + ")");
System.out.println("There are " + grid.get(i).get(j).size() + " paths in this delta range.");
}
}
}
}
}

View File

@ -1,86 +0,0 @@
/*
* Represents each mouse path as an ArrayList of points.
*/
import java.util.ArrayList;
public class MousePath {
private ArrayList<MousePoint> path;
private int numPoints;
private int deltaX;
private int deltaY;
private int timespan;
private int boundUp;
private int boundDown;
private int boundLeft;
private int boundRight;
public MousePath(ArrayList<MousePoint> mousePoints)
{
path = new ArrayList<MousePoint>(mousePoints.size());
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
for (MousePoint point : mousePoints) {
int x = point.getX();
int y = point.getY();
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
minX = Math.min(minX, x);
minY = Math.min(minY, y);
MousePoint pointCopy = new MousePoint(x, y, point.getTime());
path.add(pointCopy);
}
numPoints = path.size();
MousePoint startingPoint = path.get(0);
MousePoint endingPoint = path.get(numPoints - 1);
boundUp = maxY - startingPoint.getY();
boundDown = startingPoint.getY() - minY;
boundLeft = startingPoint.getX() - minX;
boundRight = maxX - startingPoint.getX();
deltaX = endingPoint.getX() - startingPoint.getX();
deltaY = endingPoint.getY() - startingPoint.getY();
timespan = path.get(numPoints - 1).getTime() - startingPoint.getTime();
}
public ArrayList<MousePoint> getPath() {
return path;
}
public int getNumPoints() {
return numPoints;
}
public int getTimespan() {
return timespan;
}
public int getDeltaX() {
return deltaX;
}
public int getDeltaY() {
return deltaY;
}
public int getBoundUp() {
return boundUp;
}
public int getBoundDown() {
return boundDown;
}
public int getBoundLeft() {
return boundLeft;
}
public int getBoundRight() {
return boundRight;
}
public void display() {
for (MousePoint p : path) {
System.out.println("(" + p.getX() + ", " + p.getY() + "), " + p.getTime());
}
System.out.println("Length:" + numPoints + ", Timespan:" + timespan);
}
}

View File

@ -5,26 +5,16 @@ import java.util.ArrayList;
import org.junit.jupiter.api.Test;
class MousePathTest {
Mouse mouse = new Mouse("/home/dpapp/eclipse-workspace/RunescapeAI/testfiles/mouse_path_test1.txt");
ArrayList<MousePath> mousePaths = mouse.getMousePaths();
@Test
void mousePathLengthTest() {
assertEquals(mousePaths.get(0).getNumPoints(), 45);
assertEquals(mousePaths.get(1).getNumPoints(), 17);
assertEquals(mousePaths.get(2).getNumPoints(), 33);
assertEquals(mousePaths.get(3).getNumPoints(), 14);
assertEquals(mousePaths.get(4).getNumPoints(), 13);
}
@Test
void mousePathTimespanTest() {
assertEquals(mousePaths.get(0).getTimespan(), 1225);
assertEquals(mousePaths.get(1).getTimespan(), 192);
assertEquals(mousePaths.get(2).getTimespan(), 458);
assertEquals(mousePaths.get(3).getTimespan(), 157);
assertEquals(mousePaths.get(4).getTimespan(), 142);
}
@Test

View File

@ -1,56 +0,0 @@
import java.awt.Point;
public class MousePoint {
private int x;
private int y;
private int time;
public MousePoint(int x, int y, int time) {
this.x = x;
this.y = y;
this.time = time;
}
public int getX() {
return x;
}
public int getY(){
return y;
}
public int getTime() {
return time;
}
public boolean isSameLocation(MousePoint p2) {
return (this.x == p2.getX() && this.y == p2.getY());
}
public double distance(MousePoint p2) {
return Math.hypot(this.x - p2.getX(), this.y - p2.getY());
}
public double distance(Point p2) {
return Math.hypot(this.x - p2.getX(), this.y - p2.getY());
}
// TODO: define window size
public boolean isValid() {
return (x >= 0 && x < 1920 && y >= 0 && y < 1080 && time >= 0);
}
/*@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof MousePoint)) {
return false;
}
MousePoint p = (MousePoint) obj;
// Compare the data members and return accordingly
return (this.x == p.x && this.y == p.y && this.time == p.time);
}*/
}

View File

@ -15,15 +15,7 @@ class MouseTest {
@Test
void testMouseLengths() {
Mouse mouse = new Mouse("/home/dpapp/eclipse-workspace/RunescapeAI/testfiles/mouse_path_test1.txt");
assertEquals(mouse.getNumberOfPaths(), 5);
ArrayList<MousePath> mousePaths = mouse.getMousePaths();
assertEquals(mousePaths.get(0).getNumPoints(), 45);
assertEquals(mousePaths.get(1).getNumPoints(), 17);
assertEquals(mousePaths.get(2).getNumPoints(), 33);
assertEquals(mousePaths.get(3).getNumPoints(), 14);
assertEquals(mousePaths.get(4).getNumPoints(), 13);
}
}

View File

@ -6,55 +6,6 @@ class PointTest {
@Test
void initializePointTest() {
MousePoint p = new MousePoint(54, 4, 134);
assertEquals(p.getX(), 54);
assertEquals(p.getY(), 4);
assertEquals(p.getTime(), 134);
}
@Test
void isValidPointTest() {
MousePoint p1 = new MousePoint(54, 4, 134);
assertEquals(p1.isValid(), true);
MousePoint p2 = new MousePoint(-3, 84, 832);
assertEquals(p2.isValid(), false);
MousePoint p3 = new MousePoint(1940, 84, 832);
assertEquals(p3.isValid(), false);
MousePoint p4 = new MousePoint(3, -5, 832);
assertEquals(p4.isValid(), false);
MousePoint p5 = new MousePoint(0, 1084, 832);
assertEquals(p5.isValid(), false);
MousePoint p6 = new MousePoint(0, 1001, -4);
assertEquals(p6.isValid(), false);
}
@Test
void isSameLocationTest() {
MousePoint p1 = new MousePoint(54, 4, 134);
MousePoint p2 = new MousePoint(54, 4, 832);
MousePoint p3 = new MousePoint(85, 4, 832);
MousePoint p4 = new MousePoint(54, 12, 832);
assertEquals(p1.isSameLocation(p2), true);
assertEquals(p1.isSameLocation(p3), false);
assertEquals(p1.isSameLocation(p4), false);
}
@Test
void distanceTest() {
MousePoint p1 = new MousePoint(54, 4, 134);
MousePoint p2 = new MousePoint(54, 4, 832);
MousePoint p3 = new MousePoint(85, 4, 832);
MousePoint p4 = new MousePoint(85, 12, 832);
assertEquals(p1.distance(p2), 0.0);
assertEquals(p1.distance(p3), 31.0);
double distance = p1.distance(p4) - 32.01562118;
assertTrue(distance >= 0 && distance < 0.00001);
}
}

View File

@ -5,10 +5,9 @@ public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Starting mouse script...");
System.out.println("Fetching mouse paths from script...");
Mouse mouse = new Mouse("/home/dpapp/GhostMouse/coordinates.txt", 1920, 1080);
mouse.displayPaths();
Cursor cursor = new Cursor();
cursor.displaycursorPathsByDistance();
System.out.println("Finished...");
}
}