diff --git a/src/server/BaseGame.java b/src/server/BaseGame.java index f953a75..cb45de0 100644 --- a/src/server/BaseGame.java +++ b/src/server/BaseGame.java @@ -31,6 +31,8 @@ public static final byte MOVE_HINT = 4; public static final byte HINT_COLOR = 0; public static final byte HINT_VALUE = 1; + public static final byte CONNECTION_VALID = 8; + public static final byte CONNECTION_INVALID = 9; final static int GREEN = 0; final static int RED = 1; @@ -43,7 +45,7 @@ final static int MAX_HINTS = 8; final static int MAX_FLASHS = 3; - int nrOfPlayers = 3; + int nrOfPlayers = 0; static int CARDS_PER_PLAYER; int cardsInDeck; @@ -61,12 +63,11 @@ List history; public BaseGame(){ - init(); + //init(); } protected void init(){ createHistory(); - //establishConnection(); CARDS_PER_PLAYER = (nrOfPlayers <= 3) ? 5 : 4; hints = MAX_HINTS; flashs = 0; diff --git a/src/server/ClientGame.java b/src/server/ClientGame.java index 90ba19a..4fd1907 100644 --- a/src/server/ClientGame.java +++ b/src/server/ClientGame.java @@ -1,9 +1,12 @@ package server; +import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.net.Socket; +import java.net.UnknownHostException; import java.util.ArrayList; import org.json.JSONException; @@ -11,8 +14,9 @@ import org.json.JSONTokener; import card.Card; +import server.ServerGame.PROTOCOL; -public abstract class ClientGame extends BaseGame { +public class ClientGame extends BaseGame { public enum MSG_TYPES {ERROR, WARNING, INFORMATION, QUESTION}; @@ -22,14 +26,59 @@ int difficulty; int fontSize; + String hostName; + boolean isConnected; + boolean initGameReceived; + int ownId; + + public static void main(String[] args){ + ClientGame client = new ClientGame(); + } + + public ClientGame(){ + preinit(); + } + + protected void preinit(){ + port = 1337; + hostName = "localhost"; + isConnected = false; + try { + socket = new Socket(hostName, port); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + new Thread(new SocketListenerThread(socket)).start(); + while(!isConnected){ + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("Waiting for connection to host"); + } + System.out.println("Connection to " + hostName + " on port " + port + " as player " + ownId + " established!"); + while(!initGameReceived){ + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("Client " + ownId + " waiting for game..."); + } + } + @Override protected void init() { super.init(); loadOptions(); loadFont(); - - SocketListenerThread listenThread = new SocketListenerThread(socket); - listenThread.run(); } @Override protected void createHistory() { @@ -99,7 +148,7 @@ } } - public abstract void printMessage(String msg, MSG_TYPES type); + //public abstract void printMessage(String msg, MSG_TYPES type); @Override protected void createUI() { @@ -121,4 +170,52 @@ // TODO return null; } + + @Override + protected void receive() { + DataInputStream inStream = null; + byte[] magic = new byte[4]; + byte proto = 0; + byte msgType = 0; + byte[] useless = new byte[2]; + try { + inStream = new DataInputStream(socket.getInputStream()); + inStream.read(magic); + proto = inStream.readByte(); + msgType = inStream.readByte(); + inStream.read(useless); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // TODO check Protocol +// switch(proto){ +// case VIBE_DUMB: +// break; +// } + + String msg = null; + JSONObject jo = null; + try { + msg = inStream.readUTF(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if(msg != null) jo = new JSONObject(msg); + else return; + // TODO MOVE_DRY + switch(msgType){ + case CONNECTION_VALID: + isConnected = true; + ownId = jo.getInt("id"); + break; + case MOVE_UPDATE: + if(!initGameReceived) initGameReceived = true; + System.out.println("Received game:"); + System.out.println(jo.toString(4)); + break; + } + } } diff --git a/src/server/ServerGame.java b/src/server/ServerGame.java index 950c250..2ca285a 100644 --- a/src/server/ServerGame.java +++ b/src/server/ServerGame.java @@ -33,27 +33,46 @@ List sockets; - public ServerGame() { + + public static void main(String[] args){ + ServerGame sg = new ServerGame(); } - @Override - protected void init() { - super.init(); + public ServerGame() { + establishConnection(); + } + + protected void establishConnection(){ + port = 1337; sockets = new ArrayList<>(); try { + server = new ServerSocket(port); while(nrOfPlayers < 2){ //TODO socket = server.accept(); sockets.add(socket); + JSONObject connectionJson = new JSONObject(); + connectionJson.put("id", nrOfPlayers); + sendToClient(CONNECTION_VALID, connectionJson.toString(), sockets.get(sockets.size()-1)); nrOfPlayers++; + System.out.println(nrOfPlayers + " players connected!"); // TODO do this somewhere else - new Thread(new SocketListenerThread(socket)).start(); + System.out.println("Creating new Socket Listener for #" + (sockets.size()-1)); + new Thread(new SocketListenerThread(sockets.get(sockets.size()-1))).start(); } } catch (IOException e) { // TODO Auto-generated catch block // TODO exit e.printStackTrace(); } + System.out.println("Initalizing game..."); + init(); + System.out.println("Sending game to clients"); + sendToAll(MOVE_UPDATE, toJson().toString()); + } + @Override + protected void init() { + super.init(); seed = 0; rand = new Random(seed); movesLeft = -1; @@ -183,13 +202,13 @@ wasMove = true; break; } - if(!isValid) send(MOVE_INVALID); + if(!isValid) sendToAll(MOVE_INVALID); if(wasMove){ currentPlayer++; if(currentPlayer == nrOfPlayers) currentPlayer = 0; // TODO notify players if(movesLeft >= 0) movesLeft--; - send(MOVE_UPDATE, toJson().toString()); + sendToAll(MOVE_UPDATE, toJson().toString()); } } @@ -261,6 +280,9 @@ protected void createPlayers() { players = new BasePlayer[nrOfPlayers]; + for(int i=0; i