From e91dd145713110d0d1758ebb3580ac37753c8250 Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Thu, 21 Sep 2023 08:43:00 -0400 Subject: [PATCH 1/7] checkAIPlayers checkAIPlayers added to RulesAttachment to allow for checking of AI controlled players. --- .../triplea/attachments/RulesAttachment.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index 1908d9db9cc..fd32553ca88 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -55,6 +55,8 @@ public class RulesAttachment extends AbstractPlayerRulesAttachment { private int techCount = -1; // condition for having specific relationships private @Nullable List relationship = null; + // condition for checking ai player + private boolean checkAIPlayers = false; // condition for being at war private @Nullable Set atWarPlayers = null; private int atWarCount = -1; @@ -489,6 +491,22 @@ private void resetUnitPresence() { unitPresence = null; } + private void setCheckAIPlayers(final String s) { + checkAIPlayers = getBool(s); + } + + private void setCheckAIPlayers(final Boolean s) { + checkAIPlayers = s; + } + + public boolean getCheckAIPlayers() { + return checkAIPlayers; + } + + private void resetCheckAIPlayers() { + checkAIPlayers = false; + } + private int getAtWarCount() { return atWarCount; } @@ -735,11 +753,17 @@ public boolean isSatisfied( } objectiveMet = checkDirectOwnership(listedTerritories, players); } + // check for ai controlled player + if (objectiveMet && getCheckAIPlayers()) { + objectiveMet = checkCheckAIPlayers(players); + } // get attached to player final GamePlayer playerAttachedTo = (GamePlayer) getAttachedTo(); + // check for players at war if (objectiveMet && !getAtWarPlayers().isEmpty()) { objectiveMet = checkAtWar(playerAttachedTo, getAtWarPlayers(), getAtWarCount()); } + // check for techs if (objectiveMet && !getTechs().isEmpty()) { objectiveMet = checkTechs(playerAttachedTo, data.getTechnologyFrontier()); } @@ -1001,6 +1025,14 @@ private boolean matchTerritories( return numberMet >= getTerritoryCount(); } + private boolean checkCheckAIPlayers(final List players) { + boolean bcheck = true; + for (GamePlayer player : players) { + bcheck = (bcheck && player.isAi()); + } + return bcheck; + } + private boolean checkAtWar( final GamePlayer player, final Set enemies, final int count) { int found = CollectionUtils.countMatches(enemies, player::isAtWar); @@ -1057,6 +1089,12 @@ public MutableProperty getPropertyOrNull(String propertyName) { this::setRelationship, this::getRelationship, this::resetRelationship); + case "checkAIPlayer": + return MutableProperty.of( + this::setCheckAIPlayers, + this::setCheckAIPlayers, + this::getCheckAIPlayers, + this::resetCheckAIPlayers); case "atWarPlayers": return MutableProperty.of( this::setAtWarPlayers, From 48f0bd04c3f31bd80a46bc67367b4e1d4eb3efed Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Tue, 26 Sep 2023 11:48:17 +0000 Subject: [PATCH 2/7] Changing checkAIPlayers to isAIPlayers --- .../triplea/attachments/RulesAttachment.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index fd32553ca88..401e4a168aa 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -56,7 +56,7 @@ public class RulesAttachment extends AbstractPlayerRulesAttachment { // condition for having specific relationships private @Nullable List relationship = null; // condition for checking ai player - private boolean checkAIPlayers = false; + private Boolean isAIPlayers = null; // condition for being at war private @Nullable Set atWarPlayers = null; private int atWarCount = -1; @@ -491,20 +491,20 @@ private void resetUnitPresence() { unitPresence = null; } - private void setCheckAIPlayers(final String s) { - checkAIPlayers = getBool(s); + private void setIsAIPlayers(final String s) { + isAIPlayers = (s == null) ? null : getBool(s); } - private void setCheckAIPlayers(final Boolean s) { - checkAIPlayers = s; + private void setIsAIPlayers(final Boolean s) { + isAIPlayers = s; } - public boolean getCheckAIPlayers() { - return checkAIPlayers; + public Boolean getIsAIPlayers() { + return isAIPlayers; } - private void resetCheckAIPlayers() { - checkAIPlayers = false; + private void resetIsAIPlayers() { + isAIPlayers = null; } private int getAtWarCount() { @@ -754,8 +754,8 @@ public boolean isSatisfied( objectiveMet = checkDirectOwnership(listedTerritories, players); } // check for ai controlled player - if (objectiveMet && getCheckAIPlayers()) { - objectiveMet = checkCheckAIPlayers(players); + if (objectiveMet && getIsAIPlayers() != null) { + objectiveMet = checkIsAIPlayers(players); } // get attached to player final GamePlayer playerAttachedTo = (GamePlayer) getAttachedTo(); @@ -1025,10 +1025,11 @@ private boolean matchTerritories( return numberMet >= getTerritoryCount(); } - private boolean checkCheckAIPlayers(final List players) { + private boolean checkIsAIPlayers(final List players) { boolean bcheck = true; for (GamePlayer player : players) { - bcheck = (bcheck && player.isAi()); + // need to retain true/false + bcheck = (bcheck && (getIsAIPlayers() == player.isAi())); } return bcheck; } @@ -1089,12 +1090,12 @@ public MutableProperty getPropertyOrNull(String propertyName) { this::setRelationship, this::getRelationship, this::resetRelationship); - case "checkAIPlayer": + case "isAIPlayers": return MutableProperty.of( - this::setCheckAIPlayers, - this::setCheckAIPlayers, - this::getCheckAIPlayers, - this::resetCheckAIPlayers); + this::setIsAIPlayers, + this::setIsAIPlayers, + this::getIsAIPlayers, + this::resetIsAIPlayers); case "atWarPlayers": return MutableProperty.of( this::setAtWarPlayers, From 605d0fbb344260e98445f259e81cf837f4aa977a Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Wed, 27 Sep 2023 11:21:26 -0400 Subject: [PATCH 3/7] Rename to isAI Rename from isAIPlayers to isAI --- .../triplea/attachments/RulesAttachment.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index 401e4a168aa..b093872f069 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -55,8 +55,8 @@ public class RulesAttachment extends AbstractPlayerRulesAttachment { private int techCount = -1; // condition for having specific relationships private @Nullable List relationship = null; - // condition for checking ai player - private Boolean isAIPlayers = null; + // condition for checking AI player + private Boolean isAI = null; // condition for being at war private @Nullable Set atWarPlayers = null; private int atWarCount = -1; @@ -491,20 +491,20 @@ private void resetUnitPresence() { unitPresence = null; } - private void setIsAIPlayers(final String s) { - isAIPlayers = (s == null) ? null : getBool(s); + private void setIsAI(final String s) { + isAI = (s == null) ? null : getBool(s); } - private void setIsAIPlayers(final Boolean s) { - isAIPlayers = s; + private void setIsAI(final Boolean s) { + isAI = s; } - public Boolean getIsAIPlayers() { - return isAIPlayers; + public Boolean getIsAI() { + return isAI; } - private void resetIsAIPlayers() { - isAIPlayers = null; + private void resetIsAI() { + isAI = null; } private int getAtWarCount() { @@ -753,9 +753,9 @@ public boolean isSatisfied( } objectiveMet = checkDirectOwnership(listedTerritories, players); } - // check for ai controlled player - if (objectiveMet && getIsAIPlayers() != null) { - objectiveMet = checkIsAIPlayers(players); + // check for AI controlled player + if (objectiveMet && getIsAI() != null) { + objectiveMet = checkIsAI(players); } // get attached to player final GamePlayer playerAttachedTo = (GamePlayer) getAttachedTo(); @@ -1025,11 +1025,10 @@ private boolean matchTerritories( return numberMet >= getTerritoryCount(); } - private boolean checkIsAIPlayers(final List players) { + private boolean checkIsAI(final List players) { boolean bcheck = true; for (GamePlayer player : players) { - // need to retain true/false - bcheck = (bcheck && (getIsAIPlayers() == player.isAi())); + bcheck = (bcheck && (getIsAI() == player.isAi())); } return bcheck; } @@ -1090,12 +1089,12 @@ public MutableProperty getPropertyOrNull(String propertyName) { this::setRelationship, this::getRelationship, this::resetRelationship); - case "isAIPlayers": + case "isAI": return MutableProperty.of( - this::setIsAIPlayers, - this::setIsAIPlayers, - this::getIsAIPlayers, - this::resetIsAIPlayers); + this::setIsAI, + this::setIsAI, + this::getIsAI, + this::resetIsAI); case "atWarPlayers": return MutableProperty.of( this::setAtWarPlayers, From a5a985af17f62e66be78837d254558b194155b4b Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Wed, 27 Sep 2023 11:29:29 -0400 Subject: [PATCH 4/7] Spacing check Spacing --- .../games/strategy/triplea/attachments/RulesAttachment.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index b093872f069..7030b0fc654 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -1090,11 +1090,7 @@ public MutableProperty getPropertyOrNull(String propertyName) { this::getRelationship, this::resetRelationship); case "isAI": - return MutableProperty.of( - this::setIsAI, - this::setIsAI, - this::getIsAI, - this::resetIsAI); + return MutableProperty.of(this::setIsAI, this::setIsAI, this::getIsAI, this::resetIsAI); case "atWarPlayers": return MutableProperty.of( this::setAtWarPlayers, From fdafe0c46e50a09e6fdea9ac38e2d058be91d0e3 Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Wed, 27 Sep 2023 22:04:46 -0400 Subject: [PATCH 5/7] Added @Nullable @Nullable added --- .../games/strategy/triplea/attachments/RulesAttachment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index 7030b0fc654..de5c4f901c0 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -56,7 +56,7 @@ public class RulesAttachment extends AbstractPlayerRulesAttachment { // condition for having specific relationships private @Nullable List relationship = null; // condition for checking AI player - private Boolean isAI = null; + private @Nullable Boolean isAI = null; // condition for being at war private @Nullable Set atWarPlayers = null; private int atWarCount = -1; From a207b4a973c05502d4604ead35f87b95add150d3 Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Thu, 28 Sep 2023 08:06:05 -0400 Subject: [PATCH 6/7] Add testing for isAI Adding RulesAttachmentTest to test isAI --- .../triplea/attachments/RulesAttachment.java | 8 +- .../attachments/RulesAttachmentTest.java | 87 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index de5c4f901c0..806e431ff2e 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -37,6 +37,8 @@ import java.util.Set; import java.util.function.Predicate; import javax.annotation.Nullable; + +import org.jetbrains.annotations.VisibleForTesting; import org.triplea.java.Interruptibles; import org.triplea.java.collections.CollectionUtils; import org.triplea.java.collections.IntegerMap; @@ -491,7 +493,8 @@ private void resetUnitPresence() { unitPresence = null; } - private void setIsAI(final String s) { + @VisibleForTesting + public void setIsAI(final String s) { isAI = (s == null) ? null : getBool(s); } @@ -1025,7 +1028,8 @@ private boolean matchTerritories( return numberMet >= getTerritoryCount(); } - private boolean checkIsAI(final List players) { + @VisibleForTesting + public boolean checkIsAI(final List players) { boolean bcheck = true; for (GamePlayer player : players) { bcheck = (bcheck && (getIsAI() == player.isAi())); diff --git a/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java b/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java new file mode 100644 index 00000000000..26ffb6bef6f --- /dev/null +++ b/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java @@ -0,0 +1,87 @@ +package games.strategy.triplea.attachments; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import games.strategy.engine.data.GameData; +import games.strategy.engine.data.GamePlayer; +import games.strategy.engine.data.PlayerList; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class RulesAttachmentTest { + + @Mock private PlayerList playerList; + + private final GameData gameData = mock(GameData.class); + + private final RulesAttachment attachment = new RulesAttachment("Test attachment", null, gameData); + + @Mock private GamePlayer player1; + @Mock private GamePlayer player2; + @Mock private GamePlayer player3; + @Mock private GamePlayer player4; + + @BeforeEach + void setUp() { + when(gameData.getPlayerList()).thenReturn(playerList); + } + + /* Testing stored getIsAI values with setIsAI */ + @Test + void isAITest() { + attachment.setIsAI("true"); + assertTrue(attachment.getIsAI()); + attachment.setIsAI("false"); + assertFalse(attachment.getIsAI()); + } + + /* Testing returned values for checkGetIsAI */ + @Test + void checkGetIsAITest() { + lenient().when(player1.isAi()).thenReturn(false); + lenient().when(player2.isAi()).thenReturn(true); + lenient().when(player3.isAi()).thenReturn(true); + lenient().when(player4.isAi()).thenReturn(false); + + /* Testing with 1 non AI player */ + final List players1 = List.of(player1); + attachment.setIsAI("true"); + assertFalse(attachment.checkIsAI(players1)); + attachment.setIsAI("false"); + assertTrue(attachment.checkIsAI(players1)); + /* Testing with 1 AI player */ + final List players2 = List.of(player2); + attachment.setIsAI("true"); + assertTrue(attachment.checkIsAI(players2)); + attachment.setIsAI("false"); + assertFalse(attachment.checkIsAI(players2)); + /* Testing with 1 non AI player and 1 AI player */ + final List players12 = List.of(player1, player2); + attachment.setIsAI("true"); + assertFalse(attachment.checkIsAI(players12)); + attachment.setIsAI("false"); + assertFalse(attachment.checkIsAI(players12)); + /* Testing with 2 AI players */ + final List players23 = List.of(player2, player3); + attachment.setIsAI("true"); + assertTrue(attachment.checkIsAI(players23)); + attachment.setIsAI("false"); + assertFalse(attachment.checkIsAI(players23)); + /* Testing with 2 non AI players */ + final List players14 = List.of(player1, player4); + attachment.setIsAI("true"); + assertFalse(attachment.checkIsAI(players14)); + attachment.setIsAI("false"); + assertTrue(attachment.checkIsAI(players14)); + } +} From 55ba8b0125df6da2b348b8c6af6ef4232556b92a Mon Sep 17 00:00:00 2001 From: WCSumpton Date: Thu, 28 Sep 2023 08:13:46 -0400 Subject: [PATCH 7/7] Removing extra lines Removed extra line from each file --- .../java/games/strategy/triplea/attachments/RulesAttachment.java | 1 - .../games/strategy/triplea/attachments/RulesAttachmentTest.java | 1 - 2 files changed, 2 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java index 806e431ff2e..7fa82e707ba 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/attachments/RulesAttachment.java @@ -37,7 +37,6 @@ import java.util.Set; import java.util.function.Predicate; import javax.annotation.Nullable; - import org.jetbrains.annotations.VisibleForTesting; import org.triplea.java.Interruptibles; import org.triplea.java.collections.CollectionUtils; diff --git a/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java b/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java index 26ffb6bef6f..23bd588cc55 100644 --- a/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java +++ b/game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java @@ -10,7 +10,6 @@ import games.strategy.engine.data.GamePlayer; import games.strategy.engine.data.PlayerList; import java.util.List; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith;