69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
import requests
|
|
import os.path
|
|
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:
|
|
|
|
def __init__(self):
|
|
self.config = LibConfig()
|
|
|
|
def select(self, problem):
|
|
selectJson = {
|
|
"id": self.config.id,
|
|
"problemName": problem
|
|
}
|
|
response = requests.post(self.config.selectUrl, json=selectJson)
|
|
if not response.ok:
|
|
print(response.text)
|
|
return response.json()["problemName"]
|
|
|
|
def explore(self, plans: List[str]):
|
|
exploreJson = {
|
|
"id": self.config.id,
|
|
"plans": plans
|
|
}
|
|
response = requests.post(self.config.exploreUrl, json=exploreJson)
|
|
return response.json()
|
|
|
|
|
|
def guess(self, rooms: List[str], startingroom, connections):
|
|
guessJson = {
|
|
"id": self.config.id,
|
|
"map":{
|
|
"rooms": rooms,
|
|
"startingRoom": startingroom,
|
|
"connections": connections.connections
|
|
}
|
|
}
|
|
response = requests.post(self.config.guessUrl, json=guessJson)
|
|
return response.json()["correct"] == "true" |