Esempio n. 1
0
 def unpause_agent(self):
     print("Communicator: game update " + str(time.time()),
           file=self.logfile,
           flush=True)
     print("Communicator's game state:", file=self.logfile, flush=True)
     print(str(self.last_game_state), file=self.logfile, flush=True)
     self.last_game_state = Game.from_json(
         communication_state.get("game_state"),
         communication_state.get("available_commands"))
     self.receive_game_state_update()
Esempio n. 2
0
    def receive_game_state_update(self,
                                  block=False,
                                  perform_callbacks=True,
                                  repeat=False):
        """Using the next message from Communication Mod, update the stored game state

		:param block: set to True to wait for the next message
		:type block: bool
		:param perform_callbacks: set to True to perform callbacks based on the new game state
		:type perform_callbacks: bool
		:return: whether a message was received
		"""

        message = ""
        if repeat:
            message = self.last_msg
        else:
            message = self.get_next_raw_message(block)

        if message is not None:
            communication_state = json.loads(message)
            self.last_error = communication_state.get("error", None)
            self.game_is_ready = communication_state.get("ready_for_command")
            if self.last_error is None:
                self.in_game = communication_state.get("in_game")
                if self.in_game:
                    self.last_game_state = Game.from_json(
                        communication_state.get("game_state"),
                        communication_state.get("available_commands"))
            else:
                print("Communicator detected error",
                      file=self.logfile,
                      flush=True)
            if perform_callbacks:
                if self.last_error is not None:
                    self.action_queue.clear()
                    new_action = self.error_callback(self.last_error)
                    self.add_action_to_queue(new_action)
                elif self.in_game:
                    if len(self.action_queue) == 0 and perform_callbacks:
                        #print(str(self.last_game_state), file=self.logfile, flush=True)
                        new_action = self.state_change_callback(
                            self.last_game_state)
                        self.add_action_to_queue(new_action)
                elif self.stop_after_run:
                    self.clear_actions()
                else:
                    new_action = self.out_of_game_callback()
                    self.add_action_to_queue(new_action)
            return True
        return False