Skip to content
Snippets Groups Projects
Commit a5f8c71d authored by fheinrich's avatar fheinrich
Browse files

If number of added player is more than study capacity: create new study

parent d479117e
No related branches found
No related tags found
1 merge request!62Resolve "Game Flow"
Pipeline #46955 passed
...@@ -88,16 +88,16 @@ def main(cli_args=None): ...@@ -88,16 +88,16 @@ def main(cli_args=None):
print("Start PyGame GUI:") print("Start PyGame GUI:")
pygame_gui_2 = Process(target=start_pygame_gui, args=(cli_args,)) pygame_gui_2 = Process(target=start_pygame_gui, args=(cli_args,))
pygame_gui_2.start() pygame_gui_2.start()
# # #
# print("Start PyGame GUI:") print("Start PyGame GUI:")
# pygame_gui_3 = Process(target=start_pygame_gui, args=(cli_args,)) pygame_gui_3 = Process(target=start_pygame_gui, args=(cli_args,))
# pygame_gui_3.start() pygame_gui_3.start()
while ( # while (
pygame_gui.is_alive() # pygame_gui.is_alive()
and pygame_gui_2.is_alive() # and pygame_gui_2.is_alive()
# and pygame_gui_3.is_alive() # # and pygame_gui_3.is_alive()
): # ):
time.sleep(1) # time.sleep(1)
while pygame_gui.is_alive(): while pygame_gui.is_alive():
time.sleep(1) time.sleep(1)
......
...@@ -101,9 +101,11 @@ class StudyState: ...@@ -101,9 +101,11 @@ class StudyState:
len(self.participant_id_to_player_info) == self.study_config["num_players"] len(self.participant_id_to_player_info) == self.study_config["num_players"]
) )
def can_add_participant(self, participant_id: int) -> bool: def can_add_participant(self, num_participants: int) -> bool:
return len(self.participant_id_to_player_info) < self.study_config["num_players"] filled = self.num_connected_players + num_participants <= self.study_config["num_players"]
print(self.num_connected_players, num_participants, self.study_config["num_players"])
print("CAN ADD", self, filled)
return filled and not self.is_full
def create_env(self, level): def create_env(self, level):
with open(ROOT_DIR / "game_content" / level["item_info_path"], "r") as file: with open(ROOT_DIR / "game_content" / level["item_info_path"], "r") as file:
item_info = file.read() item_info = file.read()
...@@ -239,6 +241,9 @@ class StudyState: ...@@ -239,6 +241,9 @@ class StudyState:
for websocket in self.websockets.values(): for websocket in self.websockets.values():
websocket.close() websocket.close()
def __repr__(self):
return f"Study({self.current_running_env['env_id']})"
class StudyManager: class StudyManager:
def __init__(self): def __init__(self):
...@@ -257,11 +262,14 @@ class StudyManager: ...@@ -257,11 +262,14 @@ class StudyManager:
def add_participant(self, participant_id: str, number_players: int): def add_participant(self, participant_id: str, number_players: int):
player_info = None player_info = None
if all([s.is_full for s in self.running_studies]): if not self.running_studies or all([not s.can_add_participant(number_players) for s in self.running_studies]):
self.create_study() self.create_study()
print("all", self.running_studies)
for study in self.running_studies: for study in self.running_studies:
if not study.is_full: if study.can_add_participant(number_players):
print("connect", study)
player_info = study.add_participant(participant_id, number_players) player_info = study.add_participant(participant_id, number_players)
self.participant_id_to_study_map[participant_id] = study self.participant_id_to_study_map[participant_id] = study
return player_info return player_info
...@@ -271,6 +279,7 @@ class StudyManager: ...@@ -271,6 +279,7 @@ class StudyManager:
assigned_study.player_finished_level(participant_id) assigned_study.player_finished_level(participant_id)
def get_participant_game_connection(self, participant_id: str): def get_participant_game_connection(self, participant_id: str):
print(self.participant_id_to_study_map)
assigned_study = self.participant_id_to_study_map[participant_id] assigned_study = self.participant_id_to_study_map[participant_id]
player_info, last_level = assigned_study.get_connection(participant_id) player_info, last_level = assigned_study.get_connection(participant_id)
return player_info, last_level return player_info, last_level
......
...@@ -20,5 +20,5 @@ levels: ...@@ -20,5 +20,5 @@ levels:
name: "Level 4-2: Dark" name: "Level 4-2: Dark"
num_players: 6 num_players: 2
num_bots: 2 num_bots: 2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment