-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
355 lines (335 loc) · 10.4 KB
/
main.php
File metadata and controls
355 lines (335 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Created by PhpStorm.
* User: simon
* Date: 01/01/2018
* Time: 2:14 PM
* https://www.fushupeng.com
* contact@fushupeng.com
*/
define('DEBUG', false);
define('TRAINING', false);
/**
* 计算颜色是否相似
* @param $standard
* @param $target
* @param $tolerance
* @return bool
*/
function similar($standard, $target, $tolerance)
{
if (is_array($standard)) {
$sr = $standard['r'];
$sg = $standard['g'];
$sb = $standard['b'];
} else {
$sr = ($standard >> 16) & 0xFF;
$sg = ($standard >> 8) & 0xFF;
$sb = $standard & 0xFF;
}
$tr = ($target >> 16) & 0xFF;
$tg = ($target >> 8) & 0xFF;
$tb = $target & 0xFF;
return
abs($sr - $tr) +
abs($sg - $tg) +
abs($sb - $tb) <= $tolerance;
}
/**
* 使用 adb 驱动截屏,并 pull 到指定目录中
* @param $path
*/
function screenCapturing($path)
{
print_r("Screen capturing.....\r\n");
ob_start();
system('adb shell screencap -p /sdcard/screen.png');
system(sprintf('adb pull /sdcard/screen.png %s', $path));
ob_end_clean();
}
/**
* 获取起止点坐标
* 终点坐标获取方法仍需要进一步优化
* @param $image
* @param $width
* @param $height
* @param $conf
* @return array
*/
function getStartAndEnd($image, $width, $height, $conf)
{
$border = getScanBorder($image, $width, $height, $conf);
try {
$startPoint = getStartPoint($image, $width, $border, $conf);
$endPoint = getEndPoint($image, $width, $startPoint, $conf, $border);
return ['start' => $startPoint, 'end' => $endPoint];
} catch (Exception $e) {
echo $e -> getMessage() . "\r\n";
die;
}
}
/**
* 获取检测区域边界,提高执行效率
* @param $image
* @param $width
* @param $height
* @param $conf
* @return array
*/
function getScanBorder($image, $width, $height, $conf)
{
$heightStartBorder = $height / 3;
$heightEndBorder = 2 * $height / 3;
$checkWidth = $width - $conf['character']['width'];
for ($j = $heightStartBorder; $j < $heightEndBorder; $j++) {
$firstColorIndex = imagecolorat($image, 0, $j);
for ($i = 0; $i < $checkWidth; $i = $i + $conf['character']['bottomWidth']) {
$colorIndex = imagecolorat($image, $i, $j);
if (!similar($firstColorIndex, $colorIndex, $conf['colorTolerance'])) {
return ['start' => $j, 'end' => $heightEndBorder];
}
}
}
return ['start' => $heightStartBorder, 'end' => $heightEndBorder];
}
/**
* 获取起始点(棋子)的坐标
* @param $image
* @param $width
* @param $border
* @param $conf
* @return array
* @throws Exception
*/
function getStartPoint($image, $width, $border, $conf)
{
$horizontalStart = $width / 8;
$horizontalEnd = 7 * $width / 8;
$startPositionXSum = 0;
$startPositionXCount = 0;
$startPositionY = 0;
$points = [];
for ($j = $border['start']; $j < $border['end']; $j++) {
for ($i = $horizontalStart; $i < $horizontalEnd; $i += $conf['character']['bottomWidth']) {
$colorIndex = imagecolorat($image, $i, $j);
if (similar($conf['character']['colorIndex'], $colorIndex, $conf['colorTolerance'])) {
$startPositionXSum += $i;
$startPositionXCount += 1;
$startPositionY = max($startPositionY, $j);
$points[] = ['x' => $i, 'y' => $j];
}
}
}
if ($startPositionXCount == 0) {
throw new Exception('Error in finding the character!');
}
return [
'x' => $startPositionXSum / $startPositionXCount,
'y' => $startPositionY - $conf['character']['offset'],
'matchPoints' => $points
];
}
/**
* 获取终点坐标
* @param $image
* @param $width
* @param $startPoint
* @param $conf
* @param $border
* @return array
* @throws Exception
*/
function getEndPoint($image, $width, $startPoint, $conf, $border)
{
$points = [];
$endPositionXSum = 0;
$endPositionXCount = 0;
if ($startPoint['x'] > $width / 2) {
// 起始点在中轴的右侧,终点则在中轴左侧,水平方向检查的距离为 0 —— 中轴
$horizontalStart = 1; // 防止边缘溢出
$horizontalEnd = $width / 2 - $conf['character']['width'] / 2;
} else {
// 起始点在中轴的左侧,终点则在中轴右侧,水平方向检查的距离为 中轴 —— $width
$horizontalStart = $width / 2 + $conf['character']['width'] / 2;
$horizontalEnd = $width-1; // 防止边缘溢出
}
$line = 0;
for ($j = $border['start']; $j < $startPoint['y']; $j++) {
$lineStartColorIndex = imagecolorat($image, 0, $j);
$matchMark = false;
for ($i = $horizontalEnd; $i > $horizontalStart; $i--) {
$colorIndex = imagecolorat($image, $i, $j);
if (!similar($lineStartColorIndex, $colorIndex, $conf['colorTolerance'])) {
$endPositionXSum += $i;
$endPositionXCount += 1;
$points[] = ['x' => $i, 'y' => $j];
$matchMark = true;
}
}
// 取目标区域的前十行像素进行判断,以排除阴影区域造成的干扰
if ($matchMark) {
$line++;
if ($line > 10) {
break;
}
}
}
if ($endPositionXCount == 0) {
throw new Exception('Error in finding the end point!');
}
// 计算 Y 值:
// 根据 https://github.com/wangshub/wechat_jump_game 提示,
// 当前使用的方法为 起止点连线与水平方向呈大约30度夹角
// startPoint[Y] - tan 30度 * |endPoint[x] - startPoint[x]|
$endPositionX = $endPositionXSum / $endPositionXCount;
return [
'x' => $endPositionX,
'y' => $startPoint['y'] - abs($endPositionX - $startPoint['x']) * sqrt(3) / 3,
'matchPoints' => $points
];
}
/**
* 计算点击屏幕时间
* @param $startPoint
* @param $endPoint
* @param $conf
* @return int
*/
function getPressTime($startPoint, $endPoint, $conf)
{
return (int) (
sqrt(
pow($startPoint['x'] - $endPoint['x'], 2) + pow($startPoint['y'] - $endPoint['y'], 2)
) * $conf['pressTimeRatio']
);
}
/**
* 执行跳跃
* @param $pressTime
*/
function doJump($pressTime)
{
echo "Jumping......\r\n";
$cmd = sprintf('adb shell input swipe 320 410 320 410 %s', $pressTime);
echo sprintf("Sending jump command %s \r\n", $cmd);
system($cmd);
}
/**
* 保留 debug 信息
* @param $image
* @param $width
* @param $height
* @param $startPoint
* @param $endPoint
* @param $path
* @param null $matchPoints
*/
function debug($image, $width, $height, $startPoint, $endPoint, $path, $matchPoints = null)
{
$target = imagecreatetruecolor($width, $height);
$white = imagecolorallocate ( $target, 255, 255, 255 );
imagefill($target, 0, 0, $white);
imagecopyresampled($target, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$startPointColor = imagecolorallocate($target, 255, 0, 0);
$endPointColor = imagecolorallocate($target, 0, 0, 255);
$startToEndColor = imagecolorallocate($target, 0, 0, 0);
$midLineColor = imagecolorallocate($target, 0, 255, 0);
// 中线
imageline($target, 540, 0, 540, $height, $midLineColor);
// 起始点标记
imageellipse($target, $startPoint['x'], $startPoint['y'], 10, 10, $startPointColor);
imageline($target, 0, $startPoint['y'], $width, $startPoint['y'], $startPointColor);
imageline($target, $startPoint['x'], 0, $startPoint['x'], $height, $startPointColor);
// 终点标记
imageellipse($target, $endPoint['x'], $endPoint['y'], 10, 10, $endPointColor);
imageline($target, 0, $endPoint['y'], $width, $endPoint['y'], $endPointColor);
imageline($target, $endPoint['x'], 0, $endPoint['x'], $height, $endPointColor);
// 起止点连线
imageline($target, $startPoint['x'], $startPoint['y'], $endPoint['x'], $endPoint['y'], $startToEndColor);
if (!is_null($matchPoints)) {
$color = imagecolorallocate($target, 224, 0, 0);
foreach ($matchPoints as $point) {
imageline($target, $point['x'], $point['y'], $point['x'], $point['y'], $color);
}
}
imagepng($target, $path . '/' . time() . '.png');
imagedestroy($target);
}
/**
* 程序初始化
* @return string
*/
function init()
{
$path = '';
// 创建保存Debug文件目录
if (TRAINING || DEBUG) {
echo "创建存储debug文件目录...\r\n";
$storePath = $path = './debug/' . date('Ymd');
for ($i = 1;; $i ++) {
if (is_dir($path)) {
$path = $storePath . '_' . $i;
} else {
@mkdir($path);
break;
}
}
}
return $path;
}
/**
* 执行训练操作
* @param $max
* @param $conf
* @param $path
*/
function training($max, $conf, $path)
{
$max = $max >= 107 ? 107 : $max;
for ($i = 35; $i < $max; $i++) {
$image = imagecreatefrompng('./training-data/' . $i . '.png');
$width = imagesx($image);
$height = imagesy($image);
$points = getStartAndEnd($image, $width, $height, $conf);
$matchPoints = array_merge($points['start']['matchPoints'], $points['end']['matchPoints']);
echo "debugging....\r\n";
debug($image, $width, $height, $points['start'], $points['end'], $path, $matchPoints);
sleep(1);
}
}
/**
* 执行操作
* @param $conf
* @param $path
*/
function run($conf, $path)
{
for ($i = 0; ; $i++) {
screenCapturing($conf['captureFile']);
$image = imagecreatefrompng($conf['captureFile']);
$width = imagesx($image);
$height = imagesy($image);
$points = getStartAndEnd($image, $width, $height, $conf);
$pressTime = getPressTime($points['start'], $points['end'], $conf);
echo sprintf(
"【%s】 From [X: %s, Y: %s] -> [X: %s, Y: %s], press time is %s. \r\n",
$i, $points['start']['x'], $points['start']['y'], $points['end']['x'], $points['end']['y'], $pressTime
);
doJump($pressTime);
if (DEBUG) {
debug($image, $width, $height, $points['start'], $points['end'], $path);
}
sleep($conf['sleep']);
}
}
function main($conf = array())
{
echo "Thread starting..... \r\n";
$path = init();
if (TRAINING) {
training(100, $conf, $path);
} else {
run($conf, $path);
}
}
main(require_once 'conf.php');