mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Match: add extra Event Loop to Match for Events when the Game is closed
This commit is contained in:
@@ -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<Integer, Game> runningGames = Maps.newHashMap();
|
||||
private final Map<Integer, GameOutcome> 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.<br>
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user