use of games.strategy.triplea.delegate.dataObjects.CasualtyDetails in project triplea by triplea-game.
the class BattleCalculatorTest method testAaCasualtiesLowLuckMixedWithChooseAaCasualties.
@Test
public void testAaCasualtiesLowLuckMixedWithChooseAaCasualties() {
final GameData data = bridge.getData();
makeGameLowLuck(data);
setSelectAaCasualties(data, true);
// 6 bombers and 6 fighters
final Collection<Unit> planes = bomber(data).create(6, british(data));
planes.addAll(fighter(data).create(6, british(data)));
final Collection<Unit> defendingAa = territory("Germany", data).getUnits().getMatches(Matches.unitIsAaForAnything());
when(dummyPlayer.selectCasualties(any(), any(), anyInt(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean())).thenAnswer(new Answer<CasualtyDetails>() {
@Override
public CasualtyDetails answer(final InvocationOnMock invocation) {
final Collection<Unit> selectFrom = invocation.getArgument(0);
final int count = invocation.getArgument(2);
final List<Unit> selected = CollectionUtils.getNMatches(selectFrom, count, Matches.unitIsStrategicBomber());
return new CasualtyDetails(selected, new ArrayList<>(), false);
}
});
bridge.setRemote(dummyPlayer);
// don't allow rolling, 6 of each is deterministic
bridge.setRandomSource(new ScriptedRandomSource(new int[] { ScriptedRandomSource.ERROR }));
final DiceRoll roll = DiceRoll.rollAa(CollectionUtils.getMatches(planes, Matches.unitIsOfTypes(UnitAttachment.get(defendingAa.iterator().next().getType()).getTargetsAa(data))), defendingAa, bridge, territory("Germany", data), true);
final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, germans(data), british(data), null, territory("Germany", data), null, false, null).getKilled();
assertEquals(2, casualties.size());
// we selected all bombers
assertEquals(2, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
assertEquals(0, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
use of games.strategy.triplea.delegate.dataObjects.CasualtyDetails in project triplea by triplea-game.
the class BattleDisplay method getCasualties.
CasualtyDetails getCasualties(final Collection<Unit> selectFrom, final Map<Unit, Collection<Unit>> dependents, final int count, final String message, final DiceRoll dice, final PlayerID hit, final CasualtyList defaultCasualties, final boolean allowMultipleHitsPerUnit) {
if (SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("This method should not be run in the event dispatch thread");
}
final AtomicReference<CasualtyDetails> casualtyDetails = new AtomicReference<>(new CasualtyDetails());
final CountDownLatch continueLatch = new CountDownLatch(1);
SwingUtilities.invokeLater(() -> {
final boolean isEditMode = (dice == null);
if (!isEditMode) {
actionLayout.show(actionPanel, DICE_KEY);
dicePanel.setDiceRoll(dice);
}
final boolean plural = isEditMode || (count > 1);
final String countStr = isEditMode ? "" : "" + count;
final String btnText = hit.getName() + " select " + countStr + (plural ? " casualties" : " casualty");
actionButton.setAction(new AbstractAction(btnText) {
private static final long serialVersionUID = -2156028313292233568L;
private UnitChooser chooser;
private JScrollPane chooserScrollPane;
@Override
public void actionPerformed(final ActionEvent e) {
final String messageText = message + " " + btnText + ".";
if (chooser == null || chooserScrollPane == null) {
chooser = new UnitChooser(selectFrom, defaultCasualties, dependents, allowMultipleHitsPerUnit, mapPanel.getUiContext());
chooser.setTitle(messageText);
if (isEditMode) {
chooser.setMax(selectFrom.size());
} else {
chooser.setMax(count);
}
chooserScrollPane = new JScrollPane(chooser);
final Dimension screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
final int availHeight = screenResolution.height - 130;
final int availWidth = screenResolution.width - 30;
chooserScrollPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
final Dimension size = chooserScrollPane.getPreferredSize();
chooserScrollPane.setPreferredSize(new Dimension(Math.min(availWidth, size.width + (size.height > availHeight ? chooserScrollPane.getVerticalScrollBar().getPreferredSize().width : 0)), Math.min(availHeight, size.height)));
}
final String[] options = { "Ok", "Cancel" };
final String focus = ClientSetting.SPACE_BAR_CONFIRMS_CASUALTIES.booleanValue() ? options[0] : null;
final int option = JOptionPane.showOptionDialog(BattleDisplay.this, chooserScrollPane, hit.getName() + " select casualties", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, focus);
if (option != 0) {
return;
}
final List<Unit> killed = chooser.getSelected(false);
final List<Unit> damaged = chooser.getSelectedDamagedMultipleHitPointUnits();
if (!isEditMode && (killed.size() + damaged.size() != count)) {
JOptionPane.showMessageDialog(BattleDisplay.this, "Wrong number of casualties selected", hit.getName() + " select casualties", JOptionPane.ERROR_MESSAGE);
} else {
final CasualtyDetails response = new CasualtyDetails(killed, damaged, false);
casualtyDetails.set(response);
dicePanel.clear();
actionButton.setEnabled(false);
actionButton.setAction(nullAction);
continueLatch.countDown();
}
}
});
});
mapPanel.getUiContext().addShutdownLatch(continueLatch);
Interruptibles.await(continueLatch);
mapPanel.getUiContext().removeShutdownLatch(continueLatch);
return casualtyDetails.get();
}
Aggregations