Search in sources :

Example 11 with PyxException

use of com.gianlu.pyxreborn.Exceptions.PyxException in project PretendYoureXyzzyReborn by devgianlu.

the class PyxClientAdapter method sendMessageBlocking.

public JsonObject sendMessageBlocking(JsonObject req) throws InterruptedException, PyxException {
    JsonElement id = req.get(Fields.ID.toString());
    if (id == null)
        throw new IllegalArgumentException("The request should contain an id!");
    requests.put(id.getAsInt(), new IMessageBlocking() {

        @Override
        public void onMessage(JsonObject resp) {
        }

        @Override
        public void onException(Exception ex) {
        }
    });
    send(req.toString());
    synchronized (waitingForResponse) {
        waitingForResponse.wait();
        if (blockingResp == null)
            throw blockingEx;
        return blockingResp;
    }
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) InvalidHandshakeException(org.java_websocket.exceptions.InvalidHandshakeException) PyxException(com.gianlu.pyxreborn.Exceptions.PyxException)

Example 12 with PyxException

use of com.gianlu.pyxreborn.Exceptions.PyxException in project PretendYoureXyzzyReborn by devgianlu.

the class GameChatUI method send.

@Override
protected void send(MouseEvent event) {
    JsonObject req = client.createRequest(Operations.GAME_CHAT);
    req.addProperty(Fields.TEXT.toString(), message.getText());
    req.addProperty(Fields.GID.toString(), gameId);
    try {
        client.sendMessageBlocking(req);
    } catch (InterruptedException | PyxException ex) {
        UIClient.notifyException(ex);
        return;
    }
    message.setText(null);
}
Also used : JsonObject(com.google.gson.JsonObject) PyxException(com.gianlu.pyxreborn.Exceptions.PyxException)

Example 13 with PyxException

use of com.gianlu.pyxreborn.Exceptions.PyxException in project PretendYoureXyzzyReborn by devgianlu.

the class GameOptionsUI method apply.

@FXML
public void apply(MouseEvent event) {
    JsonObject obj = client.createRequest(Operations.CHANGE_GAME_OPTIONS);
    obj.addProperty(Fields.GID.toString(), game.gid);
    obj.add(Fields.OPTIONS.toString(), newOptions.toJson());
    try {
        client.sendMessageBlocking(obj);
    } catch (InterruptedException | PyxException ex) {
        UIClient.notifyException(ex);
        return;
    }
    stage.close();
}
Also used : JsonObject(com.google.gson.JsonObject) PyxException(com.gianlu.pyxreborn.Exceptions.PyxException) FXML(javafx.fxml.FXML)

Example 14 with PyxException

use of com.gianlu.pyxreborn.Exceptions.PyxException in project PretendYoureXyzzyReborn by devgianlu.

the class GameUI method initialize.

@FXML
public void initialize() {
    JsonObject obj = client.createRequest(Operations.GET_GAME);
    obj.addProperty(Fields.GID.toString(), game.gid);
    try {
        game = new CGame(client.sendMessageBlocking(obj).getAsJsonObject(Fields.GAME.toString()));
    } catch (InterruptedException | PyxException ex) {
        UIClient.notifyException(ex);
        return;
    }
    if (Objects.equals(me.nickname, game.host.nickname)) {
        // I am the host
        if (game.status == Game.Status.LOBBY)
            startGame.setVisible(true);
        else
            startGame.setVisible(false);
    } else {
        // I am not the host
        startGame.setVisible(false);
    }
    spectators.setCellFactory(param -> new UserCell());
    spectators.setItems(spectatorsList);
    players.setCellFactory(param -> new PlayerCell());
    players.setItems(playersList);
    client.addListener((event, request) -> true, this);
    if (isSpectating())
        instructions.setText("You're a spectator!");
}
Also used : CGame(com.gianlu.pyxreborn.Models.Client.CGame) PlayerCell(com.gianlu.pyxreborn.client.UI.ListCells.PlayerCell) JsonObject(com.google.gson.JsonObject) PyxException(com.gianlu.pyxreborn.Exceptions.PyxException) UserCell(com.gianlu.pyxreborn.client.UI.ListCells.UserCell) FXML(javafx.fxml.FXML)

Example 15 with PyxException

use of com.gianlu.pyxreborn.Exceptions.PyxException in project PretendYoureXyzzyReborn by devgianlu.

the class RegisterUI method register.

@FXML
public void register() {
    String nickname = this.nickname.getText();
    try {
        Client client = new Client(new URI(address.getText()), nickname, null, admin.isSelected() ? adminCode.getText() : null);
        if (client.connectBlocking()) {
            CUser me;
            try {
                me = new CUser(client.sendMessageBlocking(client.createRequest(Operations.GET_ME)).getAsJsonObject(Fields.USER.toString()));
            } catch (PyxException ex) {
                UIClient.notifyException(ex);
                return;
            }
            stage.close();
            GlobalChatUI.show(client);
            MainUI.show(client, me);
        } else {
            new Alert(Alert.AlertType.ERROR, "Failed connecting! Nickname may be invalid or server may be full.").show();
        }
    } catch (InterruptedException | URISyntaxException ex) {
        UIClient.notifyException(ex);
    }
}
Also used : CUser(com.gianlu.pyxreborn.Models.Client.CUser) Alert(javafx.scene.control.Alert) PyxException(com.gianlu.pyxreborn.Exceptions.PyxException) URISyntaxException(java.net.URISyntaxException) Client(com.gianlu.pyxreborn.client.Client) URI(java.net.URI) FXML(javafx.fxml.FXML)

Aggregations

PyxException (com.gianlu.pyxreborn.Exceptions.PyxException)15 JsonObject (com.google.gson.JsonObject)12 FXML (javafx.fxml.FXML)10 CGame (com.gianlu.pyxreborn.Models.Client.CGame)3 CUser (com.gianlu.pyxreborn.Models.Client.CUser)2 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 ObservableListWrapper (com.sun.javafx.collections.ObservableListWrapper)2 ChoiceAnswer (com.gianlu.consoleui.Choice.ChoiceAnswer)1 ListChoicePrompt (com.gianlu.consoleui.Choice.List.ListChoicePrompt)1 CCompactCardSet (com.gianlu.pyxreborn.Models.Client.CCompactCardSet)1 Operations (com.gianlu.pyxreborn.Operations)1 Client (com.gianlu.pyxreborn.client.Client)1 PyxCardGroup (com.gianlu.pyxreborn.client.UI.Card.PyxCardGroup)1 CheckboxCardSetCell (com.gianlu.pyxreborn.client.UI.ListCells.CheckboxCardSetCell)1 PlayerCell (com.gianlu.pyxreborn.client.UI.ListCells.PlayerCell)1 UserCell (com.gianlu.pyxreborn.client.UI.ListCells.UserCell)1 NumberStringFilteredConverter (com.gianlu.pyxreborn.client.UI.NumberStringFilteredConverter)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1