import json
import os
import time
import requests
import websocket
from colorama import init, Fore, Style
# Инициализация colorama для Windows 7
init(autoreset=True)
# --- ОФОРМЛЕНИЕ ФИШЕК (СИНИЕ И КРАСНЫЕ @) ---
# Игрок 1 (Белые) -> Красная @
RED_PIECE = Fore.RED + Style.BRIGHT + "@" + Style.RESET_ALL
RED_KING = Fore.RED + Style.BRIGHT + "K" + Style.RESET_ALL
# Игрок 2 (Черные) -> Синяя/Голубая @
BLUE_PIECE = Fore.CYAN + Style.BRIGHT + "@" + Style.RESET_ALL
BLUE_KING = Fore.CYAN + Style.BRIGHT + "K" + Style.RESET_ALL
# Пустая игровая клетка
EMPTY = Fore.LIGHTBLACK_EX + "·" + Style.RESET_ALL
def eval_js_in_opera(js_code):
"""Отправляет JavaScript-код в открытую вкладку Opera GX"""
try:
res = requests.get("http://127.0.0.1:9222/json").json()
target_tab = None
for tab in res:
if tab.get("type") == "page" and "checkers" in tab.get("url", ""):
target_tab = tab
break
if not target_tab:
for tab in res:
if tab.get("type") == "page":
target_tab = tab
break
if not target_tab:
return None
ws_url = target_tab["webSocketDebuggerUrl"]
ws = websocket.create_connection(ws_url)
payload = {
"id": 1,
"method": "Runtime.evaluate",
"params": {
"expression": js_code,
"returnByValue": True
}
}
ws.send(json.dumps(payload))
response = json.loads(ws.recv())
ws.close()
return response.get("result", {}).get("result", {}).get("value")
except Exception:
return None
def clear_screen():
"""Очистка консоли Windows"""
os.system('cls')
def render_board(board):
"""Отрисовка доски в консоли"""
clear_screen()
print(" A B C D E F G H")
print(" ┌─────────────────┐")
for r_idx, row in enumerate(board):
row_str = f" {r_idx + 1} │ "
for c_idx, cell in enumerate(row):
if cell == 1:
symbol = RED_PIECE
elif cell == 2:
symbol = BLUE_PIECE
elif cell == 3:
symbol = RED_KING
elif cell == 4:
symbol = BLUE_KING
elif cell == 0:
symbol = EMPTY
else:
symbol = " "
row_str += symbol + " "
row_str += "│"
print(row_str)
print(" └─────────────────┘")
print("\n [+] Отслеживание доски активно...")
# --- ГЛАВНЫЙ ЦИКЛ ---
last_board = None
try:
while True:
board = eval_js_in_opera("checkers.engine.getField();")
if board:
if board != last_board:
render_board(board)
last_board = board
else:
clear_screen()
print("Ожидание подключения к Opera GX...")
print("Убедитесь, что страница с шашками открыта.")
time.sleep(1)
except KeyboardInterrupt:
print("\nСкрипт остановлен.")