Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -21,104 +21,104 @@ def log(acao, status="INFO"):
|
|
| 21 |
class ServidorFalso(BaseHTTPRequestHandler):
|
| 22 |
def do_GET(self):
|
| 23 |
self.send_response(200); self.end_headers()
|
| 24 |
-
self.wfile.write(b"Bot Tribal Wars v2.
|
| 25 |
def log_message(self, format, *args): pass
|
| 26 |
|
| 27 |
def manter_dock_viva():
|
| 28 |
try: HTTPServer(('0.0.0.0', 7860), ServidorFalso).serve_forever()
|
| 29 |
except: pass
|
| 30 |
|
| 31 |
-
# --- FUNÇÃO DE CLIQUE INFALÍVEL ---
|
| 32 |
def force_click(driver, element):
|
| 33 |
-
"""Clica no elemento via JavaScript, ignorando bloqueios visuais"""
|
| 34 |
driver.execute_script("arguments[0].click();", element)
|
| 35 |
|
| 36 |
-
# --- LOGIN RESILIENTE ---
|
| 37 |
def realizar_login(driver):
|
| 38 |
-
log("
|
| 39 |
driver.get("https://www.tribalwars.com.br/")
|
| 40 |
-
time.sleep(
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
driver.type('input#user', USER_TW)
|
| 44 |
driver.type('input#password', PASS_TW)
|
| 45 |
-
time.sleep(1)
|
| 46 |
-
|
| 47 |
-
log("A enviar formulário de login...", "LOGIN")
|
| 48 |
-
# Usa JS para clicar no botão de login
|
| 49 |
login_btn = driver.find_element(By.CSS_SELECTOR, 'a.btn-login')
|
| 50 |
force_click(driver, login_btn)
|
| 51 |
-
time.sleep(
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Procura todos os links e clica no que contém o ID do mundo via JS
|
| 56 |
links = driver.find_elements(By.TAG_NAME, "a")
|
| 57 |
for link in links:
|
| 58 |
-
|
| 59 |
-
if MUNDO_ID in href:
|
| 60 |
force_click(driver, link)
|
| 61 |
-
log(f"
|
| 62 |
time.sleep(5)
|
| 63 |
return True
|
| 64 |
-
|
| 65 |
return False
|
| 66 |
-
|
| 67 |
except Exception as e:
|
| 68 |
-
log(f"Erro no login: {str(e)[:
|
| 69 |
return False
|
| 70 |
|
| 71 |
def auditoria_e_obras(driver):
|
| 72 |
-
#
|
| 73 |
driver.get(f"https://{MUNDO_ID}.tribalwars.com.br/game.php?screen=main")
|
| 74 |
-
time.sleep(
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
driver.execute_script("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
# Ordem de
|
| 86 |
-
prioridade = ["wood", "stone", "iron", "storage", "main"]
|
| 87 |
|
| 88 |
for ed in prioridade:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
btn = driver.find_element(By.CSS_SELECTOR, btn_seletor)
|
| 95 |
force_click(driver, btn)
|
| 96 |
-
log(f"
|
| 97 |
-
time.sleep(3)
|
| 98 |
return
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
-
log("Erro
|
| 101 |
|
| 102 |
def iniciar():
|
| 103 |
display = Display(visible=0, size=(1920, 1080)); display.start()
|
| 104 |
driver = Driver(uc=True, headless=False)
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
driver.get(f"https://{MUNDO_ID}.tribalwars.com.br/game.php?screen=report")
|
| 117 |
-
time.sleep(minutos * 60)
|
| 118 |
-
else:
|
| 119 |
-
log("Tentativa de login falhou. Reiniciando ciclo...", "ALERTA")
|
| 120 |
-
time.sleep(60)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
except Exception as e: log(f"Crash: {e}", "ALERTA")
|
| 123 |
finally:
|
| 124 |
driver.quit(); display.stop()
|
|
|
|
| 21 |
class ServidorFalso(BaseHTTPRequestHandler):
|
| 22 |
def do_GET(self):
|
| 23 |
self.send_response(200); self.end_headers()
|
| 24 |
+
self.wfile.write(b"Bot Tribal Wars v2.4 - Alta Frequencia")
|
| 25 |
def log_message(self, format, *args): pass
|
| 26 |
|
| 27 |
def manter_dock_viva():
|
| 28 |
try: HTTPServer(('0.0.0.0', 7860), ServidorFalso).serve_forever()
|
| 29 |
except: pass
|
| 30 |
|
|
|
|
| 31 |
def force_click(driver, element):
|
|
|
|
| 32 |
driver.execute_script("arguments[0].click();", element)
|
| 33 |
|
|
|
|
| 34 |
def realizar_login(driver):
|
| 35 |
+
log("Acedendo ao servidor br...", "LOGIN")
|
| 36 |
driver.get("https://www.tribalwars.com.br/")
|
| 37 |
+
time.sleep(4)
|
|
|
|
| 38 |
try:
|
| 39 |
driver.type('input#user', USER_TW)
|
| 40 |
driver.type('input#password', PASS_TW)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
login_btn = driver.find_element(By.CSS_SELECTOR, 'a.btn-login')
|
| 42 |
force_click(driver, login_btn)
|
| 43 |
+
time.sleep(8)
|
| 44 |
|
| 45 |
+
# Busca dinâmica pelo mundo
|
|
|
|
|
|
|
| 46 |
links = driver.find_elements(By.TAG_NAME, "a")
|
| 47 |
for link in links:
|
| 48 |
+
if MUNDO_ID in (link.get_attribute("href") or ""):
|
|
|
|
| 49 |
force_click(driver, link)
|
| 50 |
+
log(f"Sessão iniciada no {MUNDO_ID}.", "SUCESSO")
|
| 51 |
time.sleep(5)
|
| 52 |
return True
|
|
|
|
| 53 |
return False
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
+
log(f"Erro no login: {str(e)[:50]}", "ALERTA")
|
| 56 |
return False
|
| 57 |
|
| 58 |
def auditoria_e_obras(driver):
|
| 59 |
+
# Vai para o Edifício Principal
|
| 60 |
driver.get(f"https://{MUNDO_ID}.tribalwars.com.br/game.php?screen=main")
|
| 61 |
+
time.sleep(3)
|
| 62 |
|
| 63 |
+
# 🧹 VASSOURA JS: Fecha TUDO o que for pop-up ou bloqueio
|
| 64 |
+
driver.execute_script("""
|
| 65 |
+
document.querySelectorAll('.popup_box_close, .close-vn-dot, .modal-close').forEach(b => b.click());
|
| 66 |
+
document.querySelectorAll('.greymask, .modal-overlay').forEach(m => m.remove());
|
| 67 |
+
""")
|
| 68 |
+
time.sleep(1)
|
| 69 |
|
| 70 |
try:
|
| 71 |
+
# Leitura de Recursos com Fallback
|
| 72 |
+
mad = driver.execute_script("return document.getElementById('wood').innerText")
|
| 73 |
+
arg = driver.execute_script("return document.getElementById('stone').innerText")
|
| 74 |
+
fer = driver.execute_script("return document.getElementById('iron').innerText")
|
| 75 |
+
log(f"RECURSOS: 🪵 {mad} | 🧱 {arg} | ⚙️ {fer}", "AUDITORIA")
|
| 76 |
+
|
| 77 |
+
# Verifica se a fila está livre (Máximo 2 construções no grátis)
|
| 78 |
+
fila = driver.find_elements(By.CLASS_NAME, "buildorder")
|
| 79 |
+
if len(fila) >= 2:
|
| 80 |
+
log("Fila ocupada (2/2). A aguardar conclusão...", "INFO")
|
| 81 |
+
return
|
| 82 |
|
| 83 |
+
# Prioridades de Upgrade (Ordem lógica de crescimento)
|
| 84 |
+
prioridade = ["wood", "stone", "iron", "storage", "main", "farm"]
|
| 85 |
|
| 86 |
for ed in prioridade:
|
| 87 |
+
btn_css = f"a.btn_build[data-building='{ed}']"
|
| 88 |
+
# Verifica se o botão existe e se não está "desativado" (sem recursos)
|
| 89 |
+
if driver.is_element_visible(btn_css):
|
| 90 |
+
btn = driver.find_element(By.CSS_SELECTOR, btn_css)
|
| 91 |
+
if "btn-disabled" not in btn.get_attribute("class"):
|
|
|
|
| 92 |
force_click(driver, btn)
|
| 93 |
+
log(f"ORDEM ENVIADA: Expandir {ed.upper()}", "CONSTR")
|
|
|
|
| 94 |
return
|
| 95 |
+
log("Recursos insuficientes para novos upgrades.", "INFO")
|
| 96 |
except Exception as e:
|
| 97 |
+
log("Erro ao analisar Edifício Principal. A tentar novamente...", "ALERTA")
|
| 98 |
|
| 99 |
def iniciar():
|
| 100 |
display = Display(visible=0, size=(1920, 1080)); display.start()
|
| 101 |
driver = Driver(uc=True, headless=False)
|
| 102 |
|
| 103 |
try:
|
| 104 |
+
if realizar_login(driver):
|
| 105 |
+
while True:
|
| 106 |
+
auditoria_e_obras(driver)
|
| 107 |
+
|
| 108 |
+
# Tempo de ciclo de 30 segundos (conforme solicitado)
|
| 109 |
+
# Adicionamos um pequeno random (28-35) para não ser um robô óbvio
|
| 110 |
+
pausa = random.randint(28, 35)
|
| 111 |
+
log(f"Ciclo concluído. Próxima verificação em {pausa}s...", "INFO")
|
| 112 |
+
time.sleep(pausa)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# Se deslogar por inatividade, volta ao início
|
| 115 |
+
if "login" in driver.current_url.lower():
|
| 116 |
+
log("Sessão expirada. A relogar...", "ALERTA")
|
| 117 |
+
realizar_login(driver)
|
| 118 |
+
else:
|
| 119 |
+
log("Falha crítica no login. A reiniciar em 60s...", "ALERTA")
|
| 120 |
+
time.sleep(60)
|
| 121 |
+
|
| 122 |
except Exception as e: log(f"Crash: {e}", "ALERTA")
|
| 123 |
finally:
|
| 124 |
driver.quit(); display.stop()
|