tubessister.KonsesusPaxos.java Source code

Java tutorial

Introduction

Here is the source code for tubessister.KonsesusPaxos.java

Source

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tubessister;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.SwingWorker;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/*import static tubessister.GameClient.acceptTimeout;
import static tubessister.GameClient.cur_day;
import static tubessister.GameClient.cur_phase;
import static tubessister.GameClient.current_method;
import static tubessister.GameClient.failPrepareProposal;
import static tubessister.GameClient.idKPU;
import static tubessister.GameClient.is;
import static tubessister.GameClient.isProposer;
import static tubessister.GameClient.jsonResponse;
import static tubessister.GameClient.kpu_id;
import static tubessister.GameClient.leaderSelected;
import static tubessister.GameClient.listPlayer;
import static tubessister.GameClient.myId;
import static tubessister.GameClient.myName;
import static tubessister.GameClient.okPrepareProposal;
import static tubessister.GameClient.original_size;
import static tubessister.GameClient.os;
import static tubessister.GameClient.prepareTimeout;
import static tubessister.GameClient.proposal_number;*/

/**
 *
 * @author mochamadtry
 */

public class KonsesusPaxos extends javax.swing.JFrame {

    public Socket cliSocket = null;
    //public DataOutputStream outToServer;
    public BufferedReader inFromServer;
    public BufferedReader bufferReader;
    public PrintWriter outToServer;
    private String currentMessage;
    public String Nickname;
    public String Address;
    private String Port_;
    private int idUser;
    public static String myName = null;
    public static String role = null;
    public static int myId = -1;
    public static String time = null;
    public static ArrayList<String> friends = new ArrayList<String>();
    JSONObject jsonResponse = null;
    public static int cur_day = 0;
    public static String cur_phase = "";
    public static int proposal_number = 0;
    public static int original_size;
    public static int previous_prop_id = 0;
    public static int previous_player_id = 0;
    public static int kpu_id = 0;
    public static int previous_kpu_id = 0;

    public static class Player {
        int player_id;
        String username;
        String address;
        int port;
        int is_alive;
    }

    public static ArrayList<Player> listPlayer = new ArrayList<Player>();
    public static String current_method = "";
    public static boolean isProposer = false;
    public static int okPrepareProposal = 0;
    public static int failPrepareProposal = 0;
    public static boolean collectCountProposal = false;
    public static boolean majorityProposal = false;
    public static int biggestKpuID = -1;
    public static boolean leaderSelected = false;
    public static boolean prepareTimeout = true;
    public static boolean acceptTimeout = true;
    public static int idKPU = 0;
    //public static String method = null;

    /**
     * Creates new form KonsesusPaxos
     */
    public KonsesusPaxos() {
        initComponents();
        MenuAwal.setVisible(true);
        Register.setVisible(false);
        GamePlaySiang.setVisible(false);
        GamePlayMalam.setVisible(false);
        GagalLogin1.setVisible(false);
        GagalLogin2.setVisible(false);
        StatusReady.setVisible(false);
        StartGame.setVisible(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        MenuAwal = new javax.swing.JPanel();
        PlayGameButton = new javax.swing.JLabel();
        LogoGame = new javax.swing.JLabel();
        GamePlaySiang = new javax.swing.JPanel();
        PlayGameButton1 = new javax.swing.JLabel();
        username = new javax.swing.JLabel();
        ServerAddress7 = new javax.swing.JLabel();
        playrole = new javax.swing.JLabel();
        username1 = new javax.swing.JLabel();
        username2 = new javax.swing.JLabel();
        LogoGame1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        ServerAddress8 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        Register = new javax.swing.JPanel();
        ServerAddress1 = new javax.swing.JLabel();
        ServerAddress = new javax.swing.JLabel();
        InsertServerAddress = new javax.swing.JTextField();
        Port = new javax.swing.JLabel();
        InsertPort = new javax.swing.JTextField();
        Port1 = new javax.swing.JLabel();
        InsertNickname = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        GagalLogin1 = new javax.swing.JPanel();
        LoginFailedPanel1 = new javax.swing.JPanel();
        LoginFailed1 = new javax.swing.JLabel();
        LoginFailedButton1 = new javax.swing.JButton();
        LoginFailed4 = new javax.swing.JLabel();
        GagalLogin2 = new javax.swing.JPanel();
        LoginFailedPanel2 = new javax.swing.JPanel();
        LoginFailed2 = new javax.swing.JLabel();
        LoginFailedButton2 = new javax.swing.JButton();
        LoginFailed3 = new javax.swing.JLabel();
        GamePlayMalam = new javax.swing.JPanel();
        PlayGameButton2 = new javax.swing.JLabel();
        ServerAddress3 = new javax.swing.JLabel();
        StatusReady = new javax.swing.JPanel();
        ServerAddress4 = new javax.swing.JLabel();
        ServerAddress5 = new javax.swing.JLabel();
        ServerID = new javax.swing.JLabel();
        Port2 = new javax.swing.JLabel();
        PortID = new javax.swing.JLabel();
        Port3 = new javax.swing.JLabel();
        NicknameID = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        Status = new javax.swing.JLabel();
        StartGame = new javax.swing.JPanel();
        PlayGameButton3 = new javax.swing.JLabel();
        ServerAddress6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLayeredPane1.setLayout(new javax.swing.OverlayLayout(jLayeredPane1));

        MenuAwal.setBackground(new java.awt.Color(153, 204, 255));
        MenuAwal.setMinimumSize(new java.awt.Dimension(960, 560));

        PlayGameButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tubessister/playbutton.png"))); // NOI18N
        PlayGameButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                PlayGameButtonMousePressed(evt);
            }
        });

        LogoGame.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tubessister/LogoGame.png"))); // NOI18N

        javax.swing.GroupLayout MenuAwalLayout = new javax.swing.GroupLayout(MenuAwal);
        MenuAwal.setLayout(MenuAwalLayout);
        MenuAwalLayout.setHorizontalGroup(MenuAwalLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(MenuAwalLayout.createSequentialGroup().addContainerGap(405, Short.MAX_VALUE)
                        .addGroup(MenuAwalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        MenuAwalLayout.createSequentialGroup().addComponent(LogoGame).addGap(339,
                                                339, 339))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        MenuAwalLayout.createSequentialGroup().addComponent(PlayGameButton)
                                                .addGap(412, 412, 412)))));
        MenuAwalLayout.setVerticalGroup(MenuAwalLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(MenuAwalLayout.createSequentialGroup().addGap(48, 48, 48).addComponent(LogoGame)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 209, Short.MAX_VALUE)
                        .addComponent(PlayGameButton).addGap(89, 89, 89)));

        jLayeredPane1.add(MenuAwal);

        GamePlaySiang.setBackground(new java.awt.Color(153, 204, 255));

        PlayGameButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tubessister/sun (2).png"))); // NOI18N
        PlayGameButton1.setMaximumSize(new java.awt.Dimension(230, 230));
        PlayGameButton1.setMinimumSize(new java.awt.Dimension(230, 230));
        PlayGameButton1.setPreferredSize(new java.awt.Dimension(230, 239));

        username.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        username.setText("username");

        ServerAddress7.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        ServerAddress7.setText("Siang Hari");

        playrole.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        playrole.setText("playrole");

        username1.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        username1.setText("username");

        username2.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        username2.setText("username");

        LogoGame1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tubessister/LogoGame.png"))); // NOI18N

        jComboBox1.setModel(
                new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        ServerAddress8.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        ServerAddress8.setText("Player to Kill");

        jButton3.setText("Vote");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton3MousePressed(evt);
            }
        });
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout GamePlaySiangLayout = new javax.swing.GroupLayout(GamePlaySiang);
        GamePlaySiang.setLayout(GamePlaySiangLayout);
        GamePlaySiangLayout.setHorizontalGroup(GamePlaySiangLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        GamePlaySiangLayout.createSequentialGroup().addGap(0, 749, Short.MAX_VALUE).addComponent(
                                LogoGame1, javax.swing.GroupLayout.PREFERRED_SIZE, 416,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(GamePlaySiangLayout.createSequentialGroup().addGroup(GamePlaySiangLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(GamePlaySiangLayout.createSequentialGroup().addGap(31, 31, 31)
                                .addComponent(PlayGameButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(GamePlaySiangLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(GamePlaySiangLayout.createSequentialGroup()
                                                .addComponent(ServerAddress8).addGap(92, 92, 92)
                                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        164, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(ServerAddress7)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                GamePlaySiangLayout.createSequentialGroup().addGap(306, 306, 306).addComponent(
                                        jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 115,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(GamePlaySiangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(username).addComponent(username1))
                        .addGap(168, 168, 168)
                        .addGroup(GamePlaySiangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(playrole).addComponent(username2))
                        .addGap(53, 53, 53)));
        GamePlaySiangLayout.setVerticalGroup(GamePlaySiangLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(GamePlaySiangLayout.createSequentialGroup()
                        .addGroup(GamePlaySiangLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(GamePlaySiangLayout.createSequentialGroup().addGap(33, 33, 33)
                                        .addComponent(PlayGameButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(GamePlaySiangLayout.createSequentialGroup().addGap(47, 47, 47)
                                        .addGroup(GamePlaySiangLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(ServerAddress7).addComponent(playrole)
                                                .addComponent(username))))
                        .addGap(67, 67, 67)
                        .addGroup(GamePlaySiangLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(username1).addComponent(username2)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 32,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ServerAddress8))
                        .addGap(47, 47, 47)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 197, Short.MAX_VALUE)
                        .addComponent(LogoGame1)));

        jLayeredPane1.add(GamePlaySiang);

        Register.setBackground(new java.awt.Color(153, 204, 255));

        ServerAddress1.setFont(new java.awt.Font("Verdana", 0, 48)); // NOI18N
        ServerAddress1.setText("Register");

        ServerAddress.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        ServerAddress.setText("Server:");

        InsertServerAddress.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 0, 18)); // NOI18N
        InsertServerAddress.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        InsertServerAddress.setToolTipText("");
        InsertServerAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertServerAddressActionPerformed(evt);
            }
        });

        Port.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        Port.setText("Port:");

        InsertPort.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 0, 18)); // NOI18N
        InsertPort.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        InsertPort.setToolTipText("");

        Port1.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        Port1.setText("Nickname:");

        InsertNickname.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 0, 18)); // NOI18N
        InsertNickname.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        InsertNickname.setToolTipText("");
        InsertNickname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertNicknameActionPerformed(evt);
            }
        });

        jButton1.setText("Join Game");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout RegisterLayout = new javax.swing.GroupLayout(Register);
        Register.setLayout(RegisterLayout);
        RegisterLayout.setHorizontalGroup(RegisterLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegisterLayout.createSequentialGroup()
                        .addContainerGap(423, Short.MAX_VALUE)
                        .addGroup(RegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegisterLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(InsertNickname, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 160,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(RegisterLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(RegisterLayout.createSequentialGroup().addComponent(Port1)
                                                        .addGap(229, 229, 229))
                                                .addGroup(RegisterLayout.createSequentialGroup()
                                                        .addGroup(RegisterLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(ServerAddress).addComponent(Port))
                                                        .addGap(88, 88, 88)
                                                        .addGroup(RegisterLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(InsertServerAddress,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 160,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(InsertPort,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 160,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGroup(RegisterLayout.createSequentialGroup().addGroup(RegisterLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(RegisterLayout.createSequentialGroup().addGap(73, 73, 73)
                                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(RegisterLayout.createSequentialGroup().addGap(39, 39, 39)
                                                .addComponent(ServerAddress1)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(418, 418, 418)));
        RegisterLayout.setVerticalGroup(RegisterLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(RegisterLayout.createSequentialGroup().addGap(92, 92, 92).addComponent(ServerAddress1)
                        .addGap(40, 40, 40)
                        .addGroup(RegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(InsertServerAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ServerAddress))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                        .addGroup(RegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(InsertPort, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(Port))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addGroup(RegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(InsertNickname, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(Port1))
                        .addGap(38, 38, 38).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(238, 238, 238)));

        jLayeredPane1.add(Register);

        GagalLogin1.setBackground(new java.awt.Color(153, 204, 255));
        GagalLogin1.setForeground(new java.awt.Color(153, 204, 255));
        GagalLogin1.setMinimumSize(new java.awt.Dimension(960, 560));
        GagalLogin1.setOpaque(false);

        LoginFailedPanel1.setBackground(new java.awt.Color(204, 255, 255));

        LoginFailed1.setFont(new java.awt.Font("Prestige Elite Std", 1, 18)); // NOI18N
        LoginFailed1.setText("Gagal Register :");

        LoginFailedButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        LoginFailedButton1.setText("OK");
        LoginFailedButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginFailedButton1ActionPerformed(evt);
            }
        });

        LoginFailed4.setFont(new java.awt.Font("Prestige Elite Std", 1, 18)); // NOI18N
        LoginFailed4.setText("IP Address atau Port Salah");

        javax.swing.GroupLayout LoginFailedPanel1Layout = new javax.swing.GroupLayout(LoginFailedPanel1);
        LoginFailedPanel1.setLayout(LoginFailedPanel1Layout);
        LoginFailedPanel1Layout.setHorizontalGroup(
                LoginFailedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                LoginFailedPanel1Layout.createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(LoginFailedButton1).addGap(48, 48, 48))
                        .addGroup(LoginFailedPanel1Layout.createSequentialGroup()
                                .addGroup(LoginFailedPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(LoginFailedPanel1Layout.createSequentialGroup()
                                                .addGap(166, 166, 166).addComponent(LoginFailed1))
                                        .addGroup(LoginFailedPanel1Layout.createSequentialGroup()
                                                .addGap(122, 122, 122).addComponent(LoginFailed4)))
                                .addContainerGap(139, Short.MAX_VALUE)));
        LoginFailedPanel1Layout.setVerticalGroup(LoginFailedPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(LoginFailedPanel1Layout.createSequentialGroup().addContainerGap(58, Short.MAX_VALUE)
                        .addComponent(LoginFailed1).addGap(37, 37, 37).addComponent(LoginFailed4).addGap(37, 37, 37)
                        .addComponent(LoginFailedButton1).addGap(27, 27, 27)));

        javax.swing.GroupLayout GagalLogin1Layout = new javax.swing.GroupLayout(GagalLogin1);
        GagalLogin1.setLayout(GagalLogin1Layout);
        GagalLogin1Layout.setHorizontalGroup(GagalLogin1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(GagalLogin1Layout.createSequentialGroup().addGap(302, 302, 302)
                        .addComponent(LoginFailedPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(341, Short.MAX_VALUE)));
        GagalLogin1Layout.setVerticalGroup(GagalLogin1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, GagalLogin1Layout.createSequentialGroup()
                        .addContainerGap(228, Short.MAX_VALUE)
                        .addComponent(LoginFailedPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(191, 191, 191)));

        jLayeredPane1.add(GagalLogin1);

        GagalLogin2.setMinimumSize(new java.awt.Dimension(960, 560));
        GagalLogin2.setOpaque(false);

        LoginFailedPanel2.setBackground(new java.awt.Color(204, 255, 255));

        LoginFailed2.setFont(new java.awt.Font("Prestige Elite Std", 1, 18)); // NOI18N
        LoginFailed2.setText("Gagal Register: ");

        LoginFailedButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        LoginFailedButton2.setText("OK");
        LoginFailedButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginFailedButton2ActionPerformed(evt);
            }
        });

        LoginFailed3.setFont(new java.awt.Font("Prestige Elite Std", 1, 18)); // NOI18N
        LoginFailed3.setText("Nickname Sudah Digunakan");

        javax.swing.GroupLayout LoginFailedPanel2Layout = new javax.swing.GroupLayout(LoginFailedPanel2);
        LoginFailedPanel2.setLayout(LoginFailedPanel2Layout);
        LoginFailedPanel2Layout.setHorizontalGroup(
                LoginFailedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(LoginFailedPanel2Layout.createSequentialGroup().addGap(152, 152, 152)
                                .addComponent(LoginFailed2).addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(LoginFailedPanel2Layout.createSequentialGroup().addGap(94, 94, 94)
                                .addComponent(LoginFailed3).addContainerGap(116, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                LoginFailedPanel2Layout.createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(LoginFailedButton2).addGap(47, 47, 47)));
        LoginFailedPanel2Layout.setVerticalGroup(
                LoginFailedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(LoginFailedPanel2Layout.createSequentialGroup()
                                .addContainerGap(69, Short.MAX_VALUE).addComponent(LoginFailed2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(LoginFailed3).addGap(60, 60, 60).addComponent(LoginFailedButton2)
                                .addContainerGap()));

        javax.swing.GroupLayout GagalLogin2Layout = new javax.swing.GroupLayout(GagalLogin2);
        GagalLogin2.setLayout(GagalLogin2Layout);
        GagalLogin2Layout.setHorizontalGroup(GagalLogin2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(GagalLogin2Layout.createSequentialGroup().addGap(339, 339, 339)
                        .addComponent(LoginFailedPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(342, Short.MAX_VALUE)));
        GagalLogin2Layout.setVerticalGroup(GagalLogin2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, GagalLogin2Layout.createSequentialGroup()
                        .addContainerGap(221, Short.MAX_VALUE)
                        .addComponent(LoginFailedPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(206, 206, 206)));

        jLayeredPane1.add(GagalLogin2);

        GamePlayMalam.setBackground(new java.awt.Color(153, 204, 255));

        PlayGameButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tubessister/moon (1).png"))); // NOI18N
        PlayGameButton2.setMaximumSize(new java.awt.Dimension(230, 230));
        PlayGameButton2.setMinimumSize(new java.awt.Dimension(230, 230));
        PlayGameButton2.setPreferredSize(new java.awt.Dimension(230, 239));

        ServerAddress3.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        ServerAddress3.setText("Malam Hari");

        javax.swing.GroupLayout GamePlayMalamLayout = new javax.swing.GroupLayout(GamePlayMalam);
        GamePlayMalam.setLayout(GamePlayMalamLayout);
        GamePlayMalamLayout.setHorizontalGroup(
                GamePlayMalamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(GamePlayMalamLayout.createSequentialGroup().addGap(31, 31, 31)
                                .addComponent(PlayGameButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18).addComponent(ServerAddress3)
                                .addContainerGap(980, Short.MAX_VALUE)));
        GamePlayMalamLayout.setVerticalGroup(GamePlayMalamLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(GamePlayMalamLayout.createSequentialGroup()
                        .addGroup(GamePlayMalamLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(GamePlayMalamLayout.createSequentialGroup().addGap(33, 33, 33)
                                        .addComponent(PlayGameButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(GamePlayMalamLayout.createSequentialGroup().addGap(47, 47, 47)
                                        .addComponent(ServerAddress3)))
                        .addContainerGap(571, Short.MAX_VALUE)));

        jLayeredPane1.add(GamePlayMalam);

        StatusReady.setBackground(new java.awt.Color(153, 204, 255));

        ServerAddress4.setFont(new java.awt.Font("Verdana", 0, 48)); // NOI18N
        ServerAddress4.setText("Register Succesfully With");

        ServerAddress5.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        ServerAddress5.setText("Server:");

        ServerID.setFont(new java.awt.Font("Tempus Sans ITC", 0, 24)); // NOI18N
        ServerID.setText("server");

        Port2.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        Port2.setText("Port:");

        PortID.setFont(new java.awt.Font("Tempus Sans ITC", 0, 24)); // NOI18N
        PortID.setText("port");

        Port3.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        Port3.setText("Nickname:");

        NicknameID.setFont(new java.awt.Font("Tempus Sans ITC", 0, 24)); // NOI18N
        NicknameID.setText("nickname");

        jButton2.setText("Ready Game to Start");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton2MousePressed(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        Status.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Status.setText("After you click ready game, wait until 6 player ready to play the game");

        javax.swing.GroupLayout StatusReadyLayout = new javax.swing.GroupLayout(StatusReady);
        StatusReady.setLayout(StatusReadyLayout);
        StatusReadyLayout.setHorizontalGroup(StatusReadyLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(StatusReadyLayout.createSequentialGroup().addGroup(StatusReadyLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(StatusReadyLayout.createSequentialGroup().addGap(454, 454, 454).addComponent(
                                jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 146,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(StatusReadyLayout.createSequentialGroup().addGap(420, 420, 420)
                                .addGroup(StatusReadyLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(ServerAddress5).addComponent(Port2).addComponent(Port3))
                                .addGap(69, 69, 69)
                                .addGroup(StatusReadyLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(ServerID).addComponent(PortID).addComponent(NicknameID)))
                        .addGroup(StatusReadyLayout.createSequentialGroup().addGap(241, 241, 241)
                                .addComponent(ServerAddress4))
                        .addGroup(StatusReadyLayout.createSequentialGroup().addGap(314, 314, 314)
                                .addComponent(Status)))
                        .addContainerGap(285, Short.MAX_VALUE)));
        StatusReadyLayout.setVerticalGroup(StatusReadyLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(StatusReadyLayout.createSequentialGroup().addGap(186, 186, 186)
                        .addComponent(ServerAddress4).addGap(34, 34, 34)
                        .addGroup(StatusReadyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ServerAddress5).addComponent(ServerID))
                        .addGap(18, 18, 18)
                        .addGroup(StatusReadyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Port2).addComponent(PortID))
                        .addGap(18, 18, 18)
                        .addGroup(StatusReadyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(NicknameID).addComponent(Port3))
                        .addGap(18, 18, 18)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34).addComponent(Status).addContainerGap(150, Short.MAX_VALUE)));

        jLayeredPane1.add(StatusReady);

        StartGame.setBackground(new java.awt.Color(153, 204, 255));

        PlayGameButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tubessister/rotate.png"))); // NOI18N
        PlayGameButton3.setMaximumSize(new java.awt.Dimension(230, 230));
        PlayGameButton3.setMinimumSize(new java.awt.Dimension(230, 230));
        PlayGameButton3.setPreferredSize(new java.awt.Dimension(230, 239));

        ServerAddress6.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        ServerAddress6.setText("Initializing the game wait until at least 6 player ready to play the game");

        javax.swing.GroupLayout StartGameLayout = new javax.swing.GroupLayout(StartGame);
        StartGame.setLayout(StartGameLayout);
        StartGameLayout
                .setHorizontalGroup(StartGameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                StartGameLayout.createSequentialGroup().addContainerGap(322, Short.MAX_VALUE)
                                        .addComponent(ServerAddress6).addGap(319, 319, 319))
                        .addGroup(StartGameLayout.createSequentialGroup().addGap(541, 541, 541)
                                .addComponent(PlayGameButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        StartGameLayout.setVerticalGroup(StartGameLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(StartGameLayout.createSequentialGroup().addGap(325, 325, 325)
                        .addComponent(PlayGameButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(ServerAddress6, javax.swing.GroupLayout.PREFERRED_SIZE, 26,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(235, Short.MAX_VALUE)));

        jLayeredPane1.add(StartGame);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 1135, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                        .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 560, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                        .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void createSocket() throws UnknownHostException {
        InetAddress address = InetAddress.getLocalHost();

        try {
            int port;
            port = Integer.parseInt(InsertPort.getText());
            System.out.println(port);
            String _address = InsertServerAddress.getText();
            System.out.println(_address);
            cliSocket = new Socket(InsertServerAddress.getText(), Integer.parseInt(InsertPort.getText()));
            bufferReader = new BufferedReader(new InputStreamReader(System.in));
            outToServer = new PrintWriter(cliSocket.getOutputStream());
            inFromServer = new BufferedReader(new InputStreamReader(cliSocket.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(KonsesusPaxos.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void LoginFailedButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginFailedButton1ActionPerformed
        // TODO add your handling code here:
        GagalLogin1.setVisible(false);
    }//GEN-LAST:event_LoginFailedButton1ActionPerformed

    private void LoginFailedButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginFailedButton2ActionPerformed
        // TODO add your handling code here:
        GagalLogin2.setVisible(false);
    }//GEN-LAST:event_LoginFailedButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void PlayGameButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PlayGameButtonMousePressed
        // TODO add your handling code here:
        Register.setVisible(true);
        MenuAwal.setVisible(false);
    }//GEN-LAST:event_PlayGameButtonMousePressed

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
        // TODO add your handling code here:

        if (!(InsertPort.getText().equals("") || InsertServerAddress.getText().equals("")
                || InsertNickname.getText().equals("") || InsertPort.getText().contains(",")
                || InsertServerAddress.getText().contains(",") || InsertNickname.getText().contains(","))) {
            String response = null;
            Address = InsertServerAddress.getText();
            Port_ = InsertPort.getText();
            Nickname = InsertNickname.getText();

            try {
                createSocket();
                boolean isJoinGame = false;
                while (!isJoinGame) {
                    int port_ = Integer.valueOf(InsertPort.getText());
                    //Send joinGame request to server
                    outToServer.println(ClientRequest.joinRequest(Nickname, Address, port_).toString());
                    outToServer.flush(); //Send the message to server

                    System.out.println("Waiting...");

                    response = inFromServer.readLine(); //Read response from server about join game
                    jsonResponse = new JSONObject(response);
                    String status = jsonResponse.optString("status");
                    System.out.println("Waiting...");
                    if (status.equals("ok")) {
                        System.out.println("Waiting...");
                        Nickname = InsertNickname.getText();
                        isJoinGame = true;
                        StatusReady.setVisible(true);
                        Register.setVisible(false);
                        ServerID.setText(Address);
                        PortID.setText(Port_);
                        NicknameID.setText(Nickname);

                    } else if (status.equals("fail")) {
                        System.out.println(jsonResponse.optString("description"));
                    } else if (status.equals("error")) {
                        System.out.println(jsonResponse.optString("description"));
                    }
                } //End while !isJoinGame

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Socket read Error");
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }//GEN-LAST:event_jButton1MousePressed

    private void InsertServerAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertServerAddressActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_InsertServerAddressActionPerformed

    private void InsertNicknameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertNicknameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_InsertNicknameActionPerformed

    private void jButton2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MousePressed
        // TODO add your handling code here:
        System.out.println(Address);
        System.out.println(Port_);
        System.out.println(Nickname);
        String response = null;
        try {
            System.out.println("Join game passed. Ready will be sent");
            boolean isReady = false;
            while (!isReady) {
                //Send ready message to server
                outToServer.println(ClientRequest.readyUp());
                outToServer.flush(); //Send the message to server

                response = inFromServer.readLine(); //Read response from server about readyup game
                jsonResponse = new JSONObject(response);
                String status = jsonResponse.optString("status");
                if (status.equals("ok")) {
                    isReady = true;
                    System.out.println("isReady changed to true");
                    Status.setText("Wait until 6 player ready");
                    //getContentPane().add(StartGame);
                    //StartGame.setVisible(true);
                    //StatusReady.setVisible(false);
                } else { //can't play, quit
                    return;
                }
            }
            //Waiting startgame message from server
            System.out.println("Waiting start game");
            response = inFromServer.readLine();
            System.out.println(response + "nn");
            jsonResponse = new JSONObject(response);
            String method = jsonResponse.optString("method");
            if (method.equals("start")) {
                isReady = true;
                System.out.println("isReady (start game) = true");
                role = jsonResponse.optString("role");
                time = jsonResponse.optString("time");
                if (role.equals("werewolf")) {
                    JSONArray jsonFriends = jsonResponse.optJSONArray("friend");
                    System.out.println(jsonFriends.toString());
                    for (int i = 0; i < jsonFriends.length(); i++) {
                        friends.add(jsonFriends.getString(i));
                    }
                }
                gamePlaySiang();
                //GamePlaySiang.setVisible(true);
                StatusReady.setVisible(false);
            } else { //can't play, quit
                // System.out.println("gagal");
                return;
            }
            //{method?:start?,time?:day?,role?:werewolf?,friend?:[ahmad?,dariel?],description?:gameisstarted?,}

            //Playing Game
            System.out.println("Game started");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            Logger.getLogger(KonsesusPaxos.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(response);
        //startGame();
        System.out.println(response);

    }//GEN-LAST:event_jButton2MousePressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3MousePressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void gamePlaySiang() throws JSONException, IOException { //throws IOException, JSONException, InterruptedException {
        GamePlaySiang.setVisible(true);
        username.setText(Nickname);
        playrole.setText(role);
        startGame();

    }

    private void startGame() throws JSONException, IOException {
        String response = null;
        System.out.println("Game started");

        /* REQUEST LIST CLIENT */
        outToServer.println(ClientRequest.listClient());
        outToServer.flush();
        boolean listClientReceived = false;
        while (!listClientReceived) {
            response = inFromServer.readLine(); //Read response from server about listclient
            System.out.println(response);
            jsonResponse = new JSONObject(response);
            String status = jsonResponse.optString("status");
            if (status.equals("ok")) {
                listClientReceived = true;
                JSONArray clientsJSON = jsonResponse.optJSONArray("clients");
                for (int i = 0; i < clientsJSON.length(); i++) {
                    JSONObject client = clientsJSON.getJSONObject(i);
                    Player player = new Player();
                    player.player_id = Integer.parseInt(client.optString("player_id"));
                    player.address = client.optString("address");
                    player.username = client.optString("username");
                    player.port = Integer.parseInt(client.optString("port"));
                    player.is_alive = Integer.parseInt(client.optString("is_alive"));
                    listPlayer.add(player);
                }

                String daftarPlayer = "<html>";
                String statusAlive = "<html>";
                String hidup = "";
                original_size = listPlayer.size();
                System.out.println(original_size);
                for (int i = 0; i < original_size; i++) {
                    Player player = listPlayer.get(i);
                    if (listPlayer.get(i).is_alive == 1)
                        hidup = "hidup";
                    else
                        hidup = "mati";
                    daftarPlayer += "username : " + listPlayer.get(i).username + "<br>" + "<br>";
                    statusAlive += "status : " + hidup + "<br>" + "<br>";
                    if (player.username.equals(myName)) {
                        myId = player.player_id;
                        if (myId <= original_size && myId > original_size - 2) {
                            isProposer = true;
                            System.out.println("_proposer");
                        }
                    }
                }
                daftarPlayer += "<html>";
                statusAlive += "<html>";
                username1.setText(daftarPlayer);
                username2.setText(statusAlive);

                System.out.println("List client received");
            } else { //response from server is not list client. wait the server send response
                return;
            }
        }
        /* END-REQUEST LIST CLIENT */
    }
    /*private void startGame() throws IOException, JSONException, InterruptedException{
    //Playing Game
    String response = null;
        System.out.println("Game started");
            
        /* REQUEST LIST CLIENT */
    /*outToServer.println(ClientRequest.listClient());
    outToServer.flush();
    boolean listClientReceived = false;
    while(!listClientReceived){
        response = inFromServer.readLine(); //Read response from server about listclient
        System.out.println(response);
        jsonResponse = new JSONObject(response);
        String status = jsonResponse.optString("status");
        if(status.equals("ok")){
            listClientReceived = true;
            JSONArray clientsJSON = jsonResponse.optJSONArray("clients");
            for(int i=0; i<clientsJSON.length(); i++){
                JSONObject client = clientsJSON.getJSONObject(i);
                Player player = new Player();
                player.player_id = Integer.parseInt(client.optString("player_id"));
                player.address = client.optString("address");
                player.username = client.optString("username");
                player.port = Integer.parseInt(client.optString("port"));
                player.is_alive = Integer.parseInt(client.optString("is_alive"));
                listPlayer.add(player);
            }
                
            original_size = listPlayer.size() ;
            for(int i=0; i<original_size; i++){
                Player player = listPlayer.get(i);
                if (player.username.equals(myName)) {
                    myId = player.player_id ;
                    if (myId<=original_size&&myId>original_size-2){
                        isProposer = true;
                        System.out.println("_proposer");
                    }
                }
            }
                
                
            System.out.println("List client received");
        } else { //response from server is not list client. wait the server send response
            return;
        }
    }
    System.out.println("My username : "+myName);
    System.out.println("My player id : "+myId);
    System.out.println("Total player : "+listPlayer.size());
    //original_size = listPlayer.size() ;
    int portT = listPlayer.get(myId-1).port ;
    GameClient.Listener l_thread = new GameClient.Listener("list",portT); 
    l_thread.start();
    while (true) {
        /*  Get current day */
    /*response = inFromServer.readLine(); 
    System.out.println("cur_day_response = " + response);
    jsonResponse = new JSONObject(response);
    String method_ = jsonResponse.optString("method");
    if (method_.equals("change_phase")) {
        cur_day = jsonResponse.optInt("days");
        cur_phase = jsonResponse.optString("time");
    }
    System.out.println(cur_phase+" : day "+cur_day);
    if (isProposer) {
        //Paxos
        //Kirim paxos prepare proposal
        prepareTimeout = false ;
        proposal_number++ ;
        okPrepareProposal = 0 ;
        failPrepareProposal = 0;
        JSONObject obj = new JSONObject();
        obj.put("method","prepare_proposal");
        JSONArray pr_id = new JSONArray() ;
        pr_id.put(proposal_number);
        pr_id.put(myId);
        obj.put("proposal_id",pr_id);
        while(!leaderSelected){
            current_method = "prepare_proposal" ;
            for (int i=0;i<original_size-2;i++) {
                Sender s = new Sender("send",obj.toString(),listPlayer.get(i).port,listPlayer.get(i).address);
                s.start();
            }
            Thread.sleep(5000);
            prepareTimeout = true ;
            acceptTimeout = false ;
            int num_acceptor = original_size - 2 ;
            current_method = "accept_proposal" ;
            if (okPrepareProposal > num_acceptor/2 ) {
                //Tercapai leader
                String msg = ClientRequest.paxosAcceptProposal(proposal_number, myId, kpu_id);
                 for (int i=0;i<original_size-2;i++) {
                    Sender s = new Sender("send",obj.toString(),listPlayer.get(i).port,listPlayer.get(i).address);
                    s.start();
                }
            } else {
                //Kirim kalau ledaernya proposer 1 lagi
                int id = 0 ;
                if (myId == original_size) id = original_size - 1 ;
                else id = original_size ;
                String msg = ClientRequest.paxosAcceptProposal(proposal_number, id, kpu_id);
                 for (int i=0;i<original_size-2;i++) {
                    Sender s = new Sender("send",obj.toString(),listPlayer.get(i).port,listPlayer.get(i).address);
                    s.start();
                }
            }
            Thread.sleep(5000);
            acceptTimeout = false ;
                
        }
            
        //waiting kpu_selected from server
            
        //Terima response prepare proposal dari client
        //hitung jumlah response ok nya berapa
        //kalo lebih dari separo lanjutkan ke protokol 6
        //keliatannya perlu pake timeout, misalkan stlh bbrp detik belum jawab semua,
        //yg blm jawab dianggap gagal
            
            
        //{Protokol 6 Kirim paxos accept proposal (cuma kalo sesuai kondisi di atas)
        //terima response dari acceptor
             
            
            
    } else {
        //Acceptor - pake thread
        //CEK METHOD
        //1.prepare_proposal
        //terima request paxos prepare proposal
        //cek apakah id proposal lebih besar
        //kalo iya accept
        //kalo tidak reject
        //kirim response ke client
            
        //2.accept_proposal
        //Cek id proposal terakhir yg pernah diterima (kalo ada)
        //Terima yg skrg cum kalo id sblmnya ga lebih besar
        //Kalo meng-accept, set id proposal terakhir
        //Response ke proposer
        //Kirim ke server
            
        //3.vote_now
        //Ubah tampilan jadi untuk ngevote
            
    }
        
    //GET KPU_ID FROM SERVER
    boolean getKpuID = false;
    while(!getKpuID){
        response = inFromServer.readLine();
        jsonResponse = new JSONObject(response);
        String method = jsonResponse.optString("method");
        if (method.equals("kpu_selected")) {
            idKPU = Integer.parseInt(jsonResponse.optString("kpu_id"));
            getKpuID = true;
        }
    }
        
    //GET VOTE NOW FROM SERVER
    boolean getVoteNow = false;
    String phase = "";
    while(!getVoteNow){
        response = inFromServer.readLine();
        jsonResponse = new JSONObject(response);
        String method = jsonResponse.optString("method");
        if (method.equals("vote_now")) {
            phase = jsonResponse.optString("phase");
            getVoteNow = true;
        }
    }
        
    //Minta input pengguna
    Scanner s  = new Scanner(System.in);
    int target = s.nextInt();
        
    }
        
        
    /*
    while(line.compareTo("QUIT")!=0){
    os.println(ClientRequest.joinRequest(line,"",0).toString());
    os.flush();
    response=is.readLine();
    System.out.println("Server Response : "+response);
    line=br.readLine();
    }*/
    //}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(KonsesusPaxos.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KonsesusPaxos.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KonsesusPaxos.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KonsesusPaxos.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KonsesusPaxos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel GagalLogin1;
    private javax.swing.JPanel GagalLogin2;
    private javax.swing.JPanel GamePlayMalam;
    private javax.swing.JPanel GamePlaySiang;
    private javax.swing.JTextField InsertNickname;
    private javax.swing.JTextField InsertPort;
    private javax.swing.JTextField InsertServerAddress;
    private javax.swing.JLabel LoginFailed1;
    private javax.swing.JLabel LoginFailed2;
    private javax.swing.JLabel LoginFailed3;
    private javax.swing.JLabel LoginFailed4;
    private javax.swing.JButton LoginFailedButton1;
    private javax.swing.JButton LoginFailedButton2;
    private javax.swing.JPanel LoginFailedPanel1;
    private javax.swing.JPanel LoginFailedPanel2;
    private javax.swing.JLabel LogoGame;
    private javax.swing.JLabel LogoGame1;
    private javax.swing.JPanel MenuAwal;
    private javax.swing.JLabel NicknameID;
    private javax.swing.JLabel PlayGameButton;
    private javax.swing.JLabel PlayGameButton1;
    private javax.swing.JLabel PlayGameButton2;
    private javax.swing.JLabel PlayGameButton3;
    private javax.swing.JLabel Port;
    private javax.swing.JLabel Port1;
    private javax.swing.JLabel Port2;
    private javax.swing.JLabel Port3;
    private javax.swing.JLabel PortID;
    private javax.swing.JPanel Register;
    private javax.swing.JLabel ServerAddress;
    private javax.swing.JLabel ServerAddress1;
    private javax.swing.JLabel ServerAddress3;
    private javax.swing.JLabel ServerAddress4;
    private javax.swing.JLabel ServerAddress5;
    private javax.swing.JLabel ServerAddress6;
    private javax.swing.JLabel ServerAddress7;
    private javax.swing.JLabel ServerAddress8;
    private javax.swing.JLabel ServerID;
    private javax.swing.JPanel StartGame;
    private javax.swing.JLabel Status;
    private javax.swing.JPanel StatusReady;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLabel playrole;
    private javax.swing.JLabel username;
    private javax.swing.JLabel username1;
    private javax.swing.JLabel username2;
    // End of variables declaration//GEN-END:variables
}