Skip to content

Commit

Permalink
network: reworked client-server events (related to triggers dialog pr…
Browse files Browse the repository at this point in the history
…oblem from #11173) (#11189)

* added bad connection mode to test client works on slow network, use -Dxmage.badconnection;
* added bad connection protection in events processing due event type;
* split events to different types (can be ignored, must be synced, etc);
* removed some unused server events.
  • Loading branch information
JayDi85 committed Sep 21, 2023
1 parent fa8e93a commit 342979a
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 202 deletions.
5 changes: 5 additions & 0 deletions Mage.Client/src/main/java/mage/client/MageFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ public void windowClosing(WindowEvent e) {
LOGGER.fatal(null, ex);
}

// other settings
if (ClientCallback.SIMULATE_BAD_CONNECTION) {
LOGGER.info("Network: bad connection mode enabled");
}

// DATA PREPARE
RepositoryUtil.bootstrapLocalDb();
// re-create database on empty (e.g. after new build cleaned db on startup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public enum FeedbackMode {
private FeedbackMode mode;
private MageDialog connectedDialog;
private ChatPanelBasic connectedChatPanel;
private int lastMessageId;
private Map<String, Serializable> lastOptions = new HashMap<>();

private static final ScheduledExecutorService WORKER = Executors.newSingleThreadScheduledExecutor();
Expand Down Expand Up @@ -66,14 +65,8 @@ private void setGUISize() {
}

public void prepareFeedback(FeedbackMode mode, String message, boolean special, Map<String, Serializable> options,
int messageId, boolean gameNeedUserFeedback, TurnPhase gameTurnPhase) {
boolean gameNeedUserFeedback, TurnPhase gameTurnPhase) {
synchronized (this) {
if (messageId < this.lastMessageId) {
// if too many warning messages here then look at GAME_REDRAW_GUI event logic
LOGGER.warn("catch un-synced message from later source (possible reason: connection or performance problems): " + messageId + ", text=" + message);
return;
}
this.lastMessageId = messageId;
this.lastOptions = options;
this.mode = mode;
}
Expand Down
80 changes: 41 additions & 39 deletions Mage.Client/src/main/java/mage/client/game/GamePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private enum PopUpMenuType {
private JPopupMenu popupMenuTriggerOrder;

// keep game data for updates/re-draws
// warning, it keeps updates from GAME_UPDATE events only and ignore another events with GameView
static class LastGameData {
int messageId;
GameView game;
boolean showPlayable;
Map<String, Serializable> options;
Expand Down Expand Up @@ -571,7 +571,7 @@ public void removeGame() {
}
}

public synchronized void init(GameView game) {
public synchronized void init(int messageId, GameView game) {
addPlayers(game);
// default menu states
setMenuStates(
Expand All @@ -581,7 +581,7 @@ public synchronized void init(GameView game) {
holdingPriority
);

updateGame(game);
updateGame(messageId, game);
}

private void addPlayers(GameView game) {
Expand Down Expand Up @@ -706,12 +706,12 @@ private void addPlayers(GameView game) {
*/
}

public synchronized void updateGame(GameView game) {
updateGame(game, false, null, null);
public synchronized void updateGame(int messageId, GameView game) {
updateGame(messageId, game, false, null, null);
}

public synchronized void updateGame(GameView game, boolean showPlayable, Map<String, Serializable> options, Set<UUID> targets) {
keepLastGameData(game, showPlayable, options, targets);
public synchronized void updateGame(int messageId, GameView game, boolean showPlayable, Map<String, Serializable> options, Set<UUID> targets) {
keepLastGameData(messageId, game, showPlayable, options, targets);
prepareSelectableView();
updateGame();
}
Expand Down Expand Up @@ -946,6 +946,7 @@ public synchronized void updateGame() {
}
}

//logger.info("game update, message = " + lastGameData.messageId + ", options = " + lastGameData.options + ", priority = " + lastGameData.game.getPriorityPlayerName());
feedbackPanel.disableUndo();
feedbackPanel.updateOptions(lastGameData.options);

Expand Down Expand Up @@ -1361,12 +1362,13 @@ private void removeClosedCardInfoWindows(Map<String, CardInfoWindowDialog> windo
windowMap.entrySet().removeIf(entry -> entry.getValue().isClosed());
}

public void ask(String question, GameView gameView, int messageId, Map<String, Serializable> options) {
updateGame(gameView, false, options, null);
this.feedbackPanel.prepareFeedback(FeedbackMode.QUESTION, question, false, options, messageId, true, gameView.getPhase());
public void ask(int messageId, GameView gameView, String question, Map<String, Serializable> options) {
updateGame(messageId, gameView, false, options, null);
this.feedbackPanel.prepareFeedback(FeedbackMode.QUESTION, question, false, options, true, gameView.getPhase());
}

private void keepLastGameData(GameView game, boolean showPlayable, Map<String, Serializable> options, Set<UUID> targets) {
private void keepLastGameData(int messageId, GameView game, boolean showPlayable, Map<String, Serializable> options, Set<UUID> targets) {
lastGameData.messageId = messageId;
lastGameData.game = game;
lastGameData.showPlayable = showPlayable;
lastGameData.options = options;
Expand Down Expand Up @@ -1623,8 +1625,8 @@ private void prepareSelectableWindows(
* @param options
* @param messageId
*/
public void pickTarget(GameView gameView, Map<String, Serializable> options, String message, CardsView cardsView, Set<UUID> targets, boolean required, int messageId) {
updateGame(gameView, false, options, targets);
public void pickTarget(int messageId, GameView gameView, Map<String, Serializable> options, String message, CardsView cardsView, Set<UUID> targets, boolean required) {
updateGame(messageId, gameView, false, options, targets);
hideAll();
DialogManager.getManager(gameId).fadeOut();
clearPickTargetDialogs();
Expand Down Expand Up @@ -1652,28 +1654,28 @@ public void pickTarget(GameView gameView, Map<String, Serializable> options, Str
dialog = prepareCardsDialog(message, cardsView, required, options0, popupMenuType);
options0.put("dialog", dialog);
}
this.feedbackPanel.prepareFeedback(required ? FeedbackMode.INFORM : FeedbackMode.CANCEL, message, gameView.getSpecial(), options0, messageId, true, gameView.getPhase());
this.feedbackPanel.prepareFeedback(required ? FeedbackMode.INFORM : FeedbackMode.CANCEL, message, gameView.getSpecial(), options0, true, gameView.getPhase());
if (dialog != null) {
this.pickTarget.add(dialog);
}
}

public void inform(String information, GameView gameView, int messageId) {
updateGame(gameView);
this.feedbackPanel.prepareFeedback(FeedbackMode.INFORM, information, gameView.getSpecial(), null, messageId, false, gameView.getPhase());
public void inform(int messageId, GameView gameView, String information) {
updateGame(messageId, gameView);
this.feedbackPanel.prepareFeedback(FeedbackMode.INFORM, information, gameView.getSpecial(), null, false, gameView.getPhase());
}

public void endMessage(GameView gameView, Map<String, Serializable> options, String message, int messageId) {
updateGame(gameView, false, options, null);
public void endMessage(int messageId, GameView gameView, Map<String, Serializable> options, String message) {
updateGame(messageId, gameView, false, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

this.feedbackPanel.prepareFeedback(FeedbackMode.END, message, false, null, messageId, true, null);
this.feedbackPanel.prepareFeedback(FeedbackMode.END, message, false, null, true, null);
ArrowBuilder.getBuilder().removeAllArrows(gameId);
}

public void select(GameView gameView, Map<String, Serializable> options, String message, int messageId) {
updateGame(gameView, true, options, null);
public void select(int messageId, GameView gameView, Map<String, Serializable> options, String message) {
updateGame(messageId, gameView, true, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

Expand Down Expand Up @@ -1716,31 +1718,31 @@ public void select(GameView gameView, Map<String, Serializable> options, String
priorityPlayerText = " / priority " + gameView.getPriorityPlayerName();
}
String messageToDisplay = message + FeedbackPanel.getSmallText(activePlayerText + " / " + gameView.getStep().toString() + priorityPlayerText);
this.feedbackPanel.prepareFeedback(FeedbackMode.SELECT, messageToDisplay, gameView.getSpecial(), panelOptions, messageId, true, gameView.getPhase());
this.feedbackPanel.prepareFeedback(FeedbackMode.SELECT, messageToDisplay, gameView.getSpecial(), panelOptions, true, gameView.getPhase());
}

public void playMana(GameView gameView, Map<String, Serializable> options, String message, int messageId) {
updateGame(gameView, true, options, null);
public void playMana(int messageId, GameView gameView, Map<String, Serializable> options, String message) {
updateGame(messageId, gameView, true, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

this.feedbackPanel.prepareFeedback(FeedbackMode.CANCEL, message, gameView.getSpecial(), options, messageId, true, gameView.getPhase());
this.feedbackPanel.prepareFeedback(FeedbackMode.CANCEL, message, gameView.getSpecial(), options, true, gameView.getPhase());
}

public void playXMana(GameView gameView, Map<String, Serializable> options, String message, int messageId) {
updateGame(gameView, true, options, null);
public void playXMana(int messageId, GameView gameView, Map<String, Serializable> options, String message) {
updateGame(messageId, gameView, true, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

this.feedbackPanel.prepareFeedback(FeedbackMode.CONFIRM, message, gameView.getSpecial(), null, messageId, true, gameView.getPhase());
this.feedbackPanel.prepareFeedback(FeedbackMode.CONFIRM, message, gameView.getSpecial(), null, true, gameView.getPhase());
}

public void replayMessage(String message) {
//TODO: implement this
}

public void pickAbility(GameView gameView, Map<String, Serializable> options, AbilityPickerView choices) {
updateGame(gameView, false, options, null);
public void pickAbility(int messageId, GameView gameView, Map<String, Serializable> options, AbilityPickerView choices) {
updateGame(messageId, gameView, false, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

Expand All @@ -1764,8 +1766,8 @@ private ShowCardsDialog prepareCardsDialog(String title, CardsView cards, boolea
return showCards;
}

public void getAmount(GameView gameView, Map<String, Serializable> options, int min, int max, String message) {
updateGame(gameView, false, options, null);
public void getAmount(int messageId, GameView gameView, Map<String, Serializable> options, int min, int max, String message) {
updateGame(messageId, gameView, false, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

Expand All @@ -1777,18 +1779,18 @@ public void getAmount(GameView gameView, Map<String, Serializable> options, int
}
}

public void getMultiAmount(List<MultiAmountMessage> messages, GameView gameView, Map<String, Serializable> options,
public void getMultiAmount(int messageId, GameView gameView, List<MultiAmountMessage> messages, Map<String, Serializable> options,
int min, int max) {
updateGame(gameView, false, options, null);
updateGame(messageId, gameView, false, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

pickMultiNumber.showDialog(messages, min, max, lastGameData.options);
SessionHandler.sendPlayerString(gameId, pickMultiNumber.getMultiAmount());
}

public void getChoice(GameView gameView, Map<String, Serializable> options, Choice choice, UUID objectId) {
updateGame(gameView, false, options, null);
public void getChoice(int messageId, GameView gameView, Map<String, Serializable> options, Choice choice, UUID objectId) {
updateGame(messageId, gameView, false, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

Expand All @@ -1813,8 +1815,8 @@ public void getChoice(GameView gameView, Map<String, Serializable> options, Choi
pickChoice.removeDialog();
}

public void pickPile(GameView gameView, Map<String, Serializable> options, String message, CardsView pile1, CardsView pile2) {
updateGame(gameView, false, options, null);
public void pickPile(int messageId, GameView gameView, Map<String, Serializable> options, String message, CardsView pile1, CardsView pile2) {
updateGame(messageId, gameView, false, options, null);
hideAll();
DialogManager.getManager(gameId).fadeOut();

Expand Down

0 comments on commit 342979a

Please sign in to comment.