diff --git a/src/MainTest.java b/src/MainTest.java deleted file mode 100644 index 9c2fea3..0000000 --- a/src/MainTest.java +++ /dev/null @@ -1,39 +0,0 @@ -import java.io.File; -import java.io.FileReader; -import java.io.IOException; - -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONTokener; - -import player.BasePlayer; - -public class MainTest { - private static class Opts { - String playerFile; - - Opts(String[] args) { - // TODO dummy - // TODO error handling here, ctor is assumed to never fail - playerFile = "test.json"; - } - } - - public static void main(String[] args) { - Opts opts = new Opts(args); - JSONTokener jt = null; - try { - FileReader fr = new FileReader(new File(opts.playerFile)); - jt = new JSONTokener(fr); - } catch (JSONException | IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - JSONObject o = new JSONObject(jt); - - BasePlayer p = new BasePlayer(o); - - System.out.println(p.toJson().toString(4)); - } - -} diff --git a/src/card/Card.java b/src/card/Card.java index 96ade6e..003ab55 100644 --- a/src/card/Card.java +++ b/src/card/Card.java @@ -1,15 +1,8 @@ package card; -import server.InternalHanabiError; -import server.Message; - import java.util.ArrayList; import java.util.List; -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONException; - /* * COLORS: * GREEN @@ -46,14 +39,14 @@ cardInfos = new ArrayList<>(); } - public Card(Message m) { - this(); - try { - fromJson(m.getJson()); - } catch(JSONException ex) { - throw new InternalHanabiError(ex); - } - } +// public Card(Message m) { +// this(); +// try { +// fromJson(m.getJson()); +// } catch(JSONException ex) { +// throw new InternalHanabiError(ex); +// } +// } public void addCardInfo(int type, boolean is, int what, int from) { cardInfos.add(new CardInfo(type, is, what, from)); @@ -109,6 +102,8 @@ return id; } + public int getColor() { return color; } + // public JSONObject toJson() throws JSONException { // // TODO: handle dummy card // JSONObject root = new JSONObject(); diff --git a/src/server/Client.java b/src/server/Client.java index 9886619..661f999 100644 --- a/src/server/Client.java +++ b/src/server/Client.java @@ -53,7 +53,7 @@ listen(); } - void listen() { + private void listen() { while(keepRunning) { Message m = null; boolean retry = true; @@ -69,4 +69,7 @@ game.processMsg(m); } } + + public int getOrigin() { return id; } + public void setOrigin(int newId) { id = newId; } // TODO: remove this method? } diff --git a/src/server/ClientGame.java b/src/server/ClientGame.java index 5296363..d54db0b 100644 --- a/src/server/ClientGame.java +++ b/src/server/ClientGame.java @@ -62,7 +62,7 @@ JSONObject jo = new JSONObject(); jo.put("origin", ownId); Message m = new Message(jo); - m.setMsgType(Message.MAG_READY); + m.setMsgType(Message.MSG_READY); send(m); } catch(JSONException ex) { throw new InternalHanabiError(ex); diff --git a/src/server/Message.java b/src/server/Message.java index 0b034ae..193a3fa 100644 --- a/src/server/Message.java +++ b/src/server/Message.java @@ -1,5 +1,7 @@ package server; +import card.Card; + public class Message { public static final byte[] MAGIC = { 1, 3, 3, 7 }; @@ -8,15 +10,18 @@ public static final byte MSG_READY = 1; public static final byte MSG_UNREADY = 2; public static final byte MSG_INVALID = 3; //FIXME first, fix all other errors (META_MOVE_INVALID) - public static final byte MSG_VALID = 4; //FIXME first, fix all other errors (META_MOVE_VALID) +// public static final byte MSG_VALID = 4; //FIXME first, fix all other errors (META_MOVE_VALID) public static final byte MSG_WON = 5; public static final byte MSG_LOST = 6; public static final byte MSG_LEAVE = 7; - public static final byte MSG_CONNECTION_VALID = 8; - public static final byte MSG_CONNECTION_INVALID = 9; - public static final byte MSG_MOVE = 10; - public static final byte MSG_UPDATE = 11; - public static final byte MSG_LAST_MSG = MSG_UPDATE; + public static final byte MSG_LEFT = 8; + public static final byte MSG_CONNECTION_VALID = 9; + public static final byte MSG_CONNECTION_INVALID = 10; +// public static final byte MSG_UPDATE = 11; + public static final byte MSG_MOVE = 12; + public static final byte MSG_CARD = 13; + public static final byte MSG_INITIAL_GAME = 14; + public static final byte MSG_LAST_MSG = MSG_INITIAL_GAME; private static void throwOnInvalidMsgType(byte b) { if(b < 0 || b > MSG_LAST_MSG) throw new RuntimeException("Invalid message Type " + b + "."); @@ -26,7 +31,14 @@ private Move move; + /* ClientGame fields: */ private int ownId; + private int leftId; + private InitialPlayer[] initialPlayers; + private NewCard newCard; + + /* ServerGame fields: */ + private int id; public Message(byte msgType) { throwOnInvalidMsgType(msgType); @@ -38,6 +50,55 @@ move = m; // TODO: clone? } + public static class InitialPlayer { + private int id; + private String name; + private Card[] cards; + InitialPlayer(int i, String n, Card[] cs) { + id = i; + name = n; + cards = cs; + } + int getId() { return id; } + String getName() { return name; } + Card[] getCards() { return cards; } + } + + public static class NewCard { + private int id; + private Card c; + NewCard(int id, Card c) { + this.id = id; + this.c = c; + } + int getId() { return id; } + Card getCard() { return c; } + } + + public Message(InitialPlayer[] players) { + this(MSG_INITIAL_GAME); + initialPlayers = players; + } + + public Message(int id, Card c) { this(new NewCard(id, c)); } + public Message(NewCard c) { this(MSG_CARD); newCard = c; } + + public Message(byte b, int leftId) { + this(b); + if(b != MSG_LEFT) { + throw new InternalHanabiError("Attempt to create non-LEFT message with leftId."); + } + this.leftId = leftId; + } + + public static Message makeInvalidMoveAnswer(Message m) { + return new Message(MSG_INVALID); + } + + public static Message makeItsYourTurnMessage() { + return new Message(MSG_YOURTURN); + } + //public Message(JSONObject jo) throws JSONException { //this.jo = (JSONObject)jo.clone(); //this.jo = jo; // TODO: clone? @@ -76,8 +137,72 @@ // if(jo == null) return ""; // return jo.toString(); // } - public byte getMsgType() { - return msgType; + + public byte getMsgType() { return msgType; } + + public int getOwnId() { + if(msgType != MSG_CONNECTION_VALID) { + throw new RuntimeException("Internal Error: ownId of non-connection valid message requested."); + } + return ownId; + } + public void setOwnId(int id) { + if(msgType != MSG_CONNECTION_VALID) { + throw new RuntimeException("Internal Error: Attempt to set ownId of non-connection valid message."); + } + ownId = id; + } + + public int getOrigin() { + // TODO: Should we throw if we're called by ClientGame? (if so, how?) + return id; + } + public void setOrigin(int newId) { + // TODO: cf. getOrigin() + id = newId; + } + + public boolean isNonMove() { + return msgType != MSG_MOVE; + } + + public void setMsgType(byte msgType) { + this.msgType = msgType; + } + + public Move getMove() { + return move; + } + + public void setMove(Move move) { + this.move = move; // TODO clone? + } + + public InitialPlayer[] getInitialPlayers() { + if(msgType != MSG_INITIAL_GAME) { + throw new InternalHanabiError("Attempt to get initial players from non-INITIAL_GAME message."); + } + return initialPlayers; + } + + public int getLeftId() { + if(msgType != MSG_LEFT) { + throw new InternalHanabiError("Attempt to get leftId of non-LEFT message."); + } + return leftId; + } + + public Card getNewCard() { + if(msgType != MSG_CARD) { + throw new InternalHanabiError("Attempt to get card of new card of non-CARD message."); + } + return newCard.getCard(); + } + public int getNewCardPlayerId() { + if(msgType != MSG_CARD) { + throw new InternalHanabiError("Attempt to get player id of new card of non-CARD message."); + } + return newCard.getId(); } // public int getOrigin() { @@ -118,42 +243,4 @@ // throw new InternalHanabiError(ex); // } // } - - public int getOwnId() { - if(msgType != MSG_CONNECTION_VALID) { - throw new RuntimeException("Internal Error: ownId of non-connection valid message requested."); - } - return ownId; - } - - public void setOwnId(int id) { - if(msgType != MSG_CONNECTION_VALID) { - throw new RuntimeException("Internal Error: Attempt to set ownId of non-connection valid message."); - } - ownId = id; - } - - public static Message makeInvalidMoveAnswer(Message m) { - return new Message(MSG_INVALID); - } - - public static Message makeItsYourTurnMessage() { - return new Message(MSG_YOURTURN); - } - - public boolean isNonMove() { - return msgType != MSG_MOVE; - } - - public void setMsgType(byte msgType) { - this.msgType = msgType; - } - - public Move getMove() { - return move; - } - - public void setMove(Move move) { - this.move = move; // TODO clone? - } } \ No newline at end of file diff --git a/src/server/ServerGame.java b/src/server/ServerGame.java index d1b376b..8561afa 100644 --- a/src/server/ServerGame.java +++ b/src/server/ServerGame.java @@ -9,10 +9,6 @@ import java.util.List; import java.util.Random; -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONException; - import card.Card; import player.BasePlayer; @@ -25,7 +21,7 @@ private boolean gameStarted; - private int movesLeft; + int movesLeft; private boolean won; private boolean lost; @@ -433,44 +429,7 @@ history.add(m.getMove()); } - protected JSONObject toJson() throws JSONException { - JSONObject root = new JSONObject(); - root.put("nCardsInDeck", nCardsInDeck); - root.put("hints", hints); - root.put("nFlashs", flashs); - root.put("movesLeft", movesLeft); - JSONArray trashArray = new JSONArray(); - for(Card c : trash){ - trashArray.put(c.toJson()); - } - root.put("trash", trashArray); - JSONObject deckRow = new JSONObject(); - for(int i=0; i cardInfos = c.getCardInfos(); + if(cardInfos == null) return infoArray; + for(CardInfo ci : cardInfos){ + infoArray.put(cardInfoToJson(ci)); + } + return infoArray; + } + + public JSONObject cardInfoToJson(CardInfo ci) throws JSONException { + JSONObject root = new JSONObject(); + root.put("type", ci.getType()); + root.put("is", ci.isIs()); + root.put("what", ci.getWhat()); + root.put("from", ci.getFrom()); + return root; + } + +// public Message jsonToMsg(JSONObject jo) throws JSONException { +// this.jo = jo; // TODO: clone? +// moveType = (byte)jo.getInt("msgType"); +// } }