use of games.strategy.engine.data.ResourceCollection in project triplea by triplea-game.
the class BidPurchaseDelegate method canWePurchaseOrRepair.
@Override
protected boolean canWePurchaseOrRepair() {
final ResourceCollection bidCollection = new ResourceCollection(getData());
// TODO: allow bids to have more than just PUs
bidCollection.addResource(getData().getResourceList().getResource(Constants.PUS), bid);
if (player.getProductionFrontier() != null && player.getProductionFrontier().getRules() != null) {
for (final ProductionRule rule : player.getProductionFrontier().getRules()) {
if (bidCollection.has(rule.getCosts())) {
return true;
}
}
}
if (player.getRepairFrontier() != null && player.getRepairFrontier().getRules() != null) {
for (final RepairRule rule : player.getRepairFrontier().getRules()) {
if (bidCollection.has(rule.getCosts())) {
return true;
}
}
}
return false;
}
use of games.strategy.engine.data.ResourceCollection in project triplea by triplea-game.
the class BidPurchaseDelegate method canAfford.
@Override
protected boolean canAfford(final IntegerMap<Resource> costs, final PlayerID player) {
final ResourceCollection bidCollection = new ResourceCollection(getData());
// TODO: allow bids to have more than just PUs
bidCollection.addResource(getData().getResourceList().getResource(Constants.PUS), bid);
return bidCollection.has(costs);
}
use of games.strategy.engine.data.ResourceCollection in project triplea by triplea-game.
the class TerritoryAttachment method setResources.
private void setResources(final String value) throws GameParseException {
if (value == null) {
m_resources = null;
return;
}
if (m_resources == null) {
m_resources = new ResourceCollection(getData());
}
final String[] s = value.split(":");
final int amount = getInt(s[0]);
if (s[1].equals(Constants.PUS)) {
throw new GameParseException("Please set PUs using production, not resource" + thisErrorMsg());
}
final Resource resource = getData().getResourceList().getResource(s[1]);
if (resource == null) {
throw new GameParseException("No resource named: " + s[1] + thisErrorMsg());
}
m_resources.putResource(resource, amount);
}
use of games.strategy.engine.data.ResourceCollection in project triplea by triplea-game.
the class RouteTest method testCorrectParameterHandling.
@Test
public void testCorrectParameterHandling() {
final MapPanel mockedMapPanel = mock(MapPanel.class);
final MapRouteDrawer routeDrawer = spy(new MapRouteDrawer(mockedMapPanel, dummyMapData));
when(mockedMapPanel.getXOffset()).thenReturn(0);
when(mockedMapPanel.getYOffset()).thenReturn(0);
when(mockedMapPanel.getScale()).thenReturn(0.0);
when(mockedMapPanel.getImageWidth()).thenReturn(1);
when(mockedMapPanel.getImageHeight()).thenReturn(1);
final Shape mockShape = mock(Shape.class);
final Graphics2D mockGraphics = mock(Graphics2D.class);
when(mockShape.contains(any(Point2D.class))).thenReturn(true);
final ResourceCollection mockResourceCollection = mock(ResourceCollection.class);
when(mockResourceCollection.getResourcesCopy()).thenReturn(new IntegerMap<>());
final ResourceImageFactory mockResourceImageFactory = mock(ResourceImageFactory.class);
routeDrawer.drawRoute(mockGraphics, dummyRouteDescription, "2", mockResourceCollection, mockResourceImageFactory);
verify(mockGraphics, atLeastOnce()).fill(any(Shape.class));
verify(mockGraphics, atLeastOnce()).draw(any(Shape.class));
// Those methods are needed
verify(mockedMapPanel, atLeastOnce()).getXOffset();
verify(mockedMapPanel, atLeastOnce()).getYOffset();
verify(mockedMapPanel, atLeastOnce()).getScale();
verify(dummyRouteDescription, times(2)).getRoute();
verify(dummyRouteDescription.getRoute(), atLeastOnce()).getAllTerritories();
}
use of games.strategy.engine.data.ResourceCollection in project triplea by triplea-game.
the class TripleAFrame method scrambleUnitsQuery.
public HashMap<Territory, Collection<Unit>> scrambleUnitsQuery(final Territory scrambleTo, final Map<Territory, Tuple<Collection<Unit>, Collection<Unit>>> possibleScramblers) {
if (messageAndDialogThreadPool == null) {
return null;
}
messageAndDialogThreadPool.waitForAll();
if (SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("Should not be called from dispatch thread");
}
final CountDownLatch continueLatch = new CountDownLatch(1);
final HashMap<Territory, Collection<Unit>> selection = new HashMap<>();
final Collection<Tuple<Territory, UnitChooser>> choosers = new ArrayList<>();
SwingUtilities.invokeLater(() -> {
mapPanel.centerOn(scrambleTo);
final JDialog dialog = new JDialog(this, "Select units to scramble to " + scrambleTo.getName());
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
final String optionScramble = "Scramble";
final String optionNone = "None";
final JButton scrambleButton = new JButton(optionScramble);
scrambleButton.addActionListener(e -> getOptionPane((JComponent) e.getSource()).setValue(scrambleButton));
final JButton noneButton = new JButton(optionNone);
noneButton.addActionListener(e -> getOptionPane((JComponent) e.getSource()).setValue(noneButton));
final Object[] options = { scrambleButton, noneButton };
final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[1]);
final JLabel whereTo = new JLabel("Scramble To: " + scrambleTo.getName());
whereTo.setFont(new Font("Arial", Font.ITALIC, 12));
panel.add(whereTo, BorderLayout.NORTH);
final JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createEmptyBorder());
panel2.setLayout(new FlowLayout());
final JPanel fuelCostPanel = new JPanel(new GridBagLayout());
panel.add(fuelCostPanel, BorderLayout.SOUTH);
for (final Territory from : possibleScramblers.keySet()) {
final JPanel panelChooser = new JPanel();
panelChooser.setLayout(new BoxLayout(panelChooser, BoxLayout.Y_AXIS));
panelChooser.setBorder(BorderFactory.createLineBorder(getBackground()));
final JLabel whereFrom = new JLabel("From: " + from.getName());
whereFrom.setHorizontalAlignment(SwingConstants.LEFT);
whereFrom.setFont(new Font("Arial", Font.BOLD, 12));
panelChooser.add(whereFrom);
panelChooser.add(new JLabel(" "));
final Collection<Unit> possible = possibleScramblers.get(from).getSecond();
final int maxAllowed = Math.min(BattleDelegate.getMaxScrambleCount(possibleScramblers.get(from).getFirst()), possible.size());
final UnitChooser chooser = new UnitChooser(possible, Collections.emptyMap(), false, uiContext);
chooser.setMaxAndShowMaxButton(maxAllowed);
chooser.addChangeListener(field -> {
final Collection<Unit> units = new HashSet<>();
final Map<PlayerID, ResourceCollection> playerFuelCost = new HashMap<>();
for (final Tuple<Territory, UnitChooser> tuple : choosers) {
units.addAll(tuple.getSecond().getSelected(false));
final Map<PlayerID, ResourceCollection> map = Route.getScrambleFuelCostCharge(tuple.getSecond().getSelected(false), tuple.getFirst(), scrambleTo, data);
for (final Entry<PlayerID, ResourceCollection> playerAndCost : map.entrySet()) {
if (playerFuelCost.containsKey(playerAndCost.getKey())) {
playerFuelCost.get(playerAndCost.getKey()).add(playerAndCost.getValue());
} else {
playerFuelCost.put(playerAndCost.getKey(), playerAndCost.getValue());
}
}
}
fuelCostPanel.removeAll();
boolean hasEnoughFuel = true;
int count = 0;
for (final Entry<PlayerID, ResourceCollection> entry : playerFuelCost.entrySet()) {
final JLabel label = new JLabel(entry.getKey().getName() + ": ");
fuelCostPanel.add(label, new GridBagConstraints(0, count, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
fuelCostPanel.add(uiContext.getResourceImageFactory().getResourcesPanel(entry.getValue(), data), new GridBagConstraints(1, count++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
if (!entry.getKey().getResources().has(entry.getValue().getResourcesCopy())) {
hasEnoughFuel = false;
label.setForeground(Color.RED);
}
}
scrambleButton.setEnabled(hasEnoughFuel);
dialog.pack();
});
choosers.add(Tuple.of(from, chooser));
panelChooser.add(chooser);
final JScrollPane chooserScrollPane = new JScrollPane(panelChooser);
panel2.add(chooserScrollPane);
}
panel.add(panel2, BorderLayout.CENTER);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setLocationRelativeTo(getParent());
dialog.setAlwaysOnTop(true);
dialog.pack();
dialog.setVisible(true);
dialog.requestFocusInWindow();
optionPane.addPropertyChangeListener(e -> {
if (!dialog.isVisible()) {
return;
}
final String option = ((JButton) optionPane.getValue()).getText();
if (option.equals(optionNone)) {
choosers.clear();
selection.clear();
dialog.setVisible(false);
dialog.removeAll();
dialog.dispose();
continueLatch.countDown();
} else if (option.equals(optionScramble)) {
for (final Tuple<Territory, UnitChooser> terrChooser : choosers) {
selection.put(terrChooser.getFirst(), terrChooser.getSecond().getSelected());
}
dialog.setVisible(false);
dialog.removeAll();
dialog.dispose();
continueLatch.countDown();
}
});
});
mapPanel.getUiContext().addShutdownLatch(continueLatch);
Interruptibles.await(continueLatch);
mapPanel.getUiContext().removeShutdownLatch(continueLatch);
return selection;
}
Aggregations