-
Notifications
You must be signed in to change notification settings - Fork 396
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
DanVanAtta
merged 4 commits into
triplea-game:master
from
trevan:player-owner-change-error
Jul 1, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a669461
Save the owner of the new units in case another action changes the ow…
trevan db68d72
Handle case where unit isn't in the gameData yet
trevan 59b252e
Merge remote-tracking branch 'upstream/master' into player-owner-chan…
trevan 9d9e114
Improve naming and add documentation for the unit-owner map
trevan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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) { | ||
return units.stream().collect(Collectors.toMap(Unit::getId, unit -> unit.getOwner().getName())); | ||
} | ||
|
||
@Override | ||
public Change invert() { | ||
return new RemoveUnits(name, type, units); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.