import os
import time
import numpy as np
import numpy.random as rd
import warnings
import matplotlib.pyplot as plt
from itertools import product
class Jump:
def __init__(self, show=False, test=False, mistake=False):
self.show = show
self.test = test
self.mistake = mistake
self.py_cwd = os.getcwd()
self.adb_pwd = "E:/\"Program Files\"/platform-tools"
# get screen size
screen_size = self.adb_read("shell wm size")[len("Physical size: "):]
screen_size = self.adb_read("shell wm size")
screen_size_id0 = screen_size.find("Physical size: ")
screen_size_id1 = screen_size.find("Override size: ")
if screen_size_id1 >= 0:
screen_size = screen_size[screen_size_id1 + len("Override size: "):]
else:
screen_size = screen_size[len("Physical size: "):]
size_ary = screen_size[:].split("x")
print(size_ary)
try:
# len0 = screen_len0
self.len0 = int(size_ary[0])
# len1 = screen_len1
self.len1 = int(size_ary[1])
print("|||Screen_size :%dx%d" % (self.len0, self.len1))
except ValueError:
print("|||Check your pwd about [adb.exe]")
print("|||Enter the correct path by change self.adb_cwd ")
print("|||For example: {Program Files} should be {\"Program Files\"}")
if self.test:
print("|||TEST MODEL")
try:
self.img = plt.imread("screen.png")
except FileNotFoundError:
print("|||screen.png, FileNotFound")
else:
self.screen_shot()
self.img = plt.imread("screen.png")
self.img = self.img[:, :, :-1] * 256
self.img = self.img.astype(np.uint8)
self.imgLeft = self.img[:, -1, :].flatten()
self.dollX, self.dollY = 0, 0
self.platX, self.platY = 0, 0
self.platColor = (0, 0, 0)
if self.show:
print("|||SHOW MODEL")
warnings.filterwarnings("ignore", ".*GUI is implemented.*")
plt.figure(figsize=(5 * 0.8, 5))
plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
self.pltMOD = plt.imshow(self.img)
plt.axis('off')
plt.pause(0.01)
def adb(self, command):
os.system("%s/adb%s" % (self.adb_pwd, command))
def adb_read(self, command):
return os.popen("%s/adb%s" % (self.adb_pwd, command)).read()
def adb_swipe(self, srcX, srcY, dstX, dstY, pressTime):
self.adb_read("shell input swipe%s%s%s%s%s" % (srcX, srcY, dstX, dstY, pressTime))
def screen_shot(self):
self.adb("shell screencap -p sdcard/screen.png")
self.adb_read("pull sdcard/screen.png%s/screen.png" % self.py_cwd)
self.img = plt.imread("screen.png")
self.img = self.img[:, :, :-1] * 256
self.img = self.img.astype(np.uint8)
# check failed
check = self.img[self.len1//4, self.len0//2].astype(np.int)
failed = (255, 0, 0)
if sum(abs(check - failed)) < 4:
print("FAILED || rename [screen.png], and save")
os.rename("screen.png", "screen_%.0f.png" % (time.time()))
def draw_spot(self, i, j, outer=8, inter=2):
# black spot, gray=0
# white spot, gray=255
self.img[i - outer:i + outer, j - outer:j + outer] = 0
self.img[i - inter:i + inter, j - inter:j + inter] = 255
def jump_test(self):
pass
def jump_press(self, ):
if self.show:
self.draw_spot(self.platX, self.platY)
self.draw_spot(self.dollX, self.dollY)
self.pltMOD.set_data(self.img)
plt.pause(0.02)
if self.test:
input("|||Press ENTER to continue")
distance = np.linalg.norm((self.platX - self.dollX, self.platY - self.dollY))
swipe_time = int(distance / self.len0 * 1482)
(srcPX, srcPY, dstPX, dstPY) = rd.randint(178, self.len0, 4)
print("Time || ", swipe_time)
self.adb_swipe(srcPX, srcPY, dstPX, dstPY, swipe_time)
if self.mistake:
time.sleep(rd.randint(1782, 3292)*0.001)
else:
time.sleep(1.5)
def doll_loc(self, accuracy=1):
# the color of the doll
prev = (56, 55, 65)
for i, j in product(
range(int(self.len1*0.3), int(self.len1*0.6), accuracy),
range(int(self.len0*0.1), int(self.len0*0.9), accuracy),
):
ignored_line = -1
pres = self.img[i, j].astype(np.int)
if i != ignored_line and sum(abs(pres - prev)) <= 8:
left_j = j - accuracy
pres = self.img[i, left_j].astype(np.int)
while sum(abs(pres - prev)) > 24:
left_j += 1
pres = self.img[i, left_j].astype(np.int)
riht_j = left_j + 1
pres = self.img[i, riht_j].astype(np.int)
while sum(abs(pres - prev)) < 32:
riht_j += 1
pres = self.img[i, riht_j].astype(np.int)
# rectangle
self.dollX, self.dollY = i + self.len1//10, (left_j + riht_j + 3)//2
print("doll_loc|| ", self.dollX, self.dollY)
if riht_j - left_j > self.len0 // 100:
break
else:
# [ignored_line] value is used
ignored_line = i
print("ignored ||| Outlier Occur, next line")
def plat_loc(self, accuracy=1):
left_ary = self.img[0:self.len1//2, +0].astype(np.int)
riht_ary = self.img[0:self.len1//2, -1].astype(np.int)
for i, j in product(
range(int(self.len1*0.2), int(self.len1*0.5), accuracy),
range(int(self.len0*0.1), int(self.len0*0.9), accuracy),
):
ignored_line = -1
pres = self.img[i, j].astype(np.int)
if ignored_line != i and sum(abs(pres - left_ary[i])) > 16 or sum(abs(pres - left_ary[i])) > 16:
delta = 0
try:
while sum(abs(self.img[i+delta, 0] - self.img[i+delta, j+int(delta*1.725191)])) > 16:
# self.img[i+delta:i+delta+4, j+int(delta*1.725191)] = 0
delta += 1
except IndexError:
delta -= 1
if delta > self.len0 // 30:
# rectangle, normally
self.platX, self.platY = i + delta - self.len0//200, j
# round((1 + (2 ** 0.5 - 1) / 2.0)/2, 3) = 0.604
# round((1 + (2 ** 0.5 - 1) / 2.0)/2 * 1.725191, 3) = 1.041
check_point = self.img[self.platX - int(delta*0.60), self.platY + int(delta*1.04)].astype(np.int)
if sum(abs(check_point - left_ary[i])) > 16 and sum(abs(check_point - left_ary[i])) > 16:
# circle platform, abnormally
print("CIRCLE? ||")
self.platX -= self.len0 // 80
self.platY += self.len0 // 50
# large platform, high fault tolerance
if self.mistake and delta > self.len0 // 15 and rd.randint(4):
print("random ||")
self.platX += rd.randint(-delta//2, +delta//2)
print("plat_loc|| ", i, j)
break
else:
# [ignored_line] value is used
ignored_line = i
print("ignored ||| Outlier Occur, next line")
pass
# jump = Jump(show=False, test=False)
jump = Jump(show=False, test=False, mistake=False)
if False:
jump.jump_test()
jump.doll_loc(accuracy=4)
jump.plat_loc(accuracy=4)
jump.jump_press()
for stepNum in range(512):
jump.screen_shot()
jump.doll_loc(accuracy=2)
jump.plat_loc(accuracy=2)
jump.jump_press()
pass