Class TradeService
- java.lang.Object
-
- ch.sahits.game.openpatrician.clientserverinterface.service.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 Summary
Fields Modifier and Type Field Description private java.util.List<IWare>
basicNeeds
protected com.google.common.eventbus.AsyncEventBus
clientServerEventBus
private ComputablePriceV2
computablePrice
private PopulationConsume
consume
private DialogTemplateFactory
dialogTemplateFactory
private static java.util.Map<java.lang.Integer,java.lang.Integer>
UPGRADE_LIMIT_LEVEL
Limits for the level that have to be reached before upgrading.
-
Constructor Summary
Constructors Constructor Description TradeService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 theamount
ofware
fromcity
and move it to the ship.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 theamount
ofware
fromcity
and store it in the trading office.(package private) 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.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.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)
(package private) 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.private double
calculateExperianceBonusFactor(int experianceLevel)
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.private void
distributeCostToConvoyParticipants(IConvoy convoy, IPlayer player, int avgPrice, int moved)
private void
distributeIncomeToConvoyParticipants(IConvoy vessel, int avgPrice, int sold)
private void
distributeIncomeToConvoyParticipants(IConvoy convoy, IPlayer player, int avgPrice, int moved)
Distribute the the income to all participants of the convoy saveplayer
.private java.util.List<IWare>
getBasicNeededWares()
java.util.List<IWare>
getBasicNeeds()
Retrieve a list of the basic needs.private void
initialize()
int
sellFromStorageToCity(ITradingOffice office, IPlayer player, ICity city, IWare ware, int amount, java.util.Optional<java.lang.Integer> minAvgPrice)
Sellamount
ofware
from theoffice
to thecity
.void
sellWareShipToCity(INavigableVessel vessel, IPlayer player, ICity city, IWare ware, int amount, java.util.Optional<java.lang.Integer> minAvgPrice)
Sellamount
ofware
fromvessel
to thecity
.int
transferFromStorageToVessel(ITradingOffice office, INavigableVessel vessel, IPlayer player, IWare ware, int amount)
Moveamount
ofware
from theoffice
onto thevessel
.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.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.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.
-
-
-
Field Detail
-
computablePrice
@Autowired private ComputablePriceV2 computablePrice
-
consume
@Autowired private PopulationConsume consume
-
dialogTemplateFactory
@Autowired private DialogTemplateFactory dialogTemplateFactory
-
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
-
-
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 transferedplayer
- owner of the trading office and the vesselcity
- in which the transfer happensamounts
- map of wares and the amounts that should be transfered.
-
transferFromStorageToVessel
public int transferFromStorageToVessel(ITradingOffice office, INavigableVessel vessel, IPlayer player, IWare ware, int amount)
Moveamount
ofware
from theoffice
onto thevessel
. If the vessel is a convoy the distribution to not player ships is considered by a cash transfer.- Parameters:
office
- where the wares are storedvessel
- to which the wares are transferedplayer
- owner of the trading office and vesselware
- to be transferredamount
- 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)
Sellamount
ofware
from theoffice
to thecity
.- Parameters:
office
- where the ware is stored.player
- owner of the officecity
- to which to sell toware
- to be soldamount
- to be soldminAvgPrice
- 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)
Sellamount
ofware
fromvessel
to thecity
. This method also considers distribution of the gained income, in the case of a mixed convoy.- Parameters:
vessel
- from which to sell the wareplayer
- owner of the vesselcity
- to which to sellware
- to be soldamount
- 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 theamount
ofware
fromcity
and store it in the trading office. The price to be payed does not matter only the available amount.- Parameters:
office
- trading officeplayer
- doing the transactioncity
- from which to buyware
- that is to be boughtamount
- of the ware that is requiredavgPrice
- 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 theamount
ofware
fromcity
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 vesselcity
- from which to buyware
- to buyamount
- 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 saveplayer
.
-
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 cashdesiredAmount
- amount that is desired to be boughtavailableAmountCity
- amount of the ware available in the citymaxAvgPrice
- 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)
-
-