1 | /*
|
---|
2 | * Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
3 | * License BSDLike http://rickvanderzwet.nl/LICENSE
|
---|
4 | */
|
---|
5 |
|
---|
6 | package nl.rickvanderzwet.farmville;
|
---|
7 |
|
---|
8 | import java.awt.AWTException;
|
---|
9 | import java.awt.Color;
|
---|
10 | import java.awt.PointerInfo;
|
---|
11 | import java.awt.Robot;
|
---|
12 | import java.awt.event.InputEvent;
|
---|
13 | import java.util.Random;
|
---|
14 | import java.awt.MouseInfo;
|
---|
15 | import java.io.IOException;
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | public class Automator {
|
---|
20 |
|
---|
21 | public enum Direction {
|
---|
22 | UP, DOWN, LEFT, RIGHT
|
---|
23 | }
|
---|
24 |
|
---|
25 | private static Robot bot = null;
|
---|
26 | private static Random randomGenerator = null;
|
---|
27 |
|
---|
28 | private static void pressMouse() throws InterruptedException {
|
---|
29 | bot.mousePress(InputEvent.BUTTON1_MASK);
|
---|
30 | // Constant values are tricky, computers can only do that
|
---|
31 | Thread.sleep(100 + randomGenerator.nextInt(250));
|
---|
32 | bot.mouseRelease(InputEvent.BUTTON1_MASK);
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static void moveMouse(Direction direction) throws InterruptedException {
|
---|
37 | moveMouse(direction, 100);
|
---|
38 | }
|
---|
39 |
|
---|
40 | private static void moveMouse(Direction direction, Integer sleep_msec) throws InterruptedException {
|
---|
41 | /* Relocate to next field */
|
---|
42 | PointerInfo pointer = MouseInfo.getPointerInfo();
|
---|
43 |
|
---|
44 | Integer XMOVE = 24;
|
---|
45 | Integer YMOVE = 11;
|
---|
46 |
|
---|
47 | Integer x = pointer.getLocation().x;
|
---|
48 | Integer y = pointer.getLocation().y;
|
---|
49 |
|
---|
50 | switch (direction) {
|
---|
51 | case UP:
|
---|
52 | bot.mouseMove(x - XMOVE, y - YMOVE);
|
---|
53 | break;
|
---|
54 | case DOWN:
|
---|
55 | bot.mouseMove(x + XMOVE, y + YMOVE);
|
---|
56 | break;
|
---|
57 | case LEFT:
|
---|
58 | bot.mouseMove(x - XMOVE, y + YMOVE);
|
---|
59 | break;
|
---|
60 | case RIGHT:
|
---|
61 | bot.mouseMove(x + XMOVE, y - YMOVE);
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | Thread.sleep(sleep_msec);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @param args
|
---|
69 | * @throws InterruptedException
|
---|
70 | * @throws IOException
|
---|
71 | */
|
---|
72 | public static void main(String[] args) throws InterruptedException, IOException {
|
---|
73 | try {
|
---|
74 | //bot.mouseMove(100, 100);
|
---|
75 | bot = new Robot();
|
---|
76 | PointerInfo pointer = null;
|
---|
77 | randomGenerator = new Random();
|
---|
78 | // Allow time to focus Firefox window
|
---|
79 | Thread.sleep(3000);
|
---|
80 |
|
---|
81 | int x = 0 ,y = 0;
|
---|
82 | /*
|
---|
83 | bot.mouseMove(500, 500);
|
---|
84 | bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
---|
85 | bot.mouseMove(700, 700);
|
---|
86 | bot.mouseRelease(InputEvent.BUTTON1_MASK);
|
---|
87 | */
|
---|
88 |
|
---|
89 | /* Constants */
|
---|
90 | Boolean press_mouse = true;
|
---|
91 | Integer farm_size_y = 9;
|
---|
92 | Integer farm_size_x = 22;
|
---|
93 | /* 0 = up, 1 = down, 2 = left, 3 = right */
|
---|
94 |
|
---|
95 | if (press_mouse) {
|
---|
96 | pressMouse();
|
---|
97 | }
|
---|
98 |
|
---|
99 | Direction direction = Direction.UP;
|
---|
100 |
|
---|
101 | // Save the mouse button :-)
|
---|
102 | for (int i = 0; i < farm_size_y; i++) {
|
---|
103 | for (int j = 0; j < farm_size_x; j++) {
|
---|
104 | pointer = MouseInfo.getPointerInfo();
|
---|
105 |
|
---|
106 | x = pointer.getLocation().x;
|
---|
107 | y = pointer.getLocation().y;
|
---|
108 | System.out.println("x: " + x + ", y: " + y);
|
---|
109 |
|
---|
110 |
|
---|
111 | /* Too difficult, try larger parts */
|
---|
112 | Color p = bot.getPixelColor(x, y);
|
---|
113 | System.out.println("Pixel color R:" + p.getRed() + ",G:" +
|
---|
114 | p.getGreen() + ",B:" + p.getBlue());
|
---|
115 |
|
---|
116 | if (press_mouse) {
|
---|
117 | pressMouse();
|
---|
118 | }
|
---|
119 | moveMouse(direction);
|
---|
120 | }
|
---|
121 |
|
---|
122 | moveMouse(Direction.LEFT);
|
---|
123 | pressMouse();
|
---|
124 |
|
---|
125 | if (direction == Direction.UP) {
|
---|
126 | direction = Direction.DOWN;
|
---|
127 | } else {
|
---|
128 | direction = Direction.UP;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | } catch (AWTException e) {
|
---|
132 | // TODO Auto-generated catch block
|
---|
133 | e.printStackTrace();
|
---|
134 | }
|
---|
135 |
|
---|
136 | // TODO Auto-generated method stub
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|