diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py index 510ce83a3b2baa2849997ccee63b9854cf0a159d..de7a26d822ab5674f2a66b6b180b3cc901e73dc6 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -28,6 +28,7 @@ from overcooked_simulator.overcooked_environment import ( ActionType, InterActionData, ) +from overcooked_simulator.state_representation import StateRepresentation from overcooked_simulator.utils import ( custom_asdict_factory, url_and_port_arguments, @@ -176,8 +177,9 @@ class PyGameGUI: ) self.current_layout_idx = 0 - self.last_level = False + self.last_state: StateRepresentation + self.last_level = False self.beeped_once = False def setup_player_keys(self, players: list[str], number_key_sets=1, disjunct=False): @@ -650,6 +652,16 @@ class PyGameGUI: anchors={"centerx": "centerx", "top": "top"}, ) + self.text_recipes_label = pygame_gui.elements.UILabel( + text=f"Recipes in this level:", + relative_rect=pygame.Rect( + (0, 0), + (self.window_width * 0.5, 80), + ), + manager=self.manager, + anchors={"centerx": "centerx", "top_target": self.level_name_label}, + ) + self.all_recipes_labels = [] self.last_recipes_labels = [] @@ -784,8 +796,8 @@ class PyGameGUI: ] self.pregame_screen_elements = [ - # self.recipes_container, self.level_name_label, + self.text_recipes_label, self.quit_button, self.continue_button, ] @@ -882,14 +894,10 @@ class PyGameGUI: ) match self.menu_state: - case MenuStates.Start: - pass case MenuStates.ControllerTutorial: self.draw_tutorial_screen_frame() case MenuStates.Game: self.draw_game_screen_frame() - case MenuStates.PostGame: - self.update_conclusion_label(self.last_state) self.manager.draw_ui(self.main_window) self.manager.update(self.time_delta) @@ -911,6 +919,24 @@ class PyGameGUI: game_screen_rect.center = self.game_center self.main_window.blit(self.game_screen, game_screen_rect) + def update_conclusion_label(self, state): + score = state["score"] + print(state["served_meals"]) + self.conclusion_label.set_text(f"Your final score is {score}. Hurray!") + + def exit_game(self): + self.menu_state = MenuStates.PostGame + + if self.CONNECT_WITH_STUDY_SERVER: + self.send_level_done() + else: + self.stop_game_on_server("finished_button_pressed") + self.disconnect_websockets() + + self.update_conclusion_label(self.last_state) + self.update_screen_elements() + self.beeped_once = False + def draw_game_screen_frame(self): self.last_state = self.request_state() @@ -922,15 +948,7 @@ class PyGameGUI: self.play_bell_sound() if self.last_state["ended"]: - self.menu_state = MenuStates.PostGame - self.finished_button_press() - self.disconnect_websockets() - - if self.CONNECT_WITH_STUDY_SERVER: - self.send_level_done() - - self.update_screen_elements() - self.beeped_once = False + self.exit_game() else: self.draw_game(self.last_state) @@ -1009,10 +1027,6 @@ class PyGameGUI: score = state["score"] self.score_label.set_text(f"Score {score}") - def update_conclusion_label(self, state): - score = state["score"] - self.conclusion_label.set_text(f"Your final score is {score}. Hurray!") - def update_remaining_time(self, remaining_time: float): hours, rem = divmod(int(remaining_time), 3600) minutes, seconds = divmod(rem, 60) @@ -1065,7 +1079,6 @@ class PyGameGUI: if tutorial: self.player_ids = [str(list(self.player_info.keys())[0])] - def set_level_info_screen(self): self.level_name_label.set_text(f"{self.level_info['name']}") @@ -1083,7 +1096,7 @@ class PyGameGUI: manager=self.manager, # container=self.recipes_container, object_id="#recipe", - anchors={"centerx": "centerx", "top_target": self.level_name_label}, + anchors={"centerx": "centerx", "top_target": self.text_recipes_label}, ) else: recipe_label = pygame_gui.elements.UILabel( @@ -1215,7 +1228,7 @@ class PyGameGUI: self.kitchen_width = state["kitchen"]["width"] self.kitchen_height = state["kitchen"]["height"] - def stop_game(self, reason: str) -> None: + def stop_game_on_server(self, reason: str) -> None: log.debug(f"Stopping game: {reason}") if not self.CONNECT_WITH_STUDY_SERVER: requests.post( @@ -1234,7 +1247,7 @@ class PyGameGUI: def finished_button_press(self): if not self.CONNECT_WITH_STUDY_SERVER: - self.stop_game("finished_button_pressed") + self.stop_game_on_server("finished_button_pressed") self.menu_state = MenuStates.PostGame log.debug("Pressed finished button") self.update_screen_elements() @@ -1388,7 +1401,7 @@ class PyGameGUI: if event.ui_element == self.quit_button: self.running = False self.disconnect_websockets() - self.stop_game("Quit button") + self.stop_game_on_server("Quit button") self.menu_state = MenuStates.Start log.debug("Pressed quit button") return @@ -1455,15 +1468,16 @@ class PyGameGUI: ############################################ case MenuStates.Game: - match event.ui_element: - case self.finished_button: - self.menu_state = MenuStates.PostGame - self.disconnect_websockets() - self.finished_button_press() - self.handle_joy_stick_input(joysticks=self.joysticks) - - if self.CONNECT_WITH_STUDY_SERVER: - self.send_level_done() + pass + # match event.ui_element: + # case self.finished_button: + # self.menu_state = MenuStates.PostGame + # self.disconnect_websockets() + # self.finished_button_press() + # self.handle_joy_stick_input(joysticks=self.joysticks) + # + # if self.CONNECT_WITH_STUDY_SERVER: + # self.send_level_done() ############################################ @@ -1533,7 +1547,7 @@ class PyGameGUI: print(f"Joystick {event.instance_id} disconnected") print("Number of joysticks:", pygame.joystick.get_count()) - # Press key instead of mouse button press + # Press enter key or controller start button instead of mouse button press if ( event.type == pygame.JOYBUTTONDOWN and any( @@ -1597,10 +1611,12 @@ class PyGameGUI: except (KeyboardInterrupt, SystemExit): self.running = False self.disconnect_websockets() - self.stop_game("Program exited.") + if not self.CONNECT_WITH_STUDY_SERVER: + self.stop_game_on_server("Program exited.") self.disconnect_websockets() - self.stop_game("Program exited") + if not self.CONNECT_WITH_STUDY_SERVER: + self.stop_game_on_server("Program exited.") pygame.quit() sys.exit() @@ -1612,7 +1628,7 @@ class PyGameGUI: self.get_game_connection(tutorial=False) else: - self.stop_game("tutorial_finished") + self.stop_game_on_server("tutorial_finished") self.create_env_on_game_server(tutorial=False) self.disconnect_websockets() diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index 8c4e1f92ad92df7293e30e3536f4eb112695e825..2db78efdf746a88e6f7b052dd20bc9ffb6094d68 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -854,6 +854,7 @@ class Environment: ] if self.player_view_restricted else None, + "served_meals": [("?", str(meal)) for (meal, time) in self.order_and_score.served_meals], "info_msg": [ (msg["msg"], msg["level"]) for msg in self.info_msgs_per_player[player_id] diff --git a/overcooked_simulator/state_representation.py b/overcooked_simulator/state_representation.py index 319e15cef011a535a051db2426acf834c2c61da9..4479c39216537278e8180166f21090ae541bc690 100644 --- a/overcooked_simulator/state_representation.py +++ b/overcooked_simulator/state_representation.py @@ -96,10 +96,12 @@ class StateRepresentation(BaseModel): kitchen: KitchenInfo score: float | int orders: list[OrderState] + all_players_ready: bool ended: bool env_time: datetime # isoformat str remaining_time: float view_restrictions: None | list[ViewRestriction] + served_meals: list[tuple[str, str]] info_msg: list[tuple[str, str]]