Files
grimhold/test_user5.py
T

52 lines
1.7 KiB
Python

import sqlite3
import json
import urllib.request
import urllib.error
base_url = "http://127.0.0.1:8004"
def post_json(url, data, token=None):
req = urllib.request.Request(
url,
data=json.dumps(data).encode("utf-8"),
headers={
"Content-Type": "application/json",
**({"Authorization": f"Bearer {token}"} if token else {})
}
)
with urllib.request.urlopen(req) as resp:
return resp.status, json.loads(resp.read().decode("utf-8"))
def get_json(url, token=None):
req = urllib.request.Request(
url,
headers={"Authorization": f"Bearer {token}"} if token else {}
)
with urllib.request.urlopen(req) as resp:
return resp.status, json.loads(resp.read().decode("utf-8"))
# We will create a test user or login as user 5 by setting a password or creating a token
import jwt
import time
SECRET_KEY = "grimhold_dark_fantasy_2026_super_secret_jwt"
token = jwt.encode({"sub": "5", "username": "Amacatan", "exp": time.time() + 86400}, SECRET_KEY, algorithm="HS256")
print("Token for Amacatan:", token)
try:
status, state = get_json(f"{base_url}/api/game/state", token)
print("User:", state["user"])
print("Resources:", state["resources"])
print("Army:", state["army"])
print("Total army pool:", state.get("total_army_pool"))
print("Defense guards:", state.get("defense_guards"))
# Try recruit 1 scout
try:
status, rec_res = post_json(f"{base_url}/api/army/recruit", {"unit_id": "scout", "count": 1}, token)
print("Recruit scout result:", rec_res)
except urllib.error.HTTPError as e:
print("Recruit error HTTP:", e.code, e.read().decode())
except Exception as e:
print("Error:", e)