Some bug fix
This commit is contained in:
+53
-12
@@ -4,6 +4,8 @@ import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class Room
|
||||
{
|
||||
@@ -18,8 +20,13 @@ public class Room
|
||||
|
||||
private int currentPlayer = 0;
|
||||
|
||||
private long timeOut = 0;
|
||||
private Timer t;
|
||||
|
||||
public Room(String roomCode, Server server)
|
||||
{
|
||||
timeOut = System.currentTimeMillis();
|
||||
|
||||
this.roomCode = roomCode;
|
||||
startPositions = new Vector3[4];
|
||||
|
||||
@@ -29,10 +36,31 @@ public class Room
|
||||
startPositions[3] = new Vector3(92, 1, 128);
|
||||
|
||||
this.server = server;
|
||||
|
||||
try
|
||||
{
|
||||
t = new Timer();
|
||||
TimerTask tt = new TimerTask()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if(System.currentTimeMillis() - timeOut > 600000)
|
||||
{
|
||||
System.out.println("[" + Server.getDateTime() + "] Room " + roomCode + " closed due to timeout!");
|
||||
server.deleteRoom(roomCode);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
t.scheduleAtFixedRate(tt, 0, 60000);
|
||||
}catch(Exception e){}
|
||||
}
|
||||
|
||||
public void addClient(Client client)
|
||||
{
|
||||
timeOut = System.currentTimeMillis();
|
||||
|
||||
for (int i = 0; i < players.length; i++)
|
||||
{
|
||||
if (players[i] == null)
|
||||
@@ -40,7 +68,7 @@ public class Room
|
||||
players[i] = client;
|
||||
playerCount++;
|
||||
|
||||
System.out.println("Client joined room " + roomCode + " successfully as player " + i);
|
||||
System.out.println("[" + Server.getDateTime() + "] Client joined room " + roomCode + " successfully as player " + i);
|
||||
|
||||
Thread t = new Thread(new Runnable()
|
||||
{
|
||||
@@ -66,6 +94,8 @@ public class Room
|
||||
|
||||
input.read(readData);
|
||||
|
||||
timeOut = System.currentTimeMillis();
|
||||
|
||||
switch (readData[0])
|
||||
{
|
||||
|
||||
@@ -183,39 +213,50 @@ public class Room
|
||||
|
||||
public void closeRoom()
|
||||
{
|
||||
t.cancel();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
players[0].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
if(players[0] != null)
|
||||
{
|
||||
players[0].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
}
|
||||
|
||||
} catch (IOException ex)
|
||||
} catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(players[1] != null)
|
||||
{
|
||||
players[1].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
}
|
||||
|
||||
players[1].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
|
||||
} catch (IOException ex)
|
||||
} catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(players[2] != null)
|
||||
{
|
||||
players[2].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
}
|
||||
|
||||
players[2].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
|
||||
} catch (IOException ex)
|
||||
} catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(players[3] != null)
|
||||
{
|
||||
players[3].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
}
|
||||
|
||||
players[3].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
|
||||
} catch (IOException ex)
|
||||
} catch (Exception ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
+24
-4
@@ -4,6 +4,8 @@ import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Server
|
||||
@@ -13,26 +15,36 @@ public class Server
|
||||
|
||||
private final Server server = this;
|
||||
|
||||
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
|
||||
|
||||
public static String getDateTime()
|
||||
{
|
||||
return dtf.format(LocalDateTime.now());
|
||||
}
|
||||
|
||||
public void deleteRoom(String code)
|
||||
{
|
||||
rooms.get(code).closeRoom();
|
||||
rooms.remove(code);
|
||||
System.out.println("Room " + code + " successfully deleted");
|
||||
|
||||
|
||||
System.out.println("[" + getDateTime() + "] Room" + code + " successfully deleted");
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
serverSocket = new ServerSocket(8051);
|
||||
|
||||
System.out.println("Server started! IP 185.245.96.48 PORT 8051");
|
||||
System.out.println("[" + getDateTime() + "] Server started! IP 185.245.96.48 PORT 8051");
|
||||
|
||||
while(true)
|
||||
{
|
||||
Socket tempClient = serverSocket.accept();
|
||||
|
||||
System.out.println("Client with IP " + tempClient.getInetAddress().getHostName() + " connected!");
|
||||
System.out.println("[" + getDateTime() + "] Client with IP " + tempClient.getInetAddress().getHostName() + " connected!");
|
||||
|
||||
new Thread(new Runnable()
|
||||
{
|
||||
@@ -58,7 +70,7 @@ public class Server
|
||||
Room room = new Room(new String(code, StandardCharsets.US_ASCII), server);
|
||||
rooms.put(new String(code, StandardCharsets.US_ASCII), room);
|
||||
|
||||
System.out.println("Room " + new String(code, StandardCharsets.US_ASCII) + " successfully created!");
|
||||
System.out.println("[" + getDateTime() + "] Room " + new String(code, StandardCharsets.US_ASCII) + " successfully created!");
|
||||
break;
|
||||
case 1:
|
||||
|
||||
@@ -106,6 +118,14 @@ public class Server
|
||||
}
|
||||
}
|
||||
|
||||
String roomCode = new String(code, StandardCharsets.US_ASCII);
|
||||
|
||||
//avoide double codes (just in case^^)
|
||||
if(rooms.containsKey(roomCode))
|
||||
{
|
||||
code = generateRoomCode();
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user