/** * Redistribute the cash between the players and the convoy owner. The amounts deduced in one place are added in the * other. */ private void updatePlayerByCapacityPercentageRedistribution(IPlayer player, IConvoy convoy, Map<IPlayer, Integer> capacityMap, long totalCash) { double totalCapacity = convoy.getCapacity(); ICompany company = player.getCompany(); for (Entry<IPlayer, Integer> entry : capacityMap.entrySet()) { double percentage = entry.getValue()/totalCapacity; long partialAmount = Math.round(totalCash*percentage); IPlayer player1 = entry.getKey(); ICompany company1 = player1.getCompany(); if (player1 instanceof IHumanPlayer) { company1.updateCash(partialAmount); } else { company1.updateCashDirectly(partialAmount); } if (player instanceof IHumanPlayer) { company.updateCash(-partialAmount); } else { company.updateCashDirectly(-partialAmount); } } }
private EventHandler<MouseEvent> joinGuild(final IHumanPlayer player,final ICity city, final int fee) { return mouseEvent -> { try { ICompany company = player.getCompany(); company.updateCash(-fee); IGuild guild = guildList.findGuild(city).get(); guild.join(player); ViewChangeCityPlayerProxyJFX proxy = new ViewChangeCityPlayerProxyJFX(this.city, EViewChangeEvent.MAIN_VIEW_GUILD); clientEventBus.post(new NoticeBoardUpdate(proxy)); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to join the quild", e); } }; } }
/** * Even distirbution of the total cash by ship capacity in the convoy. */ private void updatePlayerByCapacityPercentage(IConvoy convoy, Map<IPlayer, Integer> capacityMap, long totalCash) { double totalCapacity = convoy.getCapacity(); for (Entry<IPlayer, Integer> entry : capacityMap.entrySet()) { double percentage = entry.getValue()/totalCapacity; long partialAmount = Math.round(totalCash*percentage); IPlayer player = entry.getKey(); if (player instanceof IHumanPlayer) { player.getCompany().updateCash(partialAmount); } else { player.getCompany().updateCashDirectly(partialAmount); } } }
private EventHandler<MouseEvent> getAction() { return evt -> { try { final ICompany company = city.getPlayer().getCompany(); company.updateCash(-moneyTransfer.getAmount()); city.getCity().getReputation(city.getPlayer()).update(moneyTransfer.getAmount() / 1000); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to make a donation to the city", e); } }; }
@Override public void run() { if (player instanceof IHumanPlayer) { player.getCompany().updateCash(tenant.getRentPerWeek()); } else { player.getCompany().updateCashDirectly(tenant.getRentPerWeek()); } } }
@PostConstruct private void initializeDialog() { setTitle(messageSource.getMessage("ch.sahits.game.openpatrician.display.dialog.TavernInformantDialogPageOne.title", new Object[]{informant.getName()}, locale.getCurrentLocal())); String loadedText = messageSource.getMessage("ch.sahits.game.openpatrician.display.dialog.TavernInformantDialogPageOne.dialog", new Object[0], locale.getCurrentLocal()); HashMap<String, Object> parameters = new HashMap<>(); final int price = informant.getPrice(city.getPlayer()); parameters.put("price", price); DecoratedText dt = decoratedTextFactory.createDecoratedText(loadedText, parameters); final OpenPatricianLargeWaxButton acceptButton = new OpenPatricianLargeWaxButton(messageSource.getMessage("ch.sahits.game.openpatrician.display.dialog.TavernInformantDialogPageOne.buy", new Object[]{}, locale.getCurrentLocal())); acceptButton.getStyleClass().add("actionButton"); int BUTTON_X = (WIDTH - 124) / 2; acceptButton.setLayoutX(BUTTON_X); int LOWER_BUTTON_Y = CLOSE_BTN_Y_POS - 24; acceptButton.setLayoutY(LOWER_BUTTON_Y); acceptButton.setOnAction(mouseEvent -> { try { city.getPlayer().getCompany().updateCash(-price); clientEventBus.post(new ViewChangeEvent(MainGameView.class, EDialogType.TAVERN_INFORMANT_2)); } catch (RuntimeException e) { logger.error("Failed to accept informant offer", e); } }); getContent().addAll(dt, acceptButton); }
@Override public void run() { DisplayMessage msg = new DisplayMessage(messageKey, fine); clientEventBus.post(msg); if (player instanceof IHumanPlayer) { player.getCompany().updateCash(-fine); } else { player.getCompany().updateCashDirectly(-fine); } } }
@Override protected EventHandler<MouseEvent> createAcceptHandler() { return mouseEvent -> { try { removeDecoratedContent(); removeAcceptButton(); MapSegmentedImage imageMap = segmentFactory.getRandomTreasureNest(); String loadedText = messageSource.getMessage("ch.sahits.game.openpatrician.display.dialog.TavernTreasureMapOwnerDialog.acceptMessage", new Object[]{imageMap.getImageName()}, locale.getCurrentLocal()); HashMap<String, Object> parameters = new HashMap<>(); List<Integer> visibleSegs = new ArrayList<>(imageMap.getSegmentIndices()); parameters.put("visibleSegments", visibleSegs); final DecoratedText dt = decoratedTextFactory.createDecoratedText(loadedText, parameters); Platform.runLater(() -> getContent().add(dt)); IHumanPlayer player = city.getPlayer(); player.setSegmentedMap(imageMap); locationDetector.addSegment(imageMap); ITreasureMapOwner mapOwner = (ITreasureMapOwner) currentPerson.get(); player.getCompany().updateCash(-mapOwner.getPrice()); mapOwner.leave(); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to add treasure map owners offer", e); } }; }
@Override public boolean execute() { clientEventBus.post(new DisplayMessage("ch.sahits.game.event.handler.impl.SinkConcurrentShipOrder.thankyou")); if (player instanceof IHumanPlayer) { player.getCompany().updateCash(premium); } else { player.getCompany().updateCashDirectly(premium); } if (reconciliation != null) { list.remove(reconciliation); } return true; }}
@Override protected EventHandler<MouseEvent> getAction() { return mouseEvent -> { try { ICity city = getCity(); Optional<ITradingOffice> office = getPlayer().findTradingOffice(getCity()); Preconditions.checkArgument(office.isPresent(), "Trading office must be present"); for (EWare ware : MATERIALS) { int buyAmount = requirementsMap.get(ware).getBuyAmount().intValue(); city.move(ware, -buyAmount, getPlayer()); int needed = requirementsMap.get(ware).requiredProperty().get(); int fromWarhouse = needed - buyAmount; office.get().move(ware, -fromWarhouse, 0); } ShipyardOrderBuild event = new ShipyardOrderBuild(city.getCityState().getShipyardState(), getCurrentShipType(), getPlayer()); clientServerEventBus.post(event); getPlayer().getCompany().updateCash(-calculateTotalBuildCosts()); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to order ship construction", e); } }; }
@Override public void run() { int bounty = (int)(Math.abs(opponent.getCompany().getCash()/2000*rnd.nextDouble())); if (opponent instanceof IHumanPlayer) { opponent.getCompany().updateCash(-bounty); } else { opponent.getCompany().updateCashDirectly(-bounty); } DisplayMessage msg = new DisplayMessage("ch.sahits.game.graphic.event.task.BreakInAction.message", bounty, opponent.getLastName()); clientEventBus.post(msg); }
private EventHandler<MouseEvent> getAction(final IChurch church) { return evt -> { try { final ICompany company = city.getPlayer().getCompany(); company.updateCash(-moneyTransfer.getAmount()); church.makeDonation(moneyTransfer.getAmount()); city.getCity().getReputation(city.getPlayer()).update(moneyTransfer.getAmount() / 1000); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to make donation", e); } }; }
@Override public void run() { ICompany company = debitor.getCompany(); int requiredAmount = debt.getAmount(); int amount = (int) Math.min(requiredAmount, company.getCash()); if (debitor instanceof IHumanPlayer) { company.updateCash(-amount); } else { company.updateCashDirectly(-amount); } if (debt.getCreditor() instanceof IPlayer) { if (debt.getCreditor() instanceof IHumanPlayer) { ((IPlayer) debt.getCreditor()).getCompany().updateCash(amount); } else { ((IPlayer) debt.getCreditor()).getCompany().updateCashDirectly(amount); } if (debt.getCreditor() instanceof IHumanPlayer) { DisplayMessage msg = new DisplayMessage("ch.sahits.game.openpatrician.engine.land.city.LoanerEngine.paybackAfterWithoutAuction", new Object[]{debitor.getName(), debitor.getLastName(), amount}); TargetedEvent message = new TargetedEvent((IHumanPlayer) debt.getCreditor(), msg); clientServerEventBus.post(message); } } } }
private EventHandler<MouseEvent> createAcceptHandler() { return mouseEvent -> { try { final ICompany company = city.getPlayer().getCompany(); long cash = company.getCash(); final IDebt debt = selectedDebt.get(); int debtTotal = loanerService.calculatePaybacksum(debt); if (cash < debtTotal) { clearContent(); addDecoratedText("ch.sahits.game.openpatrician.display.dialog.LoanerPaybackDialog.noRepay", "noRepay"); } else { debt.getCreditor().receiveSum(debtTotal); company.updateCash(-debtTotal); loaner.getDebts().remove(debt); executeOnCloseButtonClicked(); } } catch (RuntimeException e) { logger.error("Failed to pay back loan", e); } }; } /**
@Override public void run() { IHumanPlayer debitor = (IHumanPlayer) debt.getDebitor(); ICompany company = debitor.getCompany(); int requiredAmount = debt.getAmount(); int amount = (int) Math.min(requiredAmount, company.getCash()); company.updateCash(-amount); if (debt.getCreditor() instanceof IPlayer) { if (debt.getCreditor() instanceof IHumanPlayer) { ((IPlayer) debt.getCreditor()).getCompany().updateCash(amount); } else { ((IPlayer) debt.getCreditor()).getCompany().updateCashDirectly(amount); } if (debt.getCreditor() instanceof IHumanPlayer) { DisplayMessage msg = new DisplayMessage("ch.sahits.game.openpatrician.engine.land.city.LoanerEngine.paybackAfterShipAuction", new Object[]{debitor.getName(), debitor.getLastName(), amount}); TargetedEvent message = new TargetedEvent((IHumanPlayer) debt.getCreditor(), msg); clientServerEventBus.post(message); } } } }
@VisibleForTesting Runnable createPawnRunnable(final IDebt debt, final ICity city) { return () -> { if (debt.getDebitor() instanceof IHumanPlayer) { pawnFromHumanDebitor(debt, city); // Add task for the following day to collect the money } else if (debt.getCreditor() instanceof IHumanPlayer){ int value = rnd.nextInt(debt.getAmount()/2) + debt.getAmount(); value = (int) Math.min(value, debt.getAmount()*debt.getInterest()); ((IPlayer)debt.getCreditor()).getCompany().updateCash(value); ICitizen debitor = debt.getDebitor(); DisplayMessage msg = new DisplayMessage("ch.sahits.game.openpatrician.engine.land.city.LoanerEngine.pawnMsg1", new Object[]{debitor.getName(), debitor.getLastName(), value}); TargetedEvent message = new TargetedEvent((IHumanPlayer) debt.getCreditor(), msg); clientServerEventBus.post(message); } }; } @VisibleForTesting
@Override public boolean execute(ICity city) { Optional<ICity> nextCity = state.nextDestination(); if (!nextCity.isPresent()){ // last leg int premium = state.getPremium()+state.getNumberOfAttacks()*state.getBonusPerAttack(); if (player instanceof IHumanPlayer) { player.getCompany().updateCash(premium); } else { player.getCompany().updateCashDirectly(premium); } clientEventBus.post(new DisplayMessage("ch.sahits.game.event.handler.impl.NextPatrolStation.premium", premium)); clientServerEventBus.unregister(atackListener); unregisterEventBus(); clientServerEventBus.unregister(atackListener); } else { // next leg ICity next = nextCity.get(); state.removeFirstCity(); int minutes = rnd.nextInt(600); LocalDateTime postMessage = date.getCurrentDate().plusMinutes(minutes); taskFactory.getPostponedDisplayMessage(postMessage, "ch.sahits.game.event.handler.impl.NextCourierPatrolStation.nextStation", next.getName()); LocalDateTime deadline = date.getCurrentDate().plusDays(6); new NextPatrolStation((IShip) getShip(), state.nextDestination().get(), state, player, deadline, atackListener); } return true; }}
@Override protected EventHandler<MouseEvent> createAcceptHandler() { return mouseEvent -> { try { IHumanPlayer player = city.getPlayer(); ThieveState burglar = (ThieveState) currentPerson.get(); player.getCompany().updateCash(-burglar.getPrice()); int probability = rnd.nextInt(3); if (probability < 1) { timedTaskListener.add((TimedTask) taskFactory.getDelayedCaughtAction(player, city.getCity())); } else { timedTaskListener.add((TimedTask) taskFactory.getBreakInAction(selectedOponent.get())); } player.updateCrimialDrive(1); burglar.leave(); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to accept burglar offer", e); } }; }
@Override protected EventHandler<MouseEvent> getAction() { return mouseEvent -> { try { city.getPlayersShips().remove(currentShip.get()); ShipyardOrderRepair event = new ShipyardOrderRepair(getCity().getCityState().getShipyardState(), currentShip.get(), city); clientServerEventBus.post(event); getPlayer().getCompany().updateCash(-calculateRepairCosts()); executeOnCloseButtonClicked(); } catch (RuntimeException e) { logger.error("Failed to order repair of ship", e); } }; }
@Override protected EventHandler<MouseEvent> getAction() { return mouseEvent -> { Optional<ITradingOffice> office = getPlayer().findTradingOffice(getCity()); Preconditions.checkArgument(office.isPresent(), "Trading office must be present"); for (EWare ware : MATERIALS) { int buyAmount = requirementsMap.get(ware).getBuyAmount().intValue(); getCity().move(ware, -buyAmount, getPlayer()); int needed = shipFactory.getUpgradeAmount(currentShip.get().getShipType(), ware); int fromWarhouse = needed - buyAmount; office.get().move(ware, -fromWarhouse, 0); } ShipyardOrderRefit event = new ShipyardOrderRefit(getCity().getCityState().getShipyardState(),currentShip.get(), currentShip.get().getShipUpgradeLevel().nextLevel(), city); clientServerEventBus.post(event); city.getPlayersShips().remove(currentShip.get()); getPlayer().getCompany().updateCash(-calculateTotalUpgradeCosts()); executeOnCloseButtonClicked(); }; }