import asyncio
import json
import math
import random
import re
import ctypes
from playwright.async_api import async_playwright

# --- Ẩn cửa sổ CMD ngay khi khởi động ---
def hide_console():
try:
hwnd = ctypes.windll.kernel32.GetConsoleWindow()
if hwnd:
ctypes.windll.user32.ShowWindow(hwnd, 0) # 0 = SW_HIDE
except Exception:
pass

hide_console()

# ================== CẤU HÌNH ==================
USERNAME = "hoangchan2"
PASSWORD = "abc13579"
LOGIN_URL = "https://1900.fun/login.php"
THAM_HIEM_URL = "https://1900.fun/city/thamhiem/"
INTERVAL_SECONDS = 24 * 60 * 60 # 24 giờ
# ==============================================


def clean_and_parse_json(raw_text: str) -> dict:
"""Trích xuất JSON sạch từ response có thể lẫn HTML lỗi PHP."""
try:
match = re.search(r"\{.*\}", raw_text, re.DOTALL)
if match:
return json.loads(match.group(0))
return {"success": False, "error": f"Không tìm thấy JSON: {raw_text[:150]}"}
except Exception as e:
return {"success": False, "error": f"Lỗi parse JSON: {e}"}


async def fetch_ajax(page, body: str) -> dict:
"""Gọi AJAX và parse an toàn."""
raw = await page.evaluate(
f"""async () => {{
try {{
const res = await fetch('ajax_dungeon.php', {{
method: 'POST',
headers: {{ 'Content-Type': 'application/x-www-form-urlencoded' }},
body: '{body}'
}});
return await res.text();
}} catch (e) {{
return e.toString();
}}
}}"""
)
return clean_and_parse_json(raw)


async def run_boss_bot():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True) # đổi False nếu muốn xem
context = await browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
page = await context.new_page()

try:
# 1. Đăng nhập
print(f"[*] Đăng nhập: {USERNAME}")
await page.goto(LOGIN_URL, wait_until="domcontentloaded")
await page.fill("input[name='username']", USERNAME)
await page.fill("input[name='password']", PASSWORD)
async with page.expect_navigation(wait_until="domcontentloaded"):
await page.click("button[type='submit']")
await page.wait_for_timeout(1200)

# 2. Vào trang Thám hiểm
print("[*] Vào trang Thám hiểm...")
await page.goto(THAM_HIEM_URL, wait_until="domcontentloaded")
await page.wait_for_timeout(1500)

# 3. Lấy dữ liệu cửa ải hiện tại
init_data = await fetch_ajax(page, "action=get_current_floor")
if not init_data.get("success"):
print(f"[!] Lỗi khởi tạo: {init_data.get('error')}")
return

current_floor = init_data.get("floor", 1)
monster = init_data.get("monster", {})
monster_hp = monster.get("hp", 0)
monster_strength = monster.get("strength", 0)
monster_name = monster.get("name", "Boss")
player_hp = init_data.get("player_hp", 0)
player_strength = init_data.get("player_strength", 0)

# 4. Vòng lặp leo ải
while True:
print(f"\n========== [ CỬA ẢI {current_floor} ] ==========")
print(f"👤 Bạn | HP: {player_hp:,} | SM: {player_strength}")
print(f"👾 {monster_name} | HP: {monster_hp:,} | SM: {monster_strength}")

# Mô phỏng trận đấu client-side (giống JS gốc)
curr_p_hp = player_hp
curr_m_hp = monster_hp
is_my_turn = True

while curr_p_hp > 0 and curr_m_hp > 0:
if is_my_turn:
dmg = max(5, math.floor(random.random() * player_strength) + 8)
curr_m_hp = max(0, curr_m_hp - dmg)
is_my_turn = False
else:
dmg = max(5, math.floor(random.random() * monster_strength) + 8)
curr_p_hp = max(0, curr_p_hp - dmg)
is_my_turn = True

is_win = curr_m_hp <= 0
print(f"[⚔️] Kết quả: {'CHIẾN THẮNG' if is_win else 'THẤT BẠI'}")

# Gửi kết quả lên server
result = "win" if is_win else "lose"
battle_res = await fetch_ajax(
page,
f"action=end_battle&result={result}&floor={current_floor}"
)

if not battle_res.get("success"):
print(f"[!] Server từ chối: {battle_res.get('error')}")
break

print(f" └─ {battle_res.get('message', 'OK')}")
if battle_res.get("drop_item"):
item = battle_res["drop_item"]
print(f" └─ 🎁 {item.get('name')} (+{item.get('enhance_level', 0)})")
if battle_res.get("new_xu"):
print(f" └─ 🪙 Xu mới: {battle_res['new_xu']:,}")

if not is_win:
print("\n[⛔] Thua → về cửa ải 1. Kết thúc lượt hôm nay.")
break

# Chờ nhẹ rồi lấy cửa ải tiếp
await asyncio.sleep(random.uniform(1.2, 2.0))
next_data = await fetch_ajax(page, "action=get_current_floor")
if not next_data.get("success"):
print("[!] Không lấy được cửa ải tiếp theo.")
break

current_floor = next_data.get("floor", current_floor + 1)
monster = next_data.get("monster", {})
monster_hp = monster.get("hp", 0)
monster_strength = monster.get("strength", 0)
monster_name = monster.get("name", "Boss")
player_hp = next_data.get("player_hp", player_hp)
player_strength = next_data.get("player_strength", player_strength)

except Exception as e:
print(f"[!] Lỗi: {e}")
finally:
await browser.close()


async def main():
while True:
print("\n" + "=" * 60)
print(" BẮT ĐẦU CHUYẾN THÁM HIỂM HẰNG NGÀY")
print("=" * 60)
try:
await run_boss_bot()
except Exception as e:
print(f"[!] Lỗi ngoài: {e}")

print(f"\n[⏳] Chờ {INTERVAL_SECONDS // 3600} giờ trước lượt tiếp theo...")
await asyncio.sleep(INTERVAL_SECONDS)


if __name__ == "__main__":
asyncio.run(main())