Spaces:
Running
Running
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,60 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import yaml
|
| 5 |
|
| 6 |
+
# --- BACKGROUND MAIA TARGETER FOR HUGGING FACE ---
|
| 7 |
+
def maia_targeter_loop():
|
| 8 |
+
print("[Background Matchmaker] Waiting 20 seconds for the main bot to initialize...")
|
| 9 |
+
time.sleep(20)
|
| 10 |
+
|
| 11 |
+
# 1. Try to read the token from config.yml
|
| 12 |
+
try:
|
| 13 |
+
with open("config.yml", "r") as f:
|
| 14 |
+
config = yaml.safe_load(f)
|
| 15 |
+
# Handle Hugging Face environment variables if referenced via $
|
| 16 |
+
token = config.get("token", "")
|
| 17 |
+
if token.startswith("$"):
|
| 18 |
+
import os
|
| 19 |
+
token = os.environ.get(token.replace("$", ""), "")
|
| 20 |
+
except Exception:
|
| 21 |
+
token = ""
|
| 22 |
|
| 23 |
+
if not token:
|
| 24 |
+
print("[Background Matchmaker] Error: Could not resolve Lichess Token. Loop stopped.")
|
| 25 |
+
return
|
| 26 |
|
| 27 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 28 |
+
target_bots = ["maia1", "maia5", "maia9"]
|
| 29 |
+
challenge_data = {
|
| 30 |
+
"rated": "false",
|
| 31 |
+
"clock.limit": 180,
|
| 32 |
+
"clock.increment": 0,
|
| 33 |
+
"variant": "standard",
|
| 34 |
+
"keepAliveStream": "false"
|
| 35 |
+
}
|
| 36 |
|
| 37 |
+
print("[Background Matchmaker] Loop successfully activated!")
|
| 38 |
+
while True:
|
| 39 |
+
try:
|
| 40 |
+
# Check if our bot is busy playing a game
|
| 41 |
+
status_resp = requests.get("https://lichess.org/api/account", headers=headers)
|
| 42 |
+
if status_resp.status_code == 200 and not status_resp.json().get("playing", False):
|
| 43 |
+
# Bot is free! Try challenging the Maias
|
| 44 |
+
for bot in target_bots:
|
| 45 |
+
url = f"https://challenge.lichess.org/api/challenge/{bot}"
|
| 46 |
+
resp = requests.post(url, headers=headers, data=challenge_data)
|
| 47 |
+
if resp.status_code == 200:
|
| 48 |
+
print(f"[Background Matchmaker] Successfully challenged {bot}!")
|
| 49 |
+
time.sleep(90) # Hold while match is accepted
|
| 50 |
+
break
|
| 51 |
+
time.sleep(4) # Slight delay between target checks
|
| 52 |
+
|
| 53 |
+
time.sleep(45) # Sleep before checking status again
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"[Background Matchmaker] Loop encountered an error: {e}")
|
| 56 |
+
time.sleep(30)
|
| 57 |
|
| 58 |
+
# Start the background matchmaking thread immediately upon script startup
|
| 59 |
+
threading.Thread(target=maia_targeter_loop, daemon=True).start()
|
| 60 |
+
# --------------------------------------------------
|