-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
232 lines (225 loc) · 10.3 KB
/
Server.java
File metadata and controls
232 lines (225 loc) · 10.3 KB
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
/**
* Final Game Server Class
* @Author Ilya Kononov
* @Date = January 22 2023
* This is the Server class of the game
* This class basically encompasses all of the lobbies where the actual games happens
* This is where the program is started
*/
//imports for network communication
import java.io.*;
import java.util.*;
import java.net.*;
public class Server {
ServerSocket serverSocket;
Socket clientSocket;
PrintWriter output;
BufferedReader input;
HashSet<PlayerHandler> handlers;
ArrayList<Lobby> lobbies;
HashMap<Integer, PlayerHandler> handlerMap;
public static void main(String[] args) throws Exception{
Server game = new Server();
game.go();
}
public void go() throws Exception{
//create a socket with the local IP address and wait for connection request
System.out.println("Launching server...");
serverSocket = new ServerSocket(Const.PORT); //create and bind a socket
System.out.println("Connected at port " + serverSocket.getLocalPort());
this.setup();
// Create a thread that updates the game state
while (true) {
clientSocket = serverSocket.accept(); //wait for connection request
System.out.println("Player connected");
PlayerHandler handler = new PlayerHandler(clientSocket);
handlers.add(handler);
handler.start();
}
}
public void setup() {
this.handlers = new HashSet<PlayerHandler>();
this.lobbies = new ArrayList<Lobby>();
this.handlerMap = new HashMap<Integer, PlayerHandler>();
}
public void cleanSockets() {
for (PlayerHandler handler: this.handlers) {
if (handler.isDead()) {
System.out.println("removed");
this.handlers.remove(handler);
}
}
}
//------------------------------------------------------------------------------
class PlayerHandler extends Thread {
private Socket socket;
private PrintWriter output;
private BufferedReader input;
public boolean inLobby = false;
private Heartbeat heartbeat;
private boolean alive = true;
private String lobbyName;
private boolean newLobby; // This variable makes it so that when the player sends the name command something different can happen depending on the state
public PlayerHandler(Socket socket){
this.socket = socket;
this.heartbeat = new Heartbeat(this);
this.heartbeat.start();
this.lobbyName = "";
this.newLobby = false;
}
// Whether the socket is dead
public boolean isDead(){
return this.socket == null || this.output == null || this.input == null;
}
public void run(){
try{
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream());
String msg;
while (true){
//receive a message from the client
msg = null;
/*try {Thread.sleep(1000 / Const.TPS);} catch (Exception e) {};
} catch (Exception e) {
e.printStackTrace();
}*/
System.out.print("");
if(!(inLobby)){ // Server doesn't need to listen to anything but this while the player is in a lobby (besides PING). After the server gets this message it means the player isn't in a lobby
try {
msg = input.readLine();
} catch (Exception e) {
this.close();
break;
}
}
if (msg != null) {
System.out.println("Message from the client: " + msg);
String[] args = msg.split(" ");
try {
if (args[0].equals(Const.PING)) { // PING
this.alive = true;
}
if (args[0].equals(Const.LOBBIES_LIST)) { // LOBBIES
this.print(Const.CLEAR_LOBBIES);
for (Lobby lobby: lobbies) {
if((lobby.playerCount() != 4 || lobby.playerCount() != 0) && !(lobby.locked())){
this.print(Const.LOBBY + " " + lobby.name() + " " + lobby.playerCount());
}
}
this.print(Const.LOBBY_SELECT);
} else if (args[0].equals(Const.NEW_LOBBY)) { // NEW
this.print(Const.NAME + " " + Const.NEW_LOBBY);
newLobby = true;
}else if (args[0].equals(Const.JOIN_LOBBY)) { // JOIN lobbyName playerName
String lobbyName = args[1];
this.print(Const.NAME + " " + Const.JOIN_LOBBY + " " + lobbyName);
}else if (args[0].equals(Const.NAME)) { // NAME lobbyName playerName
lobbyName = args[1]; // Lobby name is the (players name)'s lobby
if(newLobby){
boolean nameRepeated = true;
int repeatCounter = 2; // If there is a lobby that has the same name it will add the value of this number to the end so that the lobby names don't match Ex. Ilya2's Lobby
while(nameRepeated){
nameRepeated = false;
for (Lobby lobby: lobbies) {
if(lobby.name().equals(lobbyName + "'s")){
nameRepeated = true;
lobbyName = args[1] + repeatCounter;
repeatCounter++;
break;
}
}
}
lobbyName = lobbyName + "'s";
Lobby newLobby = new Lobby(lobbyName);
this.print(Const.JOINED + " " + lobbyName);
lobbies.add(newLobby);
inLobby = true;
newLobby.addPlayer(clientSocket, this, args[1]); // Even though the lobby name might now have a number that doesn't mean the actual players name should change
newLobby.start();
}else{
String playerName = args[2];
for (Lobby lobby: lobbies) {
if(lobby.name().equals(lobbyName) && !(lobby.locked())){
this.print(Const.JOINED + " " + lobbyName);
inLobby = true;
lobby.addPlayer(clientSocket, this, playerName);
break;
}
}
if(inLobby == false){ // This if statement will happen if the server couldn't find a lobby with the name that matches the one the client gave
this.print(Const.NO_LOBBY);
}
}
}
} catch (Exception e) {
this.print("SERVER ERROR invalid arguments");
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
this.close();
}
}
public void print(String text) {
if (this.isDead()) {
System.out.println("Dead socket, message send failure");
return;
};
output.println(text);
output.flush();
}
public void leaveLobby(boolean alive){
inLobby = false;
newLobby = false;
this.alive = alive;
for (Lobby lobby: lobbies){ // Making sure that there are no empty lobbies
if(lobby.name().equals(this.lobbyName) && lobby.playerCount() == 0){
lobbies.remove(lobby);
break;
}
}
if(!(this.alive)){
this.close();
}
}
public void close() {
this.interrupt();
// Stop the heartbeat subthread
this.heartbeat.interrupt();
try {
input.close();
output.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
this.socket = null;
}
// If there is no "heartbeat" from the client for 60 seconds, assume the connection has failed
class Heartbeat extends Thread {
PlayerHandler playerHandler;
Heartbeat(PlayerHandler playerHandler) {
this.playerHandler = playerHandler;
}
public void run() {
while (true) {
try {
Thread.sleep(Const.HEARTBEAT_RATE);
} catch (Exception e) {}
if(!(playerHandler.inLobby)){
System.out.println("Check for heartbeat");
if (this.playerHandler.alive) {
this.playerHandler.alive = false;
} else {
System.out.println("test");
this.playerHandler.close();
break;
}
}
}
}
}
}
}