Пример #1
0
  @Test
  public void testgoodInput() {
    FinishTurnInput input = new FinishTurnInput(0);
    Player testPlayer = model.getPlayer(new PlayerID(0));
    try {
      // (int soldier, int monument, int monopoly, int yearOfPlenty, int roadBuild)
      testPlayer.getPlayerBank().setDC(new DevelopmentHand(0, 0, 0, 0, 0));
      testPlayer.getPlayerBank().addNewDC(new DevelopmentHand(1, 1, 1, 1, 1));
    } catch (BankException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    FinishTurnCommand command = new FinishTurnCommand();
    command.setGameModel(model);
    try {
      model = (GameModel) command.execute(new ObjectMapper().writeValueAsString(input));
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    assertEquals(testPlayer.getPlayerBank().getMonopoly().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getMonument().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getRoadBuild().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getSoldier().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getYearOfPlenty().getQuantity(), 1);
    assertEquals(testPlayer.getPlayerBank().getNewMonopoly().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewMonument().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewRoadBuild().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewSoldier().getQuantity(), 0);
    assertEquals(testPlayer.getPlayerBank().getNewYearOfPlenty().getQuantity(), 0);

    assertEquals(testPlayer.getPlayerFacade().canBeRobbed(), true);
  }
Пример #2
0
  /**
   * creates a new command based on which /move/ method is called and executes that command one the
   * correct game.
   *
   * @post the command is executed correctly. If invalid params, returns 400. If error in server,
   *     returns 500.
   */
  @Override
  public void handle(HttpExchange exchange) throws IOException {
    String extension = "";
    System.out.println("move handler");
    String json = jsonStringFromExchange(exchange.getRequestBody());
    System.out.println(json);
    Input input = new Gson().fromJson(json, Input.class);
    switch (input.getMethod()) {
      case "/moves/sendChat":
        extension = "sent a chat";
        command = new SendChatCommand();
        break;
      case "/moves/rollNumber":
        extension = "rolled a ";
        System.out.println(extension);
        command = new RollNumberCommand();
        break;
      case "/moves/robPlayer":
        extension = "burgled a player";
        command = new RobPlayerCommand();
        break;
      case "/moves/finishTurn":
        extension = "finished his/her turn";
        command = new FinishTurnCommand();
        break;
      case "/moves/buyDevCard":
        extension = "bought a dev card";
        command = new BuyDevCardCommand();
        break;
      case "/moves/Year_of_Plenty":
        extension = "cast Year-of-Plenty";
        command = new PlayYearOfPlentyCommand();
        break;
      case "/moves/Road_Building":
        extension = "cast Road-Building";
        command = new PlayRoadBuildingCommand();
        break;
      case "/moves/Soldier":
        extension = "Sent a Chat";
        command = new PlaySoldierCommand();
        break;
      case "/moves/Monument":
        extension = "cast Monument";
        command = new PlayMonumentCommand();
        break;
      case "/moves/Monopoly":
        extension = "cast Monopoly";
        command = new PlayMonopolyCommand();
        break;
      case "/moves/buildRoad":
        extension = "built a road";
        command = new BuildRoadCommand();
        break;
      case "/moves/buildCity":
        extension = "built a city";
        command = new BuildCityCommand();
        break;
      case "/moves/buildSettlement":
        extension = "built a settlement";
        command = new BuildSettlementCommand();
        break;
      case "/moves/offerTrade":
        extension = "offered a trade";
        command = new OfferTradeCommand();
        break;
      case "/moves/acceptTrade":
        extension = "accepted a trade";
        command = new AcceptTradeCommand();
        break;
      case "/moves/maritimeTrade":
        extension = "traded with pirates";
        command = new MaritimeTradeCommand();
        break;
      case "/moves/discardCards":
        extension = "sent his cards to the graveyard";
        command = new DiscardCardsCommand();
        break;
    }

    String cookie = exchange.getRequestHeaders().getFirst("Cookie");
    String[] cookieArray = cookie.split(";");

    if (command != null && cookieArray.length == 2) {
      try {
        System.out.println("q");
        String gameCookie = cookieArray[1].trim();
        StringBuilder temp = new StringBuilder(gameCookie);
        System.out.println("w");
        int index = temp.lastIndexOf("catan.game=") + 11;
        int gameId = Integer.parseInt(temp.substring(index, temp.length()));
        System.out.println("e");
        GameModel model = GameHub.getInstance().getModel(gameId);
        System.out.println("r");
        MoveCommand moveCommand = (MoveCommand) command;
        moveCommand.setGameModel(model);
        moveCommand.setInput(json);
        System.out.println("t");
        GameModel updatedModel = (GameModel) moveCommand.execute(json);
        GameHub.getInstance().updateModel(updatedModel);
        System.out.println("y");
        // add log to GameHistory
        gameCookie = cookieArray[0].trim();
        temp = new StringBuilder(gameCookie);
        System.out.println("u");
        index = temp.lastIndexOf("catan.user="******"i");
        if (extension.equals("rolled a ")) {
          System.out.println("o");
          RollNumberInput rollInput = new ObjectMapper().readValue(json, RollNumberInput.class);
          extension = extension + rollInput.getNumber();
        }
        if (extension.equals("accepted a trade")) {
          System.out.println("p");
          AcceptTradeInput atinput = new ObjectMapper().readValue(json, AcceptTradeInput.class);
          if (!atinput.isWillAccept()) extension = "rejected a trade";
        }
        System.out.println("a");
        String message = name + " " + extension + ".";
        System.out.printf("message %s and id %d\n", message, pId);
        LogEntry le = new LogEntry(cc, message);
        GameHub.getInstance().getModel(gameId).getLogs().add(le);
        // finished with log

        exchange.getResponseHeaders().set("Content-Type", "text/html");
        exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);

        // write to response body
        Writer writer = new OutputStreamWriter(exchange.getResponseBody());
        String toWrite = new ObjectMapper().writeValueAsString(updatedModel);
        writer.write(toWrite);
        writer.close();

        exchange.getResponseBody().close();

        // add command to persistence
        GameHub.getInstance().getGameDAO().addCommand(gameId, moveCommand);

      } catch (ServerException e) {
        exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, -1);
      }
    } else {
      exchange.sendResponseHeaders(HttpURLConnection.HTTP_BAD_REQUEST, -1);
    }
  }
Пример #3
0
 @Before
 public void setUp() {
   model = GameModel.getDefaultModel();
 }