Skip to content

Commit

Permalink
The translation function for several languages is added within Settin…
Browse files Browse the repository at this point in the history
…g (place only the first 5, any can be added within Google Translate)

NN recognition of table cards is added, the cloth must be mapped to avoid false positives
  • Loading branch information
v3rchi3l committed Feb 23, 2024
1 parent 7228415 commit a474a3c
Show file tree
Hide file tree
Showing 7 changed files with 2,479 additions and 2,395 deletions.
92 changes: 84 additions & 8 deletions poker/gui/action_and_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import logging



# pylint: disable=unnecessary-lambda

class UIActionAndSignals(QObject): # pylint: disable=undefined-variable
Expand Down Expand Up @@ -377,6 +376,9 @@ def open_table_setup(self):
def open_setup(self):
self.ui_setup = SetupForm()
self.ui_setup.pushButton_save.clicked.connect(lambda: self.save_setup())
self.ui_setup.pushButton_Translates.clicked.connect(lambda: self.translate())


vm_list = ['Direct mouse control']
try:
vm = VirtualBoxController()
Expand All @@ -387,23 +389,34 @@ def open_setup(self):
self.ui_setup.comboBox_vm.addItems(vm_list)
timeouts = ['8', '9', '10', '11', '12']
self.ui_setup.comboBox_2.addItems(timeouts)
translates_list = ['en', 'es', 'hi', 'fr', 'zh-TW']
self.ui_setup.comboBox_Translates.addItems(translates_list)

config = get_config()
try:
mouse_control = config.config.get('main', 'control')
except:
mouse_control = 'Direct mouse control'
for i in [i for i, x in enumerate(vm_list) if x == mouse_control]:
idx = i
self.ui_setup.comboBox_vm.setCurrentIndex(idx)
for i, x in enumerate(vm_list):
if x == mouse_control:
self.ui_setup.comboBox_vm.setCurrentIndex(i)

try:
timeout = config.config.get('main', 'montecarlo_timeout')
except:
timeout = 10
for i in [i for i, x in enumerate(timeouts) if x == timeout]:
idx = i
self.ui_setup.comboBox_2.setCurrentIndex(idx)
timeout = '10'
for i, x in enumerate(timeouts):
if x == timeout:
self.ui_setup.comboBox_2.setCurrentIndex(i)

try:
translate_config = config.config.get('main', 'Translates')
except:
translate_config = 'en'
for i, x in enumerate(translates_list):
if x == translate_config:
self.ui_setup.comboBox_Translates.setCurrentIndex(i)


login = config.config.get('main', 'login')
password = config.config.get('main', 'password')
Expand All @@ -412,12 +425,75 @@ def open_setup(self):
self.ui_setup.login.setText(login)
self.ui_setup.password.setText(password)

def translate(self):
import xml.etree.ElementTree as ET
from googletrans import Translator
from tqdm import tqdm
import os

def translate_text(self, text, source_language='en', target_language=None):
exceptions = {'2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC', 'AC',
'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS', 'AS',
'2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH', 'AH',
'2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD', 'AD'}

if target_language is None:
selected_translate = self.ui_setup.comboBox_Translates.currentText()
target_language = selected_translate
if text in exceptions:
return text
translator = Translator()
translated_text = translator.translate(text, dest=target_language)
translated_text = translated_text.text
# Realizar los reemplazos específicos
translated_text = translated_text.replace("<html> <Head/> <Body> <P> ", "<html><Head/><Body><P>")
translated_text = translated_text.replace(". </p> </body> </html>", "</p></body></html>")
return translated_text

def translate_ui_files(ui_directory='gui/ui'):
ui_files = [os.path.join(ui_directory, filename) for filename in os.listdir(ui_directory) if
filename.endswith('.ui')]
processed_files = []
with tqdm(total=len(ui_files), desc="Processing files") as pbar:
for ui_file in ui_files:
processed_files.extend(translate_ui_file(self, ui_file))
pbar.update(1)
print('Please reboot for the changes to take effect.')
return processed_files

def translate_ui_file(self, file_path):
tree = ET.parse(file_path)
root = tree.getroot()

processed_files = []

for string_tag in root.iter('string'):
try:
if string_tag.text is not None and isinstance(string_tag.text, str):
cleaned_text = string_tag.text.strip().replace('&lt;', '<').replace('&gt;', '>')
translated_text = translate_text(self, cleaned_text)
string_tag.text = translated_text
except Exception as e:
print("\n")
print(f"File error {file_path}: {e}")
print(f"Problematic line: {string_tag.text}")

tree.write(file_path, encoding='utf-8', xml_declaration=True)
processed_files.append(file_path)

return processed_files

# Llamar a la función interna translate_ui_files

return translate_ui_files()

def save_setup(self):
config = get_config()
config.config.set('main', 'control', self.ui_setup.comboBox_vm.currentText())
config.config.set('main', 'montecarlo_timeout', self.ui_setup.comboBox_2.currentText())
config.config.set('main', 'login', self.ui_setup.login.text())
config.config.set('main', 'password', self.ui_setup.password.text())
config.config.set('main', 'Translate', self.ui_setup.Translates.currentText())
config.update_file()
self.ui_setup.close()

Expand Down
151 changes: 88 additions & 63 deletions poker/gui/ui/setup_form.ui
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version='1.0' encoding='utf-8'?>
<ui version="4.0">
<class>setup_form</class>
<widget class="QWidget" name="setup_form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>263</width>
<height>326</height>
<width>249</width>
<height>292</height>
</rect>
</property>
<property name="sizeIncrement">
Expand All @@ -23,7 +23,7 @@
<string>Setup</string>
</property>
<property name="toolTip">
<string/>
<string />
</property>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
Expand All @@ -41,24 +41,57 @@
</size>
</property>
</widget>
<widget class="QWidget" name="verticalLayoutWidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<width>201</width>
<height>257</height>
<x>10</x>
<y>10</y>
<width>228</width>
<height>272</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Direct Mouse control and Virtual Machines</string>
<string>Timeout</string>
</property>
</widget>
</item>
<item>
<item row="9" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLineEdit" name="login" />
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QComboBox" name="comboBox_vm">
<property name="toolTip">
<string notr="true">&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Choose if you want to have direct access to VirtualBox via API or whether the bot should simply move the mouse.&lt;/p&gt;&lt;p&gt;The dropdown will automatically show all available virtualbox virtualmachines.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
Expand All @@ -70,73 +103,65 @@
<string>wahtst this</string>
</property>
<property name="accessibleName">
<string/>
<string />
</property>
<property name="accessibleDescription">
<string/>
<string />
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Timeout</string>
<string>Direct Mouse control and Virtual Machines</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_2">
<property name="toolTip">
<string notr="true">Shows after how many seconds of discovering the buttons a timeout will be triggered, assuming you have reached the montecarlo simulation by then. </string>
</property>
</widget>
<item row="7" column="0">
<widget class="QLineEdit" name="password" />
</item>
<item>
<widget class="QLabel" name="label_3">
<item row="10" column="0">
<widget class="QPushButton" name="pushButton_save">
<property name="text">
<string>Login</string>
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="login"/>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Password</string>
<item row="3" column="0">
<widget class="QComboBox" name="comboBox_2">
<property name="toolTip">
<string notr="true">Shows after how many seconds of discovering the buttons a timeout will be triggered, assuming you have reached the montecarlo simulation by then. </string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="password"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_save">
<property name="text">
<string>Save</string>
</property>
</widget>
<item row="8" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Translate</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_Translates">
<property name="toolTip">
<string notr="true">Shows after how many seconds of discovering the buttons a timeout will be triggered, assuming you have reached the montecarlo simulation by then. </string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_Translates">
<property name="text">
<string>Translate</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
<resources />
<connections />
</ui>
Loading

0 comments on commit a474a3c

Please sign in to comment.