package com.github.versatilevelociraptor.safevelociraptorserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

public class Server {

	public static final int PORT = 2585;
	private ServerSocket serverSocket;
	private Controller controller;
	private RobotController roboController;
	public static final int MAX_CLIENTS = 2;

	public Server() {
		try {
			serverSocket = new ServerSocket(PORT);
			serverSocket.setReuseAddress(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		initializeConnections();
	}

	public void initializeConnections() {
		Socket client;
		int connectedClients = 0;
		boolean controlerConnected = false , robotControllerConnected = false;
		while(connectedClients < 1) {
			try {
				System.out.println("Looking for clients..");
				client = serverSocket.accept();
				System.out.println("Loocking for clients");
				BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
				String ID = reader.readLine();
				System.out.println(ID);
				if(ID.equals("" + Controller.ID) && !controlerConnected) {
					controller = new Controller(client);
					controller.sendCommand("Michael is a gypsy");
					System.out.println("Client Connected!");
					connectedClients++;
					controlerConnected = true;
				} else if(ID.equals("" + RobotController.ID) && !robotControllerConnected) {
					InetAddress address = client.getInetAddress();
					System.out.println(address.getHostAddress() + " " + address.getHostName());
					roboController = new RobotController(client, address);
					System.out.println("Client Connected!");
					//roboController.sendCommand("Michael is a gypsy");
					connectedClients++;
					robotControllerConnected = true;
				} else {
					System.out.println("NOT CORRECT ID");
				}
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
	}

	public void restart() {
		initializeConnections();
		createCommunicationSession();
	}

	public synchronized void setController() {

	}

	public void createCommunicationSession() {
		System.out.println("Created");
		new Thread(new ServerThread(controller, roboController)).start();
	}

	public static void main(String[] args) {
		Server server = new Server();
		server.createCommunicationSession();
	}

	private class ServerThread implements Runnable{
		private Client send;
		private Client recieve;

		public ServerThread(Client send, Client recieve) {
			this.send = send;
			this.recieve = recieve;
		}


		/* (non-Javadoc)
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run() {
			try {
				while(true) {
					String command = send.recieveCommand();
					int[] data;
					if(command != null) {
						data = Arrays.asList(command.split(" ")).stream().map(s -> Integer.parseInt(s)).mapToInt(Integer::intValue).toArray();
						System.out.println(Arrays.toString(data));
						if(recieve instanceof RobotController) {							
							if(data[0] != 0) {
								recieve.sendCommand(data[0] > 0 ? "L" : "R");
							}
							if(data[2] > 0)
								recieve.sendCommand("F");
							if(data[1] != 0)
								recieve.sendCommand(data[1] > 0 ? "D" : "U");
						}
					}

					Thread.sleep(10);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
				recieve.sendInt(2587);
				send = null;
				while(send == null) {
					try {
						Socket sock = serverSocket.accept();
						send = new Controller(sock);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			} 
		}	
	}
}
//You will never resolve SD1
@@ -853,17 +853,17 @@
 ients %3C 
-1
+2
 ) %7B%0A%09%09%09t
@@ -2596,16 +2596,88 @@
 e;%0A%09%09%7D%0A%0A
+%09%09public boolean isInDeadZone(double value) %7B%0A%09%09%09return value == 0;%0A%09%09%7D%0A
 %0A%09%09/* (n
@@ -3099,23 +3099,16 @@
 oller) %7B
-%09%09%09%09%09%09%09
 %0A%09%09%09%09%09%09%09
@@ -3114,21 +3114,126 @@
 %09if(
-data%5B0%5D != 0)
+isInDeadZone(data%5B0%5D) && isInDeadZone(data%5B1%5D) && isInDeadZone(data%5B2%5D))%0A%09%09%09%09%09%09%09%09recieve.sendCommand(%22S%22);%0A%09%09%09%09%09%09%09else
  %7B%0A%09
@@ -3286,24 +3286,16 @@
  %22R%22);%0A%09
-%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%09i
@@ -3305,24 +3305,25 @@
 ata%5B2%5D %3E 0)%0A
+%09
 %09%09%09%09%09%09%09%09reci
@@ -3348,32 +3348,8 @@
 %22);%0A
-%09%09%09%09%09%09%09if(data%5B1%5D != 0)%0A
 %09%09%09%09
@@ -3394,24 +3394,33 @@
 %22D%22 : %22U%22);%0A
+%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%7D%0A%09%09%09%09
@@ -3763,16 +3763,18 @@
 %7D%09%0A%09%7D%0A%7D%0A
+%0A%0A
 //You wi

Add deadzone