import sys
import re

with open('tools/autonomous_mapping_coordinator.py', encoding='utf-8') as f:
    code = f.read()

new_code = '''        # Nhận diện chữ ENDGAME hoặc WORLD để xác nhận bằng winocr
        is_open = False
        try:
            import winocr
            from PIL import Image
            img = Image.open(img_opened)
            crop = img.crop((int(900*self.scale_x), 0, int(1650*self.scale_x), int(100*self.scale_y)))
            res = winocr.recognize_pil_sync(crop)
            text = " ".join([line['text'] for line in res.get('lines', [])]).upper()
            if "ENDGAME" in text or "WORLD" in text or "MAP" in text or "ACT" in text:
                is_open = True
        except Exception as e:
            self.log(f"OCR Error: {e}")'''

code = re.sub(r'        # Nhận diện chữ ENDGAME.*?except:\n            pass', new_code, code, flags=re.DOTALL)

with open('tools/autonomous_mapping_coordinator.py', 'w', encoding='utf-8') as f:
    f.write(code)
