diff --git a/cooperative_cuisine/pygame_2d_vis/gui.py b/cooperative_cuisine/pygame_2d_vis/gui.py index 3044944610e5f3c4471ab10bb83be5ae0f80a26d..4ce67a125536db72f7bc79ecd8d2d892aa283e65 100644 --- a/cooperative_cuisine/pygame_2d_vis/gui.py +++ b/cooperative_cuisine/pygame_2d_vis/gui.py @@ -47,7 +47,9 @@ log = logging.getLogger(__name__) class PlayerKeySet: """Set of keyboard keys for controlling a player. - First four keys are for movement. Order: Down, Up, Left, Right. 5th key is for interacting with counters. 6th key ist for picking up things or dropping them. + First four keys are for movement. Order: Down, Up, Left, Right. + 5th key is for interacting with counters. + 6th key ist for picking up things or dropping them. """ def __init__( @@ -123,7 +125,7 @@ class PyGameGUI: self.participant_id = uuid.uuid4().hex - self.game_screen: pygame.Surface = None + self.game_screen: pygame.Surface | None = None self.running = True self.key_sets: list[PlayerKeySet] = [] @@ -1282,9 +1284,9 @@ class PyGameGUI: if self.menu_state == MenuStates.ControllerTutorial: answer = requests.post( f"{self.request_url}/connect_to_tutorial/{self.participant_id}" - ).json() - if answer["status_code"] == 200: - self.player_info = answer["data"] + ) + if answer.status_code == 200: + self.player_info = answer.json() self.player_info = {self.player_info["player_id"]: self.player_info} else: self.menu_state = MenuStates.Start @@ -1292,10 +1294,10 @@ class PyGameGUI: else: answer = requests.post( f"{self.request_url}/get_game_connection/{self.participant_id}" - ).json() - if answer["status_code"] == 200: - self.player_info = answer["data"]["player_info"] - self.level_info = answer["data"]["level_info"] + ) + if answer.status_code == 200: + self.player_info = answer.json()["player_info"] + self.level_info = answer.json()["level_info"] self.last_level = self.level_info["last_level"] else: log.warning("COULD NOT GET GAME CONNECTION") @@ -1538,14 +1540,13 @@ class PyGameGUI: def start_study(self): answer = requests.post( f"{self.request_url}/start_study/{self.participant_id}/{self.number_humans_to_be_added}" - ).json() - if answer["status_code"] == 200: + ) + if answer.status_code == 200: self.last_level = False - self.player_info = answer["data"] self.get_game_connection(tutorial=False) else: self.menu_state = MenuStates.Start - print("COULD NOT START STUDY; Response:", answer["status_code"]) + print("COULD NOT START STUDY; Response:", answer.status_code) def send_level_done(self): _ = requests.post(f"{self.request_url}/level_done/{self.participant_id}").json() diff --git a/cooperative_cuisine/study_server.py b/cooperative_cuisine/study_server.py index 6733d577cdb2baec42fde6e186eb8ced33daa824..9e9f41e9a4000441a9f7e31342858d8d591047de 100644 --- a/cooperative_cuisine/study_server.py +++ b/cooperative_cuisine/study_server.py @@ -26,7 +26,8 @@ import requests import uvicorn import yaml from fastapi import FastAPI -from typing_extensions import TypedDict, Literal +from fastapi.responses import JSONResponse +from typing_extensions import TypedDict from cooperative_cuisine import ROOT_DIR from cooperative_cuisine.environment import EnvironmentConfig @@ -71,10 +72,10 @@ class StudyConfig(TypedDict): num_bots: int -class StudyServerResponse(TypedDict): - status_code: Literal[200] | Literal[409] | Literal[403] - msg: str - data: None | dict[str, PlayerInfo] | dict +# class StudyServerResponse(TypedDict): +# status_code: Literal[200] | Literal[409] | Literal[403] +# msg: str +# data: None | dict[str, PlayerInfo] | dict class StudyState: @@ -369,39 +370,44 @@ study_manager = StudyManager() @app.post("/start_study/{participant_id}/{number_players}") -async def start_study(participant_id: str, number_players: int) -> StudyServerResponse: +async def start_study(participant_id: str, number_players: int) -> JSONResponse: log.debug(f"ADDING PLAYERS: {number_players}") success, player_info = study_manager.add_participant(participant_id, number_players) + if success: - return {"status_code": 200, "msg": "", "data": player_info} + return JSONResponse(content=player_info, status_code=200) else: - log.warning(f"TOO MANY PLAYERS FOR STUDY; {number_players}") - return {"status_code": 409, "msg": "Could not add participant(s)", "data": None} + response = JSONResponse( + content="Could not add participant(s), too many players.", + status_code=409, + ) + return response @app.post("/level_done/{participant_id}") -async def level_done(participant_id: str) -> StudyServerResponse: +async def level_done(participant_id: str) -> JSONResponse: ok = study_manager.player_finished_level(participant_id) if ok: - return {"status_code": 200, "msg": "Ok", "data": None} + return JSONResponse(content="Ok", status_code=200) else: - return {"status_code": 409, "msg": "Not Ok", "data": None} + return JSONResponse(content="Not Ok", status_code=409) @app.post("/get_game_connection/{participant_id}") -async def get_game_connection(participant_id: str) -> StudyServerResponse: +async def get_game_connection(participant_id: str) -> JSONResponse: player_info, level_info = study_manager.get_participant_game_connection( participant_id ) - return { - "status_code": 200 if player_info and level_info else 409, - "msg": "Ok" if player_info and level_info else "Not Ok", - "data": {"player_info": player_info, "level_info": level_info}, - } + return JSONResponse( + content={"player_info": player_info, "level_info": level_info} + if player_info and level_info + else "Not Ok", + status_code=200 if player_info and level_info else 409, + ) @app.post("/connect_to_tutorial/{participant_id}") -async def connect_to_tutorial(participant_id: str) -> StudyServerResponse: +async def connect_to_tutorial(participant_id: str) -> JSONResponse: environment_config_path = ROOT_DIR / "configs" / "tutorial_env_config.yaml" layout_path = ROOT_DIR / "configs" / "layouts" / "tutorial.layout" item_info_path = ROOT_DIR / "configs" / "item_info.yaml" @@ -430,17 +436,13 @@ async def connect_to_tutorial(participant_id: str) -> StudyServerResponse: case 200: env_info = env_info.json() study_manager.running_tutorials[participant_id] = env_info - return { - "status_code": 200, - "msg": "Ok", - "data": env_info["player_info"]["0"], - } + return JSONResponse(content=env_info["player_info"]["0"], status_code=200) case 403: raise ValueError(f"Forbidden Request: {env_info.json()['detail']}") @app.post("/disconnect_from_tutorial/{participant_id}") -async def disconnect_from_tutorial(participant_id: str) -> StudyServerResponse: +async def disconnect_from_tutorial(participant_id: str) -> JSONResponse: answer = requests.post( f"{study_manager.game_server_url}/manage/stop_env/", json={ @@ -450,17 +452,11 @@ async def disconnect_from_tutorial(participant_id: str) -> StudyServerResponse: }, ) if answer.status_code == 200: - return { - "status_code": 200, - "msg": "Ok", - "data": None, - } + return JSONResponse(content="Ok", status_code=200) else: - return { - "status_code": 403, - "msg": "Could not disconnect from tutorial", - "data": None, - } + return JSONResponse( + content="Could not disconnect from tutorial", status_code=403 + ) def main(study_host, study_port, game_host, game_port, manager_ids, study_config_path):