Skip to content
Snippets Groups Projects
Commit e237cd3a authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

StudyServerResponse for all study server requests

parent 33bb1d1c
No related branches found
No related tags found
1 merge request!72Resolve "Too large number of selected players does not break the gui and environment"
Pipeline #47728 failed
......@@ -1280,11 +1280,15 @@ class PyGameGUI:
def get_game_connection(self, tutorial):
if self.menu_state == MenuStates.ControllerTutorial:
self.player_info = requests.post(
answer = requests.post(
f"{self.request_url}/connect_to_tutorial/{self.participant_id}"
).json()
self.player_info = {self.player_info["player_id"]: self.player_info}
if answer["status_code"] == 200:
self.player_info = answer["data"]
self.player_info = {self.player_info["player_id"]: self.player_info}
else:
self.menu_state = MenuStates.Start
log.warning("Could not connect to tutorial.")
else:
answer = requests.post(
f"{self.request_url}/get_game_connection/{self.participant_id}"
......
......@@ -72,7 +72,8 @@ class StudyConfig(TypedDict):
class StudyServerResponse(TypedDict):
status_code: Literal[200] | Literal[409]
status_code: Literal[200] | Literal[409] | Literal[403]
msg: str
data: None | dict[str, PlayerInfo] | dict
......@@ -372,16 +373,19 @@ async def start_study(participant_id: str, number_players: int) -> StudyServerRe
log.debug(f"ADDING PLAYERS: {number_players}")
success, player_info = study_manager.add_participant(participant_id, number_players)
if success:
return {"status_code": 200, "data": player_info}
return {"status_code": 200, "msg": "", "data": player_info}
else:
log.warning(f"TOO MANY PLAYERS FOR STUDY; {number_players}")
return {"status_code": 409, "data": None}
return {"status_code": 409, "msg": "Could not add participant(s)", "data": None}
@app.post("/level_done/{participant_id}")
async def level_done(participant_id: str) -> StudyServerResponse:
ok = study_manager.player_finished_level(participant_id)
return {"status_code": 200 if ok else 409, "data": None}
if ok:
return {"status_code": 200, "msg": "Ok", "data": None}
else:
return {"status_code": 409, "msg": "Not Ok", "data": None}
@app.post("/get_game_connection/{participant_id}")
......@@ -391,12 +395,13 @@ async def get_game_connection(participant_id: str) -> StudyServerResponse:
)
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},
}
@app.post("/connect_to_tutorial/{participant_id}")
async def connect_to_tutorial(participant_id: str):
async def connect_to_tutorial(participant_id: str) -> StudyServerResponse:
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"
......@@ -425,13 +430,17 @@ async def connect_to_tutorial(participant_id: str):
case 200:
env_info = env_info.json()
study_manager.running_tutorials[participant_id] = env_info
return env_info["player_info"]["0"]
return {
"status_code": 200,
"msg": "Ok",
"data": env_info["player_info"]["0"],
}
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):
async def disconnect_from_tutorial(participant_id: str) -> StudyServerResponse:
answer = requests.post(
f"{study_manager.game_server_url}/manage/stop_env/",
json={
......@@ -441,9 +450,17 @@ async def disconnect_from_tutorial(participant_id: str):
},
)
if answer.status_code == 200:
return "Ok"
return {
"status_code": 200,
"msg": "Ok",
"data": None,
}
else:
return "Not Ok"
return {
"status_code": 403,
"msg": "Could not disconnect from tutorial",
"data": None,
}
def main(study_host, study_port, game_host, game_port, manager_ids, study_config_path):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment