icfp25/michael/icfprequests.py

70 lines
2.0 KiB
Python

import requests
import os.path
from argparse import Namespace
from typing import List
class APIError(Exception):
pass
class LibConfig:
endpointUrl = "https://31pwr5t6ij.execute-api.eu-west-2.amazonaws.com/"
selectUrl = endpointUrl + "select"
exploreUrl = endpointUrl + "explore"
guessUrl = endpointUrl + "guess"
def __init__(self):
config = {}
with open(os.path.join(os.path.dirname(__file__), "..", "config")) as h:
for line in h.readlines():
parts = line.strip().split("=")
self.__setattr__(parts[0].lower(), parts[1].replace('"', ""))
class Connections:
def __init__(self):
self.connections = []
def add_connection(self, from_room, from_door, to_room, to_door):
connection = { "from":{"room": from_room, "door": from_door},
"to":{"room": to_room, "door": to_door}}
self.connections.append(connection)
class LibRequests:
config ={}
def __init__(self):
self.config = LibConfig()
def select(self, problem, id=config.id):
selectJson = {
"id": id,
"problemName": problem
}
response = requests.post(self.config.selectUrl, json=selectJson)
return response.json()["problemName"]
def explore(self, plans: List[str], id=config.id):
exploreJson = {
"id": id,
"plans": plans
}
response = requests.post(self.config.exploreUrl, json=exploreJson)
return response.json()
def guess(self, rooms: List[str], startingroom, connections , id=config.id ):
guessJson = {
"id": id,
"map":{
"rooms": rooms,
"startingRoom": startingroom,
"connections": connections.connections
}
}
response = requests.post(self.config.guessUrl, json=guessJson)
return response.json()["correct"] == "true"