private TBGToken joinToRoom(TBGToken msg) { GameRoom room = gameRooms.get(msg.getArgs()); if (room == null) { room = new GameRoom(msg.getArgs()); gameRooms.put(msg.getArgs(), room); room.join(this); myRoom = room; return new TBGToken("SYSMSG", msg.getCommandType() + " ACCEPTED"); } else { if (room.join(this)) { myRoom = room; return new TBGToken("SYSMSG", msg.getCommandType() + " ACCEPTED"); } else return new TBGToken( "SYSMSG", msg.getCommandType() + " REJECTED ; room is closed for new players"); } }
private void handleInGameCommands(TBGToken msg, ProtocolCallback<TBGToken> callback) { if (msg.getCommandType().equals("MSG") && msg.getArgs() != null && !msg.getArgs().equals("")) { myRoom.sendMessage(this, msg.getArgs()); } else if (msg.getCommandType().equals("START") && msg.getArgs() != null && !msg.getArgs().equals("") && myRoom.startGame(msg.getArgs())) { } else if (msg.getCommandType().equals("TXTRESP") && msg.getArgs() != null && !msg.getArgs().equals("")) { myRoom.play(this, msg); } else if (msg.getCommandType().equals("SELECTRESP") && msg.getArgs() != null && !msg.getArgs().equals("")) { myRoom.play(this, msg); } }
/** * detetmine whether the given message is the termination message * * @param msg the message to examine * @return false - this simple OLD.protocol doesn't allow termination... */ public boolean isEnd(TBGToken msg) { return msg.getCommandType().equals("QUIT") && msg.getArgs() == null; }
public void processMessage(TBGToken msg, ProtocolCallback<TBGToken> newCallback) { callback = newCallback; try { switch (myState) { case unauthorized: if (msg.getCommandType().equals("NICK")) { if (msg.getArgs() == null || msg.getArgs().equals("")) callback.sendMessage( new TBGToken( "SYSMSG", msg.getCommandType() + " REJECTED ; Choose an appropriete nick")); else { callback.sendMessage(new TBGToken("SYSMSG", msg.getCommandType() + " ACCEPTED")); register(msg); } } else if (isEnd(msg)) { callback.sendMessage(new TBGToken("SYSMSG", msg.getCommandType() + " ACCEPTED")); this.connectionTerminated(); } else callback.sendMessage( new TBGToken( "SYSMSG", msg.getCommandType() + " REJECTED ; need to register first")); case Lobby: if (msg.getCommandType().equals("JOIN") && msg.getArgs() != null && !msg.getArgs().equals("")) callback.sendMessage(joinToRoom(msg)); else if (msg.getCommandType().equals("LISTGAMES") && msg.getArgs() == null) { StringBuffer ss = new StringBuffer(); for (Map.Entry<String, GameHandlerFactory> sg : GameRoom.supportedGames.entrySet()) { ss.append(sg.getKey() + ","); } ss.deleteCharAt(ss.length() - 1); callback.sendMessage( new TBGToken( "SYSMSG", msg.getCommandType() + " ACCEPTED ; Games : " + ss.toString() + " .")); // TODO change this after adding bluufer to database } else if (isEnd(msg)) { callback.sendMessage(new TBGToken("SYSMSG", msg.getCommandType() + " ACCEPTED")); this.connectionTerminated(); } else callback.sendMessage( new TBGToken( "SYSMSG", msg.getCommandType() + " REJECTED ; U can only use : JOIN , QUIT , LISTGAMES")); case inGame: if (isEnd(msg)) callback.sendMessage( new TBGToken( "SYSMSG", msg.getCommandType() + " REJECTED ; U can't leave in the middle of a game !")); else handleInGameCommands(msg, callback); } } catch (IOException e) { e.printStackTrace(); } }
private void register(TBGToken msg) { myNick = msg.getArgs(); }