Search in sources :

Example 1 with CPlayer

use of com.gianlu.pyxreborn.Models.Client.CPlayer in project PretendYoureXyzzyReborn by devgianlu.

the class GameUI method onMessage.

@Override
public void onMessage(Events event, JsonObject obj) {
    switch(event) {
        case GAME_CHAT:
        case NEW_GAME:
        case NEW_USER:
        case GAME_REMOVED:
        case USER_LEFT:
        case CHAT:
            // Not interested
            return;
        case GAME_NEW_PLAYER:
            Platform.runLater(() -> playersList.add(new CPlayer(obj.getAsJsonObject(Fields.PLAYER.toString()))));
            break;
        case GAME_PLAYER_LEFT:
            Platform.runLater(() -> {
                CPlayer toRemove = Utils.find(playersList, obj.get(Fields.NICKNAME.toString()).getAsString());
                if (toRemove != null)
                    playersList.remove(toRemove);
            });
            break;
        case GAME_HAND_CHANGED:
            List<WhiteCard> whiteCards = Utils.toList(obj.getAsJsonArray(Fields.HAND.toString()), WhiteCard.class);
            Platform.runLater(() -> {
                this.hand.getChildren().clear();
                for (WhiteCard card : whiteCards) this.hand.getChildren().add(new PyxCardGroup(Collections.singletonList(card), this));
            });
            break;
        case GAME_NEW_ROUND:
            game.status = Game.Status.PLAYING;
            judge = new CPlayer(obj.getAsJsonObject(Fields.JUDGE.toString()));
            BlackCard blackCard = new BlackCard(obj.getAsJsonObject(Fields.BLACK_CARD.toString()));
            Platform.runLater(() -> {
                this.blackCard.getChildren().clear();
                this.blackCard.getChildren().add(new PyxCard(blackCard, null));
                this.playedCards.getChildren().clear();
                if (!isSpectating()) {
                    if (Objects.equals(judge.user, me)) {
                        // I am the judge
                        instructions.setText("You're the Card Czar.");
                        hand.setDisable(true);
                    } else {
                        // I am not the judge
                        instructions.setText("Pick " + blackCard.numPick + " card(s) to play...");
                        hand.setDisable(false);
                    }
                } else {
                    instructions.setText("You're a spectator.");
                }
            });
            break;
        case GAME_JUDGING:
            game.status = Game.Status.JUDGING;
            List<List<WhiteCard>> playedCards = new ArrayList<>();
            for (JsonElement element : obj.getAsJsonArray(Fields.PLAYED_CARDS.toString())) playedCards.add(Utils.toList(element.getAsJsonArray(), WhiteCard.class));
            Platform.runLater(() -> {
                this.hand.getChildren().clear();
                this.playedCards.getChildren().clear();
                for (List<WhiteCard> cards : playedCards) {
                    PyxCardGroup group = new PyxCardGroup(cards, this);
                    this.playedCards.getChildren().add(group);
                }
            });
            break;
        case GAME_ROUND_ENDED:
            int winningCard = obj.get(Fields.WINNER_CARD_ID.toString()).getAsInt();
            CPlayer winner = new CPlayer(obj.getAsJsonObject(Fields.WINNER.toString()));
            Platform.runLater(() -> {
                instructions.setText(winner.user.nickname + " wins this round!");
                for (Node child : this.playedCards.getChildren()) if (child instanceof PyxCardGroup)
                    for (WhiteCard card : ((PyxCardGroup) child).cards) if (card.id == winningCard)
                        ((PyxCardGroup) child).setWinning();
            });
            break;
        case GAME_STOPPED:
            game.status = Game.Status.LOBBY;
            Platform.runLater(() -> {
                this.blackCard.getChildren().clear();
                this.playedCards.getChildren().clear();
                this.hand.getChildren().clear();
            });
            break;
        case GAME_OPTIONS_CHANGED:
            game.options = new CGame.Options(obj.getAsJsonObject(Fields.OPTIONS.toString()));
            break;
        case GAME_PLAYER_STATUS_CHANGED:
            CPlayer player = new CPlayer(obj.getAsJsonObject(Fields.PLAYER.toString()));
            Platform.runLater(() -> {
                int index = playersList.indexOf(player);
                if (index != -1)
                    playersList.set(index, player);
            });
            if (Objects.equals(player.user, me) && player.status == Player.Status.WAITING) {
                Platform.runLater(() -> {
                    if (!isSpectating()) {
                        instructions.setText("Waiting for other players...");
                        hand.setDisable(true);
                    } else {
                        instructions.setText("You're a spectator.");
                    }
                });
            }
            break;
    }
}
Also used : CPlayer(com.gianlu.pyxreborn.Models.Client.CPlayer) CGame(com.gianlu.pyxreborn.Models.Client.CGame) Node(javafx.scene.Node) ArrayList(java.util.ArrayList) PyxCardGroup(com.gianlu.pyxreborn.client.UI.Card.PyxCardGroup) JsonElement(com.google.gson.JsonElement) PyxCard(com.gianlu.pyxreborn.client.UI.Card.PyxCard) ArrayList(java.util.ArrayList) List(java.util.List) ObservableList(javafx.collections.ObservableList)

Aggregations

CGame (com.gianlu.pyxreborn.Models.Client.CGame)1 CPlayer (com.gianlu.pyxreborn.Models.Client.CPlayer)1 PyxCard (com.gianlu.pyxreborn.client.UI.Card.PyxCard)1 PyxCardGroup (com.gianlu.pyxreborn.client.UI.Card.PyxCardGroup)1 JsonElement (com.google.gson.JsonElement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ObservableList (javafx.collections.ObservableList)1 Node (javafx.scene.Node)1