Skip to content

Commit

Permalink
chore: Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
peavers committed Jul 2, 2021
1 parent 63c728d commit 8b52d1c
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 250 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ out/

### Debug stuff ###
.debug
.DS_Store
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
plugins {
id 'org.springframework.boot' version '2.5.0'
id 'org.springframework.boot' version '2.5.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.sonarqube' version '3.3'
id 'java'
}

group = 'io.skyshard'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 16
sourceCompatibility = 15

configurations {
compileOnly {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/io/skyshard/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
@EnableConfigurationProperties(AppProperties.class)
public class Application {

public static void main(final String[] args) {
public static void main(final String[] args) {

Loader.load(opencv_java.class);
Loader.load(opencv_java.class);

avutil.av_log_set_level(avutil.AV_LOG_QUIET);
avutil.av_log_set_level(avutil.AV_LOG_QUIET);

final var builder = new SpringApplicationBuilder(Application.class);
builder.headless(false);
builder.run(args);
}

final SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
builder.headless(false);
builder.run(args);
}
}
18 changes: 9 additions & 9 deletions src/main/java/io/skyshard/configuration/GrabConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.skyshard.configuration;

import io.skyshard.exceptions.UnsupportedSystemException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.bytedeco.javacv.FFmpegFrameGrabber;
Expand All @@ -19,9 +20,8 @@ public class GrabConfig {
@Bean
public FFmpegFrameGrabber grabber() {

final Dimension dimension = getDimension();

final FFmpegFrameGrabber grabber = getSystemSpecificGrabber();
final var dimension = getDimension();
final var grabber = getSystemSpecificGrabber();

grabber.setFrameRate(30);
grabber.setImageWidth(dimension.width);
Expand All @@ -42,20 +42,21 @@ private FFmpegFrameGrabber getSystemSpecificGrabber() {
}

if (SystemUtils.IS_OS_MAC) {

// 2:0 represents the screen you want to grab, if primary display set as 1:0, if external monitor 2:0.
// On a Mac, the filename represents the screen you want to capture. In this case 2:0 is my second screen
// with no audio input, and 1:0 would be your primary screen with no audio input. There is an FFMPEG command
// you can execute to identify what screen is which (google it).
return buildGrabber("2:0", "avfoundation");
}

throw new RuntimeException("Unsupported System. Exiting...");
throw new UnsupportedSystemException();
}

/**
* Function to create a new grabber with predefined settings.
*/
private FFmpegFrameGrabber buildGrabber(final String filename, final String format) {

FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(filename);
final var grabber = new FFmpegFrameGrabber(filename);
grabber.setFormat(format);

return grabber;
Expand All @@ -68,8 +69,7 @@ private Dimension getDimension() {

final var dimension = Toolkit.getDefaultToolkit().getScreenSize();

log.info(
"Using screen dimensions {}x{} as reference points", dimension.width, dimension.height);
log.info("Screen dimensions {}x{}", dimension.width, dimension.height);

return dimension;
}
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/io/skyshard/configuration/RobotConfig.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package io.skyshard.configuration;

import java.awt.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.*;

@Configuration
public class RobotConfig {

/**
* Create a singleton robot, the robot is used for moving the mouse pointer as well as keyboard
* inputs.
*/
@Bean
public Robot robot() {
/**
* Create a singleton robot, the robot is used for moving the mouse pointer as well as keyboard
* inputs.
*/
@Bean
public Robot robot() {

try {
final var robot = new Robot();
robot.setAutoWaitForIdle(true);
try {
final var robot = new Robot();
robot.setAutoWaitForIdle(true);

return robot;
} catch (final Exception exception) {
throw new RuntimeException(exception.getMessage());
return robot;
} catch (final AWTException exception) {
throw new RuntimeException(exception.getMessage());
}
}
}

}
37 changes: 20 additions & 17 deletions src/main/java/io/skyshard/configuration/TemplateConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.skyshard.configuration;

import io.skyshard.exceptions.MissingTemplateException;
import io.skyshard.properties.AppProperties;
import java.io.FileNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.Mat;
Expand All @@ -10,31 +10,34 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class TemplateConfig {

private final AppProperties appProperties;
private final AppProperties appProperties;

/**
* Read in the matching template file from the resource directory. If this can't be found or its
* not set in the application properties file, exit the application.
*/
@Bean
public Mat loadTemplate() {
/**
* Read in the matching template file from the resource directory. If this can't be found or its
* not set in the application properties file, exit the application.
*/
@Bean
public Mat loadTemplate() {

try {
final var templateSource =
ResourceUtils.getFile(String.format("classpath:%s", appProperties.getTemplate()));
try {
final var templateSource =
ResourceUtils.getFile(String.format("classpath:%s", appProperties.getTemplate()));

log.info("Using template file {}", templateSource.getAbsolutePath());
log.info("Using template file {}", templateSource.getAbsolutePath());

return Imgcodecs.imread(templateSource.getAbsolutePath());
return Imgcodecs.imread(templateSource.getAbsolutePath());

} catch (final FileNotFoundException fileNotFoundException) {
log.error("Cannot find template file {}, exiting...", appProperties.getTemplate());
throw new RuntimeException(fileNotFoundException.getMessage());
} catch (final FileNotFoundException fileNotFoundException) {
log.error("Cannot find template file {}, exiting...", appProperties.getTemplate());
throw new MissingTemplateException();
}
}
}

}
35 changes: 19 additions & 16 deletions src/main/java/io/skyshard/domain/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@
import lombok.Data;
import org.opencv.core.Point;

/** Represents where a target is found on the screen. */
/**
* Represents where a target is found on the screen.
*/
@Data
@Builder
public class Target {

private Point point;
private Point point;

/**
* Get the X location of the point. Since we're talking about pixels here we don't care about the
* decimal point.
*/
public int x() {
/**
* Get the X location of the point. Since we're talking about pixels here we don't care about the
* decimal point.
*/
public int x() {

return (int) Math.round(this.point.x);
}
return (int) Math.round(this.point.x);
}

/**
* Get the Y location of the point. Since we're talking about pixels here we don't care about the
* decimal point.
*/
public int y() {
/**
* Get the Y location of the point. Since we're talking about pixels here we don't care about the
* decimal point.
*/
public int y() {

return (int) Math.round(this.point.y);
}

return (int) Math.round(this.point.y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.skyshard.exceptions;

public class MissingTemplateException extends RuntimeException {

public MissingTemplateException() {
super("Unable to find a template to match on. Exiting...");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.skyshard.exceptions;

public class UnsupportedSystemException extends RuntimeException {

public UnsupportedSystemException() {
super("Attempting to run on an unsupported system. Exiting...");
}

}
35 changes: 22 additions & 13 deletions src/main/java/io/skyshard/properties/AppProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
@ConfigurationProperties("application")
public class AppProperties {

/** Includes writing images to disk, don't run in production Default: false */
private boolean debug = false;
/**
* Includes writing images to disk, don't run in production Default: false
*/
private boolean debug = false;

/** Template file location relative to resource root. Default: /templates/default.png */
private String template = "templates/default-1.png";
/**
* Template file location relative to resource root. Default: /templates/default.png
*/
private String template = "templates/default-1.png";

/** If true, only a single target is processed before a new image is scanned. Default: false */
private boolean singleTargetMode = true;
/**
* If true, only a single target is processed before a new image is scanned. Default: false
*/
private boolean singleTargetMode = true;

/** Threshold used when searching for a template match via OpenCV. Default: 0.90 */
private double matchThreshold = 0.90;
/**
* Threshold used when searching for a template match via OpenCV. Default: 0.90
*/
private double matchThreshold = 0.90;

/**
* How many pixels on X and Y to check for the same template image. This stops the same target
* being picked up many times. Default: 30
*/
private double duplicateThreshold = 30;

/**
* How many pixels on X and Y to check for the same template image. This stops the same target
* being picked up many times. Default: 30
*/
private double duplicateThreshold = 30;
}
3 changes: 2 additions & 1 deletion src/main/java/io/skyshard/services/AttackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

public interface AttackService {

void attack(Target target);
void attack(Target target);

}
27 changes: 14 additions & 13 deletions src/main/java/io/skyshard/services/AttackServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
@RequiredArgsConstructor
public class AttackServiceImpl implements AttackService {

private final Robot robot;
private final Robot robot;

/**
* Move the mouse to the target location, then offset it slightly to counter the fact we always
* find the very bottom right/left corner of a template match. Once the mouse has been moved over
* the target, press the '1' keyboard button.
*/
@Override
public void attack(final Target target) {
/**
* Move the mouse to the target location, then offset it slightly to counter the fact we always
* find the very bottom right/left corner of a template match. Once the mouse has been moved over
* the target, press the '1' keyboard button.
*/
@Override
public void attack(final Target target) {

robot.mouseMove(target.x() - 5, target.y() - 5);
robot.keyPress(KeyEvent.VK_1);
robot.delay(MathUtils.random(25, 50));
robot.keyRelease(KeyEvent.VK_1);
}

robot.mouseMove(target.x() - 5, target.y() - 5);
robot.keyPress(KeyEvent.VK_1);
robot.delay(MathUtils.random(25, 50));
robot.keyRelease(KeyEvent.VK_1);
}
}
8 changes: 5 additions & 3 deletions src/main/java/io/skyshard/services/FindTargetService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.skyshard.services;

import io.skyshard.domain.Target;
import org.opencv.core.Mat;

import java.util.List;
import java.util.Optional;
import org.opencv.core.Mat;

public interface FindTargetService {

List<Target> findMultipleTarget(Mat source);
List<Target> findMultipleTarget(Mat source);

Optional<Target> findSingleTarget(Mat source);

Optional<Target> findSingleTarget(Mat source);
}
Loading

0 comments on commit 8b52d1c

Please sign in to comment.