Esempio n. 1
0
    def recognize(self):
        get_screenshot()
        Image.open(SCREENSHOT).crop(TROPHY_BOX).convert('L').\
            point(lambda xxx: 0 if xxx < 248 else 1, '1').save(TROPHY_SCREENSHOT)
        img = cv2.imread(TROPHY_SCREENSHOT, -1)

        edge = list()
        out_edge = True
        for index, column in enumerate(zip(*img)):
            if out_edge:
                if sum(column):
                    out_edge = False
                    edge.append(index)
            else:
                if not sum(column):
                    out_edge = True
                    edge.append(index)
        numbers = list()
        if len(edge) > 1:
            for number_edge in [edge[x:x + 2] for x in range(0, len(edge), 2)]:
                numbers.append(img[:, number_edge[0]:number_edge[1]])

        # for index, number in enumerate(numbers):
        #     cv2.imwrite('number{}.png'.format(index), number)
        # print(len(numbers))

        ans = '0'
        for number in numbers:
            diffs = list()
            for i in range(10):
                diff = image_minus(number, self.numbers_data[i])
                # print(diff)
                diffs.append(diff)
            ans += str(diffs.index(min(diffs)))
        return int(ans)
Esempio n. 2
0
def do_missions(mission_tuple, base_coordinate=None):
    global controller
    timer = time.clock()
    while True:
        try:
            if time.clock() - timer > 300:
                restart_bluestacks()
            if mission_tuple is not MISSION_EXCEPTION:
                controller = Controller(get_screenshot())
                do_missions(MISSION_EXCEPTION)
            for m in mission_tuple:
                if mission(m, base_coordinate):
                    controller = Controller(get_screenshot())
        except BreakMissions:
            break
        except LoopDone:
            timer = time.clock()
            while True:
                controller = Controller(get_screenshot())
                if get_coordinates(SHOP_BUTTON):
                    break
                if time.clock() - timer > 20:
                    break
            trophies = trophy_recognizer.recognize()
            print('{} trophies'.format(trophies))
            if 0 < trophies < TROPHY_TARGET:
                break
        except GameException as game_exception:
            if game_exception.args[0] == EXCEPTION_FOR_EXCEPTION_LOOP:
                raise GameException(EXCEPTION_FOR_MAIN_LOOP)
            else:
                do_missions(MISSION_START_GAME)
                do_missions(mission_tuple, base_coordinate)
                break
def do_missions(mission_tuple, base_coordinate=None):
    global controller
    timer = time.clock()
    while True:
        try:
            if time.clock() - timer > 300:
                restart_bluestacks()
            if mission_tuple is not MISSION_EXCEPTION:
                controller = Controller(get_screenshot())
                do_missions(MISSION_EXCEPTION)
            for m in mission_tuple:
                if mission(m, base_coordinate):
                    controller = Controller(get_screenshot())
        except BreakMissions:
            break
        except LoopDone:
            timer = time.clock()
            while True:
                controller = Controller(get_screenshot())
                if get_coordinates(SHOP_BUTTON):
                    break
                if time.clock() - timer > 20:
                    break
            trophies = trophy_recognizer.recognize()
            print('{} trophies'.format(trophies))
            if 0 < trophies < TROPHY_TARGET:
                break
        except GameException as game_exception:
            if game_exception.args[0] == EXCEPTION_FOR_EXCEPTION_LOOP:
                raise GameException(EXCEPTION_FOR_MAIN_LOOP)
            else:
                do_missions(MISSION_START_GAME)
                do_missions(mission_tuple, base_coordinate)
                break
Esempio n. 4
0
def restart_bluestacks():
    global controller
    while get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        click_points_by_name(BLUESTACKS_EXIT_BUTTON)
    time.sleep(1)
    while not get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        if get_coordinates(START_BLUESTACKS):
            controller.click(get_coordinates(START_BLUESTACKS)[0], click_times=2)
        time.sleep(10)
    raise GameException(EXCEPTION_FOR_MAIN_LOOP)
def restart_bluestacks():
    global controller
    while get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        click_points_by_name(BLUESTACKS_EXIT_BUTTON)
    time.sleep(1)
    while not get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        if get_coordinates(START_BLUESTACKS):
            controller.click(get_coordinates(START_BLUESTACKS)[0],
                             click_times=2)
        time.sleep(10)
    raise GameException(EXCEPTION_FOR_MAIN_LOOP)
Esempio n. 6
0
def produce_army():
    global controller
    while True:
        controller = Controller(get_screenshot())
        barracks_coordinates = get_coordinates(BARRACKS_LV10)
        if len(barracks_coordinates) >= BARRACKS_NUMBER:
            break
    for coordinate in barracks_coordinates[:4]:
        do_missions(MISSION_PRODUCE_ARMY, [coordinate])
        controller.click_blank()
def produce_army():
    global controller
    while True:
        controller = Controller(get_screenshot())
        barracks_coordinates = get_coordinates(BARRACKS_LV10)
        if len(barracks_coordinates) >= BARRACKS_NUMBER:
            break
    for coordinate in barracks_coordinates[:4]:
        do_missions(MISSION_PRODUCE_ARMY, [coordinate])
        controller.click_blank()
Esempio n. 8
0
    def recognize(self):
        get_screenshot()
        Image.open(SCREENSHOT).crop(TROPHY_BOX).convert("L").point(lambda xxx: 0 if xxx < 248 else 1, "1").save(
            TROPHY_SCREENSHOT
        )
        img = cv2.imread(TROPHY_SCREENSHOT, -1)

        edge = list()
        out_edge = True
        for index, column in enumerate(zip(*img)):
            if out_edge:
                if sum(column):
                    out_edge = False
                    edge.append(index)
            else:
                if not sum(column):
                    out_edge = True
                    edge.append(index)
        numbers = list()
        if len(edge) > 1:
            for number_edge in [edge[x : x + 2] for x in range(0, len(edge), 2)]:
                numbers.append(img[:, number_edge[0] : number_edge[1]])

        # for index, number in enumerate(numbers):
        #     cv2.imwrite('number{}.png'.format(index), number)
        # print(len(numbers))

        ans = "0"
        for number in numbers:
            diffs = list()
            for i in range(10):
                diff = image_minus(number, self.numbers_data[i])
                # print(diff)
                diffs.append(diff)
            ans += str(diffs.index(min(diffs)))
        return int(ans)
Esempio n. 9
0
__author__ = 'CRVV'

import time

from src.control import Controller
from src.image_process import get_coordinates
from src.image_process import get_screenshot
from src.trophy import TrophyRecognizer
from src.resource import *


controller = Controller(get_screenshot())
trophy_recognizer = TrophyRecognizer()


def do_missions(mission_tuple, base_coordinate=None):
    global controller
    timer = time.clock()
    while True:
        try:
            if time.clock() - timer > 300:
                restart_bluestacks()
            if mission_tuple is not MISSION_EXCEPTION:
                controller = Controller(get_screenshot())
                do_missions(MISSION_EXCEPTION)
            for m in mission_tuple:
                if mission(m, base_coordinate):
                    controller = Controller(get_screenshot())
        except BreakMissions:
            break
        except LoopDone:
Esempio n. 10
0
__author__ = 'CRVV'

import time

from src.control import Controller
from src.image_process import get_coordinates
from src.image_process import get_screenshot
from src.trophy import TrophyRecognizer
from src.resource import *

controller = Controller(get_screenshot())
trophy_recognizer = TrophyRecognizer()


def do_missions(mission_tuple, base_coordinate=None):
    global controller
    timer = time.clock()
    while True:
        try:
            if time.clock() - timer > 300:
                restart_bluestacks()
            if mission_tuple is not MISSION_EXCEPTION:
                controller = Controller(get_screenshot())
                do_missions(MISSION_EXCEPTION)
            for m in mission_tuple:
                if mission(m, base_coordinate):
                    controller = Controller(get_screenshot())
        except BreakMissions:
            break
        except LoopDone:
            timer = time.clock()