Skip to content

Commit

Permalink
#172 Separate events for picking up skat and discarding
Browse files Browse the repository at this point in the history
  • Loading branch information
b0n541 committed Mar 17, 2024
1 parent 2012c25 commit c600dce
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 35 deletions.
2 changes: 1 addition & 1 deletion jskat-base/src/main/java/org/jskat/control/SkatGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private void discarding() {
log.info("Player " + activePlayer + " looks into the skat...");
log.info("Skat before discarding: " + data.getSkat());

eventBus.post(new SkatCardsChangedEvent(tableName, data.getSkat()));
eventBus.post(new SkatCardsPickedUpEvent(tableName, data.getSkat()));

final CardList skatBefore = data.getSkat().getImmutableCopy();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jskat.control.event.table;

import org.jskat.util.CardList;

public final class SkatCardsPickedUpEvent extends AbstractTableEvent {

public final CardList cards;

public SkatCardsPickedUpEvent(final String tableName, final CardList cards) {
super(tableName);
this.cards = new CardList(cards);
}

@Override
public String toString() {
return "SkatCardsPickedUpEvent: " + cards;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,15 @@ public void updateISSMove(final String tableName, final SkatGameData gameData, f
case PICK_UP_SKAT:
JSkatEventBus.INSTANCE.post(new SkatGameStateChangedEvent(tableName, GameState.DISCARDING));
if (moveInformation.getSkat().size() == 2) {
JSkatEventBus.INSTANCE.post(new SkatCardsChangedEvent(tableName, moveInformation.getSkat()));
JSkatEventBus.INSTANCE.post(new SkatCardsPickedUpEvent(tableName, moveInformation.getSkat()));
}
break;
case GAME_ANNOUNCEMENT:
JSkatEventBus.INSTANCE.post(new SkatGameStateChangedEvent(tableName, GameState.DECLARING));
JSkatEventBus.INSTANCE.post(new TableGameMoveEvent(tableName,
new GameAnnouncementEvent(movePlayer, moveInformation.getGameAnnouncement())));
JSkatEventBus.INSTANCE.post(new TableGameMoveEvent(tableName, new GameAnnouncementEvent(movePlayer, moveInformation.getGameAnnouncement())));
if (!moveInformation.getGameAnnouncement().contract().hand()) {
JSkatEventBus.INSTANCE.post(new SkatCardsChangedEvent(tableName, moveInformation.getGameAnnouncement().discardedCards()));
}
if (moveInformation.getGameAnnouncement().contract().ouvert()) {
showCardsForPlayer(tableName, movePlayer, moveInformation.getGameAnnouncement().contract().ouvertCards());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class DeclaringContextPanel extends JPanel {
}

public void resetPanel() {

discardPanel.resetPanel();
announcePanel.resetPanel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
* Holds widgets for deciding of looking into skat or playing hand game
*/
class DiscardPanel extends JPanel {

private static final Logger log = LoggerFactory.getLogger(DiscardPanel.class);
private static final String PICK_UP_SKAT_BUTTON = "PICK_UP_SKAT_BUTTON";

private static final String CARD_PANEL = "CARD_PANEL";


private Action pickUpSkatAction;
private JButton pickUpSkatButton;
private boolean userPickedUpSkat = false;
Expand Down Expand Up @@ -57,13 +55,12 @@ void initPanel() {
pickUpSkatAction = getActionMap().get(JSkatAction.PICK_UP_SKAT);
pickUpSkatButton = new JButton(pickUpSkatAction);
pickUpSkatButton.addActionListener(e -> {

log.debug("user picked up skat");
userPickedUpSkat = true;
if (announcePanel != null) {
announcePanel.setUserPickedUpSkat(true);
}
DiscardPanel.this.showPanel(CARD_PANEL);
showPanel(CARD_PANEL);

// fire event again
pickUpSkatButton.dispatchEvent(e);
Expand All @@ -79,12 +76,9 @@ void initPanel() {
}

protected void setSkat(final CardList skat) {

clearCards();
addCard(skat.get(0));
addCard(skat.get(1));

userPickedUpSkat = true;
}

void clearCards() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class SchieberamschContextPanel extends JPanel {
blankPanel.setOpaque(false);
add(blankPanel, "width 25%");

this.centerPanel = new JPanel(new CardLayout());
centerPanel = new JPanel(new CardLayout());
final JPanel grandHandPanel = getGrandHandSchiebeRamschPanel(actions);
this.centerPanel.add(grandHandPanel, GRAND_HAND);
centerPanel.add(grandHandPanel, GRAND_HAND);

discardPanel = new DiscardPanel(actions, 4);
this.centerPanel.add(discardPanel, DISCARD);
centerPanel.add(discardPanel, DISCARD);

this.centerPanel.setOpaque(false);
add(this.centerPanel, "grow");
centerPanel.setOpaque(false);
add(centerPanel, "grow");

add(new SkatSchiebenPanel(actions, discardPanel), "width 25%");

Expand All @@ -61,16 +61,16 @@ public JPanel getGrandHandSchiebeRamschPanel(final ActionMap actions) {
final JPanel result = new JPanel(LayoutFactory.getMigLayout("fill"));

final JPanel question = new JPanel();
final JLabel questionIconLabel = new JLabel(new ImageIcon(this.bitmaps.getUserBidBubble()));
final JLabel questionIconLabel = new JLabel(new ImageIcon(bitmaps.getUserBidBubble()));
question.add(questionIconLabel);
final JLabel questionLabel = new JLabel(this.strings.getString("want_play_grand_hand"));
final JLabel questionLabel = new JLabel(strings.getString("want_play_grand_hand"));
questionLabel.setFont(new Font(Font.DIALOG, Font.BOLD, 16));
question.add(questionLabel);
result.add(question, "center, growx, span 2, wrap");

final JButton grandHandButton = new JButton(actions.get(JSkatAction.PLAY_GRAND_HAND));
grandHandButton.setIcon(new ImageIcon(this.bitmaps.getIconImage(Icon.OK, IconSize.BIG)));
grandHandButton.setText(this.strings.getString("yes"));
grandHandButton.setIcon(new ImageIcon(bitmaps.getIconImage(Icon.OK, IconSize.BIG)));
grandHandButton.setText(strings.getString("yes"));
grandHandButton.addActionListener(event -> {
try {
final GameContract contract = new GameContract(GameType.GRAND).withHand();
Expand All @@ -84,8 +84,8 @@ public JPanel getGrandHandSchiebeRamschPanel(final ActionMap actions) {
});

final JButton schieberamschButton = new JButton(actions.get(JSkatAction.PLAY_SCHIEBERAMSCH));
schieberamschButton.setIcon(new ImageIcon(this.bitmaps.getIconImage(Icon.STOP, IconSize.BIG)));
schieberamschButton.setText(this.strings.getString("no"));
schieberamschButton.setIcon(new ImageIcon(bitmaps.getIconImage(Icon.STOP, IconSize.BIG)));
schieberamschButton.setText(strings.getString("no"));
schieberamschButton.addActionListener(event -> {
try {
showPanel(DISCARD);
Expand Down Expand Up @@ -115,7 +115,7 @@ public void resetPanel() {
}

void showPanel(final String panelName) {
((CardLayout) this.centerPanel.getLayout()).show(this.centerPanel, panelName);
((CardLayout) centerPanel.getLayout()).show(centerPanel, panelName);
}

public void removeCard(final Card card) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
* Holds widgets for announcing a game
*/
class SkatSchiebenPanel extends JPanel {


private static final Logger log = LoggerFactory
.getLogger(SkatSchiebenPanel.class);
private static final Logger log = LoggerFactory.getLogger(SkatSchiebenPanel.class);

JSkatResourceBundle strings;
JSkatOptions options;
Expand All @@ -38,15 +35,15 @@ class SkatSchiebenPanel extends JPanel {
*/
SkatSchiebenPanel(final ActionMap actions, final DiscardPanel discardPanel) {

this.strings = JSkatResourceBundle.INSTANCE;
strings = JSkatResourceBundle.INSTANCE;
this.discardPanel = discardPanel;

initPanel(actions);
}

private void initPanel(final ActionMap actions) {

this.setLayout(LayoutFactory.getMigLayout("fill"));
setLayout(LayoutFactory.getMigLayout("fill"));
setOpaque(false);

final JPanel panel = new JPanel(LayoutFactory.getMigLayout("fill"));
Expand All @@ -73,8 +70,8 @@ public void actionPerformed(final ActionEvent e) {
}

private CardList getDiscardedCards() {
if (SkatSchiebenPanel.this.discardPanel.isUserLookedIntoSkat()) {
final CardList discardedCards = SkatSchiebenPanel.this.discardPanel.getDiscardedCards();
if (discardPanel.isUserLookedIntoSkat()) {
final CardList discardedCards = discardPanel.getDiscardedCards();
if (discardedCards.size() != 2) {
JSkatEventBus.INSTANCE.post(new InvalidNumberOfCardsInDiscardedSkatEvent());
return null;
Expand All @@ -93,6 +90,6 @@ private CardList getDiscardedCards() {
});
panel.add(schiebenButton, "center");

this.add(panel, "center");
add(panel, "center");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,21 @@ public void startGame() {
clearTable();
}

@Subscribe
public void setSkatOn(final SkatCardsPickedUpEvent event) {
setSkat(event.cards);
}

@Subscribe
public void setSkatOn(final SkatCardsChangedEvent event) {
setSkat(event.cards);
}

private void setSkat(final CardList skat) {
if (ramsch) {
schieberamschPanel.setSkat(event.cards);
schieberamschPanel.setSkat(skat);
} else {
declaringPanel.setSkat(event.cards);
declaringPanel.setSkat(skat);
}
}

Expand Down

0 comments on commit c600dce

Please sign in to comment.