Search in sources :

Example 1 with MageException

use of mage.interfaces.MageException 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)

Aggregations

NotBoundException (java.rmi.NotBoundException)1 RemoteException (java.rmi.RemoteException)1 LocateRegistry (java.rmi.registry.LocateRegistry)1 Registry (java.rmi.registry.Registry)1 MageException (mage.interfaces.MageException)1 CallbackClient (mage.interfaces.callback.CallbackClient)1 CallbackClientDaemon (mage.interfaces.callback.CallbackClientDaemon)1 ClientCallback (mage.interfaces.callback.ClientCallback)1