Class DiplomacySession


  • public class DiplomacySession
    extends TimedSession
    A type of session to handle diplomacy. Diplomacy may involve human players, so it *must* be a TimedSession.
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • agreement

        private DiplomaticTrade agreement
        The agreement under consideration.
      • unit

        private final Unit unit
        The initiating unit.
      • settlement

        private final Settlement settlement
        The other player's settlement.
      • otherUnit

        private final Unit otherUnit
        The other player's unit (only non-null in first contact cases).
    • Constructor Detail

      • DiplomacySession

        public DiplomacySession​(Unit unit,
                                Settlement settlement,
                                long timeout)
        Start a new diplomacy session with our unit and another player settlement.
        Parameters:
        unit - The Unit that is initiating diplomacy.
        settlement - The Settlement that is contacted.
        timeout - The amount of time to wait for a response.
      • DiplomacySession

        public DiplomacySession​(Unit unit,
                                Unit otherUnit,
                                long timeout)
        Start a new diplomacy session with our unit and another player unit.
        Parameters:
        unit - The Unit that is initiating diplomacy.
        otherUnit - The other Unit that is contacted.
        timeout - The amount of time to wait for a response.
    • Method Detail

      • makeDiplomacySessionKey

        public static java.lang.String makeDiplomacySessionKey​(Ownable o1,
                                                               Ownable o2)
        Make a diplomacy session key for the given ownables.
        Parameters:
        o1 - The first Ownable.
        o2 - The second Ownable.
        Returns:
        A diplomacy session key.
      • getGame

        private ServerGame getGame()
        Get the game this session is in.
        Returns:
        The enclosing ServerGame.
      • getAgreement

        public DiplomaticTrade getAgreement()
        Get the session agreement.
        Returns:
        The DiplomaticTrade under negotiation.
      • setAgreement

        public void setAgreement​(DiplomaticTrade agreement)
        Set the session agreement.
        Parameters:
        agreement - The new DiplomaticTrade to negotiate.
      • getUnit

        public Unit getUnit()
        Get the initiating unit.
        Returns:
        The Unit that started the session.
      • getSettlement

        public Settlement getSettlement()
        Get the settlement if any.
        Returns:
        The Settlement that is negotiating.
      • getOtherUnit

        public Unit getOtherUnit()
        Get the other unit if any.
        Returns:
        The other Unit that is negotiating.
      • getOwner

        private ServerPlayer getOwner()
        Get the owner of the initiating unit.
        Returns:
        The ServerPlayer that owns the initiating unit.
      • getOtherPlayer

        private ServerPlayer getOtherPlayer()
        Get the owner of the other settlement or unit.
        Returns:
        The ServerPlayer that owns the other settlement or unit.
      • getOtherPlayer

        public ServerPlayer getOtherPlayer​(ServerPlayer serverPlayer)
        Utility to find the other player in this session from a given one.
        Parameters:
        serverPlayer - The ServerPlayer *not* to find.
        Returns:
        The other ServerPlayer.
      • getMessage

        public DiplomacyMessage getMessage​(Player destination)
        Utility to create a message using the current session parameters and trade agreement, given a desired destination player.
        Parameters:
        destination - The Player to send the message to.
        Returns:
        A new DiplomacyMessage for the destination player.
      • isCompatible

        public boolean isCompatible​(FreeColGameObject fcgo1,
                                    FreeColGameObject fcgo2)
        Was this session started by the given objects?
        Parameters:
        fcgo1 - The first FreeColGameObject.
        fcgo2 - The second FreeColGameObject.
        Returns:
        True if the objects started this session.
      • findContactSession

        public static DiplomacySession findContactSession​(Unit unit,
                                                          Unit other)
        Find any contact session already underway between the owners of the given units.
        Parameters:
        unit - The first Unit.
        other - The second Unit.
        Returns:
        Any DiplomacySession found.
      • findContactSession

        public static DiplomacySession findContactSession​(Unit unit,
                                                          Settlement settlement)
        Find any contact session already underway between the owners of a given unit and settlement.
        Parameters:
        unit - The Unit.
        settlement - The Settlement.
        Returns:
        Any DiplomacySession found.
      • findContactSession

        private static DiplomacySession findContactSession​(Player p1,
                                                           Player p2)
        Find any contact session already underway between the given players.
        Parameters:
        p1 - The first Player.
        p2 - The second Player.
        Returns:
        Any DiplomacySession found.
      • completeInternal

        private void completeInternal​(boolean result,
                                      ChangeSet cs)
        Primitive level to finishing the session with the given result.
        Parameters:
        result - The result of the session.
        cs - A ChangeSet to update.
      • completeFirstContact

        public boolean completeFirstContact​(ChangeSet cs)
        Complete first contact session. Just do nothing but superclass action as we do not want the offer accept/reject messages, and the unit already has zero moves.
        Parameters:
        cs - A ChangeSet to update.
        Returns:
        Whether the session was already complete.
      • complete

        public boolean complete​(boolean result,
                                ChangeSet cs)
        Explicit completion of the session with a given result. Called from the controller when the player returns a definite response.
        Parameters:
        result - Whether to accept or reject the demand.
        cs - A ChangeSet to update.
        Returns:
        Whether the session was already complete.
      • complete

        protected boolean complete​(boolean result)
        Complete this task with the given result. By default this will be called (with value == false) when the timer expires.
        Specified by:
        complete in class TimedSession
        Parameters:
        result - The result to complete the session with.
        Returns:
        The result of the session.
      • complete

        public boolean complete​(ChangeSet cs)
        All transaction types must implement a completion action. This is called by the controller at the end of turn to complete any sessions that have not yet completed, or if the controller gets the required response to complete the session.
        Overrides:
        complete in class TimedSession
        Parameters:
        cs - A ChangeSet to update with changes that occur when completing this session.
        Returns:
        True if the session was already complete.