@Override protected String computeValue() { return String.valueOf(computablePrice.buyPrice(ware, availableAmount, ONE_INT_BINDING)); } };
@Override protected String computeValue() { return String.valueOf(computablePrice.sellPrice(ware, availableAmount, ONE_INT_BINDING)); } };
/** * 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. * @param cash available cash * @param desiredAmount amount that is desired to be bought * @param availableAmountCity amount of the ware available in the city * @param maxAvgPrice limit of the maximal avg price to be payed per item. * @return Pair containing the affordable amount and the average price. */ @VisibleForTesting Pair<Integer, Integer> calculateAffordableAmountToBuy(IWare ware, long cash, int desiredAmount, int availableAmountCity, int maxAvgPrice) { int buyAmount = computablePrice.calculateBuyAmount(ware, availableAmountCity, maxAvgPrice, desiredAmount, cash); if (buyAmount > 10) { buyAmount--; } if (buyAmount == 0) { return new Pair<>(0, 0); } int price = computablePrice.buyPrice(ware, new SimpleIntegerProperty(availableAmountCity), new ConstantIntegerBinding(buyAmount)); return new Pair<>(buyAmount, price); }
/** * Sell <code>amount</code> of <code>ware</code> from the <code>office</code> to the <code>city</code>. * @param office where the ware is stored. * @param player owner of the office * @param city to which to sell to * @param ware to be sold * @param amount to be sold * @param minAvgPrice minimal avg price that has to be payed per item. If no value is passed the whole lot will be sold. * @return total price of all sold wares (always positiv) */ public int sellFromStorageToCity(ITradingOffice office, IPlayer player, ICity city, IWare ware, int amount, Optional<Integer> minAvgPrice) { int availableAmountCity = city.getWare(ware).getAmount(); int amountToBeSold = amount; if (minAvgPrice.isPresent()) { amountToBeSold = computablePrice.calculateSellAmount(ware, availableAmountCity, minAvgPrice.get(), amount); } final int avgPrice = computablePrice.sellPrice(ware, new SimpleIntegerProperty(availableAmountCity), new ConstantIntegerBinding(amountToBeSold)); city.move(ware, amountToBeSold, player); // The price here is irrelevant int sold = office.move(ware, -amountToBeSold, avgPrice); if (player instanceof IHumanPlayer) { player.getCompany().updateCash(avgPrice * sold); } else { player.getCompany().updateCashDirectly(avgPrice * sold); } return avgPrice * sold; }
@Override protected int computeValue() { int availableAmount = weaponsDealer.amountAvailableProperty(weapon).get(); int buyAmount = transferUtil.calculateAvailableAmount(amount, availableAmount); int affordableAmount = computablePrice.calculateAffordableAmount(weapon, availableAmount, buyAmount, city.getPlayer().getCompany().getCash()); if (affordableAmount>0) { return computablePrice.buyPrice(weapon, new SimpleIntegerProperty(availableAmount), new StaticIntegerBinding(affordableAmount)); } else { return 0; } } };
if (dealerOwnedAmount.get() > 0) { int amountToBuy = transferUtil.calculateAvailableAmount(movableAmount, dealerOwnedAmount.get()); int affordableAmount = computablePrice.calculateAffordableAmount(weapon, dealerOwnedAmount.get(), amountToBuy, city.getPlayer().getCompany().getCash());
/** * Sell <code>amount</code> of <code>ware</code> from <code>vessel</code> to the <code>city</code>. This method also * considers distribution of the gained income, in the case of a mixed convoy. * @param vessel from which to sell the ware * @param player owner of the vessel * @param city to which to sell * @param ware to be sold * @param amount to be sold */ public void sellWareShipToCity(INavigableVessel vessel, IPlayer player, ICity city, IWare ware, int amount, Optional<Integer> minAvgPrice) { int availableAmountCity = city.getWare(ware).getAmount(); int amountToBeSold = amount; if (minAvgPrice.isPresent()) { amountToBeSold = computablePrice.calculateSellAmount(ware, availableAmountCity, minAvgPrice.get(), amount); } if (amountToBeSold > 0) { int avgPrice = computablePrice.sellPrice(ware, new SimpleIntegerProperty(availableAmountCity), new ConstantIntegerBinding(amountToBeSold)); city.move(ware, amountToBeSold, player); // The price here is irrelevant int sold = vessel.unload(ware, amountToBeSold); if (vessel instanceof IShip) { if (player instanceof IHumanPlayer) { player.getCompany().updateCash(avgPrice * sold); } else { player.getCompany().updateCashDirectly(avgPrice * sold); } } else { distributeIncomeToConvoyParticipants((IConvoy) vessel, avgPrice, sold); } } }
@Override protected int computeValue() { if (buyAmount.get() > 0) { return computablePrice.buyPrice(ware, inCity, buyAmount); } else { return 0; } } };
@Override protected String computeValue() { switch (type) { case PORT_CITY_TO_SHIP: case PORT_CITY_TO_STORAGE: if (movableAmount.get()==ETransferAmount.MAX){ return String.valueOf(ware.getMaxValueSell()); } else if (amountToSell.get() > 0){ return String.valueOf(computablePrice.sellPrice(ware, availableAmount, amountToSell)); } else { return String.valueOf(ware.getMaxValueSell()); } default: return ">"; } } };
@Override protected String computeValue() { switch (type) { case PORT_CITY_TO_SHIP: case PORT_CITY_TO_STORAGE: if (availableAmount.get() > 0){ return String.valueOf(computablePrice.buyPrice(ware, availableAmount, amountToBuy)); } else { return "0"; // cannot buy anything if nothing is there } default: return "<"; } } };
@Override protected int computeValue() { if (amountToSell.get() > 0) { int availableAmount = weaponsDealer.amountAvailableProperty(weapon).getValue(); // that should be the amount of the dealer return computablePrice.sellPrice(weapon, new SimpleIntegerProperty(availableAmount), amountToSell); // amountToSell is the amount stored on ship/office } else { return 0; } } };
@Override protected int computeValue() { int availableAmount = getAmountInArmory(weapon).get(); int buyAmount = transferUtil.calculateAvailableAmount(ETransferAmount.ONE, availableAmount); if (buyAmount>0) { return computablePrice.buyPrice(weapon, new SimpleIntegerProperty(availableAmount), new StaticIntegerBinding(buyAmount)) * 2; } else { return 0; } } };
@Override protected int computeValue() { int availableAmount = getAmountInArmory(weapon).get(); int buyAmount = transferUtil.calculateAvailableAmount(ETransferAmount.ONE, availableAmount); if (buyAmount>0) { return computablePrice.sellPrice(weapon, new SimpleIntegerProperty(availableAmount), new StaticIntegerBinding(buyAmount)) * 2; } else { return 0; } } };
@Override protected int computeValue() { int availableAmount = getAmountInArmory(weapon).get(); int buyAmount = transferUtil.calculateAvailableAmount(ETransferAmount.ONE, availableAmount); if (buyAmount>0) { return computablePrice.buyPrice(weapon, new SimpleIntegerProperty(availableAmount), new StaticIntegerBinding(buyAmount)) * 2; } else { return 0; } } };
Optional<IWare> ware = state.findMostNeededWare(); if (ware.isPresent()) { int price = computablePrice.sellPrice(ware.get(), iCity.getWare(ware.get()).amountProperty(), new IntegerBinding() { @Override protected int computeValue() {
buyer.setWare(ware); int amount = rnd.nextInt(21)+3; // TODO aho Jan 27, 2013: differentiate between bale and barrel int price = computablePrice.buyPrice(ware, new SimpleIntegerProperty(5), new StaticIntegerBinding(amount)); AmountablePrice<IWare> amt = amountableProvider.createWareAmountable(); amt.add(amount, price);
trader.setAmount(amount); double variance = rnd.nextDouble() * 0.25 - 0.125; int newPrice = (int) Math.abs(Math.rint(variance * computablePrice.sellPrice(ware, new SimpleIntegerProperty(availableAmount), new StaticIntegerBinding(amount)))); trader.setAvgPricePerItem(newPrice); trader.setWare(ware);
private Pair<Integer, Integer> buyWareFromCity(IPlayer player, ICity city, int availableAmountCity, IWare ware, long cash, int amountToBuy, int maxAvgPrice) { Pair<Integer, Integer> affordable = calculateAffordableAmountToBuy(ware, cash, amountToBuy, availableAmountCity, maxAvgPrice); amountToBuy = affordable.getKey(); int avgPriceCompleteAmount = affordable.getValue(); if (amountToBuy == 0) { return new Pair<>(0, 0); } int movedAmount = city.move(ware, -amountToBuy, player); if (amountToBuy != -movedAmount) { avgPriceCompleteAmount = computablePrice.buyPrice(ware, new SimpleIntegerProperty(city.getWare(ware).getAmount() + movedAmount), new ConstantIntegerBinding(movedAmount)); amountToBuy = -movedAmount; } return new Pair<>(amountToBuy, avgPriceCompleteAmount); }
@Override public boolean execute() { int availableAmount = city.getWare(ware).getAmount(); int sellAmount = vessel.getWare(ware).getAmount(); if (sellAmount > 0) { int sellPrice = computablePrice.sellPrice(getWare(), new SimpleIntegerProperty(availableAmount), new ConstantIntegerBinding(sellAmount)); IAIPlayer player = (IAIPlayer) vessel.getOwner(); ICompany company = player.getCompany(); LOGGER.trace("Before: cash={}, in city={}, on ship={}", company.getCash(), city.getWare(ware).getAmount(), vessel.getWare(ware).getAmount()); company.updateCashDirectly(sellPrice * sellAmount); city.move(getWare(), sellAmount, player); vessel.load(getWare(), sellAmount, sellPrice); LOGGER.trace("Sold {} of {} for a total of: {} ", sellAmount, ware.name(), sellPrice * sellAmount); LOGGER.trace("After: cash={}, in city={}, on ship={}", company.getCash(), city.getWare(ware).getAmount(), vessel.getWare(ware).getAmount()); // TODO ahotz 20.05.2016: handle case where there are ships owned by other players in the convoy } return true; } }
/** * Check the amount of bricks required and if the price is not too heigh buy it. */ @Override public void run() { ICity city = cityHall.getCity(); CityWall cityWall = city.getCityState().getCityWall(); if (cityWall.getUsedBricks() == cityWall.getBoughtBricks()) { int amountRequired = Math.min(10, cityWall.getRequiredBricks() - cityWall.getBoughtBricks()); int availableAmountCity = city.getWare(EWare.BRICK).getAmount(); int avgPrice = computablePrice.buyPrice(EWare.BRICK, new SimpleIntegerProperty(availableAmountCity), new ConstantIntegerBinding(amountRequired)); if (avgPrice < 90) { int bought = city.move(EWare.BRICK,-amountRequired, cityHall.getMayor()); cityWall.setBoughtBricks(cityWall.getBoughtBricks() - bought); avgPrice = computablePrice.buyPrice(EWare.BRICK, new SimpleIntegerProperty(availableAmountCity), new ConstantIntegerBinding(-bought)); ((Treasury)cityHall.getTreasury()).subtractBuildingCosts(avgPrice * bought); LocalDateTime buildDate = date.getCurrentDate().plusDays(5); CityWallBuildingTask task = engineFactory.getCityWallBuildingTask(cityWall, buildDate); timedTaskListener.add(task); } } } }