Match: add extra Event Loop to Match for Events when the Game is closed

This commit is contained in:
Hans Mackowiak
2020-11-28 20:22:45 +01:00
parent 88c017f962
commit 81bfaf2a15
5 changed files with 25 additions and 17 deletions

View File

@@ -1,6 +1,8 @@
package forge.game; package forge.game;
import com.google.common.collect.*; import com.google.common.collect.*;
import com.google.common.eventbus.EventBus;
import forge.LobbyPlayer; import forge.LobbyPlayer;
import forge.deck.CardPool; import forge.deck.CardPool;
import forge.deck.Deck; import forge.deck.Deck;
@@ -8,6 +10,7 @@ import forge.deck.DeckFormat;
import forge.deck.DeckSection; import forge.deck.DeckSection;
import forge.game.card.Card; import forge.game.card.Card;
import forge.game.card.CardCollectionView; import forge.game.card.CardCollectionView;
import forge.game.event.Event;
import forge.game.event.GameEventAnteCardsSelected; import forge.game.event.GameEventAnteCardsSelected;
import forge.game.event.GameEventGameFinished; import forge.game.event.GameEventGameFinished;
import forge.game.player.Player; import forge.game.player.Player;
@@ -29,7 +32,7 @@ public class Match {
private final GameRules rules; private final GameRules rules;
private final String title; 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, Game> runningGames = Maps.newHashMap();
private final Map<Integer, GameOutcome> gameOutcomes = 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 // will pull UI dialog, when the UI is listening
game.fireEvent(new GameEventGameFinished()); game.fireEvent(new GameEventGameFinished());
// FIXME needed to close the Match Dialog because that this moment there isn't any game // FIXME needed to close the Match Dialog because that this moment there isn't any game
lastGame = game;
runningGames.remove(game.getId()); runningGames.remove(game.getId());
//run GC after game is finished //run GC after game is finished
@@ -103,11 +105,7 @@ public class Match {
} }
public Game getGameById(int id) { public Game getGameById(int id) {
if (runningGames.containsKey(id)) { return runningGames.get(id);
return runningGames.get(id);
}
// FIXME fallback for last game in case the UI is outdated
return lastGame;
} }
public GameOutcome getOutcomeById(int 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 // 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);
}
} }

View File

@@ -126,6 +126,8 @@ public class HostedMatch {
title = TextUtil.concatNoSpace("Multiplayer Game (", String.valueOf(sortedPlayers.size()), " players)"); title = TextUtil.concatNoSpace("Multiplayer Game (", String.valueOf(sortedPlayers.size()), " players)");
} }
this.match = new Match(gameRules, sortedPlayers, title); this.match = new Match(gameRules, sortedPlayers, title);
this.match.subscribeToEvents(SoundSystem.instance);
this.match.subscribeToEvents(visitor);
startGame(); startGame();
} }

View File

@@ -269,7 +269,7 @@ public class InputAttack extends InputSyncronizedBase {
combat.addAttacker(card, currentDefender, activeBand); combat.addAttacker(card, currentDefender, activeBand);
activateBand(activeBand); activateBand(activeBand);
card.getGame().fireEvent(new UiEventAttackerDeclared( card.getGame().getMatch().fireEvent(new UiEventAttackerDeclared(
CardView.get(card), CardView.get(card),
GameEntityView.get(currentDefender))); GameEntityView.get(currentDefender)));
} }
@@ -280,7 +280,7 @@ public class InputAttack extends InputSyncronizedBase {
// When removing an attacker clear the attacking band // When removing an attacker clear the attacking band
activateBand(null); activateBand(null);
card.getGame().fireEvent(new UiEventAttackerDeclared( card.getGame().getMatch().fireEvent(new UiEventAttackerDeclared(
CardView.get(card), null)); CardView.get(card), null));
return true; return true;
} }

View File

@@ -122,7 +122,7 @@ public class InputBlock extends InputSyncronizedBase {
boolean isCorrectAction = false; boolean isCorrectAction = false;
if (triggerEvent != null && triggerEvent.getButton() == 3 && card.getController() == defender) { if (triggerEvent != null && triggerEvent.getButton() == 3 && card.getController() == defender) {
combat.removeFromCombat(card); combat.removeFromCombat(card);
card.getGame().fireEvent(new UiEventBlockerAssigned(CardView.get(card), null)); card.getGame().getMatch().fireEvent(new UiEventBlockerAssigned(CardView.get(card), null));
isCorrectAction = true; isCorrectAction = true;
} }
else { else {
@@ -137,14 +137,14 @@ public class InputBlock extends InputSyncronizedBase {
if (combat.isBlocking(card, currentAttacker)) { if (combat.isBlocking(card, currentAttacker)) {
//if creature already blocking current attacker, remove blocker from combat //if creature already blocking current attacker, remove blocker from combat
combat.removeBlockAssignment(currentAttacker, card); 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; isCorrectAction = true;
} }
else { else {
isCorrectAction = CombatUtil.canBlock(currentAttacker, card, combat); isCorrectAction = CombatUtil.canBlock(currentAttacker, card, combat);
if (isCorrectAction) { if (isCorrectAction) {
combat.addBlocker(currentAttacker, card); combat.addBlocker(currentAttacker, card);
card.getGame().fireEvent(new UiEventBlockerAssigned( card.getGame().getMatch().fireEvent(new UiEventBlockerAssigned(
CardView.get(card), CardView.get(card),
CardView.get(currentAttacker))); CardView.get(currentAttacker)));
} }

View File

@@ -2936,11 +2936,7 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont
@Override @Override
public void nextGameDecision(final NextGameDecision decision) { public void nextGameDecision(final NextGameDecision decision) {
Game game = getGame(); gameView.getMatch().fireEvent(new UiEventNextGameDecision(this, decision));
// 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));
}
} }
@Override @Override