|
import argparse |
|
import io |
|
import subprocess |
|
import threading |
|
import time |
|
import urllib.request |
|
|
|
import numpy |
|
from PIL import Image |
|
from numpy import random |
|
|
|
adb_path = "/usr/bin/adb" |
|
|
|
|
|
def adb_exec(commands): |
|
commands = [str(c) for c in commands] |
|
commands.insert(0, adb_path) |
|
commands.insert(1, "shell") |
|
commands.insert(2, "input") |
|
subprocess.check_output(commands) |
|
|
|
|
|
def throw_ball(): |
|
height = random.randint(100, 1100) |
|
adb_exec(["swipe", 540, 1400, 540, height, 250]) |
|
|
|
|
|
def random_touch(): |
|
radius = 250 |
|
center = (540, 1150) |
|
|
|
rad = random.randint(0, 1000) / 1000.0 * numpy.pi * 2 |
|
r = random.randint(0, 1000) / 1000.0 * radius |
|
y = numpy.sin(rad) * r |
|
x = numpy.cos(rad) * r |
|
cx, cy = center |
|
x += cx |
|
y += cy |
|
adb_exec(["touchscreen", "tap", x, y]) |
|
|
|
|
|
def swipe_poke_stop(): |
|
adb_exec(["swipe", 200, 800, 600, 800, 200]) |
|
|
|
|
|
def send(): |
|
adb_exec(["touchscreen", "tap", 540, 540]) |
|
time.sleep(1) |
|
adb_exec(["swipe", 800, 1700, 800, 100, 1200]) |
|
time.sleep(1) |
|
adb_exec(["touchscreen", "tap", "700", "1500"]) |
|
adb_exec(["touchscreen", "tap", "540", "1000"]) |
|
|
|
|
|
def power_up(): |
|
adb_exec(["touchscreen", "tap", "700", "1500"]) |
|
adb_exec(["touchscreen", "tap", "540", "1000"]) |
|
|
|
|
|
def tap_cancel_button(): |
|
adb_exec(["touchscreen", "tap", 540, 1700]) |
|
|
|
|
|
def crop(img, center, edge): |
|
x, y = center |
|
x -= edge / 2 |
|
y -= edge / 2 |
|
c = img.crop((x, y, x + edge, y + edge)) |
|
return c |
|
|
|
|
|
def crop_ball(img): |
|
return crop(img=img, center=(540, 1625), edge=150) |
|
|
|
|
|
def convert_to_array(img): |
|
return numpy.asarray(img) |
|
|
|
|
|
def match(img1, img2): |
|
a1 = convert_to_array(img1) |
|
a2 = convert_to_array(img2) |
|
cnt = 0 |
|
dif = 0 |
|
for i in range(0, len(a1)): |
|
for j in range(0, len(a1[i])): |
|
cnt += 255 |
|
x = int(a1[i][j]) - int(a2[i][j]) |
|
dif += abs(x) |
|
return (cnt - dif) / float(cnt) |
|
|
|
|
|
def crop_away(img): |
|
return crop(img=img, center=(121, 150), edge=144) |
|
|
|
|
|
def crop_camera(img): |
|
return crop(img=img, center=(939, 1428), edge=182) |
|
|
|
|
|
def crop_nap(img): |
|
return crop(img=img, center=(939, 1652), edge=182) |
|
|
|
|
|
def crop_stop(img): |
|
return crop(img=img, center=(50, 1700), edge=100) |
|
|
|
|
|
def is_in_field(img): |
|
return match(crop_ball(img), Image.open("ball.png")) > 0.9 |
|
|
|
|
|
def is_in_battle(img): |
|
c = match(crop_camera(img), Image.open("camera.png")) |
|
n = match(crop_nap(img), Image.open("nap.png")) |
|
a = match(crop_away(img), Image.open("away.png")) |
|
|
|
return max(c, n, a) > 0.9 |
|
|
|
|
|
def is_poke_stop(img): |
|
return match(crop_stop(img), Image.open("stop.png")) > 0.95 |
|
|
|
|
|
def init_q(): |
|
q = [] |
|
for i in range(0, 500): |
|
radius = 250 |
|
center = (540, 1150) |
|
|
|
rad = random.randint(0, 1000) / 1000.0 * numpy.pi * 2 |
|
r = random.randint(0, 1000) / 1000.0 * radius |
|
y = numpy.sin(rad) * r |
|
x = numpy.cos(rad) * r |
|
cx, cy = center |
|
x += cx |
|
y += cy |
|
q.append((x, y)) |
|
return q |
|
|
|
|
|
class TouchThread(threading.Thread): |
|
def __init__(self): |
|
super().__init__() |
|
self.state = "" |
|
self.q = init_q() |
|
self.cnt = 0 |
|
|
|
def set_state(self, s): |
|
self.state = s |
|
print(s) |
|
|
|
def random_touch(self): |
|
x, y = self.q[self.cnt] |
|
self.cnt += 1 |
|
if self.cnt >= len(self.q): |
|
self.cnt = 0 |
|
adb_exec(["touchscreen", "tap", x, y]) |
|
|
|
def run(self): |
|
while True: |
|
if self.state == "battle": |
|
throw_ball() |
|
elif self.state == "field": |
|
self.random_touch() |
|
elif self.state == "stop": |
|
swipe_poke_stop() |
|
time.sleep(2) |
|
tap_cancel_button() |
|
else: |
|
tap_cancel_button() |
|
|
|
|
|
def main(args): |
|
t = TouchThread() |
|
t.start() |
|
state = "" |
|
cnt = 0 |
|
while True: |
|
url = args.u |
|
path = io.BytesIO(urllib.request.urlopen(url).read()) |
|
img_color = Image.open(path) |
|
img = img_color.convert("L") |
|
|
|
if is_in_field(img): |
|
state = "field" |
|
elif is_in_battle(img): |
|
state = "battle" |
|
elif is_poke_stop(img): |
|
state = "stop" |
|
else: |
|
if state == "field": |
|
state = "stop" |
|
else: |
|
state = "unknown" |
|
|
|
t.set_state(state) |
|
cnt += 1 |
|
|
|
|
|
if __name__ == '__main__': |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("-u", default="http://127.0.0.1:53516/screenshot.jpg?password=27023775", help="URL") |
|
main(parser.parse_args()) |