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

Save the owner of the new units in case another action changes the ownership #6943

Merged
merged 4 commits into from
Jul 1, 2020
Merged
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 @@ -2,11 +2,16 @@

import games.strategy.engine.data.Change;
import games.strategy.engine.data.GameData;
import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.UnitCollection;
import games.strategy.engine.data.UnitHolder;
import java.util.ArrayList;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/** Add units. */
public class AddUnits extends Change {
Expand All @@ -15,19 +20,31 @@ public class AddUnits extends Change {
private final String name;
private final Collection<Unit> units;
private final String type;
/**
* The unit's owner can be modified sometime after this Change is created but before it is
* performed. To ensure that the newly created units have the correct ownership, their original
* owners are stored in this separate map.
*/
private Map<UUID, String> unitOwnerMap;

AddUnits(final UnitCollection collection, final Collection<Unit> units) {
this.units = new ArrayList<>(units);
this.units = units;
unitOwnerMap = buildUnitOwnerMap(units);
name = collection.getHolder().getName();
type = collection.getHolder().getType();
}

AddUnits(final String name, final String type, final Collection<Unit> units) {
this.units = new ArrayList<>(units);
this.units = units;
unitOwnerMap = buildUnitOwnerMap(units);
this.type = type;
this.name = name;
}

private Map<UUID, String> buildUnitOwnerMap(final Collection<Unit> units) {
Copy link
Member

Choose a reason for hiding this comment

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

side-note / big nit, this method could be static. I think we need to try and see if we can share a config that turns on the static analysis warning that methods can be marked as static.

For me it's really helpful as I watch for private methods being static. If they are, I know they do not interact with the class state. If they are not static, I assume they cannot be static and hence interact with class state.

return units.stream().collect(Collectors.toMap(Unit::getId, unit -> unit.getOwner().getName()));
}

@Override
public Change invert() {
return new RemoveUnits(name, type, units);
Expand All @@ -36,11 +53,36 @@ public Change invert() {
@Override
protected void perform(final GameData data) {
final UnitHolder holder = data.getUnitHolder(name, type);
holder.getUnitCollection().addAll(units);
final Collection<Unit> unitsWithCorrectOwner = buildUnitsWithOwner(data);
holder.getUnitCollection().addAll(unitsWithCorrectOwner);
Copy link
Member

Choose a reason for hiding this comment

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

A test of some sort to verify the right functionality would be a good thing to have. If we change & break this, we'll have the extra safety net. Testing is really good for ensuring correctness, which is a further safety net that would be good to have as well.

}

private Collection<Unit> buildUnitsWithOwner(final GameData data) {
final Map<UUID, Unit> uuidToUnits =
units.stream().collect(Collectors.toMap(Unit::getId, unit -> unit));
return unitOwnerMap.entrySet().stream()
.map(
entry -> {
Unit unit = data.getUnits().get(entry.getKey());
if (unit == null) {
unit = uuidToUnits.get(entry.getKey());
}
final GamePlayer player = data.getPlayerList().getPlayerId(entry.getValue());
unit.setOwner(player);
return unit;
})
.collect(Collectors.toList());
}

@Override
public String toString() {
return "Add unit change. Add to:" + name + " units:" + units;
}

private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (unitOwnerMap == null) {
unitOwnerMap = buildUnitOwnerMap(units);
}
}
}