Browse Source

Some better exception handeling & return to mainmenu

jancoow 10 years ago
parent
commit
3a0c3d9f56

+ 8 - 2
server/server/NetworkUser.java

@@ -22,7 +22,7 @@ public class NetworkUser {
 			dout = new DataOutputStream(client.getOutputStream());
 			din = new DataInputStream(client.getInputStream());
 		} catch (IOException e) {
-			System.err.println("Somethings nasty happends with a user");
+			match.stopMatch();
 		}
 		try {
 			client.setSoTimeout(5000);
@@ -91,10 +91,16 @@ public class NetworkUser {
 			}
 		} catch (IOException e) {
 			System.err.println("User disconnected");
-			receivethread.stop();
 			match.stopMatch();
 		}
 	}
+	
+	public void close() throws IOException{
+		receivethread.stop();
+		din.close();
+		dout.close();
+		client.close();
+	}
 
 	/* GETTERS AND SETTERS */
 

+ 15 - 2
server/server/match/Match.java

@@ -1,5 +1,6 @@
 package server.match;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.ConcurrentModificationException;
 import java.util.ListIterator;
@@ -216,13 +217,25 @@ public class Match implements Runnable {
 
 	public void playerShoot(int pid) {
 		if (pid == 1)
-			line1 = new ShootingLine(player1.getX() + player1.getWidth() / 2, player1.getY() + player1.getHeigth());
+			if(line1 == null)
+				line1 = new ShootingLine(player1.getX() + player1.getWidth() / 2, player1.getY() + player1.getHeigth());
 		if (pid == 2)
-			line2 = new ShootingLine(player2.getX() + player2.getWidth() / 2, player2.getY() + player2.getHeigth());
+			if(line2 == null)
+				line2 = new ShootingLine(player2.getX() + player2.getWidth() / 2, player2.getY() + player2.getHeigth());
 	}
 
 	public void stopMatch() {
 		timer.cancel();
+		try {
+			nwuser1.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		try {
+			nwuser2.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 	}
 
 	@Override

+ 12 - 13
src/model/NetworkConnection.java

@@ -17,6 +17,7 @@ public class NetworkConnection {
 	private DataInputStream din;
 	private Thread receivethread;
 	private PlayState play;
+	private Socket server;
 
 	public NetworkConnection(PlayState play) {
 		this.play = play;
@@ -28,12 +29,12 @@ public class NetworkConnection {
 			throw new IOException("Already connected!");
 		}
 		try{
-			Socket server = new Socket(address, port);
+			server = new Socket(address, port);
 			dout = new DataOutputStream(server.getOutputStream());
 			din = new DataInputStream(server.getInputStream());
 			dout.writeUTF(name);
 		}catch (ConnectException e){
-			play.setOverlayText("Can't connect to the server");
+			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");
 			throw new IOException("Can't connect to the server");
 		}
@@ -91,7 +92,8 @@ public class NetworkConnection {
 				play.setOverlayText(messagesplit[1]);
 			}
 		} catch (IOException e) {
-			play.setOverlayText("Connection to the server closed");
+			play.setOverlayText("Connection to the server closed \n Press ESC to return to the mainmenu");
+			play.stopMatch();
 			System.err.println("Connection to the server closed");
 			receivethread.stop();
 		}
@@ -101,6 +103,13 @@ public class NetworkConnection {
 	public void sendInput(int direction, int shoot) {
 		sendMessage("1|" + direction + "|" + shoot);
 	}
+	
+	public void close() throws IOException{
+		receivethread.stop();
+		din.close();
+		dout.close();
+		server.close();
+	}
 
 	private void sendMessage(String m) {
 		try {
@@ -109,14 +118,4 @@ public class NetworkConnection {
 			System.err.println("Connection to server closed");
 		}
 	}
-	
-	public void close()
-	{
-		try {
-			din.close();
-			dout.close();
-		} catch (IOException e) {
-		}
-		receivethread.stop();
-	}
 }

+ 28 - 6
src/model/state/PlayState.java

@@ -26,13 +26,10 @@ public class PlayState extends State {
 	private NetworkConnection nwc;
 	private int timeleftpercent, direction, shoot;
 	private String overlayText;
+	private boolean matchStarted;
 
 	public PlayState(GameStateManager gsm) {
 		super("play", gsm);
-		player1 = new Player(Images.getImage(ImageType.PLAYER1), 40, 550, "");
-		player2 = new Player(Images.getImage(ImageType.PLAYER2), 90, 550, "");
-		infopanel = new InfoPanel(this);
-		balls = new ArrayList<Ball>();
 	}
 
 	/* NETWORK CONTROL */
@@ -41,16 +38,29 @@ public class PlayState extends State {
 		player2 = new Player(Images.getImage(ImageType.PLAYER2), 90, 550, p2);
 		infopanel = new InfoPanel(this);
 		balls = new ArrayList<Ball>();
+		matchStarted = true;
 	}
 
 	/* INIT AND EXIT */
 	@Override
 	public void init() {
 		nwc = MatchMakingState.nwc;
+		matchStarted = false;
+		nwc = new NetworkConnection(this);
+		try {
+			nwc.connect(MenuState.name);
+		} catch (IOException e) {
+		}
 	}
 
 	@Override
 	public void exit() {
+		try {
+			matchStarted = false;
+			nwc.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 	}
 
 	/* UPDATE AND PAINTING */
@@ -83,6 +93,11 @@ public class PlayState extends State {
 	@Override
 	public void paint(Graphics2D g2d) {
 		g2d.drawImage(Images.getImage(ImageType.BACKGROUND), null, 0, 0);
+		g2d.setColor(Color.RED);
+		g2d.drawString(overlayText, 100, 300);
+		g2d.setColor(Color.black);
+		if(!matchStarted)
+			return;
 		if (line1 != null) {
 			line1.paint(g2d);
 		}
@@ -95,8 +110,6 @@ public class PlayState extends State {
 		player1.paint(g2d);
 		player2.paint(g2d);
 		infopanel.paint(g2d);
-		g2d.setColor(Color.RED);
-		g2d.drawString(overlayText, 100, 300);
 	}
 
 	/* GETTERS AND SETTERS */
@@ -133,6 +146,15 @@ public class PlayState extends State {
 	public void setOverlayText(String text){
 		this.overlayText = text;
 	}
+	
+	public void stopMatch(){
+		try {
+			nwc.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		matchStarted = false;
+	}
 	/* EVENTS */
 	@Override
 	public void keyPressed(KeyEvent e) {