Search in sources :

Example 1 with GamePanel

use of mage.client.game.GamePanel in project mage by magefree.

the class CallbackClientImpl method processCallback.

@Override
public synchronized void processCallback(final ClientCallback callback) {
    callback.decompressData();
    SaveObjectUtil.saveObject(callback.getData(), callback.getMethod().toString());
    SwingUtilities.invokeLater(() -> {
        try {
            logger.debug(callback.getMessageId() + " -- " + callback.getMethod());
            switch(callback.getMethod()) {
                case START_GAME:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        GameManager.instance.setCurrentPlayerUUID(message.getPlayerId());
                        gameStarted(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case START_TOURNAMENT:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        tournamentStarted(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case START_DRAFT:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        draftStarted(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case REPLAY_GAME:
                    {
                        replayGame(callback.getObjectId());
                        break;
                    }
                case SHOW_TOURNAMENT:
                    {
                        showTournament(callback.getObjectId());
                        break;
                    }
                case WATCHGAME:
                    {
                        watchGame(callback.getObjectId());
                        break;
                    }
                case CHATMESSAGE:
                    {
                        ChatMessage message = (ChatMessage) callback.getData();
                        // Drop messages from ignored users
                        if (message.getUsername() != null && IgnoreList.IGNORED_MESSAGE_TYPES.contains(message.getMessageType())) {
                            final String serverAddress = SessionHandler.getSession().getServerHostname().orElse("");
                            if (IgnoreList.userIsIgnored(serverAddress, message.getUsername())) {
                                break;
                            }
                        }
                        ChatPanelBasic panel = MageFrame.getChat(callback.getObjectId());
                        if (panel != null) {
                            // play the sound related to the message
                            if (message.getSoundToPlay() != null) {
                                switch(message.getSoundToPlay()) {
                                    case PlayerLeft:
                                        AudioManager.playPlayerLeft();
                                        break;
                                    case PlayerQuitTournament:
                                        AudioManager.playPlayerQuitTournament();
                                        break;
                                    case PlayerSubmittedDeck:
                                        AudioManager.playPlayerSubmittedDeck();
                                        break;
                                    case PlayerWhispered:
                                        AudioManager.playPlayerWhispered();
                                        break;
                                }
                            }
                            // send start message to chat if not done yet
                            if (!panel.isStartMessageDone()) {
                                createChatStartMessage(panel);
                            }
                            // send the message to subchat if exists and it's not a game message
                            if (message.getMessageType() != MessageType.GAME && panel.getConnectedChat() != null) {
                                panel.getConnectedChat().receiveMessage(message.getUsername(), message.getMessage(), message.getTime(), message.getTurnInfo(), message.getMessageType(), ChatMessage.MessageColor.BLACK);
                            } else {
                                panel.receiveMessage(message.getUsername(), message.getMessage(), message.getTime(), message.getTurnInfo(), message.getMessageType(), message.getColor());
                            }
                        }
                        break;
                    }
                case SERVER_MESSAGE:
                    {
                        if (callback.getData() != null) {
                            ChatMessage message = (ChatMessage) callback.getData();
                            if (message.getColor() == ChatMessage.MessageColor.RED) {
                                JOptionPane.showMessageDialog(null, message.getMessage(), "Server message", JOptionPane.WARNING_MESSAGE);
                            } else {
                                JOptionPane.showMessageDialog(null, message.getMessage(), "Server message", JOptionPane.INFORMATION_MESSAGE);
                            }
                        }
                        break;
                    }
                case JOINED_TABLE:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        joinedTable(message.getRoomId(), message.getTableId(), message.getFlag());
                        break;
                    }
                case REPLAY_INIT:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.init((GameView) callback.getData());
                        }
                        break;
                    }
                case REPLAY_DONE:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.endMessage(null, null, (String) callback.getData(), callback.getMessageId());
                        }
                        break;
                    }
                case REPLAY_UPDATE:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.updateGame((GameView) callback.getData());
                        }
                        break;
                    }
                case GAME_INIT:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_INIT", callback.getObjectId(), callback.getData());
                            panel.init((GameView) callback.getData());
                        }
                        break;
                    }
                case GAME_OVER:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            Session session = SessionHandler.getSession();
                            if (session.isJsonLogActive()) {
                                UUID gameId = callback.getObjectId();
                                appendJsonEvent("GAME_OVER", callback.getObjectId(), message);
                                String logFileName = "game-" + gameId + ".json";
                                S3Uploader.upload(logFileName, gameId.toString());
                            }
                            panel.endMessage(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_ERROR:
                    {
                        frame.showErrorDialog("Game Error", (String) callback.getData());
                        break;
                    }
                case GAME_ASK:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_ASK", callback.getObjectId(), message);
                            panel.ask(message.getMessage(), message.getGameView(), callback.getMessageId(), message.getOptions());
                        }
                        break;
                    }
                case GAME_TARGET:
                    {
                        // e.g. Pick triggered ability
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_TARGET", callback.getObjectId(), message);
                            panel.pickTarget(message.getGameView(), message.getOptions(), message.getMessage(), message.getCardsView1(), message.getTargets(), message.isFlag(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_SELECT:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_SELECT", callback.getObjectId(), message);
                            panel.select(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_CHOOSE_ABILITY:
                    {
                        AbilityPickerView abilityPickerView = (AbilityPickerView) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_CHOOSE_ABILITY", callback.getObjectId(), callback.getData());
                            panel.pickAbility(abilityPickerView.getGameView(), null, abilityPickerView);
                        }
                        break;
                    }
                case GAME_CHOOSE_PILE:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_CHOOSE_PILE", callback.getObjectId(), message);
                            panel.pickPile(message.getGameView(), message.getOptions(), message.getMessage(), message.getCardsView1(), message.getCardsView2());
                        }
                        break;
                    }
                case GAME_CHOOSE_CHOICE:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_CHOOSE_CHOICE", callback.getObjectId(), message);
                            panel.getChoice(message.getGameView(), message.getOptions(), message.getChoice(), callback.getObjectId());
                        }
                        break;
                    }
                case GAME_PLAY_MANA:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_PLAY_MANA", callback.getObjectId(), message);
                            panel.playMana(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_PLAY_XMANA:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_PLAY_XMANA", callback.getObjectId(), message);
                            panel.playXMana(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_GET_AMOUNT:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_GET_AMOUNT", callback.getObjectId(), message);
                            panel.getAmount(message.getGameView(), message.getOptions(), message.getMin(), message.getMax(), message.getMessage());
                        }
                        break;
                    }
                case GAME_GET_MULTI_AMOUNT:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_GET_MULTI_AMOUNT", callback.getObjectId(), message);
                            panel.getMultiAmount(message.getMessages(), message.getGameView(), message.getOptions(), message.getMin(), message.getMax());
                        }
                        break;
                    }
                case GAME_UPDATE:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_UPDATE", callback.getObjectId(), callback.getData());
                            // update after undo wtf?!
                            panel.updateGame((GameView) callback.getData(), true, null, null);
                        }
                        break;
                    }
                case GAME_REDRAW_GUI:
                    {
                        // re-draw game's gui elements like attack arrows
                        // uses for client side only (example: update after scrollbars support)
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.updateGame();
                        }
                        break;
                    }
                case END_GAME_INFO:
                    {
                        MageFrame.getInstance().showGameEndDialog((GameEndView) callback.getData());
                        break;
                    }
                case SHOW_USERMESSAGE:
                    {
                        List<String> messageData = (List<String>) callback.getData();
                        if (messageData.size() == 2) {
                            JOptionPane.showMessageDialog(null, messageData.get(1), messageData.get(0), JOptionPane.WARNING_MESSAGE);
                        }
                        break;
                    }
                case GAME_INFORM:
                    {
                        if (callback.getMessageId() > gameInformMessageId) {
                            {
                                GameClientMessage message = (GameClientMessage) callback.getData();
                                GamePanel panel = MageFrame.getGame(callback.getObjectId());
                                if (panel != null) {
                                    appendJsonEvent("GAME_INFORM", callback.getObjectId(), message);
                                    panel.inform(message.getMessage(), message.getGameView(), callback.getMessageId());
                                }
                            }
                        // no longer needed because phase skip handling on server side now
                        } else {
                            logger.warn(new StringBuilder("message out of sequence - ignoring").append("MessageId = ").append(callback.getMessageId()).append(" method = ").append(callback.getMethod()));
                        }
                        gameInformMessageId = messageId;
                        break;
                    }
                case GAME_INFORM_PERSONAL:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            JOptionPane.showMessageDialog(panel, message.getMessage(), "Game message", JOptionPane.INFORMATION_MESSAGE);
                        }
                        break;
                    }
                case SIDEBOARD:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        DeckView deckView = message.getDeck();
                        Deck deck = DeckUtil.construct(deckView);
                        if (message.getFlag()) {
                            construct(deck, message.getTableId(), message.getTime());
                        } else {
                            sideboard(deck, message.getTableId(), message.getTime());
                        }
                        break;
                    }
                case VIEW_LIMITED_DECK:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        DeckView deckView = message.getDeck();
                        Deck deck = DeckUtil.construct(deckView);
                        viewLimitedDeck(deck, message.getTableId(), message.getTime());
                        break;
                    }
                case VIEW_SIDEBOARD:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        viewSideboard(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case CONSTRUCT:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        DeckView deckView = message.getDeck();
                        Deck deck = DeckUtil.construct(deckView);
                        construct(deck, message.getTableId(), message.getTime());
                        break;
                    }
                case DRAFT_OVER:
                    {
                        MageFrame.removeDraft(callback.getObjectId());
                        break;
                    }
                case DRAFT_INIT:
                case DRAFT_PICK:
                    {
                        DraftClientMessage message = (DraftClientMessage) callback.getData();
                        DraftPanel panel = MageFrame.getDraft(callback.getObjectId());
                        if (panel != null) {
                            panel.loadBooster(message.getDraftPickView());
                        }
                        break;
                    }
                case DRAFT_UPDATE:
                    {
                        DraftPanel panel = MageFrame.getDraft(callback.getObjectId());
                        DraftClientMessage message = (DraftClientMessage) callback.getData();
                        if (panel != null) {
                            panel.updateDraft(message.getDraftView());
                        }
                        break;
                    }
                case TOURNAMENT_INIT:
                    {
                        break;
                    }
                case USER_REQUEST_DIALOG:
                    {
                        frame.showUserRequestDialog((UserRequestMessage) callback.getData());
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            // sync message for server side events only
            if (!callback.getMethod().isClientSideMessage()) {
                messageId = callback.getMessageId();
            }
        } catch (Exception ex) {
            handleException(ex);
        }
    });
}
Also used : ChatPanelBasic(mage.client.chat.ChatPanelBasic) Deck(mage.cards.decks.Deck) GamePanel(mage.client.game.GamePanel) DraftPanel(mage.client.draft.DraftPanel) IgnoreList(mage.client.util.IgnoreList) List(java.util.List) UUID(java.util.UUID) Session(mage.remote.Session)

Example 2 with GamePanel

use of mage.client.game.GamePanel in project mage by magefree.

the class MagePaneMenuItem method sendUserReplay.

public void sendUserReplay(PlayerAction playerAction, UserRequestMessage userRequestMessage) {
    switch(playerAction) {
        case CLIENT_DOWNLOAD_SYMBOLS:
            Plugins.instance.downloadSymbols();
            break;
        case CLIENT_DOWNLOAD_CARD_IMAGES:
            DownloadPicturesService.startDownload();
            break;
        case CLIENT_DISCONNECT:
            if (SessionHandler.isConnected()) {
                SessionHandler.disconnect(false);
            }
            tablesPane.clearChat();
            showMessage("You have disconnected");
            setWindowTitle();
            break;
        case CLIENT_QUIT_TOURNAMENT:
            SessionHandler.quitTournament(userRequestMessage.getTournamentId());
            break;
        case CLIENT_QUIT_DRAFT_TOURNAMENT:
            SessionHandler.quitDraft(userRequestMessage.getTournamentId());
            MageFrame.removeDraft(userRequestMessage.getTournamentId());
            break;
        case CLIENT_CONCEDE_GAME:
            SessionHandler.sendPlayerAction(PlayerAction.CONCEDE, userRequestMessage.getGameId(), null);
            break;
        case CLIENT_CONCEDE_MATCH:
            SessionHandler.quitMatch(userRequestMessage.getGameId());
            break;
        case CLIENT_STOP_WATCHING:
            SessionHandler.stopWatching(userRequestMessage.getGameId());
            GamePanel gamePanel = getGame(userRequestMessage.getGameId());
            if (gamePanel != null) {
                gamePanel.removeGame();
            }
            removeGame(userRequestMessage.getGameId());
            break;
        case CLIENT_EXIT:
            if (SessionHandler.isConnected()) {
                SessionHandler.disconnect(false);
            }
            CardRepository.instance.closeDB();
            tablesPane.cleanUp();
            Plugins.instance.shutdown();
            dispose();
            System.exit(0);
            break;
        case CLIENT_REMOVE_TABLE:
            SessionHandler.removeTable(userRequestMessage.getRoomId(), userRequestMessage.getTableId());
            break;
        case CLIENT_RECONNECT:
            if (performConnect(true)) {
                enableButtons();
            }
            break;
        case CLIENT_REPLAY_ACTION:
            SessionHandler.stopReplay(userRequestMessage.getGameId());
            break;
        default:
            if (SessionHandler.getSession() != null && playerAction != null) {
                SessionHandler.sendPlayerAction(playerAction, userRequestMessage.getGameId(), userRequestMessage.getRelatedUserId());
            }
    }
}
Also used : GamePanel(mage.client.game.GamePanel)

Example 3 with GamePanel

use of mage.client.game.GamePanel in project mage by magefree.

the class GameEndDialog method saveGameLog.

private void saveGameLog(GameEndView gameEndView) {
    String dir = "gamelogs";
    File saveDir = new File(dir);
    // Here comes the existence check
    if (!saveDir.exists()) {
        saveDir.mkdirs();
    }
    // get game log
    try {
        if (gameEndView.getMatchView().getGames().size() > 0) {
            GamePanel gamePanel = MageFrame.getGame(gameEndView.getMatchView().getGames().get(gameEndView.getMatchView().getGames().size() - 1));
            if (gamePanel != null) {
                SimpleDateFormat sdf = new SimpleDateFormat();
                sdf.applyPattern("yyyyMMdd_HHmmss");
                String fileName = new StringBuilder(dir).append(File.separator).append(sdf.format(gameEndView.getStartTime())).append('_').append(gameEndView.getMatchView().getGameType()).append('_').append(gameEndView.getMatchView().getGames().size()).append(".txt").toString();
                PrintWriter out = new PrintWriter(fileName);
                out.print(gamePanel.getGameLog());
                out.close();
            }
        }
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, "Error while writing game log to file\n\n" + ex, "Error writing gamelog", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : GamePanel(mage.client.game.GamePanel) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter)

Aggregations

GamePanel (mage.client.game.GamePanel)3 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 UUID (java.util.UUID)1 Deck (mage.cards.decks.Deck)1 ChatPanelBasic (mage.client.chat.ChatPanelBasic)1 DraftPanel (mage.client.draft.DraftPanel)1 IgnoreList (mage.client.util.IgnoreList)1 Session (mage.remote.Session)1