client.ReceivingData.java Source code

Java tutorial

Introduction

Here is the source code for client.ReceivingData.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Andrzej Ressel (jereksel@gmail.com)
 * Copyright (c) 2014 Mateusz Kaleciski
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package client;

import client.gui.MainPanel;
import client.gui.MyDialog;
import server.Card;
import server.Helpers;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.swing.JLabel;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

public class ReceivingData extends Thread {

    private final BufferedReader in;

    public ReceivingData(final BufferedReader bufferedReader) {
        in = bufferedReader;
    }

    @Override
    public void run() {

        String result;
        JSONArray jsonMainArr;
        JSONObject childJSONObject;

        try {
            while (GlobalVariables.daemon) {

                result = in.readLine();
                System.out.println("[DEBUG] Got: " + result);

                jsonMainArr = new JSONArray(result);

                String action = jsonMainArr.getJSONObject(0).getString("action");

                switch (action) {

                case "bet_change":
                    final String nick = jsonMainArr.getJSONObject(1).getString("player");
                    final int bet = jsonMainArr.getJSONObject(1).getInt("bet");
                    final int chips = jsonMainArr.getJSONObject(1).getInt("chips");

                    for (final Player player : GlobalVariables.players) {
                        if (player.getNick().equals(nick)) {
                            player.setBet(bet);
                            player.setChips(chips);
                        }
                    }

                    break;

                case "first_connection":
                    GlobalVariables.startChips = jsonMainArr.getJSONObject(1).getInt("chips");
                    GlobalVariables.smallBlind = jsonMainArr.getJSONObject(2).getInt("chips");
                    GlobalVariables.bigBlind = jsonMainArr.getJSONObject(3).getInt("chips");
                    ((MainPanel) GlobalVariables.panel).gameData();
                    break;

                case "cards":

                    for (int i = 1; i < jsonMainArr.length(); i++) {
                        childJSONObject = jsonMainArr.getJSONObject(i);
                        final String rank = childJSONObject.getString("rank");
                        final String suit = childJSONObject.getString("suit");
                        GlobalVariables.hand[i - 1] = new Card(suit, rank);
                    }

                    GlobalVariables.panel.repaint();
                    break;

                case "exchange_cards":

                    GlobalVariables.hand = new Card[4];

                    for (int i = 1; i < jsonMainArr.length(); i++) {
                        childJSONObject = jsonMainArr.getJSONObject(i);
                        final String rank = childJSONObject.getString("rank");
                        final String suit = childJSONObject.getString("suit");
                        GlobalVariables.hand[i - 1] = new Card(suit, rank);
                    }

                    GlobalVariables.panel.repaint();
                    break;

                case "players_info":

                    ((MainPanel) GlobalVariables.panel).clean();

                    final JLabel[] playerPositions = ((MainPanel) GlobalVariables.panel)
                            .getPlayerPositions(jsonMainArr.length() - 1);
                    String[] otherPlayers = new String[jsonMainArr.length() - 1];
                    Integer[] chipsArray = new Integer[jsonMainArr.length() - 1];
                    String dealerPlayer = "";

                    GlobalVariables.players = new ArrayList<>();

                    for (int i = 1; i < jsonMainArr.length(); i++) {
                        otherPlayers[i - 1] = jsonMainArr.getJSONObject(i).getString("player");
                        chipsArray[i - 1] = jsonMainArr.getJSONObject(i).getInt("chips");

                        if (jsonMainArr.getJSONObject(i).getBoolean("dealer")) {
                            dealerPlayer = otherPlayers[i - 1];
                        }
                    }

                    while (!(otherPlayers[0].equals(GlobalVariables.getMyNick()))) {
                        Helpers.shiftRight(otherPlayers, 1);
                        Helpers.shiftRight(chipsArray, 1);
                    }

                    for (int i = 0; i < jsonMainArr.length() - 1; i++) {
                        if (dealerPlayer.equals(otherPlayers[i])) {
                            GlobalVariables.players
                                    .add(new Player(playerPositions[i], otherPlayers[i], chipsArray[i], true));
                        } else {
                            GlobalVariables.players
                                    .add(new Player(playerPositions[i], otherPlayers[i], chipsArray[i], false));
                        }
                    }

                    GlobalVariables.me = GlobalVariables.players.get(0);

                    (GlobalVariables.buttonPanel).hideAll();

                    break;

                case "available_choices":

                    (GlobalVariables.buttonPanel).hideAll();

                    for (int i = 1; i < jsonMainArr.length(); i++) {
                        childJSONObject = jsonMainArr.getJSONObject(i);
                        final String choice = childJSONObject.getString("choice");
                        GlobalVariables.buttonPanel.showButton(choice);
                    }

                    break;

                case "pot":
                    GlobalVariables.pot = jsonMainArr.getJSONObject(1).getInt("pot_size");
                    GlobalVariables.panel.repaint();
                    break;

                case "game_already_started":
                    new MyDialog("Game already started").setVisible(true);
                    throw new SocketException();

                case "you_lost":
                    new MyDialog("You Lost").setVisible(true);
                    throw new SocketException();

                case "you_won":
                    new MyDialog("You Won").setVisible(true);
                    throw new SocketException();

                case "the_same_nick":
                    new MyDialog("Please choose another nick").setVisible(true);
                    throw new SocketException();

                case "message":
                    final String message = jsonMainArr.getJSONObject(1).getString("message");
                    new MyDialog(message).setVisible(true);
                    break;

                case "connection_successful":
                    new MyDialog("Connection successful").setVisible(true);
                    break;

                case "show_cards":
                    final String cards = jsonMainArr.getJSONObject(1).getString("cards");
                    new MyDialog(cards).setVisible(true);
                    break;

                default:
                    System.out.println("[FATAL] Unknown command" + action);
                }

            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }

        new MyDialog("Lost connection with server").setVisible(true);
        GlobalVariables.daemon = false;
        GlobalVariables.connected = false;
        (GlobalVariables.buttonPanel).hideAll();

        for (final Player player : GlobalVariables.players) {
            player.setNick("");
        }

        GlobalVariables.bigBlind = 0;
        GlobalVariables.smallBlind = 0;
        GlobalVariables.me.setNick("ME");
        GlobalVariables.connect.setEnabled(true);
        GlobalVariables.chosen = new boolean[] { false, false, false, false };
        GlobalVariables.coordinates = new int[4][4];
        GlobalVariables.pot = 0;
        GlobalVariables.hand = new Card[4];
        GlobalVariables.panel.repaint();
        ((MainPanel) GlobalVariables.panel).gameData();

    }
}