from typing import Dict, Tuple, List import requests import json import os from compLib.LogstashLogging import Logging API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/" API_URL_GET_POS = API_URL + "getPos" API_URL_GET_OP = API_URL + "getOp" API_URL_GET_GOAL = API_URL + "getGoal" API_URL_GET_ITEMS = API_URL + "getItems" API_URL_GET_PARK = API_URL + "getPark" API_URL_PAY_PARK = API_URL + "payPark" API_URL_SIMON_SAYS = API_URL + "simonSays" API_URL_GET_SCORES = API_URL + "getScores" class Seeding: """Class used for communicating with seeding api """ @staticmethod def get_park() -> Tuple[Dict, int]: """Get a parkingsapce from the api. :return: Json Object and status code as returned by the api. :rtype: Tuple[Dict, int] """ res = requests.get(API_URL_GET_PARK) result = json.loads(res.content) Logging.get_logger().debug(f"Seeding.get_park = {result}, status code = {res.status_code}") return result, res.status_code @staticmethod def pay_park() -> int: """Pay for parking. :return: Status code as returned by the api. :rtype: int """ result = requests.get(API_URL_PAY_PARK).status_code Logging.get_logger().debug(f"Seeding.pay_park = {result}") return result @staticmethod def simon_says() -> Tuple[Dict, int]: """Get next simon says zone from the api. :return: Json Object and status code as returned by the api. :rtype: Tuple[Dict, int] """ res = requests.get(API_URL_SIMON_SAYS) result = json.loads(res.content) Logging.get_logger().debug(f"Seeding.simon_says = {result}, status code = {res.status_code}") return result, res.status_code class Position: """Datastructure for holding a position """ def __init__(self, x, y, degrees): self.x = x self.y = y self.degrees = degrees def __repr__(self): return f"Position(x={self.x}, y={self.y}, degrees={self.degrees})" def __str__(self): return f"Position(x={round(self.x, 5)}, y={round(self.y, 5)}, degrees={round(self.degrees, 5)})" class DoubleElim: """Class used for communicating with double elimination api """ @staticmethod def get_position() -> Tuple[Position, int]: """Get position of the robot :return: A Position object with robot position :rtype: Tuple[Position, int] """ res = requests.get(API_URL_GET_POS) response = json.loads(res.content) Logging.get_logger().debug(f"DoubleElim.get_position = {response}, status code = {res.status_code}") return Position(response["x"], response["y"], response["degrees"]), res.status_code @staticmethod def get_opponent() -> Tuple[Position, int]: """Get position of the opponents robot :return: A Position object with opponents robot position :rtype: Tuple[Position, int] """ res = requests.get(API_URL_GET_OP) response = json.loads(res.content) Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}, status code = {res.status_code}") return Position(response["x"], response["y"], response["degrees"]), res.status_code @staticmethod def get_goal() -> Tuple[Position, int]: """Get position of the goal :return: A Position object with x and y coordinates of the goal, rotation is always -1 :rtype: Tuple[Position, int] """ res = requests.get(API_URL_GET_GOAL) response = json.loads(res.content) Logging.get_logger().debug(f"DoubleElim.get_goal = {response}, status code = {res.status_code}") return Position(response["x"], response["y"], -1), res.status_code @staticmethod def get_items() -> Tuple[List[Dict], int]: """Get a list with all current items :return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0, "x": 0, "y": 0} :rtype: Tuple[List[Dict], int] """ res = requests.get(API_URL_GET_ITEMS) response = json.loads(res.content) Logging.get_logger().debug(f"DoubleElim.get_items = {response}, status code = {res.status_code}") return response, res.status_code @staticmethod def get_scores() -> Tuple[Dict, int]: """Get the current scores :return: A dictionary with all scores included like: {"self":2,"opponent":0} :rtype: Tuple[Dict, int] """ res = requests.get(API_URL_GET_SCORES) response = json.loads(res.content) Logging.get_logger().debug(f"DoubleElim.get_scores = {response}, status code = {res.status_code}") return response, res.status_code