Commits

Arne Babenhauserheide  committed 58d5a41

FIX: result commands did not return their results.

  • Participants
  • Parent commits 983f7a7

Comments (0)

Files changed (2)

File hexbattle_gui.py

         # that we can do so.
         # TODO: Add an inactive state: There’s nothing we can do
         # (action already scheduled).
-        self.logic.update()
+        result = self.logic.update()
 
-        # do the computer actions. As soon as it’s the players turn, computer_turn() changes the state.
-        if self.logic.model.phase["player"] == "setup":
-            schedule_once(self.logic.model.setup_step, 0.01)
-        elif self.logic.model.phase["player"] == "CPU":
-            self.computer_turn()
-    
+        if not result: return # inactive state. TODO: add an explicit result.
+        
+        if result.name == "init":
+            schedule_once(result.command, 0.01)
+        
+        if result.name == "computer turn":
+            res = result.command()
+            self.eval_computer_turn(res)
+            
     def update_interactive_elements(self, dt=0): 
         """Update the interactive elements so they stay fast at all times."""	
         self.blob.update()
         """wait for delay seconds. Then hand over to CPU turn."""
         schedule_once(self.logic.model.switch_to_cpu_turn, delay)
     
-    def computer_turn(self):
+    def eval_computer_turn(self, result):
         """Do computer turns until it’s time for the player to act again."""
         # game end condition
-        result = self.logic.model.computer_turn()
         if result.name == "finished":
             self.text_overlay.show("Game finished. \n" + result.data["winner"] + " won \n< Escape to quit >.")
             return

File hexbattle_logic.py

         """The result of the action."""
         self.name = name
         self.data = data
+        self.description = description
         self.commands = commands
         # often used case of having only one command.
         if len(commands) == 1:
             self.command = commands[0]
         else:
             self.command = None
+    def __repr__(self):
+        return self.__class__.__name__ + "(\"" + self.name + "\", data=" + str(self.data) + ", description=" + str(self.description) + ", commands=" + str(self.commands) + ")"
 
 class Command:
     def __init__(self, fun, name, **arguments):
         self.name = name
         self.arguments = arguments
     def __call__(self, *args, **kwds): # the args are only there to catch dt=… from schedule.
-        self.fun(**kwds)
+        return self.fun(**kwds)
+    def __repr__(self):
+        return self.__class__.__name__ + "(\"" + repr(self.fun) + "\", " + str(self.name) + ",".join(self.arguments) + ")"
 
 class TerrainTile:
     """A tile of ground with properties."""
         # Update the visible representation of the char.
         for char in self.model.chars:
             char.update_model()
+        # do the computer actions. As soon as it’s the players turn, computer_turn() changes the state.
+        if self.model.phase["player"] == "setup":
+            return Result("init", description="initialized the logic. Setup needed.",
+                          commands=[Command(self.model.setup_step, "setup")])
+        elif self.model.phase["player"] == "CPU":
+            return Result("computer turn", commands=[Command(self.model.computer_turn, "computer turn")])