diff --git a/src/MainTest.java b/src/MainTest.java index 73bbcbb..9c2fea3 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -6,7 +6,6 @@ import org.json.JSONObject; import org.json.JSONTokener; -import card.Card; import player.BasePlayer; public class MainTest { diff --git a/src/card/Card.java b/src/card/Card.java index e5125a0..7deee28 100644 --- a/src/card/Card.java +++ b/src/card/Card.java @@ -1,7 +1,7 @@ package card; import server.InternalHanabiError; -import server.Move; +import server.Message; import java.util.ArrayList; import java.util.List; @@ -46,7 +46,7 @@ cardInfos = new ArrayList<>(); } - public Card(Move m) { + public Card(Message m) { this(); try { fromJson(m.getJson()); @@ -137,7 +137,7 @@ JSONArray cardInfoArray = jo.getJSONArray("cardInfos"); for(int i=0; i 0 && b < MSG_FIRST_MSG) return MsgByteCat.MOVE; + if(b >= MSG_FIRST_MSG && b <= MSG_LAST_MSG) return MsgByteCat.MSG; + return MsgByteCat.INVALID; + } + + private static void throwOnWrongMsgByte(byte b, MsgByteCat shouldBe) { + MsgByteCat is = msgByteCat(b); + if(is != shouldBe) { + throw new InternalHanabiError("Message type is " + is + ", but it should be " + shouldBe + "."); + } + } + + private int origin; + private byte msgType; + private Move move; + //private JSONObject jo; + + public Message(byte mT) { this(-1, mT); } + public Message(byte mT, Move m) { this(-1, mT, m); } + + public Message(int o, byte mT) { + throwOnWrongMsgByte(mT, MsgByteCat.MSG); + origin = o; + setMsgType(mT); + } + + public Message(int o, byte moveType, Move m) { + throwOnWrongMsgByte(moveType, MsgByteCat.MOVE); + origin = o; + setMsgType(moveType); + move = m; + } + + //public Message(JSONObject jo) throws JSONException { + //this.jo = (JSONObject)jo.clone(); + //this.jo = jo; // TODO: clone? +// moveType = (byte)jo.getInt("msgType"); + //} + +// public static Message newMove(Card c) { +// try { +// return new Message(c.toJson()); +// } catch(JSONException ex) { +// throw new InternalHanabiError(ex); +// } +// } + + public Message(byte[] bytes) { + throw new RuntimeException("Not implemented. This is a bug."); + } + + public byte[] toBytes() { + return toBytes(ServerGame.PROTO_VIBE_DUMB); + } + + public byte[] toBytes(byte proto) { + switch(proto) { + case ServerGame.PROTO_VIBE_DUMB: + throw new RuntimeException("Not implemented. This is a bug."); + default: + throw new InternalHanabiError("Message.toBytes(byte) called with invalid protocol code " + proto + "."); + } + } + +// public JSONObject getJson() { +// return jo; +// } +// public String toJsonString() { +// if(jo == null) return ""; +// return jo.toString(); +// } + public byte getMsgType() { + return msgType; + } + + public int getOrigin() { + return origin; +// try { +// return jo.getInt("origin"); +// } catch(JSONException ex) { +// throw new InternalHanabiError(ex); +// } + } + + public void setOrigin(int orig) { origin = orig; } + + public int getHintType() { + if(msgType != Move.HINT) { + throw new RuntimeException("Internal Error: Hint type of non-hint move requested."); + } + try { + return jo.getInt("type"); + } catch(JSONException ex) { + throw new InternalHanabiError(ex); + } + } + public int getValue() { + if(msgType != Move.HINT) { + throw new RuntimeException("Internal Error: Hint value of non-hint message requested."); + } + try { + return jo.getInt("value"); + } catch(JSONException ex) { + throw new InternalHanabiError(ex); + } + } + public int getHintRecipient() { + if(msgType != Move.HINT) { + throw new RuntimeException("Internal Error: Target player ID of non-hint message requested."); + } + try { + return jo.getInt("playerId"); + } catch(JSONException ex) { + throw new InternalHanabiError(ex); + } + } + + public int getOwnId() { + if(msgType != MSG_CONNECTION_VALID) { + throw new RuntimeException("Internal Error: ownId of non-connection valid message requested."); + } + try { + return jo.getInt("id"); + } catch(JSONException ex) { + throw new InternalHanabiError(ex); + } + } + + public static Message makeInvalidMoveAnswer(Message m) { + return new Message(MSG_INVALID); + } + + public static Message makeItsYourTurnMessage() { + return new Message(MSG_YOURTURN); + } + + public boolean isNonMove() { + return msgByteCat(msgType) == MsgByteCat.MSG; + //return msgType >= MSG_FIRST_MSG; + } + + public void setMsgType(byte msgType) { + this.msgType = msgType; + try { + jo.put("msgType", msgType); + } catch (JSONException ex) { + throw new InternalHanabiError(ex); + } + } + public Move getMove() { + return move; + } +} diff --git a/src/server/Move.java b/src/server/Move.java deleted file mode 100644 index 62d6514..0000000 --- a/src/server/Move.java +++ /dev/null @@ -1,132 +0,0 @@ -package server; - -import org.json.JSONObject; - -import card.Card; - -import org.json.JSONException; - -public class Move { - public static final byte[] MAGIC = { 1, 3, 3, 7 }; - - public static final byte DRY = 1 << 5; - public static final byte UPDATE = 2; - public static final byte PLACE_CARD = 3; - public static final byte TRASH_CARD = 4; - public static final byte HINT = 5; - public static final byte META_FIRST_META = DRY << 1; - public static final byte META_YOURTURN = META_FIRST_META + 0; - public static final byte META_READY = META_FIRST_META + 1; - public static final byte META_UNREADY = META_FIRST_META + 2; - public static final byte META_INVALID = META_FIRST_META + 3; //FIXME first, fix all other errors (META_MOVE_INVALID) - public static final byte META_VALID = META_FIRST_META + 4; //FIXME first, fix all other errors (META_MOVE_VALID) - public static final byte META_WON = META_FIRST_META + 5; - public static final byte META_LOST = META_FIRST_META + 6; - public static final byte META_LEAVE = META_FIRST_META + 7; - public static final byte META_CONNECTION_VALID = META_FIRST_META + 8; - public static final byte META_CONNECTION_INVALID = META_FIRST_META + 9; - - public static final byte HINT_COLOR = 0; - public static final byte HINT_VALUE = 1; - - private byte moveType; - private JSONObject jo; - - public Move(byte mT) { - jo = new JSONObject(); - setMoveType(mT); - } - public Move(JSONObject jo) throws JSONException { - //this.jo = (JSONObject)jo.clone(); - this.jo = jo; // TODO: clone? -// moveType = (byte)jo.getInt("msgType"); - } - public static Move newMove(Card c) { - try { - return new Move(c.toJson()); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } - - public JSONObject getJson() { - return jo; - } - public String toJsonString() { - if(jo == null) return ""; - return jo.toString(); - } - public byte getMoveType() { - return moveType; - } - - public int getOrigin() { - try { - return jo.getInt("origin"); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } - - public int getHintType() { - if(moveType != HINT) { - throw new RuntimeException("Internal Error: Hint type of non-hint move requested."); - } - try { - return jo.getInt("type"); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } - public int getValue() { - if(moveType != HINT) { - throw new RuntimeException("Internal Error: Hint value of non-hint move requested."); - } - try { - return jo.getInt("value"); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } - public int getHintRecipient() { - if(moveType != HINT) { - throw new RuntimeException("Internal Error: Target player ID of non-hint move requested."); - } - try { - return jo.getInt("playerId"); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } - - public int getOwnId() { - if(moveType != META_CONNECTION_VALID) { - throw new RuntimeException("Internal Error: ownId of non-connection valid move requested."); - } - try { - return jo.getInt("id"); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } - - public static Move makeInvalidMoveAnswer(Move m) { - return new Move(META_INVALID); - } - - public static Move makeItsYourTurnMessage() { - return new Move(META_YOURTURN); - } - - public boolean isMeta() { - return moveType >= META_FIRST_META; - } - public void setMoveType(byte msgType) { - this.moveType = msgType; - try { - jo.put("msgType", msgType); - } catch (JSONException ex) { - throw new InternalHanabiError(ex); - } - } -} diff --git a/src/server/ServerGame.java b/src/server/ServerGame.java index fc522a6..f28c362 100644 --- a/src/server/ServerGame.java +++ b/src/server/ServerGame.java @@ -40,9 +40,30 @@ return sock; } } +// private static class ServerGameClient extends Client { +// ServerGameClient(ServerGame sg, Connector conn, byte protocol, int id) { +// super(sg, conn, protocol, id); +// } +// @Override +// public Message recieveMsg() { +// return null; +// } +// } + + static class VibeDumbServer extends VibeDumb { + int id; + VibeDumbServer(Connector c, int id) { super(c); this.id = id; } + @Override + public Message receiveMsg() throws IOException { + Message m = super.receiveMsg(); + m.setOrigin(id); + return m; + } + } + private class TcpClient extends Client { TcpClient(ServerSocket s, byte protocol, int id) throws IOException { - super(ServerGame.this, new TcpConnector(getClientSocket(ServerGame.this)), protocol, id); + super(ServerGame.this, new TcpConnector(getClientSocket(ServerGame.this)), protocol, true, id); } } @@ -281,8 +302,8 @@ Card[] cards = getPlayer(playerId).getCards(); for(Card c : cards){ boolean is = false; - if(type == Move.HINT_COLOR) is = c.getColorInt() == value; - else if(type == Move.HINT_VALUE) is = c.getValue() == value; + if(type == Message.HINT_COLOR) is = c.getColorInt() == value; + else if(type == Message.HINT_VALUE) is = c.getValue() == value; else throw new RuntimeException(); // TODO "vorerst" c.addCardInfo(type, is, value, currentPlayer); } @@ -364,27 +385,27 @@ won = false; } if(movesLeft == 0) lost = true; - if(won) sendToAll(Move.META_WON); - else if(lost) sendToAll(Move.META_LOST); + if(won) sendToAll(Message.MSG_WON); + else if(lost) sendToAll(Message.MSG_LOST); this.won = won; this.lost = lost; } protected void sendToClient(byte msgType, Client c) { - sendToClient(new Move(msgType), c); + sendToClient(new Message(msgType), c); } - protected void sendToClient(Move m, Client c) { + protected void sendToClient(Message m, Client c) { try { - c.comm.sendMove(m); + c.comm.sendMsg(m); } catch(IOException ex) { // FIXME } } protected void sendToAll(byte msgType) { - sendToAll(new Move(msgType)); + sendToAll(new Message(msgType)); } - protected void sendToAll(Move m) { + protected void sendToAll(Message m) { for(Client c : clients) sendToClient(m, c); } @@ -408,8 +429,8 @@ // protected void addToHistory(String json) { // history.add(json); // } - protected void addToHistory(Move m) { - history.add(m); + protected void addToHistory(Message m) { + history.add(m.getMove()); } protected JSONObject toJson() throws JSONException { @@ -461,11 +482,11 @@ trash = new ArrayList<>(); } - protected void processMove(Move m) { - byte moveType = m.getMoveType(); - if(m.isMeta()) { - switch(moveType) { - case Move.META_READY: { + protected void processMsg(Message m) { + byte msgType = m.getMsgType(); + if(m.isNonMove()) { + switch(msgType) { + case Message.MAG_READY: { Client client = clients.get(m.getOrigin()); synchronized (clients) { if(client.isReady) break; @@ -474,7 +495,7 @@ } } break; - case Move.META_UNREADY: { + case Message.MSG_UNREADY: { Client client = clients.get(m.getOrigin()); synchronized (clients) { if(!client.isReady) break; @@ -483,7 +504,7 @@ } } break; - case Move.META_LEAVE: + case Message.MSG_LEAVE: clients.get(m.getOrigin()).stopListening(); break; default: @@ -498,7 +519,7 @@ boolean isValid = true; boolean isMove = false; // TODO MOVE_DRY - switch(moveType){ + switch(msgType){ case Move.PLACE_CARD: isValid = placeCard(new Card(m)); isMove = true; @@ -527,8 +548,8 @@ currentPlayer %= nPlayers; sendToAll(m); addToHistory(m); - if(won) sendToAll(Move.META_WON); - else if(lost) sendToAll(Move.META_LOST); + if(won) sendToAll(Message.MSG_WON); + else if(lost) sendToAll(Message.MSG_LOST); else itsYourTurn(clients.get(currentPlayer)); } } @@ -536,13 +557,13 @@ //FIXME protected boolean handleIOException(IOException ex) { throw new RuntimeException(ex); } - protected void answerInvalidMove(Move m) { - Move answer = Move.makeInvalidMoveAnswer(m); + protected void answerInvalidMove(Message m) { + Message answer = Message.makeInvalidMoveAnswer(m); sendToClient( answer, clients.get(m.getOrigin()) ); } protected void itsYourTurn(Client c) { - Move t = Move.makeItsYourTurnMessage(); + Message t = Message.makeItsYourTurnMessage(); sendToClient(t, c); } } diff --git a/src/server/SwingClientGame.java b/src/server/SwingClientGame.java index be8b228..43a340d 100644 --- a/src/server/SwingClientGame.java +++ b/src/server/SwingClientGame.java @@ -355,7 +355,7 @@ } @Override - protected BasePlayer createPlayer(Move m){ + protected BasePlayer createPlayer(Message m){ return new SwingPlayer(m); } @@ -433,7 +433,7 @@ } } - public SwingPlayer(Move m) { + public SwingPlayer(Message m) { super(m); playerUi = new JPanel(); playerUi.setBackground(BACKGROUND); @@ -476,7 +476,7 @@ String toolTipText = ""; for(CardInfo info : infos){ toolTipText += info.isIs() ? "is " : "not "; - if(info.getType() == Move.HINT_COLOR){ + if(info.getType() == Message.HINT_COLOR){ toolTipText += Card.intColorToText(info.getWhat()); } else { toolTipText += String.valueOf(info.getWhat() + 1); diff --git a/src/server/TcpConnector.java b/src/server/TcpConnector.java index 7539bc8..11afe1a 100644 --- a/src/server/TcpConnector.java +++ b/src/server/TcpConnector.java @@ -17,7 +17,7 @@ public TcpConnector(Socket s) { if(s == null) throw new NullPointerException(); socket = s; - } + } public TcpConnector(String hostname, int port) throws UnknownHostException, IOException { socket = new Socket(hostname, port); this.hostname = hostname; diff --git a/src/server/VibeDumb.java b/src/server/VibeDumb.java index 858c408..148a1f5 100644 --- a/src/server/VibeDumb.java +++ b/src/server/VibeDumb.java @@ -20,12 +20,12 @@ conn = c; } - public void sendMove(Move m) throws IOException { + public void sendMsg(Message m) throws IOException { if(outStream == null) { outStream = conn.getOutputStream(); } byte version = 0; - byte msgType = m.getMoveType(); + byte msgType = m.getMsgType(); byte[] unused = new byte[]{ 0, 0 }; String msg = m.toJsonString(); outStream.write(MAGIC); @@ -34,7 +34,7 @@ outStream.write(unused); outStream.writeUTF(msg); } - public Move receiveMove() throws IOException { + public Message receiveMsg() throws IOException { if(inStream == null) { inStream = conn.getInputStream(); } @@ -58,13 +58,13 @@ String msg = inStream.readUTF(); // TODO ddos? if(msg == null) throw new InternalHanabiError(); // TODO try { - Move m; + Message m; if(msg.equals("")) { - m = new Move(msgType); + m = new Message(msgType); } else { JSONObject jo = new JSONObject(msg); - m = new Move(jo); - m.setMoveType(msgType); + m = new Message(jo); + m.setMsgType(msgType); } return m; } catch(JSONException ex) {