def on_propose(self, propose: Propose, dialogue: Dialogue) -> Union[Accept, Decline]: """ Handle a Propose. :param propose: the Propose :param dialogue: the dialogue :return: an Accept or a Decline """ logger.debug("[{}]: on propose as {}.".format(self.agent_name, dialogue.role)) proposal = propose.proposals[0] transaction_id = generate_transaction_id(self.crypto.public_key, propose.destination, dialogue.dialogue_label, dialogue.is_seller) transaction = Transaction.from_proposal(proposal, transaction_id, is_sender_buyer=not dialogue.is_seller, counterparty=propose.destination, sender=self.crypto.public_key, crypto=self.crypto) new_msg_id = propose.msg_id + 1 if self._is_profitable_transaction(transaction, dialogue): logger.debug("[{}]: Accepting propose (as {}).".format(self.agent_name, dialogue.role)) self.game_instance.lock_manager.add_lock(transaction, as_seller=dialogue.is_seller) self.game_instance.lock_manager.add_pending_initial_acceptance(dialogue, new_msg_id, transaction) result = Accept(new_msg_id, propose.dialogue_id, propose.destination, propose.msg_id, Context()) else: logger.debug("[{}]: Declining propose (as {})".format(self.agent_name, dialogue.role)) result = Decline(new_msg_id, propose.dialogue_id, propose.destination, propose.msg_id, Context()) self.game_instance.stats_manager.add_dialogue_endstate(EndState.DECLINED_PROPOSE, dialogue.is_self_initiated) return result
def on_accept(self, msg_id: int, dialogue_id: int, origin: str, target: int) -> None: """ Handle an Accept. :param msg_id: the message id :param dialogue_id: the dialogue id :param origin: the public key of the sending agent :param target: the message id targetted by this message :return: None """ self.in_queue.put( Accept(msg_id, dialogue_id, origin, target, Context()))
def _on_initial_accept( self, accept: Accept, dialogue: Dialogue ) -> Union[List[Decline], List[Union[OutContainer, Accept]]]: """ Handle an initial Accept. :param accept: the accept :param dialogue: the dialogue :return: a Deline or an Accept and a Transaction (in OutContainer """ transaction = self.game_instance.transaction_manager.pop_pending_proposal( dialogue.dialogue_label, accept.target) new_msg_id = accept.msg_id + 1 results = [] is_profitable_transaction, message = self.game_instance.is_profitable_transaction( transaction, dialogue) logger.debug(message) if is_profitable_transaction: if self.game_instance.strategy.is_world_modeling: self.game_instance.world_state.update_on_initial_accept( transaction) logger.debug("[{}]: Locking the current state (as {}).".format( self.agent_name, dialogue.role)) self.game_instance.transaction_manager.add_locked_tx( transaction, as_seller=dialogue.is_seller) results.append( OutContainer(message=transaction.serialize(), message_id=STARTING_MESSAGE_ID, dialogue_id=accept.dialogue_id, destination=self.game_instance.controller_pbk)) results.append( Accept(new_msg_id, accept.dialogue_id, accept.destination, accept.msg_id, Context())) else: logger.debug("[{}]: Decline the accept (as {}).".format( self.agent_name, dialogue.role)) results.append( Decline(new_msg_id, accept.dialogue_id, accept.destination, accept.msg_id, Context())) self.game_instance.stats_manager.add_dialogue_endstate( EndState.DECLINED_ACCEPT, dialogue.is_self_initiated) return results
def send_accept(self, msg_id: int, dialogue_id: int, destination: str, target: int) -> None: msg = Accept(msg_id, dialogue_id, destination, target) self._send(msg)
def send_accept(self, msg_id: int, dialogue_id: int, destination: str, target: int): msg = Accept(msg_id, dialogue_id, destination, target) self._send(msg.to_envelope())