Skip to content

Commit

Permalink
tests: added runtime check for wrong usage of inform messages inside …
Browse files Browse the repository at this point in the history
…layer effects (disabled by default, related to #13259, #11285)
  • Loading branch information
JayDi85 committed Feb 1, 2025
1 parent bbd0fa2 commit d757923
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions Mage/src/main/java/mage/game/GameImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,7 @@ public void fireInformEvent(String message) {
if (simulation) {
return;
}
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.INFO, message, this);
}

Expand All @@ -3177,6 +3178,7 @@ public void fireStatusEvent(String message, boolean withTime, boolean withTurnIn
if (simulation) {
return;
}
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.STATUS, message, withTime, withTurnInfo, this);
}

Expand All @@ -3185,7 +3187,7 @@ public void fireUpdatePlayersEvent() {
if (simulation) {
return;
}
logger.trace("fireUpdatePlayersEvent");
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.UPDATE, null, this);
getState().clearLookedAt();
getState().clearRevealed();
Expand All @@ -3196,15 +3198,27 @@ public void fireGameEndInfo() {
if (simulation) {
return;
}
logger.trace("fireGameEndIfo");
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.END_GAME_INFO, null, this);
}

@Override
public void fireErrorEvent(String message, Exception ex) {
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.ERROR, message, ex, this);
}

private void makeSureCalledOutsideLayersEffects() {
// very slow, enable/comment it for debug or load/stability tests only
// TODO: enable check and remove/rework all wrong usages
if (true) return;
Arrays.stream(Thread.currentThread().getStackTrace()).forEach(e -> {
if (e.toString().contains("GameState.applyEffects")) {
throw new IllegalStateException("Wrong code usage: client side events can't be called from layers effects (wrong informPlayers usage?");
}
});
}

@Override
public Players getPlayers() {
return state.getPlayers();
Expand Down Expand Up @@ -3871,20 +3885,23 @@ public void setStartMessage(String startMessage) {
@Override
public void initTimer(UUID playerId) {
if (priorityTime > 0) {
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.INIT_TIMER, playerId, null, this);
}
}

@Override
public void resumeTimer(UUID playerId) {
if (priorityTime > 0) {
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.RESUME_TIMER, playerId, null, this);
}
}

@Override
public void pauseTimer(UUID playerId) {
if (priorityTime > 0) {
makeSureCalledOutsideLayersEffects();
tableEventSource.fireTableEvent(EventType.PAUSE_TIMER, playerId, null, this);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Mage/src/main/java/mage/game/events/TableEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public TableEvent(EventType eventType, String message, boolean withTime, Game ga

public TableEvent(EventType eventType, String message, boolean withTime, boolean withTurnInfo, Game game) {
super(game);
this.game = game;
this.game = game; // TODO: potentially bugged and need game copy? See related makeSureCalledOutsideLayersEffects
this.message = message;
this.eventType = eventType;
this.withTime = withTime;
Expand Down

0 comments on commit d757923

Please sign in to comment.