Skip to content

Commit

Permalink
#172 Fix wrong number of cards or Jacks during discarding in Ramsch g…
Browse files Browse the repository at this point in the history
…ames
  • Loading branch information
b0n541 committed Feb 22, 2024
1 parent 8a781fa commit 0444fb0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jskat.control.event.skatgame;

/**
* This event is created when a player discards the wrong number of cards.
*/
public class NoJacksAllowedInDiscardedSkatEvent {

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public void actionPerformed(final JSkatActionEvent e) {
pickUpSkat = true;
discardSkat = new CardList(cards);
}
} else {
log.warn("Wrong source for " + command);
interrupt = false;
}
} else if (JSkatAction.ANNOUNCE_GAME.toString().equals(command)) {
if (source instanceof final GameAnnouncement announcement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,13 @@ public void showErrorMessageOn(final InvalidNumberOfCardsInDiscardedSkatEvent ev
strings.getString("invalid_number_of_cards_in_skat_message"));
}

@Subscribe
public void showErrorMessageOn(final NoJacksAllowedInDiscardedSkatEvent event) {

showErrorMessage(strings.getString("no_jacks_allowed_in_schieberamsch_skat_title"),
strings.getString("no_jacks_allowed_in_schieberamsch_skat_message"));
}

@Subscribe
public void showErrorMessageOn(final DuplicateTableNameInputEvent event) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public void actionPerformed(final ActionEvent e) {
announceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
e.setSource(null);

if (getSelectedGameType() != null) {

try {
final GameAnnouncement announcement = getGameAnnouncement();
if (announcement == null) {
Expand All @@ -148,8 +148,6 @@ public void actionPerformed(final ActionEvent e) {
} catch (final IllegalArgumentException except) {
log.error(except.getMessage());
}
} else {
e.setSource(null);
}

// fire event again
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jskat.gui.swing.table;

import org.jskat.control.JSkatEventBus;
import org.jskat.control.event.skatgame.InvalidNumberOfCardsInDiscardedSkatEvent;
import org.jskat.control.event.skatgame.NoJacksAllowedInDiscardedSkatEvent;
import org.jskat.control.gui.action.JSkatAction;
import org.jskat.data.JSkatOptions;
import org.jskat.gui.swing.LayoutFactory;
Expand Down Expand Up @@ -53,42 +56,32 @@ private void initPanel(final ActionMap actions) {
schiebenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
e.setSource(null);
try {
final CardList discardedCards = getDiscardedCards();
if (discardedCards == null) {
return; // no valid announcement
}

e.setSource(discardedCards);
// fire event again
schiebenButton.dispatchEvent(e);
} catch (final IllegalArgumentException except) {
log.error(except.getMessage());
}

// fire event again
schiebenButton.dispatchEvent(e);
}

private CardList getDiscardedCards() {
if (SkatSchiebenPanel.this.discardPanel.isUserLookedIntoSkat()) {
final CardList discardedCards = SkatSchiebenPanel.this.discardPanel.getDiscardedCards();
if (discardedCards.size() != 2) {
JOptionPane
.showMessageDialog(
SkatSchiebenPanel.this,
SkatSchiebenPanel.this.strings.getString("invalid_number_of_cards_in_skat_message"),
SkatSchiebenPanel.this.strings.getString("invalid_number_of_cards_in_skat_title"),
JOptionPane.ERROR_MESSAGE);
JSkatEventBus.INSTANCE.post(new InvalidNumberOfCardsInDiscardedSkatEvent());
return null;
}
if (!JSkatOptions.instance().isSchieberamschJacksInSkat()
&& (discardedCards.get(0).getRank() == Rank.JACK || discardedCards.get(1).getRank() == Rank.JACK)) {
JOptionPane
.showMessageDialog(
SkatSchiebenPanel.this,
// FIXME: should not be checked in GUI
// code
SkatSchiebenPanel.this.strings.getString("no_jacks_allowed_in_schieberamsch_skat_message"),
SkatSchiebenPanel.this.strings.getString("no_jacks_allowed_in_schieberamsch_skat_title"),
JOptionPane.ERROR_MESSAGE);
JSkatEventBus.INSTANCE.post(new NoJacksAllowedInDiscardedSkatEvent());
return null;
}

Expand Down

0 comments on commit 0444fb0

Please sign in to comment.