|
@@ -19,23 +19,23 @@ public class NetworkConnection {
|
|
|
private PlayState play;
|
|
private PlayState play;
|
|
|
private Socket server;
|
|
private Socket server;
|
|
|
|
|
|
|
|
- //Connection to the server
|
|
|
|
|
|
|
+ // Connection to the server
|
|
|
public NetworkConnection(PlayState play) {
|
|
public NetworkConnection(PlayState play) {
|
|
|
this.play = play;
|
|
this.play = play;
|
|
|
play.setOverlayText("Waiting for other players..");
|
|
play.setOverlayText("Waiting for other players..");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Try to connect to the server
|
|
|
|
|
|
|
+ // Try to connect to the server
|
|
|
public void connect(String name) throws UnknownHostException, IOException {
|
|
public void connect(String name) throws UnknownHostException, IOException {
|
|
|
if (checkConnection()) {
|
|
if (checkConnection()) {
|
|
|
throw new IOException("Already connected!");
|
|
throw new IOException("Already connected!");
|
|
|
}
|
|
}
|
|
|
- try{
|
|
|
|
|
|
|
+ try {
|
|
|
server = new Socket(address, port);
|
|
server = new Socket(address, port);
|
|
|
dout = new DataOutputStream(server.getOutputStream());
|
|
dout = new DataOutputStream(server.getOutputStream());
|
|
|
din = new DataInputStream(server.getInputStream());
|
|
din = new DataInputStream(server.getInputStream());
|
|
|
dout.writeUTF(name);
|
|
dout.writeUTF(name);
|
|
|
- }catch (ConnectException e){
|
|
|
|
|
|
|
+ } catch (ConnectException e) {
|
|
|
play.setOverlayText("Can't connect to the server \n Press ESC to return to the mainmenu");
|
|
play.setOverlayText("Can't connect to the server \n Press ESC to return to the mainmenu");
|
|
|
System.err.println("Can't connect to the server");
|
|
System.err.println("Can't connect to the server");
|
|
|
throw new IOException("Can't connect to the server");
|
|
throw new IOException("Can't connect to the server");
|
|
@@ -51,7 +51,7 @@ public class NetworkConnection {
|
|
|
receivethread.start();
|
|
receivethread.start();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Check if the connection already exists
|
|
|
|
|
|
|
+ // Check if the connection already exists
|
|
|
public boolean checkConnection() {
|
|
public boolean checkConnection() {
|
|
|
if (dout == null)
|
|
if (dout == null)
|
|
|
return false;
|
|
return false;
|
|
@@ -63,7 +63,7 @@ public class NetworkConnection {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Receive a message from the server and decode it
|
|
|
|
|
|
|
+ // Receive a message from the server and decode it
|
|
|
private void receiveMessage() {
|
|
private void receiveMessage() {
|
|
|
try {
|
|
try {
|
|
|
String message = din.readUTF();
|
|
String message = din.readUTF();
|
|
@@ -75,12 +75,12 @@ public class NetworkConnection {
|
|
|
case "2": // Update message
|
|
case "2": // Update message
|
|
|
messagesplit = message.split(",");
|
|
messagesplit = message.split(",");
|
|
|
|
|
|
|
|
- //Update players
|
|
|
|
|
|
|
+ // Update players
|
|
|
String[] playersplit = messagesplit[0].split("\\|");
|
|
String[] playersplit = messagesplit[0].split("\\|");
|
|
|
play.updatePlayerInfo(Integer.parseInt(playersplit[1]), Integer.parseInt(playersplit[2]), Integer.parseInt(playersplit[3]), Integer.parseInt(playersplit[4]), play.getPlayer1());
|
|
play.updatePlayerInfo(Integer.parseInt(playersplit[1]), Integer.parseInt(playersplit[2]), Integer.parseInt(playersplit[3]), Integer.parseInt(playersplit[4]), play.getPlayer1());
|
|
|
play.updatePlayerInfo(Integer.parseInt(playersplit[5]), Integer.parseInt(playersplit[6]), Integer.parseInt(playersplit[7]), Integer.parseInt(playersplit[8]), play.getPlayer2());
|
|
play.updatePlayerInfo(Integer.parseInt(playersplit[5]), Integer.parseInt(playersplit[6]), Integer.parseInt(playersplit[7]), Integer.parseInt(playersplit[8]), play.getPlayer2());
|
|
|
|
|
|
|
|
- //Update Balls
|
|
|
|
|
|
|
+ // Update Balls
|
|
|
String[] ballsplit = messagesplit[1].split("\\|");
|
|
String[] ballsplit = messagesplit[1].split("\\|");
|
|
|
if (ballsplit.length / 4 != play.getBalls().size()) {
|
|
if (ballsplit.length / 4 != play.getBalls().size()) {
|
|
|
play.getBalls().clear();
|
|
play.getBalls().clear();
|
|
@@ -89,21 +89,21 @@ public class NetworkConnection {
|
|
|
play.getBall(i).updateInfo(Integer.parseInt(ballsplit[(i * 4) + 0]), Integer.parseInt(ballsplit[(i * 4) + 1]), Color.decode(ballsplit[(i * 4) + 3]), Integer.parseInt(ballsplit[(i * 4) + 2]));
|
|
play.getBall(i).updateInfo(Integer.parseInt(ballsplit[(i * 4) + 0]), Integer.parseInt(ballsplit[(i * 4) + 1]), Color.decode(ballsplit[(i * 4) + 3]), Integer.parseInt(ballsplit[(i * 4) + 2]));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Update lines
|
|
|
|
|
|
|
+ // Update lines
|
|
|
String[] linesplit = messagesplit[2].split("\\|");
|
|
String[] linesplit = messagesplit[2].split("\\|");
|
|
|
play.updateLines(Integer.parseInt(linesplit[0]), Integer.parseInt(linesplit[1]), Integer.parseInt(linesplit[2]), Integer.parseInt(linesplit[3]), Integer.parseInt(linesplit[4]), Integer.parseInt(linesplit[5]));
|
|
play.updateLines(Integer.parseInt(linesplit[0]), Integer.parseInt(linesplit[1]), Integer.parseInt(linesplit[2]), Integer.parseInt(linesplit[3]), Integer.parseInt(linesplit[4]), Integer.parseInt(linesplit[5]));
|
|
|
-
|
|
|
|
|
- //Update time
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Update time
|
|
|
play.setTimeleftpercent(Integer.parseInt(messagesplit[3]));
|
|
play.setTimeleftpercent(Integer.parseInt(messagesplit[3]));
|
|
|
break;
|
|
break;
|
|
|
- case "3": //Overlay text
|
|
|
|
|
|
|
+ case "3": // Overlay text
|
|
|
play.setOverlayText(messagesplit[1]);
|
|
play.setOverlayText(messagesplit[1]);
|
|
|
break;
|
|
break;
|
|
|
- case "4": //Chat Message
|
|
|
|
|
|
|
+ case "4": // Chat Message
|
|
|
play.addChatMessage(messagesplit[1]);
|
|
play.addChatMessage(messagesplit[1]);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
play.setOverlayText("Connection to the server closed \n Press ESC to return to the mainmenu");
|
|
play.setOverlayText("Connection to the server closed \n Press ESC to return to the mainmenu");
|
|
|
play.stopMatch();
|
|
play.stopMatch();
|
|
@@ -113,26 +113,25 @@ public class NetworkConnection {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Send control request to server
|
|
|
|
|
|
|
+ // Send control request to server
|
|
|
public void sendInput(int direction, int shoot) {
|
|
public void sendInput(int direction, int shoot) {
|
|
|
sendMessage("1|" + direction + "|" + shoot);
|
|
sendMessage("1|" + direction + "|" + shoot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Send chat message to server
|
|
|
|
|
- public void sendChatMessage(String str)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ // Send chat message to server
|
|
|
|
|
+ public void sendChatMessage(String str) {
|
|
|
sendMessage("2|" + str);
|
|
sendMessage("2|" + str);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- //Close connection to server and stop the thread
|
|
|
|
|
- public void close() throws IOException{
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Close connection to server and stop the thread
|
|
|
|
|
+ public void close() throws IOException {
|
|
|
receivethread.stop();
|
|
receivethread.stop();
|
|
|
din.close();
|
|
din.close();
|
|
|
dout.close();
|
|
dout.close();
|
|
|
server.close();
|
|
server.close();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Send a message
|
|
|
|
|
|
|
+ // Send a message
|
|
|
private void sendMessage(String m) {
|
|
private void sendMessage(String m) {
|
|
|
try {
|
|
try {
|
|
|
dout.writeUTF(m);
|
|
dout.writeUTF(m);
|