diff --git a/forge-game/src/main/java/forge/game/Match.java b/forge-game/src/main/java/forge/game/Match.java index 9ab296b45c4..a391819e691 100644 --- a/forge-game/src/main/java/forge/game/Match.java +++ b/forge-game/src/main/java/forge/game/Match.java @@ -1,6 +1,8 @@ package forge.game; import com.google.common.collect.*; +import com.google.common.eventbus.EventBus; + import forge.LobbyPlayer; import forge.deck.CardPool; import forge.deck.Deck; @@ -8,6 +10,7 @@ import forge.deck.DeckFormat; import forge.deck.DeckSection; import forge.game.card.Card; import forge.game.card.CardCollectionView; +import forge.game.event.Event; import forge.game.event.GameEventAnteCardsSelected; import forge.game.event.GameEventGameFinished; import forge.game.player.Player; @@ -29,7 +32,7 @@ public class Match { private final GameRules rules; private final String title; - private Game lastGame = null; + private final EventBus events = new EventBus("match events"); private final Map runningGames = Maps.newHashMap(); private final Map gameOutcomes = Maps.newHashMap(); @@ -95,7 +98,6 @@ public class Match { // will pull UI dialog, when the UI is listening game.fireEvent(new GameEventGameFinished()); // FIXME needed to close the Match Dialog because that this moment there isn't any game - lastGame = game; runningGames.remove(game.getId()); //run GC after game is finished @@ -103,11 +105,7 @@ public class Match { } public Game getGameById(int id) { - if (runningGames.containsKey(id)) { - return runningGames.get(id); - } - // FIXME fallback for last game in case the UI is outdated - return lastGame; + return runningGames.get(id); } public GameOutcome getOutcomeById(int id) { @@ -405,4 +403,16 @@ public class Match { // Other game types (like Quest) need to do something in their own calls to actually update data } } + + /** + * Fire only the events after they became real for gamestate and won't get replaced.
+ * The events are sent to UI, log and sound system. Network listeners are under development. + */ + public void fireEvent(final Event event) { + events.post(event); + } + public void subscribeToEvents(final Object subscriber) { + events.register(subscriber); + } + } diff --git a/forge-gui/src/main/java/forge/match/HostedMatch.java b/forge-gui/src/main/java/forge/match/HostedMatch.java index 6478c2b78db..690b13a56c0 100644 --- a/forge-gui/src/main/java/forge/match/HostedMatch.java +++ b/forge-gui/src/main/java/forge/match/HostedMatch.java @@ -126,6 +126,8 @@ public class HostedMatch { title = TextUtil.concatNoSpace("Multiplayer Game (", String.valueOf(sortedPlayers.size()), " players)"); } this.match = new Match(gameRules, sortedPlayers, title); + this.match.subscribeToEvents(SoundSystem.instance); + this.match.subscribeToEvents(visitor); startGame(); } diff --git a/forge-gui/src/main/java/forge/match/input/InputAttack.java b/forge-gui/src/main/java/forge/match/input/InputAttack.java index 5552774c24a..17e9e78f310 100644 --- a/forge-gui/src/main/java/forge/match/input/InputAttack.java +++ b/forge-gui/src/main/java/forge/match/input/InputAttack.java @@ -269,7 +269,7 @@ public class InputAttack extends InputSyncronizedBase { combat.addAttacker(card, currentDefender, activeBand); activateBand(activeBand); - card.getGame().fireEvent(new UiEventAttackerDeclared( + card.getGame().getMatch().fireEvent(new UiEventAttackerDeclared( CardView.get(card), GameEntityView.get(currentDefender))); } @@ -280,7 +280,7 @@ public class InputAttack extends InputSyncronizedBase { // When removing an attacker clear the attacking band activateBand(null); - card.getGame().fireEvent(new UiEventAttackerDeclared( + card.getGame().getMatch().fireEvent(new UiEventAttackerDeclared( CardView.get(card), null)); return true; } diff --git a/forge-gui/src/main/java/forge/match/input/InputBlock.java b/forge-gui/src/main/java/forge/match/input/InputBlock.java index dfc9977468e..aaf64e05194 100644 --- a/forge-gui/src/main/java/forge/match/input/InputBlock.java +++ b/forge-gui/src/main/java/forge/match/input/InputBlock.java @@ -122,7 +122,7 @@ public class InputBlock extends InputSyncronizedBase { boolean isCorrectAction = false; if (triggerEvent != null && triggerEvent.getButton() == 3 && card.getController() == defender) { combat.removeFromCombat(card); - card.getGame().fireEvent(new UiEventBlockerAssigned(CardView.get(card), null)); + card.getGame().getMatch().fireEvent(new UiEventBlockerAssigned(CardView.get(card), null)); isCorrectAction = true; } else { @@ -137,14 +137,14 @@ public class InputBlock extends InputSyncronizedBase { if (combat.isBlocking(card, currentAttacker)) { //if creature already blocking current attacker, remove blocker from combat combat.removeBlockAssignment(currentAttacker, card); - card.getGame().fireEvent(new UiEventBlockerAssigned(CardView.get(card), null)); + card.getGame().getMatch().fireEvent(new UiEventBlockerAssigned(CardView.get(card), null)); isCorrectAction = true; } else { isCorrectAction = CombatUtil.canBlock(currentAttacker, card, combat); if (isCorrectAction) { combat.addBlocker(currentAttacker, card); - card.getGame().fireEvent(new UiEventBlockerAssigned( + card.getGame().getMatch().fireEvent(new UiEventBlockerAssigned( CardView.get(card), CardView.get(currentAttacker))); } diff --git a/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java b/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java index a08cac623d0..0470bbf6eb6 100644 --- a/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java +++ b/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java @@ -2936,11 +2936,7 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont @Override public void nextGameDecision(final NextGameDecision decision) { - Game game = getGame(); - // in case the game ended before the button is pressed, then match doesn't remember the game anymore - if (game != null) { - game.fireEvent(new UiEventNextGameDecision(this, decision)); - } + gameView.getMatch().fireEvent(new UiEventNextGameDecision(this, decision)); } @Override