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: | |
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* 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(); } } } }

B@ggle