package com.ezardlabs.lostsector.levels;

import com.ezardlabs.dethsquare.Camera;
import com.ezardlabs.dethsquare.GameObject;
import com.ezardlabs.dethsquare.GuiText;
import com.ezardlabs.dethsquare.Level;
import com.ezardlabs.dethsquare.LevelManager;
import com.ezardlabs.dethsquare.Screen;
import com.ezardlabs.dethsquare.TextureAtlas;
import com.ezardlabs.dethsquare.Vector2;
import com.ezardlabs.dethsquare.multiplayer.Network;

public class MultiplayerLobbyLevel extends Level {

	@Override
	public void onLoad() {
		GuiText guiText = new GuiText("Searching for game...",
				new TextureAtlas("fonts/atlas.png", "fonts/atlas.txt"), 50);
		GameObject.instantiate(new GameObject("Searching", guiText),
				new Vector2(Screen.width / 2 - guiText.getWidth() / 2, Screen.height / 2 - 25));
		GameObject.instantiate(new GameObject("Camera", new Camera(true)), new Vector2());

		Network.findGame(state -> {
//			try {
				switch (state) {
					case MATCHMAKING_SEARCHING:
						guiText.setText("Searching for a game...");
						break;
					case MATCHMAKING_FOUND:
						guiText.setText("Game found");
//						Thread.sleep(1000);
						break;
					case GAME_CONNECTING:
						guiText.setText("Connecting players...");
//						Thread.sleep(1000);
						break;
					case GAME_CONNECTED:
						guiText.setText("Players connected");
//						Thread.sleep(1000);


//						new Timer().schedule(new TimerTask() {
//							@Override
//							public void run() {
//								LevelManager.loadLevel("multiplayer");
//							}
//						}, 1000);
						LevelManager.loadLevel("multiplayer");
						break;
					default:
						break;
				}
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
		});
	}
}

@@ -416,23 +416,231 @@
 iplayer.
-Network
+Matchmaker;%0Aimport com.ezardlabs.dethsquare.multiplayer.Matchmaker.MatchmakingListener;%0Aimport com.ezardlabs.dethsquare.multiplayer.MatchmakingGame;%0A%0Aimport java.net.InetAddress;%0Aimport java.net.UnknownHostException
 ;%0A%0Apubli
@@ -1083,540 +1083,257 @@
 %0A%0A%09%09
-Network.findGame(state -%3E %7B%0A//%09%09%09try %7B%0A%09%09%09%09switch (state) %7B%0A%09%09%09%09%09case MATCHMAKING_SEARCHING:%0A%09%09%09%09%09%09guiText.setText(%22Searching for a g
+InetAddress ip = null;%0A%09%09try %7B%0A%09%09%09ip = InetAddress.getByName(%228bitwarfr
 ame.
-..
+com
 %22);%0A%09%09
-%09%09%09%09break;%0A%09%09%09%09%09case MATCHMAKING_FOUND:%0A%09%09%09%09%09%09guiText.setText(%22Game found%22);%0A//%09%09%09%09%09%09Thread.sleep(1000);%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09case GAME_CONNECTING:%0A%09%09%09%09%09%09guiText.setText(%22Connecting players...%22);%0A//%09%09%09%09%09%09Thread.sleep(1000);%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09case GAME_CONNECTED:%0A%09%09%09%09%09%09guiText.setText(%22Players connected%22);%0A//%09%09%09%09%09%09Thread.sleep(1000);%0A%0A%0A//%09%09%09%09%09%09new Timer().schedule(new TimerTask() %7B%0A//%09%09%09%09
+%7D catch (UnknownHostException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09Matchmaker matchmaker = new Matchmaker(ip, 3000);%0A%09%09matchmaker.findGame(new MatchmakingListener() %7B%0A
 %09%09%09@
@@ -1341,22 +1341,16 @@
 verride%0A
-//%09%09%09%09
 %09%09%09publi
@@ -1355,27 +1355,33 @@
 lic 
-void run() %7B%0A//%09%09%09%09
+boolean onCreateGame() %7B%0A
 %09%09%09%09
@@ -1423,43 +1423,99 @@
 %22);%0A
-//
 %09%09%09%09
+return true;%0A
 %09%09%09%7D%0A
-//%09%09%09%09%09%09%7D, 1000);%0A%09
+%0A%09%09%09@Override%0A%09%09%09public void onGameFound(MatchmakingGame game) %7B%0A
 %09%09%09%09
-%09
 Leve
@@ -1556,120 +1556,94 @@
 %0A%09%09%09
-%09%09%09break;%0A%09%09%09%09%09default:%0A%09%09%09%09%09%09break;%0A%09%09%09%09%7D%0A//%09%09%09%7D catch (InterruptedException e
+%7D%0A%0A%09%09%09@Override%0A%09%09%09public void onError(String error
 ) %7B%0A
-//
 %09%09%09%09
-e.printStackTrace(
+System.err.println(error
 );%0A
-//
 %09%09%09%7D

Add new matchmaking code