-
Notifications
You must be signed in to change notification settings - Fork 86
Implementing tracking for seen action cards by Yssaril #4763
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
Open
Copilot
wants to merge
10
commits into
master
Choose a base branch
from
copilot/track-yssaril-seen-cards
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e6c903
Track known Yssaril action cards
Copilot f807e83
Finalize known action card tracking
Copilot 09c1b47
Address review feedback for known action cards
Copilot d6ce79f
Wire generic action card knowledge tracking
Copilot aec789e
Finalize generic action card knowledge tracking
Copilot 0db3e23
Stabilize known action card storage keys
Copilot 53ad95f
Refine known action card cleanup loop
Copilot 9a90156
Clarify legacy known card migration
Copilot f0b9a6a
Guard known card storage key fallback
Copilot 6296225
Merge branch 'master' into copilot/track-yssaril-seen-cards
wholton 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
154 changes: 154 additions & 0 deletions
154
src/main/java/ti4/service/actioncard/KnownActionCardsService.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package ti4.service.actioncard; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import lombok.experimental.UtilityClass; | ||
| import ti4.game.Game; | ||
| import ti4.game.Player; | ||
| import ti4.helpers.ActionCardHelper; | ||
| import ti4.helpers.StringHelper; | ||
|
|
||
| @UtilityClass | ||
| public class KnownActionCardsService { | ||
|
|
||
| private static final String KNOWN_ACTION_CARDS_PREFIX = "knownActionCards_"; | ||
|
|
||
| public static void rememberViewedHand(Player viewer, Player target) { | ||
| if (viewer == null || target == null || viewer == target) { | ||
| return; | ||
| } | ||
|
|
||
| removeTrackedKnowledge(viewer, target); | ||
| List<String> knownCards = new ArrayList<>(target.getActionCards().keySet()); | ||
| if (!knownCards.isEmpty()) { | ||
| viewer.addToStoredList(getStorageKey(target), knownCards.toArray(String[]::new)); | ||
| } | ||
| } | ||
|
|
||
| public static void rememberShownActionCard(Player viewer, Player target, String actionCardId) { | ||
| if (viewer == null || target == null || viewer == target || isUnset(actionCardId)) { | ||
| return; | ||
| } | ||
|
|
||
| String storageKey = getStorageKey(target); | ||
| if (!viewer.getStoredList(storageKey).contains(actionCardId)) { | ||
| viewer.addToStoredList(storageKey, actionCardId); | ||
| } | ||
| } | ||
|
|
||
| public static void rememberShownActionCardToAll(Game game, Player target, String actionCardId) { | ||
| if (game == null || target == null || isUnset(actionCardId)) { | ||
| return; | ||
| } | ||
|
|
||
| for (Player player : game.getRealPlayers()) { | ||
| rememberShownActionCard(player, target, actionCardId); | ||
| } | ||
| } | ||
|
|
||
| public static void forgetCardFromKnownHands(Game game, Player target, String actionCardId) { | ||
| if (game == null || target == null || actionCardId == null || actionCardId.isBlank()) { | ||
| return; | ||
| } | ||
|
|
||
| List<String> storageKeys = getStorageKeys(target); | ||
| for (Player player : game.getPlayers().values()) { | ||
| if (player == target) { | ||
| continue; | ||
| } | ||
| for (String key : storageKeys) { | ||
| player.removeFromStoredList(key, actionCardId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean shouldShowKnownActionCardsButton(Player player) { | ||
| return player != null | ||
| && (player.hasTech("mi") | ||
| || player.getExhaustedTechs().contains("mi") | ||
| || player.getStoredValueMap().keySet().stream() | ||
| .map(StringHelper::unescape) | ||
| .anyMatch(key -> key.startsWith(KNOWN_ACTION_CARDS_PREFIX))); | ||
| } | ||
|
|
||
| public static String getKnownActionCardsText(Game game, Player viewer) { | ||
| StringBuilder sb = new StringBuilder("__Known action cards in other players' hands__:"); | ||
| boolean hasAnyKnownCards = false; | ||
|
|
||
| for (Player target : game.getRealPlayers()) { | ||
| if (target == viewer) { | ||
| continue; | ||
| } | ||
|
|
||
| List<String> knownCards = getKnownCards(viewer, target); | ||
| if (knownCards.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| hasAnyKnownCards = true; | ||
| sb.append("\n### ").append(target.getRepresentationNoPing()).append('\n'); | ||
| sb.append(ActionCardHelper.actionCardListCondensedNoIds(knownCards, null)) | ||
| .append('\n'); | ||
| } | ||
|
|
||
| if (!hasAnyKnownCards) { | ||
| sb.append("\n> None"); | ||
| } | ||
|
|
||
| return sb.toString().trim(); | ||
| } | ||
|
|
||
| private static String getStorageKey(Player target) { | ||
| String targetId = target.getUserID(); | ||
| if (isUnset(targetId)) { | ||
| targetId = target.getUserName(); | ||
| } | ||
| if (isUnset(targetId)) { | ||
| targetId = "unknownPlayer"; | ||
| } | ||
| return KNOWN_ACTION_CARDS_PREFIX + targetId; | ||
| } | ||
|
|
||
| private static List<String> getStorageKeys(Player target) { | ||
| Set<String> keys = new LinkedHashSet<>(); | ||
| keys.add(getStorageKey(target)); | ||
|
|
||
| // Keep reading/removing the older non-userID keys so tracked knowledge survives migration | ||
| // from the earlier faction/color-based storage format. | ||
| String targetId = target.getFaction(); | ||
| if (isUnset(targetId)) { | ||
| targetId = target.getColor(); | ||
| } | ||
| if (isUnset(targetId)) { | ||
| targetId = target.getUserID(); | ||
| } | ||
| if (isUnset(targetId)) { | ||
| targetId = target.getUserName(); | ||
| } | ||
| if (isUnset(targetId)) { | ||
| targetId = "unknownPlayer"; | ||
| } | ||
| keys.add(KNOWN_ACTION_CARDS_PREFIX + targetId); | ||
| return new ArrayList<>(keys); | ||
| } | ||
|
|
||
| private static List<String> getKnownCards(Player viewer, Player target) { | ||
| Set<String> knownCards = new LinkedHashSet<>(); | ||
| for (String key : getStorageKeys(target)) { | ||
| knownCards.addAll(viewer.getStoredList(key)); | ||
| } | ||
| return new ArrayList<>(knownCards); | ||
| } | ||
|
|
||
| private static void removeTrackedKnowledge(Player viewer, Player target) { | ||
| for (String key : getStorageKeys(target)) { | ||
| viewer.removeStoredValue(key); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isUnset(String value) { | ||
| return value == null || value.isBlank() || "null".equalsIgnoreCase(value); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
This command still mutates the source player’s hand by calling
player.setActionCard(acID)earlier inexecute().setActionCardadds a new copy of the card (new identifier), so using /show_ac will duplicate the shown card instead of just revealing it. Remove thatsetActionCardcall (and any other hand mutation) so the command is read-only aside from tracking viaKnownActionCardsService.