// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   BoggleGame.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class BoggleGame extends JPanel
    implements ActionListener
{

    BoggleGame(String s)
        throws IOException
    {
        theServerAddress = s;
        theGrid = downloadGrid();
        thePlayer = new Player(this);
        theScore = new Score(0);
        theScore.setBackground(Color.green);
        theGrid.setScoring(theScore);
        theProgressBar = new JProgressBar(0, 180);
        theProgressBar.setValue(0);
        setLayout(null);
        add(theGrid);
        theGrid.reshape(0, 0, theGrid.getPreferredSize().width, theGrid.getPreferredSize().height);
        add(theScore);
        theScore.reshape(0, theGrid.getPreferredSize().height, theGrid.getPreferredSize().width, 40);
        add(theProgressBar);
        theProgressBar.reshape(0, theGrid.getPreferredSize().height + 40, theGrid.getPreferredSize().width, 20);
        setPreferredSize(new Dimension(theGrid.getPreferredSize().width, theGrid.getPreferredSize().height + 60));
        setSize(getPreferredSize().width, getPreferredSize().height);
        seconds = 0;
        theTimer = new Timer(1000, this);
        theTimer.setRepeats(true);
        theTimer.start();
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        seconds++;
        if(seconds < 180)
        {
            theProgressBar.setValue(seconds);
            return;
        }
        theTimer.stop();
        theGrid.setEnabled(false);
        JOptionPane.showMessageDialog(this, "You have had your three minutes!", "Time up", 2);
        try
        {
            setCursor(new Cursor(3));
            theScore.setPlayer(thePlayer);
            HighScoreTable highscoretable = uploadScore(theScore);
            highscoretable.setDefaultCloseOperation(2);
            highscoretable.show();
        }
        catch(IOException ioexception)
        {
            System.out.println(ioexception);
            JOptionPane.showMessageDialog(this, "An error occured when trying to download a highscore table.\nYou should check that the server is running and is accessable from your network.\nFor more information on this error, you should check the Java console.", "Communications error", 0);
        }
        finally
        {
            setCursor(Cursor.getDefaultCursor());
            theScore.setPlayer(null);
        }
    }

    private static BoggleGrid downloadGrid()
        throws IOException
    {
        try
        {
            Socket socket = new Socket(InetAddress.getByName(theServerAddress), 1978);
            DataOutputStream dataoutputstream = new DataOutputStream(socket.getOutputStream());
            dataoutputstream.writeInt(12);
            dataoutputstream.flush();
            ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());
            BoggleGrid bogglegrid = (BoggleGrid)objectinputstream.readObject();
            objectinputstream.close();
            return bogglegrid;
        }
        catch(UnknownHostException unknownhostexception)
        {
            throw new IOException(unknownhostexception.toString());
        }
        catch(StreamCorruptedException streamcorruptedexception)
        {
            throw new IOException(streamcorruptedexception.toString());
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            throw new IOException(classnotfoundexception.toString());
        }
    }

    private static HighScoreTable getOverallHighScores()
        throws IOException
    {
        try
        {
            Socket socket = new Socket(InetAddress.getByName(theServerAddress), 1978);
            DataOutputStream dataoutputstream = new DataOutputStream(socket.getOutputStream());
            dataoutputstream.writeInt(14);
            dataoutputstream.flush();
            ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());
            HighScoreTable highscoretable = (HighScoreTable)objectinputstream.readObject();
            objectinputstream.close();
            highscoretable.setDefaultCloseOperation(2);
            return highscoretable;
        }
        catch(UnknownHostException unknownhostexception)
        {
            System.err.println(unknownhostexception);
        }
        catch(StreamCorruptedException streamcorruptedexception)
        {
            System.err.println(streamcorruptedexception);
        }
        catch(IOException ioexception)
        {
            System.err.println(ioexception);
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            System.err.println(classnotfoundexception);
        }
        throw new IOException();
    }

    public boolean isRunning()
    {
        return theTimer.isRunning();
    }

    public static void main(String args[])
    {
        InputStreamReader inputstreamreader = new InputStreamReader(System.in);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        System.out.println("Testing localhost server");
        theServerAddress = new String("localhost");
        try
        {
            do
            {
                System.out.println("Downloading a grid...");
                BoggleGrid bogglegrid;
                System.out.println(bogglegrid = downloadGrid());
                System.out.print("Please enter the score:");
                Score score = new Score((new Integer(bufferedreader.readLine())).intValue());
                score.setPlayer(new Player(null));
                System.out.print("Please the number of the grid that this score is for:");
                score.setGridNumber((new Integer(bufferedreader.readLine())).intValue());
                System.out.println(uploadScore(score));
                System.out.println(getOverallHighScores());
            } while(true);
        }
        catch(IOException ioexception)
        {
            System.out.println(ioexception);
        }
    }

    public void stop()
    {
        theTimer.stop();
    }

    private static HighScoreTable uploadScore(Score score)
        throws IOException
    {
        try
        {
            Socket socket = new Socket(InetAddress.getByName(theServerAddress), 1978);
            DataOutputStream dataoutputstream = new DataOutputStream(socket.getOutputStream());
            dataoutputstream.writeInt(16);
            dataoutputstream.flush();
            ObjectOutputStream objectoutputstream = new ObjectOutputStream(socket.getOutputStream());
            objectoutputstream.writeObject(score);
            ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());
            HighScoreTable highscoretable = (HighScoreTable)objectinputstream.readObject();
            objectinputstream.close();
            objectoutputstream.close();
            dataoutputstream.close();
            socket.close();
            return highscoretable;
        }
        catch(UnknownHostException unknownhostexception)
        {
            System.out.println(unknownhostexception);
        }
        catch(StreamCorruptedException streamcorruptedexception)
        {
            System.out.println(streamcorruptedexception);
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            System.out.println(classnotfoundexception);
        }
        throw new IOException("Somthing went wrong!");
    }

    private BoggleGrid theGrid;
    private Score theScore;
    private Timer theTimer;
    private Player thePlayer;
    private JProgressBar theProgressBar;
    private int seconds;
    private static String theServerAddress;
}
