WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@frigoref
Copy link
Member

Should fix issue 13580 (Can´t select opponent in battle calculator)

Notes to Reviewer

BattleCalculatorPanel.java

  • new member determineUnitsOnComboSelectionChange
  • Constructor: Add ItemListener for comboBoxes; For swapSidesButton ensure new defender units are for defense

Rest: Comments added

frigoref added 2 commits July 30, 2025 13:23
BattleCalculatorPanel.java
- new member comboListenerEnabled
- Constructor: Add ItemListener for comboBoxes; For swapSidesButton ensure new defender units are for defense
Mainly comments added

BattleCalculatorPanel.java
- new member comboListenerEnabled renamed to determineUnitsOnComboSelectionChange
- methods setAttackerWithUnits/ setDefenderWithUnits: Limit determineUnitsOnComboSelectionChange to set-method for new GamePlayer
@Nullable private final Territory battleSiteTerritory;
private final JList<String> territoryEffectsJList;
private final TuvCostsCalculator tuvCalculator = new TuvCostsCalculator();
private boolean determineUnitsOnComboSelectionChange = true;
Copy link
Member

Choose a reason for hiding this comment

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

What's the rationale behind this variable and its purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is to determine the units displayed when the combo selection changes, i.e., the user has change the attacking or defending player in one of the top combo field.
I thought I finally found a good name for it -.-

@DanVanAtta
Copy link
Member

Did a git bisect to 2046ca7 to find when this problem started. Doing a selective revert of the offending code is this diff, it looks like this patch would solve the issue:

--- a/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java
+++ b/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java
@@ -372,7 +372,19 @@ class BattleCalculatorPanel extends JPanel {
     final JButton closeButton = new JButton("Close");
     buttons.add(closeButton);
     add(buttons);
-
+    defenderCombo.addActionListener(
+        e -> {
+          setDefendingUnits(
+              defendingUnitsPanel.getUnits().stream().anyMatch(Matches.unitIsOwnedBy(getDefender()))
+                  ? defendingUnitsPanel.getUnits()
+                  : List.of());
+          setWidgetActivation();
+        });
+    attackerCombo.addActionListener(
+        e -> {
+          setAttackingUnits(List.of());
+          setWidgetActivation();
+        });
     amphibiousCheckBox.addActionListener(e -> setWidgetActivation());
     landBattleCheckBox.addActionListener(
         e -> {

I suggest we first do a surgical update to get us back to baseline functionality. Then, consider any future changes (which helps mitigate risk in case those updates have issues. It's a bad situation when fixes contain new problems)

@DanVanAtta DanVanAtta assigned frigoref and unassigned DanVanAtta Aug 9, 2025
@DanVanAtta
Copy link
Member

Went ahead and submitted a minimal fix: #13610
@frigoref please rebase and we can discuss the enhancements you have made

@frigoref
Copy link
Member Author

Did a git bisect to 2046ca7 to find when this problem started. Doing a selective revert of the offending code is this diff, it looks like this patch would solve the issue:

--- a/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java
+++ b/game-app/game-core/src/main/java/games/strategy/triplea/odds/calculator/BattleCalculatorPanel.java
@@ -372,7 +372,19 @@ class BattleCalculatorPanel extends JPanel {
     final JButton closeButton = new JButton("Close");
     buttons.add(closeButton);
     add(buttons);
-
+    defenderCombo.addActionListener(
+        e -> {
+          setDefendingUnits(
+              defendingUnitsPanel.getUnits().stream().anyMatch(Matches.unitIsOwnedBy(getDefender()))
+                  ? defendingUnitsPanel.getUnits()
+                  : List.of());
+          setWidgetActivation();
+        });
+    attackerCombo.addActionListener(
+        e -> {
+          setAttackingUnits(List.of());
+          setWidgetActivation();
+        });
     amphibiousCheckBox.addActionListener(e -> setWidgetActivation());
     landBattleCheckBox.addActionListener(
         e -> {

I suggest we first do a surgical update to get us back to baseline functionality. Then, consider any future changes (which helps mitigate risk in case those updates have issues. It's a bad situation when fixes contain new problems)

I recall that there was an issue with the switch concerning the unit types did not switch as well properly, leaving extra icons. When I see this old code you found, I think it is odd as the listener would be called every time (also during initial setup or other buttons that affect the combo button). That's why I introduced variable determineUnitsOnComboSelectionChange, so the listener would only react on user changes.
The listener code itself also seems to not match the one I referred to from the swapSidesButton:

swapSidesButton.addActionListener(
        e -> {
          attackerOrderOfLosses = null;
          defenderOrderOfLosses = null;
          final GamePlayer newAttacker = getDefender();
          final List<Unit> newAttackerUnits =
              CollectionUtils.getMatches(
                  defendingUnitsPanel.getUnits(),
                  Matches.unitIsOwnedBy(getDefender())
                      .and(
                          Matches.unitCanBeInBattle(
                              true,
                              isLandBattle(),
                              1,
                              hasMaxRounds(isLandBattle(), data),
                              true,
                              List.of())));
          final GamePlayer newDefender = getAttacker();
          final List<Unit> newDefenderUnits =
              CollectionUtils.getMatches(
                  attackingUnitsPanel.getUnits(),
                  Matches.unitCanBeInBattle(false, isLandBattle(), 1, true));
          setAttackerWithUnits(newAttacker, newAttackerUnits);
          setDefenderWithUnits(newDefender, newDefenderUnits);
          setWidgetActivation();
        });

All in all, I still thing the PR makes sense the way it is.

@frigoref frigoref assigned DanVanAtta and unassigned frigoref Sep 21, 2025
@DanVanAtta DanVanAtta removed their assignment Nov 23, 2025
@DanVanAtta DanVanAtta self-requested a review November 23, 2025 21:07
Copy link
Member

@DanVanAtta DanVanAtta left a comment

Choose a reason for hiding this comment

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

The determineUnitsOnComboSelectionChange gives me a lot of pause. I'd like to do that in a cleaner way (or at least with some polish if we really can't clean it up).

First though, what concrete fixes are achieving? I think we should step back and look at the high level objective, there might be an easier approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants