websiteWebsite
baggle B@ggle
Jeu de boggle multi-joueurs en réseau - A free online multiplayer boggle game

 

Browse the code

Revision log Information on the revision
Revision: 210 (differences)
Author: inouire
Log message: fixes #1846 server: optimization of solver algorithm
Change revision:
 /* Copyright 2009-2010 Edouard Garnier de Labareyre
  *
  * This file is part of B@ggle.
  *
  * B@ggle is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * B@ggle is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with B@ggle.  If not, see <http://www.gnu.org/licenses/>.
  */
 
package server;
 
 
import boggle.Solver;
import boggle.GameBoard;
import java.util.ArrayList;
import threads.ListenningThread;
import threads.RegisterThread;
 
 
/**
 *
 * @author edouard
 */
public class Main {
 
    //server configuration parameters
    public static int PORT = 12345;
    public static int GAME_TIME=150;
    public static int MIN_WORD_LENGHT=3;
    public static int MAX_NUMBER_OF_REQUEST=40;
    public static int MAX_NUMBER_OF_PLAYERS=6;
    public static int MAX_TIME_OF_INACTIVITY;//msec
 
    public static String PASSWORD="";
    public static String ADMIN_PASSWORD="blabla";
    public static boolean VERBOSE=false;
    public static boolean COUNT_ALL_WORDS=false;
    public static boolean REGISTER=true;
    public static String DESCRIPTION="le salon de "+System.getProperty("user.name");
    public static String WELCOME_MESSAGE="Bienvenue !";
    public static String VERSION = "1.2";
    public static String MASTER_SERVER_IP="inouire.net";
    public static int MASTER_SERVER_PORT=12340;
    public static int MAX_PAQUET_LENGTH = 500;
    public static String LANGUAGE;
 
    final static double START_DATE=System.currentTimeMillis();
 
    //game ressources
    public static boolean is_playing=false;//true if playing, false if idle
    public static boolean was_reset=false;//true if players performed a reset
    public static GameBoard game_board;
    public static Solver grid_solver;
    public static String grid="                ";
    public static ArrayList<String> solutions=new ArrayList<String>();
    public static ClientList clients;
    public static int total_number_of_words_found=0;
    public static RegisterThread registerThread;
 
    public static void printUsage(){
        System.out.println("B@ggle server version "+Main.VERSION);
        System.out.println("Usage:      \n\t-p [port]" +
                                       "\n\t-L [language (fr,en)]"+
                                       "\n\t-t [game time (sec)]" +
                                       "\n\t-n [max nb players]" +
                                       "\n\t-m [min word length]" +
                                       "\n\t-A (count points for all words found by each player)"+
                                       "\n\t-v (enable verbose mode)"+
                                       "\n\t-g (ghost mode: don't register to master server)"+
                                       "\n\t-M [master server IP]"+
                                       "\n\t-d \"description\" (server name)"+
                                       "\n\t-w \"message\" (welcome message)"+
                                       "\n\t-x [password] protect server access with password"+
                                       "\n\t-X [admin password]");
        System.out.println("All parameters are optionnal.");
    }
 
    public static void main(String[] args) {
        try {
            if(args.length>0 && (args[0].startsWith("-h")||args[0].startsWith("--h"))){
                printUsage();
                return;
            }
            Main.PORT = Utils.getIntOption("p", args, Main.PORT);
            Main.LANGUAGE=Utils.getStringOption("L", args, "fr");
            Main.GAME_TIME=Utils.getIntOption("t",args,Main.GAME_TIME);
            Main.MAX_TIME_OF_INACTIVITY=GAME_TIME*1200;//in msec
            Main.MIN_WORD_LENGHT=Utils.getIntOption("m",args,Main.MIN_WORD_LENGHT);
            Main.MAX_NUMBER_OF_PLAYERS=Utils.getIntOption("n",args,Main.MAX_NUMBER_OF_PLAYERS);
            Main.COUNT_ALL_WORDS=Utils.getOption("A",args);
            Main.VERBOSE=Utils.getOption("v",args);
            Main.REGISTER=!Utils.getOption("g",args);
            Main.MASTER_SERVER_IP=Utils.getStringOption("M",args,Main.MASTER_SERVER_IP);
            Main.DESCRIPTION=Utils.getStringOption("d",args,Main.DESCRIPTION).replaceAll(":"," ");
            Main.WELCOME_MESSAGE=Utils.getStringOption("w",args,Main.WELCOME_MESSAGE);
            Main.PASSWORD=Utils.getStringOption("x",args,Main.PASSWORD);
            Main.ADMIN_PASSWORD=Utils.getStringOption("X",args,Utils.createRandomPassword());
        } catch (NumberFormatException ex) {
            printUsage();
            System.out.println("Using default values");
        }
        
        System.out.println("B@ggle server version  "+Main.VERSION+"\nTry -help option to know more about the provided options\n");
        System.out.println(" New b@ggle room: "+Main.DESCRIPTION);
        System.out.println(" _____________________________________________");
        System.out.println("| Game time                    | "+Main.GAME_TIME+" sec");
        System.out.println("| Language                     | "+Main.LANGUAGE);
        System.out.println("| Max number of players        | "+Main.MAX_NUMBER_OF_PLAYERS);
        System.out.println("| Welcome message              | "+Main.WELCOME_MESSAGE);
        if(Main.COUNT_ALL_WORDS){
            System.out.println("|\"Count all words\" mode        |");
        }
 
        System.out.println("| Min number of letters        | "+Main.MIN_WORD_LENGHT);
        System.out.print  ("| Password                     | ");
        if(Main.PASSWORD.length()>0){
            System.out.println("\""+Main.PASSWORD+"\"");
        }else{
            System.out.println("none");
        }
        System.out.println("| Admin interface password     | "+Main.ADMIN_PASSWORD);
        System.out.print("| Register to master server    | ");
        if(Main.REGISTER){
            System.out.println("yes ");
        }else{
            System.out.println("no ");
        }
        System.out.print("| Verbose mode                 | ");
        if(Main.VERBOSE){
            System.out.println("yes ");
        }else{
            System.out.println("no ");
        }
        System.out.println  ("|______________________________|______________ ");
 
        //game board init
        game_board= new GameBoard(Main.LANGUAGE);
        
        //solver initialisation
        grid_solver=new Solver(Main.LANGUAGE);
 
        
 
        //client list initialisation
        clients = new ClientList(MAX_NUMBER_OF_PLAYERS);
 
        //start of the thread that listen to all the connections on the listenning port
        new ListenningThread().start();
 
        //start the register thread (+monitor the clients)
        registerThread = new RegisterThread();
        registerThread.start();
 
        //main loop
        while(true){
            try {
                if(!was_reset){
                    Utils.printLog("Waiting for players to be ready");
                    while(!clients.hasEnoughReadyPlayers()){
                        Thread.sleep(800);
                    }
                }
                was_reset=false;
                Utils.printLog("Starting a new game ("+clients.getNumberOfPlayers()+" players)");
                grid_solver.getSolution();
                total_number_of_words_found = 0;
                clients.broadcastGrid();
                Utils.printLog("Grid: "+grid);
                Utils.printLogIfVerbose("Solutions: "+solutions);
                grid_solver.solveGrid();
                Thread.sleep(1000);
                Main.is_playing=true;
                clients.resetWordsFound();
                clients.broadcastStartSignal();
                long start_date=System.currentTimeMillis();
                int progress=100;
                int time=0;
                while(time<GAME_TIME){
                    Thread.sleep(1000);
                    time=(int)((System.currentTimeMillis()-start_date)/1000);
                    progress = 100 - (100*time)/GAME_TIME;
                    clients.broadcast(Key.PROGRESS+":"+progress);
                    System.out.print("#");
                    if(clients.everybodyWantsToReset()){
                        was_reset=true;
                        break;
                    }
                }
                Main.is_playing=false;
                Utils.printLog("End of the game");
                clients.resetStatus();
                clients.broadcastEndSignal();
                if(!was_reset){
                    Utils.printLog("Sending results");
                    clients.broadcastResults();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            
        }
    }
 
}