-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaFinalHangmanProjectServer.java
324 lines (274 loc) · 11.1 KB
/
JavaFinalHangmanProjectServer.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package javafinalhangmanprojectserver;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
//IMPORTANT!!!
//Server must be started Before any clients can connect
public class JavaFinalHangmanProjectServer {
static List<GameServer> servers = new ArrayList<>();
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(5190);
//Constantly look for new clients and connect them to servers
while (true){
Socket client_sock = ss.accept();
new ProcessConnection(client_sock, servers).start();
}
} catch (IOException ex) {
System.out.println("Could not listen on port 5190");
}
}
}
class ProcessConnection extends Thread{
Socket client_sock;
static List<GameServer> servers;
GameServer activeServer;
String username;
int newServerStatus; //want to make new server or not
ProcessConnection(Socket newClientSock, List<GameServer> servers1){
client_sock = newClientSock;
servers = servers1;
username = "";
}
@Override
public void run(){
try{
Scanner sin = new Scanner(client_sock.getInputStream());
PrintStream sout = new PrintStream(client_sock.getOutputStream());
String server = "";
booleanServer boolServ;
//Get username and server
while(("".equals(server)) && ("".equals(username))){
//see if user wants to make new server
String line = sin.nextLine();
if(line.equals("New Server")){
newServerStatus = 1;
}else if ("Existing Server".equals(line)){
newServerStatus = 0;
}
//Differect actions if want to make new server or not
server = sin.nextLine();
if(newServerStatus == 1){
//Make new server
boolServ = checkIfExistingServerName(server);
if(boolServ.status){
//Server Name Already Exists
//Send Error Message
sout.println("Error");
server = "";
username = "";
}else{
//Server Name Validated
sout.println("Server GOOD");
//Set up new Game Group
GameServer newServer = new GameServer(server, client_sock);
activeServer = newServer;
servers.add(newServer);
//get username
username = sin.nextLine();
}
}else{
// Join Existing Server
boolServ = checkIfExistingServerName(server);
if(!boolServ.status){
//Server Does not Exist
//Return Error
sout.println("Error");
server = "";
username = "";
}else{
//Server Exists
sout.println("Server GOOD");
//Check Participant Limit and If Game Started
line = sin.nextLine();
if(!boolServ.server.gameStarted && (boolServ.server.clients.size() < 4)){
// Space Open and Game Did Not Start
activeServer = boolServ.server;
//Get Username
sout.println("Username Good");
//Set Up New Game Group
boolServ.server.addClient(client_sock);
username = line;
}else{
//Game Started or No Space Open
sout.println("Error");
server = "";
username = "";
}
}
}
}
//Username and Server Set Up
sout.println("Username Good");
//Owner Server Handles all Interaction Between Clients
if(newServerStatus==1){
//Owner Server Actions
sout.println("Owner");
//Recieve Word From Owner
String line = sin.nextLine();
activeServer.Word = line;
activeServer.WordLeft = line;
//Start Game Announcement, Send Answer Word
sout.println("START GAME");
sout.println(activeServer.Word);
for(Socket client: activeServer.clients){
PrintStream soutClient = new PrintStream(client.getOutputStream());
soutClient.println("START GAME");
soutClient.println(activeServer.Word);
}
//Handle Game Interaction
while((activeServer.WordLeft.length() > 0) && (activeServer.Errors < 6)){
//Loop Through Group Participants
for(Socket client: activeServer.clients){
//Send Correct Word Progress
sendAll("WP:" + blankMaker(activeServer.Word, activeServer.LettersCorrect), 3);
//Send Incorrect Word Progress
sendAll("INCORRECT:" + activeServer.LettersIncorrect, 3);
if((activeServer.WordLeft.length() <= 0)||(activeServer.Errors >= 6)){
//Word Sucessfully Guessed or Error Limit Reached
break;
}
//Ask Participant to send Letter
PrintStream soutClient = new PrintStream(client.getOutputStream());
Scanner sinClient = new Scanner(client.getInputStream());
soutClient.println("Your Turn");
//Get Letter From Participant
String letter = sinClient.nextLine();
ProcessLetter(activeServer, letter);
//Send Letter Guessed to All Players
sendAll("Letter Guessed: " + letter, 3);
}
}
//Game Ended
if(activeServer.Errors < 6){
//Player Wins
sendAll("WIN", 2);
sendAll("LOSE", 1);
}else{
//Owner Wins
sendAll("WIN", 1);
sendAll("LOSE", 2);
}
//Clean Up Server by Closing All Client Server Connection and Own Connection
removeServer();
client_sock.close();
}else{
//Participant Server Tasks Complete
//Remaining Tasks Including Closing Socket is Done on Game Owner Server
sout.println("Participant");
}
}
catch(IOException e){
System.out.println("Error reading client message");
}
}
//Input Current Game Server and New Letter From Participant
void ProcessLetter(GameServer activeServer, String letter){
if(activeServer.WordLeft.contains(letter)){
//Letter is Correct
activeServer.WordLeft = activeServer.WordLeft.replace(letter, "");
activeServer.LettersCorrect += letter;
}else{
//Letter Is Incorrect
activeServer.Errors += 1;
if(activeServer.LettersIncorrect.length() ==0){
activeServer.LettersIncorrect+=letter;
}else{
activeServer.LettersIncorrect+=','+letter;
}
}
}
//Remove All Participant Sockets Server By Closing Them
void removeServer(){
for(Socket clientSocket: activeServer.clients){
try{
clientSocket.close();
}
catch(IOException e){
System.out.println("Error reading client message");
}
}
servers.remove(activeServer);
}
//Input: Game Group Name (String)
//Output: If Currently Exists Game Server With Such Name (Boolean)
booleanServer checkIfExistingServerName(String newServerName){
for(GameServer server: servers){
if(server.serverName.equals(newServerName)){
return new booleanServer(true, server);
}
}
return new booleanServer(false);
}
//Make Blank String To Sent To Players
String blankMaker(String word, String LetterCorrect){
String result = "";
for (int i = 0; i < word.length(); i++) {
if(LetterCorrect.indexOf(word.toCharArray()[i])!=-1){
result+=word.toCharArray()[i];
}else{
result+="_";
}
}
return result;
}
//Send Message to All Players and Owner Combination
void sendAll(String msg, int mode){
try{
if((mode == 1) || (mode == 3)){
PrintStream sout = new PrintStream(client_sock.getOutputStream());
sout.println(msg);
}
if((mode == 2) || (mode == 3)){
for(Socket client: activeServer.clients){
PrintStream soutClient = new PrintStream(client.getOutputStream());
soutClient.println(msg);
}
}
}
catch(IOException e){
System.out.println("Error Sending Message To All");
}
}
}
//Class to Showing Server and Boolean
class booleanServer{
boolean status;
GameServer server;
booleanServer(boolean status1, GameServer server1){
status = status1;
server = server1;
}
booleanServer(boolean status1){
status = status1;
}
}
//Server For Each Game
class GameServer{
String serverName;
Socket owner;
List<Socket> clients = new ArrayList<>();
boolean gameStarted;
String Word;
String WordLeft;
int Errors;
String LettersCorrect;
String LettersIncorrect;
GameServer(String serverName1, Socket owner1){
serverName = serverName1;
owner = owner1;
gameStarted = false;
Word = "";
WordLeft = "";
LettersCorrect = "";
LettersIncorrect="";
}
//Add Participant to Game Group
void addClient(Socket client_sock){
clients.add(client_sock);
}
}