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