// 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:   gamePool.java

package boggle.server;

import boggle.server.comms.serverClient;
import boggle.settings;
import java.io.*;
import java.rmi.RemoteException;

// Referenced classes of package boggle.server:
//            fileNotGeneratedException, messageServer, multiGen, playerPool

public class gamePool
{

    public static void initialise(serverClient serverclient)
    {
        netsync = serverclient;
        nextGame = 0;
        description = new String[settings.getMaxPlayers()];
        definition = new String[settings.getMaxPlayers()];
        lang = new int[settings.getMaxPlayers()];
        type = new int[settings.getMaxPlayers()];
        for(int i = 0; i < settings.getMaxPlayers(); i++)
        {
            description[i] = "";
            definition[i] = "";
            lang[i] = -2;
            type[i] = -2;
        }

    }

    public static int newGame(int i, int j, int k)
        throws RemoteException
    {
        int l = nextGame;
        if(nextGame == settings.getMaxPlayers() - 1)
            nextGame = 0;
        else
            nextGame++;
        String s = settings.getLanguages()[k];
        String s1 = settings.getGameNames()[j] + String.valueOf(randomint(100)) + "." + s;
        try
        {
            File file = new File(new File(settings.getGameDirectory()), s1);
            BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
            definition[l] = "";
            for(String s2 = bufferedreader.readLine(); s2 != null; s2 = bufferedreader.readLine())
                definition[l] += s2 + "\n";

            bufferedreader.close();
            file.delete();
        }
        catch(Exception exception)
        {
            System.out.println("fileNotGenerated thrown: " + exception.getMessage());
            messageServer.broadcast("System: Server: Error on file: " + s1 + "\n" + "\t This means " + playerPool.getPlayerName(i) + " can't play this game.", k);
            throw new fileNotGeneratedException(s1, exception);
        }
        description[l] = "game '" + l + "' (" + playerPool.getPlayerName(i) + "'s " + s + " game of " + settings.getGameNames()[j] + ")";
        lang[l] = k;
        type[l] = j;
        playerPool.setPlayerStatus(i, " playing " + description[l]);
        multiGen multigen = new multiGen();
        multigen.setPriority(1);
        multigen.start();
        messageServer.broadcast("System: " + playerPool.getPlayerName(i) + " just created " + description[l], k);
        netsync.newgame(settings.getHostName(), l, description[l], definition[l], k, j);
        return l;
    }

    private static int randomint(int i)
    {
        return (int)Math.floor(Math.random() * (double)i);
    }

    public static int joinGame(int i, int j)
        throws RemoteException
    {
        if(definition[j].equals(""))
        {
            messageServer.broadcast("System: " + playerPool.getPlayerName(i) + " requested a game that hasn't been played", -1);
            return -1;
        } else
        {
            playerPool.setPlayerStatus(i, " playing " + description[j]);
            messageServer.broadcast("System: " + playerPool.getPlayerName(i) + " just joined " + description[j], lang[j]);
            netsync.joingame(settings.getHostName(), i, j);
            return type[j];
        }
    }

    public static void finalScore(int i, int j, int k)
        throws RemoteException
    {
        messageServer.broadcast("System: " + playerPool.getPlayerName(i) + " has scored " + k + " in " + description[j], lang[j]);
        playerPool.score(i, lang[j], type[j], k);
        playerPool.setPlayerStatus(i, " Game " + j + ": scored " + k + ". Looking for another game to play ...");
        messageServer.broadcast("System: " + playerPool.getPlayerName(i) + " has played " + playerPool.getPlayerPlayed(i, lang[j], type[j]) + " games of " + settings.getLanguages()[lang[j]] + " " + settings.getGameNames()[type[j]] + "\n\tAvg score: " + (float)playerPool.getPlayerScored(i, lang[j], type[j]) / (float)playerPool.getPlayerPlayed(i, lang[j], type[j]) + " High Score: " + playerPool.getPlayerHighest(i, lang[j], type[j]), lang[j]);
        netsync.finalscore(settings.getHostName(), i, j, k);
    }

    public static String getGameDefinition(int i)
    {
        return definition[i];
    }

    public static void newgame(String s, int i, String s1, String s2, int j, int k)
    {
        description[i] = s1;
        definition[i] = s2;
        lang[i] = j;
        type[i] = k;
        netsync.newgame(s, i, s1, s2, j, k);
    }

    public static void joingame(String s, int i, int j)
    {
        playerPool.setPlayerStatus(i, " playing " + description[j]);
        netsync.joingame(s, i, j);
    }

    public static void finalscore(String s, int i, int j, int k)
    {
        playerPool.score(i, lang[j], type[j], k);
        playerPool.setPlayerStatus(i, " Game " + j + ": scored " + k + ". Looking for another game to play ...");
        netsync.finalscore(s, i, j, k);
    }

    public static String[] getDefinitions()
    {
        return definition;
    }

    public static void setDefinitions(String as[])
    {
        definition = as;
    }

    public static String[] getDescriptions()
    {
        return description;
    }

    public static void setDescriptions(String as[])
    {
        description = as;
    }

    public static int[] getLanguages()
    {
        return lang;
    }

    public static void setLanguages(int ai[])
    {
        lang = ai;
    }

    public static int[] getTypes()
    {
        return type;
    }

    public static void setTypes(int ai[])
    {
        type = ai;
    }

    public gamePool()
    {
    }

    private static int nextGame;
    private static String description[];
    private static String definition[];
    private static int lang[];
    private static int type[];
    private static serverClient netsync;
}
