// 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:   GameFrame.java

import java.applet.Applet;
import java.awt.*;

class GameFrame extends Frame
{

    GameFrame(GameControl gamecontrol, Connection connection, int i, int j, String as[])
    {
        super(_German.GAME_NAME + "  (#" + i + ")");
        setIconImage(iconImage);
        setBackground(Color.white);
        con = connection;
        timedout = false;
        tableNumber = i;
        gameId = j;
        nop = as.length;
        pids = new int[4];
        order = new int[4];
        names = new String[4];
        for(int k = 0; k < nop; k++)
            names[k] = as[k];

        alreadyFinished = false;
        GameFramePanel gameframepanel = new GameFramePanel(imageABC, image123, logoImage);
        gameframepanel.setLayout(null);
        gcontrol = gamecontrol;
        gameframepanel.add(gamecontrol.getGameBoard());
        gameframepanel.add(gamecontrol.getLetterBoard());
        gameframepanel.add(gamecontrol.getChangeBoard());
        startb = new Button(_German.START_GAME);
        inviteb = new Button(_German.INVITATION);
        endb = new Button(_German.EXIT);
        pc = new PlayerComponent[4];
        for(int l = 0; l < 4; l++)
        {
            pc[l] = new PlayerComponent(l + 1, 412, 75 + l * 24, 0);
            gameframepanel.add(pc[l]);
        }

        mp = new MovePanel(413, 188, gamecontrol, startb, inviteb, endb);
        gameframepanel.add(mp);
        if(nop > 0)
            mp.gameStartMode(names[0], false);
        gamecontrol.tcounter.statc = mp.m2_sc;
        gamecontrol.tcounter.gframe = this;
        tf = new TextField();
        tf.reshape(413, 408, 215, 26);
        gameframepanel.add(tf);
        ta = new TextArea();
        ta.reshape(413, 264, 215, 141);
        ta.setEditable(false);
        gameframepanel.add(ta);
        ta.appendText("*** " + _German.TABLE + " #" + i + "\n*** " + _German.TIME_RESTRICTIONS + ":" + "\n*** " + _German.ONE_MOVE_SHORT + gamecontrol.moveLimit + " " + _German.MIN_ABBREVIATION + ", " + _German.WHOLE_GAME_SHORT + gamecontrol.gameLimit + " " + _German.MIN_ABBREVIATION + "\n*** " + _German.NOP_WILLING_TO_PLAY + nop);
        if(nop > 0)
        {
            ta.appendText("\n*** (" + as[0]);
            for(int i1 = 1; i1 < nop; i1++)
                ta.appendText(", " + as[i1]);

            ta.appendText(")\n");
        } else
        {
            ta.appendText("\n");
        }
        add("Center", gameframepanel);
        pack();
        show();
    }

    public boolean action(Event event, Object obj)
    {
        if(event.target == startb)
        {
            startb.disable();
            con._tableGameStart(tableNumber, gameId);
        }
        if(event.target == endb)
        {
            endb.disable();
            shutdown();
        } else
        if(event.target == inviteb)
        {
            inviteb.disable();
            con._tableSay(-1, -1, "*** " + _German.INVITES_TO_TABLE_PREFIX + names[0] + " " + _German.INVITES_TO_TABLE_SUFFIX + tableNumber + "  [" + gcontrol.moveLimit + " " + _German.MIN_ABBREVIATION + "/ " + gcontrol.gameLimit + " " + _German.MIN_ABBREVIATION + ".]");
        } else
        if(event.target == tf)
        {
            String s = tf.getText();
            if(!s.equals("") && con != null)
            {
                if(s.length() > 50)
                    s = s.substring(0, 50) + "... <ciach!>";
                con._tableSay(tableNumber, gameId, s);
                tf.setText("");
            }
            return true;
        }
        return false;
    }

    void addPlayer(String s, int i)
    {
        ta.appendText("\n*** " + _German.COMES_IN_PREFIX + s + _German.COMES_IN_SUFFIX);
        try
        {
            Toolkit.getDefaultToolkit().beep();
        }
        catch(Throwable _ex) { }
        names[nop] = s;
        pids[nop] = i;
        nop++;
        if(nop == 1)
            mp.gameStartMode(s, true);
    }

    void display(String s)
    {
        ta.appendText("\n" + s);
    }

    void gameMover(int i, int j)
    {
        if(oldAct != -1)
            pc[order[oldAct]].setActiveState(0);
        for(int k = 0; k < nop; k++)
        {
            if(pids[k] != i)
                continue;
            oldAct = k;
            break;
        }

        pc[order[oldAct]].setActiveState(con.pid != i ? 1 : 2);
        if(con.pid == i)
        {
            mp.moveTriggerMode(j);
            gcontrol.startMove(j);
            try
            {
                Toolkit.getDefaultToolkit().beep();
            }
            catch(Throwable _ex) { }
        } else
        {
            mp.moveWaitMode(_German.NOW_MOVES_PREFIX + names[oldAct] + _German.NOW_MOVES_SUFFIX);
        }
    }

    void gameOver()
    {
        alreadyFinished = true;
        if(oldAct != -1)
            pc[order[oldAct]].setActiveState(0);
        mp.switchTo(4);
    }

    public boolean handleEvent(Event event)
    {
        if(event.id == 201)
            shutdown();
        return super.handleEvent(event);
    }

    void moveAccepted(boolean flag)
    {
        gcontrol.moveAccepted(flag, pids[oldAct]);
    }

    void moveProp(int ai[])
    {
        gcontrol.moveProp(ai);
        if(!timedout)
        {
            mp.switchTo(1);
            try
            {
                Toolkit.getDefaultToolkit().beep();
            }
            catch(Throwable _ex) { }
        }
    }

    void newBricks(int ai[])
    {
        for(int i = 0; i < ai.length; i += 2)
            gcontrol.newBrick(new Brick(ai[i], ai[i + 1], true));

    }

    void newHost(int i, String s)
    {
        mp.gameStartMode(s, i == con.pid);
    }

    void newPoints(int i, int j)
    {
        pc[order[i]].setPoints(j);
    }

    void newTime(int i)
    {
        pc[order[oldAct]].setTime(i);
    }

    void removePlayer(int i)
    {
        if(alreadyStarted)
        {
            display("*** " + _German.LEAVES_GAME_PREFIX + names[i] + _German.LEAVES_GAME_SUFFIX);
            pc[order[i]].setPlayerName("** " + pc[order[i]].getPlayerName());
        } else
        {
            display("*** " + _German.LEAVES_TABLE_PREFIX + names[i] + _German.LEAVES_TABLE_SUFFIX);
            for(int j = i; j < nop - 1; j++)
            {
                names[j] = names[j + 1];
                pids[j] = pids[j + 1];
            }

            nop--;
        }
    }

    void shutdown()
    {
        if(gcontrol.tcounter != null)
            gcontrol.tcounter.turnOff();
        if(con != null)
        {
            con._leaveTable(tableNumber);
            con = null;
        }
        gameId = -1;
        dispose();
        System.gc();
    }

    void startGame(int i, String as[], int ai[])
    {
        startb.disable();
        alreadyStarted = true;
        for(int j = 0; j < nop; j++)
        {
            names[j] = as[j];
            pids[j] = ai[j * 2];
            order[j] = ai[j * 2 + 1];
        }

        for(int k = 0; k < nop; k++)
        {
            pc[order[k]].init(names[k], 0, 0, pids[k] != con.pid ? 0 : 1);
            if(pids[k] == con.pid)
            {
                gcontrol.thispc = pc[order[k]];
                gcontrol.tcounter.pc = pc[order[k]];
            }
        }

        gcontrol.tcounter.start();
        oldAct = -1;
    }

    static void staticInit(Applet applet)
    {
        parent = applet;
        iconImage = Const.getImage("icon.gif");
        logoImage = Const.getImage("logo.gif");
        imageABC = Const.getImage("abc.gif");
        image123 = Const.getImage("123.gif");
        BrickBoard.staticInit(applet);
        RoundedLedComponent.staticInit(applet);
    }

    void timeoutGame()
    {
        gcontrol.timeoutGame();
        if(oldAct != -1)
            pc[order[oldAct]].setActiveState(0);
        alreadyFinished = true;
        timedout = true;
    }

    void timeoutMove()
    {
        gcontrol.timeoutMove();
    }

    static final int ody = 75;
    static final int coile = 24;
    static final int s1 = 119;
    static final int s2 = 37;
    static final int s3 = 43;
    static Image image123;
    static Image imageABC;
    static Image iconImage;
    static Image logoImage;
    static Applet parent;
    GameControl gcontrol;
    PlayerComponent pc[];
    MovePanel mp;
    TextArea ta;
    TextField tf;
    Button startb;
    Button endb;
    Button inviteb;
    int nop;
    int tableNumber;
    int oldAct;
    int gameId;
    boolean alreadyStarted;
    boolean alreadyFinished;
    boolean timedout;
    Connection con;
    String names[];
    int pids[];
    int order[];
}
