Search in sources :

Example 6 with ClientCallback

use of mage.interfaces.callback.ClientCallback in project mage by magefree.

the class GameSessionPlayer method requestPermissionToRollbackTurn.

public void requestPermissionToRollbackTurn(UUID requestingUserId, int numberTurns) {
    if (!killed) {
        Optional<User> requestingUser = userManager.getUser(requestingUserId);
        Optional<User> requestedUser = userManager.getUser(userId);
        if (requestedUser.isPresent() && requestingUser.isPresent()) {
            String message;
            switch(numberTurns) {
                case 0:
                    message = "Allow rollback to the start of the current turn?";
                    break;
                case 1:
                    message = "Allow rollback to the start of the previous turn?";
                    break;
                default:
                    message = "Allow to rollback " + numberTurns + " turns?";
            }
            UserRequestMessage userRequestMessage = new UserRequestMessage("Request by " + requestedUser.get().getName(), message);
            userRequestMessage.setRelatedUser(requestingUserId, requestingUser.get().getName());
            userRequestMessage.setGameId(game.getId());
            userRequestMessage.setButton1("Accept", PlayerAction.ADD_PERMISSION_TO_ROLLBACK_TURN);
            userRequestMessage.setButton2("Deny", PlayerAction.DENY_PERMISSON_TO_ROLLBACK_TURN);
            requestedUser.get().fireCallback(new ClientCallback(ClientCallbackMethod.USER_REQUEST_DIALOG, game.getId(), userRequestMessage));
        }
    }
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) User(mage.server.User)

Example 7 with ClientCallback

use of mage.interfaces.callback.ClientCallback in project mage by magefree.

the class DraftSession method pickCard.

public void pickCard(int timeout) {
    if (!killed) {
        setupTimeout(timeout);
        managerFactory.userManager().getUser(userId).ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_PICK, draft.getId(), new DraftClientMessage(getDraftView(), getDraftPickView(timeout)))));
    }
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) DraftClientMessage(mage.view.DraftClientMessage)

Example 8 with ClientCallback

use of mage.interfaces.callback.ClientCallback in project mage by magefree.

the class GameSessionWatcher method gameOver.

public void gameOver(final String message) {
    if (!killed) {
        userManager.getUser(userId).ifPresent(user -> {
            user.removeGameWatchInfo(game.getId());
            user.fireCallback(new ClientCallback(ClientCallbackMethod.GAME_OVER, game.getId(), new GameClientMessage(getGameView(), null, message)));
        });
    }
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) GameClientMessage(mage.view.GameClientMessage)

Example 9 with ClientCallback

use of mage.interfaces.callback.ClientCallback in project mage by magefree.

the class MageBase method connect.

public static void connect(String userName, String serverName, int port) {
    try {
        System.setSecurityManager(null);
        Registry reg = LocateRegistry.getRegistry(serverName, port);
        server = (Server) reg.lookup("mage-server");
        sessionId = server.registerClient(userName, UUID.randomUUID());
        CallbackClient client = new CallbackClient() {

            @Override
            public void processCallback(ClientCallback callback) {
                logger.log(Level.INFO, "IN >> {0} - {1}", new Object[] { callback.getMessageId(), callback.getMethod() });
                try {
                    if (callback.getMethod().equals("startGame")) {
                        TableClientMessage data = (TableClientMessage) callback.getData();
                        gameId = data.getGameId();
                        playerId = data.getPlayerId();
                        server.joinGame(gameId, sessionId);
                    } else if (callback.getMethod().equals("gameInit")) {
                        server.ack("gameInit", sessionId);
                    } else if (callback.getMethod().equals("gameAsk")) {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        logger.log(Level.INFO, "ASK >> {0}", message.getMessage());
                        if (message.getMessage().equals("Do you want to take a mulligan?")) {
                            server.sendPlayerBoolean(gameId, sessionId, false);
                        }
                        synchronized (syncStart) {
                            syncStart.notify();
                        }
                    } else if (callback.getMethod().equals("gameTarget")) {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        logger.log(Level.INFO, "TARGET >> {0} >> {1}", new Object[] { message.getMessage(), message.getTargets() });
                        if (message.getMessage().equals("Select a starting player")) {
                            logger.log(Level.INFO, "  Sending >> {0}", playerId);
                            server.sendPlayerUUID(gameId, sessionId, playerId);
                        }
                    } else if (callback.getMethod().equals("gameSelect")) {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        logger.log(Level.INFO, "SELECT >> {0}", message.getMessage());
                        if (phaseToWait == null) {
                            synchronized (sync) {
                                sync.wait();
                            }
                        }
                        if (!message.getMessage().startsWith(phaseToWait)) {
                            server.sendPlayerBoolean(gameId, sessionId, false);
                        } else {
                            phaseToWait = null;
                        }
                    /*if (!message.getMessage().startsWith("Precombat Main - play spells and sorceries.")) {
                                server.sendPlayerBoolean(gameId, sessionId, false);
                            } else {
                                if (cardPlayed == null) {
                                    CardsView cards = message.getGameView().getHand();
                                    CardView landToPlay = null;
                                    for (CardView card : cards.values()) {
                                        //System.out.println(card.getName());
                                        if (card.getName().equals("Plains") || card.getName().equals("Island")) {
                                            landToPlay = card;
                                        }
                                    }
                                    if (landToPlay != null) {
                                        logger.info("Playing " + landToPlay);
                                        server.sendPlayerUUID(gameId, sessionId, landToPlay.getId());
                                        cardPlayed = landToPlay;
                                    } else {
                                        logger.warning("Couldn't find land to play");
                                    }
                                } else {
                                    logger.info("Checking battlefield...");
                                    boolean foundPlayer = false;
                                    boolean foundLand = false;
                                    for (PlayerView player: message.getGameView().getPlayers()) {
                                        if (player.getPlayerId().equals(playerId)) {
                                            foundPlayer = true;
                                            for (PermanentView permanent : player.getBattlefield().values()) {
                                                if (permanent.getId().equals(cardPlayed.getId())) {
                                                    foundLand = true;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    logger.info("  found player: " + foundPlayer);
                                    logger.info("  found land: " + foundLand);
                                    System.exit(0);
                                }

                            }  */
                    }
                } catch (Exception e) {
                    logger.info(e.getMessage());
                }
            }
        };
        callbackDaemon = new CallbackClientDaemon(sessionId, client, server);
        serverState = server.getServerState();
    } catch (MageException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (RemoteException ex) {
        logger.log(Level.SEVERE, "Unable connect to server - ", ex);
    } catch (NotBoundException ex) {
        logger.log(Level.SEVERE, "Unable connect to server - ", ex);
    }
}
Also used : CallbackClient(mage.interfaces.callback.CallbackClient) ClientCallback(mage.interfaces.callback.ClientCallback) MageException(mage.interfaces.MageException) NotBoundException(java.rmi.NotBoundException) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) CallbackClientDaemon(mage.interfaces.callback.CallbackClientDaemon) RemoteException(java.rmi.RemoteException) MageException(mage.interfaces.MageException) RemoteException(java.rmi.RemoteException) NotBoundException(java.rmi.NotBoundException)

Example 10 with ClientCallback

use of mage.interfaces.callback.ClientCallback in project mage by magefree.

the class LocalCommands method displayLocalCommandResponse.

private static void displayLocalCommandResponse(UUID chatId, String response) {
    final String text = new StringBuilder().append("<font color=yellow>").append(response).append("</font>").toString();
    ClientCallback chatMessage = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage("", text, new Date(), null, ChatMessage.MessageColor.BLUE));
    MageFrame.getInstance().processCallback(chatMessage);
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) ChatMessage(mage.view.ChatMessage)

Aggregations

ClientCallback (mage.interfaces.callback.ClientCallback)14 User (mage.server.User)3 ChatMessage (mage.view.ChatMessage)2 DraftClientMessage (mage.view.DraftClientMessage)2 NotBoundException (java.rmi.NotBoundException)1 RemoteException (java.rmi.RemoteException)1 LocateRegistry (java.rmi.registry.LocateRegistry)1 Registry (java.rmi.registry.Registry)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MageException (mage.MageException)1 Constants (mage.constants.Constants)1 MageException (mage.interfaces.MageException)1