Class TradeService


  • public class TradeService
    extends java.lang.Object
    Service providing functionality for trading. This service also includes all side effects like reputation changes or benefits when trading with captain or steward.
    Author:
    Andi Hotz, (c) Sahits GmbH, 2016 Created on Jul 26, 2016
    • Field Detail

      • clientServerEventBus

        @Autowired
        @Qualifier("serverClientEventBus")
        protected com.google.common.eventbus.AsyncEventBus clientServerEventBus
      • UPGRADE_LIMIT_LEVEL

        private static final java.util.Map<java.lang.Integer,​java.lang.Integer> UPGRADE_LIMIT_LEVEL
        Limits for the level that have to be reached before upgrading.
      • basicNeeds

        private java.util.List<IWare> basicNeeds
    • Constructor Detail

      • TradeService

        public TradeService()
    • Method Detail

      • initialize

        @PostConstruct
        private void initialize()
      • getBasicNeededWares

        private java.util.List<IWare> getBasicNeededWares()
      • getBasicNeeds

        public java.util.List<IWare> getBasicNeeds()
        Retrieve a list of the basic needs.
        Returns:
        List of wares.
      • transferFromVesselToStorage

        public void transferFromVesselToStorage​(INavigableVessel vessel,
                                                IPlayer player,
                                                ICity city,
                                                java.util.Map<IWare,​java.lang.Integer> amounts)
        Transfer the wares in the specified amounts from the ship into the storage.
        PRE: there is a trading office in the city.
        Parameters:
        vessel - from which the wares are transfered
        player - owner of the trading office and the vessel
        city - in which the transfer happens
        amounts - map of wares and the amounts that should be transfered.
      • transferFromStorageToVessel

        public int transferFromStorageToVessel​(ITradingOffice office,
                                               INavigableVessel vessel,
                                               IPlayer player,
                                               IWare ware,
                                               int amount)
        Move amount of ware from the office onto the vessel. If the vessel is a convoy the distribution to not player ships is considered by a cash transfer.
        Parameters:
        office - where the wares are stored
        vessel - to which the wares are transfered
        player - owner of the trading office and vessel
        ware - to be transferred
        amount - to be transferred
        Returns:
        amount that is actually moved
      • sellFromStorageToCity

        public int sellFromStorageToCity​(ITradingOffice office,
                                         IPlayer player,
                                         ICity city,
                                         IWare ware,
                                         int amount,
                                         java.util.Optional<java.lang.Integer> minAvgPrice)
        Sell amount of ware from the office to the city.
        Parameters:
        office - where the ware is stored.
        player - owner of the office
        city - to which to sell to
        ware - to be sold
        amount - to be sold
        minAvgPrice - minimal avg price that has to be payed per item. If no value is passed the whole lot will be sold.
        Returns:
        total price of all sold wares (always positiv)
      • sellWareShipToCity

        public void sellWareShipToCity​(INavigableVessel vessel,
                                       IPlayer player,
                                       ICity city,
                                       IWare ware,
                                       int amount,
                                       java.util.Optional<java.lang.Integer> minAvgPrice)
        Sell amount of ware from vessel to the city. This method also considers distribution of the gained income, in the case of a mixed convoy.
        Parameters:
        vessel - from which to sell the ware
        player - owner of the vessel
        city - to which to sell
        ware - to be sold
        amount - to be sold
      • buyFromCityToStorage

        public int buyFromCityToStorage​(ITradingOffice office,
                                        IPlayer player,
                                        ICity city,
                                        IWare ware,
                                        int amount,
                                        java.util.Optional<java.lang.Integer> avgPrice,
                                        java.util.Optional<ISteward> optSteward,
                                        long retainCash)
        Buy the amount of ware from city and store it in the trading office. The price to be payed does not matter only the available amount.
        Parameters:
        office - trading office
        player - doing the transaction
        city - from which to buy
        ware - that is to be bought
        amount - of the ware that is required
        avgPrice - average price by item. If not present the average price is calculated based on the amount.
        optSteward - possibly the buying is done by a trade manager who has some bonus when buying wares.
        retainCash - amount of cash that should be retained when buying wares.
        Returns:
        total price payed.
      • buyFromCityToShip

        public long buyFromCityToShip​(INavigableVessel vessel,
                                      IPlayer player,
                                      ICity city,
                                      IWare ware,
                                      int amount,
                                      java.util.Optional<java.lang.Integer> maxAvgPrice,
                                      java.util.Optional<ICaptain> optCaptain,
                                      long retainCash)
        Buy the amount of ware from city and move it to the ship. Also consider that the vessel might be a convoy, which necessitates the buy costs to be split.
        Parameters:
        vessel - on which to load the wares.
        player - owner of the vessel
        city - from which to buy
        ware - to buy
        amount - of ware to buy.
        maxAvgPrice - maximal average buy price per item. If there is no value it is not limited by price.
        optCaptain - possible the buying is done by a captain who has some bonus when buying wares.
        retainCash - amount of cash that should be retained when buying wares.
        Returns:
        price that is payed for the wares.
      • buyWareFromCity

        private javafx.util.Pair<java.lang.Integer,​java.lang.Integer> buyWareFromCity​(IPlayer player,
                                                                                            ICity city,
                                                                                            int availableAmountCity,
                                                                                            int amount,
                                                                                            IWare ware,
                                                                                            long cash)
        Buy from city and return the pair of the bought amount as well as the price per item.
      • buyWareFromCity

        javafx.util.Pair<java.lang.Integer,​java.lang.Integer> buyWareFromCity​(IPlayer player,
                                                                                    ICity city,
                                                                                    int availableAmountCity,
                                                                                    int amount,
                                                                                    int avgPrice,
                                                                                    IWare ware,
                                                                                    long cash)
        Buy from city and return the pair of the bought amount as well as the price per item.
      • buyWareFromCity

        private javafx.util.Pair<java.lang.Integer,​java.lang.Integer> buyWareFromCity​(IPlayer player,
                                                                                            ICity city,
                                                                                            int availableAmountCity,
                                                                                            IWare ware,
                                                                                            long cash,
                                                                                            int amountToBuy,
                                                                                            int maxAvgPrice)
      • distributeCostsToAllConvoyParticipants

        private long distributeCostsToAllConvoyParticipants​(IConvoy convoy,
                                                            javafx.util.Pair<java.lang.Integer,​java.lang.Integer> amountBougth,
                                                            int loaded,
                                                            double discountBonus)
        Distribute the income to all the participants in a convoy.
      • distributeIncomeToConvoyParticipants

        private void distributeIncomeToConvoyParticipants​(IConvoy vessel,
                                                          int avgPrice,
                                                          int sold)
      • distributeIncomeToConvoyParticipants

        private void distributeIncomeToConvoyParticipants​(IConvoy convoy,
                                                          IPlayer player,
                                                          int avgPrice,
                                                          int moved)
        Distribute the the income to all participants of the convoy save player.
      • distributeCostToConvoyParticipants

        private void distributeCostToConvoyParticipants​(IConvoy convoy,
                                                        IPlayer player,
                                                        int avgPrice,
                                                        int moved)
      • updatePlayerByCapacityPercentageRedistribution

        private void updatePlayerByCapacityPercentageRedistribution​(IPlayer player,
                                                                    IConvoy convoy,
                                                                    java.util.Map<IPlayer,​java.lang.Integer> capacityMap,
                                                                    long totalCash)
        Redistribute the cash between the players and the convoy owner. The amounts deduced in one place are added in the other.
      • updatePlayerByCapacityPercentage

        private void updatePlayerByCapacityPercentage​(IConvoy convoy,
                                                      java.util.Map<IPlayer,​java.lang.Integer> capacityMap,
                                                      long totalCash)
        Even distirbution of the total cash by ship capacity in the convoy.
      • calculateAffordableAmountToBuy

        javafx.util.Pair<java.lang.Integer,​java.lang.Integer> calculateAffordableAmountToBuy​(IWare ware,
                                                                                                   long cash,
                                                                                                   int desiredAmount,
                                                                                                   int availableAmountCity,
                                                                                                   int maxAvgPrice)
        Calculate how much can be bought due to monetary restrictions. To be on the save side, if the amount is larger than 10 items, one is deduced.
        Parameters:
        cash - available cash
        desiredAmount - amount that is desired to be bought
        availableAmountCity - amount of the ware available in the city
        maxAvgPrice - limit of the maximal avg price to be payed per item.
        Returns:
        Pair containing the affordable amount and the average price.
      • calculateExperianceBonusFactor

        private double calculateExperianceBonusFactor​(int experianceLevel)