Files
DirtyLeague_Bot/test_chest_flow.py

143 lines
4.8 KiB
Python

"""
Verification script for complete chest opening flow:
1. Check which chests are ready (OPEN).
2. Open the first ready chest.
3. Handle CHEST_OPEN_SCREEN (Turn all cards over -> Collect).
4. Return to TOWER_LOBBY and re-scan slots.
"""
import json
import logging
import sys
import time
from bot_core import DirtyLeagueBot, GameState
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)]
)
def run_chest_flow_test():
with open("config.json", "r", encoding="utf-8") as f:
config = json.load(f)
bot = DirtyLeagueBot(config)
logging.info("--- Starting Chest Flow Test ---")
# Step 1: Ensure we are in TOWER_LOBBY
frame = bot.wm.capture_frame(client_only=True)
state = bot.detect_state(frame)
logging.info(f"Current State: {state.value}")
if state != GameState.TOWER_LOBBY:
logging.error(f"Expected TOWER_LOBBY, but detected {state.value}. Aborting test.")
return False
# Step 2: Scan chests
chests = bot.scan_bottom_chests(frame)
logging.info("Initial Chest Slots status:")
for slot_id, info in chests.items():
logging.info(f" Slot #{slot_id}: {info['status']}")
# Find first OPEN chest
target_slot = None
for slot_id, info in sorted(chests.items()):
if info["status"] == "OPEN":
target_slot = slot_id
break
if not target_slot:
logging.warning("No chests with status 'OPEN' found to test.")
return False
logging.info(f"Targeting first ready chest: Slot #{target_slot} at {chests[target_slot]['click_pos']}")
# Step 3: Click the target chest slot
bot.click_client_pos(*chests[target_slot]["click_pos"])
logging.info("Clicked chest slot. Waiting for CHEST_OPEN_SCREEN...")
# Step 4: Wait for CHEST_OPEN_SCREEN
opened = False
start_time = time.time()
while time.time() - start_time < 15.0:
time.sleep(0.5)
current_frame = bot.wm.capture_frame(client_only=True)
current_state = bot.detect_state(current_frame)
if current_state == GameState.CHEST_OPEN_SCREEN:
logging.info(f"Entered CHEST_OPEN_SCREEN after {time.time() - start_time:.1f}s.")
opened = True
break
if not opened:
logging.error("Failed to detect CHEST_OPEN_SCREEN within timeout.")
return False
# Step 5: Handle Turn all cards over
logging.info("Looking for 'Turn all cards over' button...")
turn_clicked = False
sub_start = time.time()
while time.time() - sub_start < 10.0:
current_frame = bot.wm.capture_frame(client_only=True)
if bot.click_template(current_frame, "btn_turn_all_cards"):
logging.info("Clicked 'Turn all cards over' successfully!")
turn_clicked = True
break
elif bot.find_template(current_frame, "btn_collect"):
# Cards might already be turned
turn_clicked = True
break
time.sleep(0.5)
# Step 6: Wait for Collect button and click it
logging.info("Waiting for 'Collect' button to appear...")
collect_clicked = False
collect_start = time.time()
while time.time() - collect_start < 12.0:
current_frame = bot.wm.capture_frame(client_only=True)
if bot.click_template(current_frame, "btn_collect"):
logging.info("Clicked 'Collect' successfully! Rewards gathered.")
collect_clicked = True
break
time.sleep(0.5)
if not collect_clicked:
logging.error("Failed to click 'Collect' button.")
return False
# Step 7: Dismiss offer popup if any
time.sleep(1.0)
after_frame = bot.wm.capture_frame(client_only=True)
bot.check_and_dismiss_offer_popup(after_frame)
# Step 8: Wait to return to TOWER_LOBBY and re-scan
logging.info("Waiting to return to TOWER_LOBBY...")
returned = False
ret_start = time.time()
while time.time() - ret_start < 10.0:
time.sleep(0.5)
lobby_frame = bot.wm.capture_frame(client_only=True)
lobby_state = bot.detect_state(lobby_frame)
if lobby_state == GameState.TOWER_LOBBY:
logging.info("Successfully returned to TOWER_LOBBY!")
returned = True
break
if not returned:
logging.warning("Did not detect TOWER_LOBBY yet, capturing current state...")
lobby_frame = bot.wm.capture_frame(client_only=True)
# Re-scan chests to verify the opened slot changed
final_chests = bot.scan_bottom_chests(lobby_frame)
logging.info("Final Chest Slots status after test:")
for slot_id, info in final_chests.items():
logging.info(f" Slot #{slot_id}: {info['status']}")
logging.info("--- Chest Flow Test COMPLETED SUCCESSFULLY! ---")
return True
if __name__ == "__main__":
run_chest_flow_test()