Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix another cause of "Could not find step name" (2.6 bug). #11780

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -154,14 +155,17 @@ private void generateNamedGroups(
final Collection<TargetGroup> targetGroups,
final Collection<Unit> canFire,
final Collection<Unit> enemyUnits) {

if (targetGroups.size() == 1) {
firingGroups.addAll(
buildFiringGroups(name, canFire, enemyUnits, CollectionUtils.getAny(targetGroups)));
} else {
// use the first unitType name of each TargetGroup as a suffix for the FiringGroup name
for (final TargetGroup targetGroup : targetGroups) {
final UnitType type = CollectionUtils.getAny(targetGroup.getFiringUnits(canFire)).getType();
// Sort units first, before getting the unit type to use for the name, so that we choose
// the same name regardless of the order of units provided.
List<Unit> firingUnits = targetGroup.getFiringUnits(canFire);
firingUnits.sort(Comparator.comparing(u -> u.getType().getName()));
UnitType type = CollectionUtils.getAny(firingUnits).getType();
firingGroups.addAll(
buildFiringGroups(name + " " + type.getName(), canFire, enemyUnits, targetGroup));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TargetGroup {
this.targetUnitTypes = targetUnitTypes;
}

public Collection<Unit> getFiringUnits(final Collection<Unit> units) {
public List<Unit> getFiringUnits(final Collection<Unit> units) {
return CollectionUtils.getMatches(units, Matches.unitIsOfTypes(firingUnitTypes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static games.strategy.triplea.delegate.battle.steps.MockGameData.givenGameData;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -338,6 +339,14 @@ void noFiringGroupIfAllTargetsAreExcluded() {
assertThat(firingGroups, is(empty()));
}

private Unit givenUnitWithCannotTarget(String name, Set<UnitType> cannotTarget) {
final Unit fireUnit = givenAnyUnit();
lenient().when(fireUnit.getType().getName()).thenReturn(name);
lenient().when(fireUnit.toString()).thenReturn(name);
when(fireUnit.getUnitAttachment().getCanNotTarget()).thenReturn(cannotTarget);
return fireUnit;
}

@Test
void twoFiringGroupsWithCanNotTarget() {
final Unit targetUnit = givenAnyUnit();
Expand All @@ -347,39 +356,38 @@ void twoFiringGroupsWithCanNotTarget() {
final Unit targetUnit3 = givenAnyUnit();
final UnitType targetUnit3Type = targetUnit3.getType();

final Unit fireUnit = givenAnyUnit();
when(fireUnit.getType().getName()).thenReturn("fireUnit");
final UnitAttachment unitAttachment =
(UnitAttachment) fireUnit.getType().getAttachment(UNIT_ATTACHMENT_NAME);
when(unitAttachment.getCanNotTarget()).thenReturn(Set.of(targetUnit2Type, targetUnit3Type));

final Unit fireUnit2 = givenAnyUnit();
when(fireUnit2.getType().getName()).thenReturn("fireUnit2");
final UnitAttachment unitAttachment2 =
(UnitAttachment) fireUnit2.getType().getAttachment(UNIT_ATTACHMENT_NAME);
when(unitAttachment2.getCanNotTarget()).thenReturn(Set.of(targetUnitType));

final List<FiringGroup> firingGroups =
FiringGroupSplitterGeneral.of(OFFENSE, FiringGroupSplitterGeneral.Type.NORMAL, UNITS)
.apply(
givenBattleStateBuilder()
.gameData(givenGameData().withAlliedAirIndependent(true).build())
.attacker(attacker)
.defender(defender)
.attackingUnits(List.of(fireUnit, fireUnit2))
.defendingUnits(List.of(targetUnit, targetUnit2, targetUnit3))
.build());

assertThat(firingGroups, hasSize(2));
assertThat(firingGroups.get(0).getDisplayName(), is(UNITS + " fireUnit"));
assertThat(firingGroups.get(0).getFiringUnits(), contains(fireUnit));
assertThat(firingGroups.get(0).getTargetUnits(), contains(targetUnit));
assertThat(firingGroups.get(0).isSuicideOnHit(), is(false));

assertThat(firingGroups.get(1).getDisplayName(), is(UNITS + " fireUnit2"));
assertThat(firingGroups.get(1).getFiringUnits(), contains(fireUnit2));
assertThat(firingGroups.get(1).getTargetUnits(), contains(targetUnit2, targetUnit3));
assertThat(firingGroups.get(1).isSuicideOnHit(), is(false));
final Unit fireUnit =
givenUnitWithCannotTarget("fireUnit", Set.of(targetUnit2Type, targetUnit3Type));
final Unit fireUnit2 = givenUnitWithCannotTarget("fireUnit2", Set.of(targetUnitType));
final Unit fireUnit3 = givenUnitWithCannotTarget("fireUnit3", Set.of(targetUnitType));

// Iterate over several different orderings of attackers to ensure step names are deterministic
// regardless of the order of units passed in.
List<Unit> attackersOrdering1 = List.of(fireUnit, fireUnit2, fireUnit3);
List<Unit> attackersOrdering2 = List.of(fireUnit, fireUnit3, fireUnit2);
for (List<Unit> attackingUnits : List.of(attackersOrdering1, attackersOrdering2)) {
final List<FiringGroup> firingGroups =
FiringGroupSplitterGeneral.of(OFFENSE, FiringGroupSplitterGeneral.Type.NORMAL, UNITS)
.apply(
givenBattleStateBuilder()
.gameData(givenGameData().withAlliedAirIndependent(true).build())
.attacker(attacker)
.defender(defender)
.attackingUnits(attackingUnits)
.defendingUnits(List.of(targetUnit, targetUnit2, targetUnit3))
.build());

assertThat(firingGroups, hasSize(2));
assertThat(firingGroups.get(0).getDisplayName(), is(UNITS + " fireUnit"));
assertThat(firingGroups.get(0).getFiringUnits(), contains(fireUnit));
assertThat(firingGroups.get(0).getTargetUnits(), contains(targetUnit));
assertThat(firingGroups.get(0).isSuicideOnHit(), is(false));

assertThat(firingGroups.get(1).getDisplayName(), is(UNITS + " fireUnit2"));
assertThat(firingGroups.get(1).getFiringUnits(), containsInAnyOrder(fireUnit2, fireUnit3));
assertThat(firingGroups.get(1).getTargetUnits(), contains(targetUnit2, targetUnit3));
assertThat(firingGroups.get(1).isSuicideOnHit(), is(false));
}
}

@Test
Expand Down