Files
Litcraft_Python_B/menti_words.py
2023-08-06 16:12:13 +02:00

184 lines
7.1 KiB
Python

import cv2 as cv
import numpy as np
import pydirectinput
from nltk.corpus import words
from pytesseract import pytesseract
import utils
from utils import mse
from game_base_class import GameBase
import random
GREEN = 1
YELLOW = 2
RED = 3
BLUE = 4
ORANGE = 5
class MentiWords(GameBase):
def __init__(self, overlay):
super().__init__(overlay)
pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
self.observation = np.zeros((9, 9), dtype=int)
self.colors = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.offset_left = 1080
self.offset_down = 870
self.menti_play_button = cv.imread("control_elements/play.jpg", cv.IMREAD_COLOR)
self.needles = {
'A': cv.imread("menti_words/a.jpg", cv.IMREAD_COLOR),
'B': cv.imread("menti_words/b.jpg", cv.IMREAD_COLOR),
'C': cv.imread("menti_words/c.jpg", cv.IMREAD_COLOR),
'D': cv.imread("menti_words/d.jpg", cv.IMREAD_COLOR),
'E': cv.imread("menti_words/e.jpg", cv.IMREAD_COLOR),
'F': cv.imread("menti_words/f.jpg", cv.IMREAD_COLOR),
'H': cv.imread("menti_words/h.jpg", cv.IMREAD_COLOR),
'I': cv.imread("menti_words/i.jpg", cv.IMREAD_COLOR),
'L': cv.imread("menti_words/l.jpg", cv.IMREAD_COLOR),
'M': cv.imread("menti_words/m.jpg", cv.IMREAD_COLOR),
'N': cv.imread("menti_words/n.jpg", cv.IMREAD_COLOR),
'O': cv.imread("menti_words/o.jpg", cv.IMREAD_COLOR),
'P': cv.imread("menti_words/p.jpg", cv.IMREAD_COLOR),
'R': cv.imread("menti_words/r.jpg", cv.IMREAD_COLOR),
'S': cv.imread("menti_words/s.jpg", cv.IMREAD_COLOR),
'T': cv.imread("menti_words/t.jpg", cv.IMREAD_COLOR),
'U': cv.imread("menti_words/u.jpg", cv.IMREAD_COLOR),
'W': cv.imread("menti_words/w.jpg", cv.IMREAD_COLOR)
}
self.masks = {
'A': cv.imread("menti_words/a-mask.png", cv.IMREAD_COLOR),
'B': cv.imread("menti_words/b-mask.png", cv.IMREAD_COLOR),
'C': cv.imread("menti_words/c-mask.png", cv.IMREAD_COLOR),
'D': cv.imread("menti_words/d-mask.png", cv.IMREAD_COLOR),
'E': cv.imread("menti_words/e-mask.png", cv.IMREAD_COLOR),
'F': cv.imread("menti_words/f-mask.png", cv.IMREAD_COLOR),
'H': cv.imread("menti_words/h-mask.png", cv.IMREAD_COLOR),
'I': cv.imread("menti_words/i-mask.png", cv.IMREAD_COLOR),
'L': cv.imread("menti_words/l-mask.png", cv.IMREAD_COLOR),
'M': cv.imread("menti_words/m-mask.png", cv.IMREAD_COLOR),
'N': cv.imread("menti_words/n-mask.png", cv.IMREAD_COLOR),
'O': cv.imread("menti_words/o-mask.png", cv.IMREAD_COLOR),
'P': cv.imread("menti_words/p-mask.png", cv.IMREAD_COLOR),
'R': cv.imread("menti_words/r-mask.png", cv.IMREAD_COLOR),
'S': cv.imread("menti_words/s-mask.png", cv.IMREAD_COLOR),
'T': cv.imread("menti_words/t-mask.png", cv.IMREAD_COLOR),
'U': cv.imread("menti_words/u-mask.png", cv.IMREAD_COLOR),
'W': cv.imread("menti_words/w-mask.png", cv.IMREAD_COLOR)
}
self.current_letters = []
self.letter_coords = {}
with open("menti_dic") as file:
self.word_list = [line.rstrip() for line in file]
self.word_list2 = words.words()
def reset_lists(self):
self.current_letters = []
self.letter_coords = {}
def assess_playfield_and_make_move(self):
if self.check_for_button_and_execute(self.capture_window.get_screenshot(), self.menti_play_button):
cv.waitKey(2000)
self.reset_lists()
self.get_current_board_state()
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
return
matches = self.possible_words(self.word_list, self.current_letters)
matches.sort()
new_matches = list(map(list,matches))
for letter_list in new_matches:
self.type_word_into_ui(letter_list)
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
return
def type_word_into_ui(self, letters):
for letter in letters:
pydirectinput.moveTo(self.letter_coords[letter][0] + self.offset_left, self.letter_coords[letter][1] + self.offset_down)
pydirectinput.mouseDown()
w = random.randint(25, 50)
cv.waitKey(100 + w)
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
return
pydirectinput.mouseUp()
cv.waitKey(400)
def get_current_board_state(self):
#screenshot = cv.imread("menti_words/screenshot.jpg")
screenshot = self.capture_window.get_screenshot()
screenshot = utils.scale_screenshot(screenshot[870:1270, 1080:1480], 200, False)
#cv.imshow("screenshot", screenshot)
#cv.waitKey(150)
#continue
for needle_key in self.needles.keys():
# gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY)
# thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
thresh_needle = utils.scale_screenshot(self.needles[needle_key], 200, False)
rectangles = self.vision_stun.find(screenshot, thresh_needle , 0.85, 1)
#rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.95, 1 ,True, self.masks[needle_key])
if len(rectangles) == 0:
continue
points = self.vision_stun.get_click_points(rectangles)
self.current_letters.append(needle_key)
self.letter_coords[needle_key] = points[0]
'''
cropped1 = self.vision_stun.draw_display_picture(screenshot, rectangles, 10)
#cropped1 = utils.scale_screenshot(cropped1)
cv.imshow("cropped1", cropped1)
cv.waitKey(150)
text_1 = pytesseract.image_to_string(cropped1, lang='eng', config='--psm 6').strip()
if str.isalpha(text_1):
#cv.imshow("cropped1", cropped1)
#cv.waitKey(150)
points = self.vision_stun.get_click_points(rectangles)
self.current_letters.append(text_1)
self.letter_coords[text_1] = points[0]'''
if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused':
return
def possible_words(self, lwords, charSet):
lst = []
for word in lwords:
if len(word) <= 2:
continue
flag = 1
chars = self.charCount(word)
for key in chars:
if key not in charSet:
flag = 0
else:
if charSet.count(key) != chars[key]:
flag = 0
if flag == 1:
lst.append(word)
#print(word)
return list(set(lst))
def charCount(self, word):
dict = {}
for i in word:
dict[i] = dict.get(i, 0) + 1
return dict