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

Issue 5256: Refactored MekHQ Unit's Gunners to be Set to ensure gunners are unique #5295

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
27 changes: 16 additions & 11 deletions MekHQ/src/mekhq/campaign/unit/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class Unit implements ITechnology {
protected int scenarioId;

private List<Person> drivers;
private List<Person> gunners;
private Set<Person> gunners;
private List<Person> vesselCrew;
// Contains unique Id of each Infantry/BA Entity assigned to this unit as
// marines
Expand Down Expand Up @@ -180,7 +180,7 @@ public Unit(Entity en, Campaign c) {
this.parts = new ArrayList<>();
this.podSpace = new ArrayList<>();
this.drivers = new ArrayList<>();
this.gunners = new ArrayList<>();
this.gunners = new HashSet<>();
this.vesselCrew = new ArrayList<>();
forceId = Force.FORCE_NONE;
scenarioId = Scenario.S_DEFAULT_ID;
Expand Down Expand Up @@ -4688,8 +4688,8 @@ public List<Person> getDrivers() {
return Collections.unmodifiableList(drivers);
}

public List<Person> getGunners() {
return Collections.unmodifiableList(gunners);
public Set<Person> getGunners() {
return Collections.unmodifiableSet(gunners);
}

public List<Person> getVesselCrew() {
Expand Down Expand Up @@ -5843,18 +5843,23 @@ public void fixReferences(Campaign campaign) {
}
}
}
for (int ii = gunners.size() - 1; ii >= 0; --ii) {
Person gunner = gunners.get(ii);
Set<Person> gunnersToRemove = new HashSet<>();
for(Person gunner : gunners){
if (gunner instanceof UnitPersonRef) {
gunners.set(ii, campaign.getPerson(gunner.getId()));
if (gunners.get(ii) == null) {
gunnersToRemove.add(gunner);
Person updatedGunner = campaign.getPerson(gunner.getId());
if(updatedGunner != null){
gunners.add(updatedGunner);
}
else{
logger.error(
String.format("Unit %s ('%s') references missing gunner %s",
getId(), getName(), gunner.getId()));
gunners.remove(ii);
String.format("Unit %s ('%s') references missing gunner %s",
getId(), getName(), gunner.getId()));
}
}
}
gunners.removeAll(gunnersToRemove);

for (int ii = vesselCrew.size() - 1; ii >= 0; --ii) {
Person crew = vesselCrew.get(ii);
if (crew instanceof UnitPersonRef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import mekhq.campaign.personnel.generator.DefaultSkillGenerator;
import mekhq.campaign.unit.Unit;

import java.util.List;
import java.util.Set;

/**
* Hires a full complement of personnel for a unit.
Expand Down Expand Up @@ -155,7 +155,7 @@ public void execute(Campaign campaign, Unit unit) {
&& unit.getEntity().getWeaponList().stream()
.anyMatch(weapon -> (weapon.getType() instanceof WeaponType)
&& (((WeaponType) weapon.getType()).getDamage() == WeaponType.DAMAGE_ARTILLERY))) {
final List<Person> gunners = unit.getGunners();
final Set<Person> gunners = unit.getGunners();
if (!gunners.isEmpty() && gunners.stream().noneMatch(person -> person.getSkills().hasSkill(SkillType.S_ARTILLERY))) {
new DefaultSkillGenerator(campaign.getRandomSkillPreferences()).generateArtillerySkill(ObjectUtility.getRandomItem(gunners));
}
Expand Down
3 changes: 2 additions & 1 deletion MekHQ/unittests/mekhq/campaign/unit/UnitPersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -301,7 +302,7 @@ public void testGunner() {
verify(unit, times(1)).resetPilotAndEntity();

// Ensure when getting the gunner that it is the same gunner
List<Person> gunners = unit.getGunners();
Set<Person> gunners = unit.getGunners();
assertTrue(gunners.contains(mockGunner));
assertTrue(unit.isGunner(mockGunner));

Expand Down
13 changes: 9 additions & 4 deletions MekHQ/unittests/mekhq/gui/model/UnitTableModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -66,7 +66,12 @@ private void setCrew(int driverCount, int totalDriverNeeds,
boolean hasNavigator, Entity entity,
String expected) {
List<Person> drivers = Collections.nCopies(driverCount, crewMember);
List<Person> gunners = Collections.nCopies(gunnerCount, crewMember);

Set<Person> gunners = new HashSet<>();
for(int i = 0; i < gunnerCount; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pop a whitespace after for

gunners.add(mock(Person.class));
}

List<Person> crew = Collections.nCopies(crewCount + drivers.size() + gunners.size()
+ (hasNavigator ? 1 : 0), crewMember);

Expand Down Expand Up @@ -134,4 +139,4 @@ public void noAssignedCrew() {
false, mock(Jumpship.class),
"<html><b>Drivers: </b>0/1<br><b>Gunners: </b>0/1<br><b>Crew: </b>0/1<br><b>Navigator: </b>0/1</html>");
}
}
}
Loading