From 5dd9a465736bce37839714e110c4a3a5c3d12f70 Mon Sep 17 00:00:00 2001
From: fheinrich <fheinrich@techfak.uni-bielefeld.de>
Date: Wed, 6 Mar 2024 11:58:54 +0100
Subject: [PATCH] Better management of when keyboard or controller button to
 continue is pressed. Updated thank you screen.

---
 cooperative_cuisine/pygame_2d_vis/gui.py | 104 +++++++++++++----------
 1 file changed, 61 insertions(+), 43 deletions(-)

diff --git a/cooperative_cuisine/pygame_2d_vis/gui.py b/cooperative_cuisine/pygame_2d_vis/gui.py
index fda81605..6b4baa86 100644
--- a/cooperative_cuisine/pygame_2d_vis/gui.py
+++ b/cooperative_cuisine/pygame_2d_vis/gui.py
@@ -839,15 +839,31 @@ class PyGameGUI:
         # End screen
         ########################################################################
 
-        conclusion_rect = pygame.Rect(
-            0, 0, self.window_width * 0.6, self.window_height * 0.4
+        rect = pygame.Rect(0, 0, self.window_width * 0.9, self.window_height * 0.4)
+        final_text_container = pygame_gui.elements.UIPanel(
+            relative_rect=rect,
+            manager=self.manager,
+            object_id="#graph_container",
+            anchors={"center": "center"},
         )
-        self.thank_you_label = pygame_gui.elements.UILabel(
-            text="Thank you!",
-            relative_rect=conclusion_rect,
+
+        rect = pygame.Rect((0, 0), (-1, -1))
+        text1 = pygame_gui.elements.UILabel(
+            text="Thank you for participanting in this study :)",
+            relative_rect=rect,
             manager=self.manager,
             object_id="#score_label",
-            anchors={"center": "center"},
+            container=final_text_container,
+            anchors={"top": "top", "left": "left", "right": "right"},
+        )
+        rect = pygame.Rect((0, 0), (-1, -1))
+        text2 = pygame_gui.elements.UILabel(
+            text="Please wait for instructions from the study supervisor.",
+            relative_rect=rect,
+            manager=self.manager,
+            object_id="#score_label",
+            container=final_text_container,
+            anchors={"top_target": text1, "left": "left", "right": "right"},
         )
 
         ########################################################################
@@ -901,7 +917,7 @@ class PyGameGUI:
         self.end_screen_elements = [
             self.fullscreen_button,
             self.quit_button,
-            self.thank_you_label,
+            final_text_container,
         ]
 
         self.rest = [
@@ -1468,7 +1484,7 @@ class PyGameGUI:
     def stop_game_on_server(self, reason: str) -> None:
         log.debug(f"Stopping game: {reason}")
         if not self.CONNECT_WITH_STUDY_SERVER:
-            requests.post(
+            answer = requests.post(
                 f"{self.request_url}/manage/stop_env/",
                 json={
                     "manager_id": self.manager_id,
@@ -1476,6 +1492,12 @@ class PyGameGUI:
                     "reason": reason,
                 },
             )
+            if answer.status_code != 200:
+                log.warning(
+                    "Could not send level done.",
+                    answer.status_code,
+                    answer.json()["detail"],
+                )
 
     def send_tutorial_finished(self):
         requests.post(
@@ -1619,7 +1641,13 @@ class PyGameGUI:
             )
 
     def send_level_done(self):
-        _ = requests.post(f"{self.request_url}/level_done/{self.participant_id}").json()
+        answer = requests.post(f"{self.request_url}/level_done/{self.participant_id}")
+        if answer.status_code != 200:
+            log.warning(
+                "Could not send level done.",
+                answer.status_code,
+                answer.json()["detail"],
+            )
 
     def button_continue_postgame_pressed(self):
         if self.CONNECT_WITH_STUDY_SERVER:
@@ -1635,7 +1663,7 @@ class PyGameGUI:
 
         self.menu_state = MenuStates.PreGame
 
-    def manage_button_event(self, button: pygame_gui.core.UIElement):
+    def manage_button_event(self, button: pygame_gui.core.UIElement | None = None):
         if button == self.quit_button:
             if self.fullscreen:
                 self.fullscreen_button_press()
@@ -1696,34 +1724,26 @@ class PyGameGUI:
             ############################################
 
             case MenuStates.ControllerTutorial:
-                match button:
-                    case self.continue_button:
-                        self.exit_tutorial()
-
-                        if self.CONNECT_WITH_STUDY_SERVER:
-                            self.start_study()
-                        else:
-                            self.create_env_on_game_server(tutorial=False)
+                self.exit_tutorial()
+                if self.CONNECT_WITH_STUDY_SERVER:
+                    self.start_study()
+                else:
+                    self.create_env_on_game_server(tutorial=False)
 
             ############################################
 
             case MenuStates.PreGame:
-                match button:
-                    case self.continue_button:
-                        self.setup_game()
-
-                        self.set_game_size()
-                        self.menu_state = MenuStates.Game
+                self.setup_game()
+                self.set_game_size()
+                self.menu_state = MenuStates.Game
 
             ############################################
 
             case MenuStates.PostGame:
-                match button:
-                    case self.next_game_button:
-                        self.button_continue_postgame_pressed()
-
-                    case self.finish_study_button:
-                        self.menu_state = MenuStates.End
+                if self.last_level:
+                    self.menu_state = MenuStates.End
+                else:
+                    self.button_continue_postgame_pressed()
 
             ############################################
 
@@ -1791,19 +1811,17 @@ class PyGameGUI:
                             and event.key == pygame.K_RETURN
                         )
                     ):
-                        match self.menu_state:
-                            case MenuStates.Start:
-                                self.manage_button_event(self.start_button)
-                            case MenuStates.ControllerTutorial:
-                                self.manage_button_event(self.continue_button)
-                            case MenuStates.PreGame:
-                                self.manage_button_event(self.continue_button)
-                            case MenuStates.PostGame:
-                                if self.last_level:
-                                    self.manage_button_event(self.next_game_button)
-                                else:
-                                    self.manage_button_event(self.finish_study_button)
-                        self.update_screen_elements()
+                        if self.menu_state == MenuStates.Start:
+                            self.manage_button_event(self.start_button)
+                            self.update_screen_elements()
+
+                        elif self.menu_state in [
+                            MenuStates.ControllerTutorial,
+                            MenuStates.PreGame,
+                            MenuStates.PostGame,
+                        ]:
+                            self.manage_button_event()
+                            self.update_screen_elements()
 
                     if event.type == pygame_gui.UI_BUTTON_PRESSED:
                         self.manage_button_event(event.ui_element)
-- 
GitLab