Esempio n. 1
0
  public static void main(String[] args) {
    staticFileLocation("/public");
    String layout = "templates/layout.vtl";
    get(
        "/",
        (request, response) -> {
          Map<String, Object> model = new HashMap<String, Object>();
          model.put("template", "templates/players.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/playerOne",
        (request, response) -> {
          Map<String, Object> model = new HashMap<String, Object>();

          String playerOne = request.queryParams("playerOne");
          request.session().attribute("playerOne", playerOne);

          String playerTwo = request.queryParams("playerTwo");
          request.session().attribute("playerTwo", playerTwo);

          Player player1 = new Player(playerOne);
          Player player2 = new Player(playerTwo);
          Deck gameDeck = new Deck();
          // gameDeck.getFullDeck()
          Card testCard = new Card("test", "card");
          ArrayList<Card> playerOneCards = player1.get7Cards(gameDeck);
          ArrayList<Card> playerTwoCards = player2.get7Cards(gameDeck);

          model.put("playerOneCards", playerOneCards);
          model.put("playerTwoCards", playerTwoCards);
          model.put("testcard", testCard);
          model.put("gameDeck", gameDeck.getFullDeck());
          model.put("template", "templates/playerOne.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
  }
Esempio n. 2
0
  public static void main(String[] args) {

    transactions = new ArrayList<Transaction>();
    transfers = new ArrayList<Transfer>();
    banks = new HashMap<String, Collection<Account>>();
    Collection<Account> accountList = new ArrayList<Account>();
    BanksService banksService = new BanksService();

    Place place = new Place("boom");
    Player player1 = new Player("test", "", "", place, 1);
    Player player2 = new Player("test2", "", "", place, 1);
    Account account1 = new Account(player1, 350);
    Account account2 = new Account(player2, 100);

    accountList.add(account1);
    accountList.add(account2);
    banks.put("test", accountList);
    System.out.println("test banks: " + gson.toJson(banks.get("test")));

    try {
      Service service =
          new Service(
              "banks",
              "gruppe ingrid",
              "banks",
              "https://vs-docker.informatik.haw-hamburg.de/ports/11431/banks");
      System.out.println("service wird registriert...");
      String tmp = gson.toJson(service);
      Unirest.post("http://vs-docker.informatik.haw-hamburg.de:8053/services")
          .header("Content-Type", "application/json")
          .body(tmp)
          .asString();
    } catch (UnirestException e) {
      System.out.println("Service konnte nicht registriert werden.");
      e.printStackTrace();
    }

    get(
        "/banks",
        (request, response) -> {
          ArrayList<String> uris = new ArrayList<>();
          for (String uri : banks.keySet()) {
            uris.add(localUri + uri);
          }
          String json = gson.toJson(uris);
          response.status(200);
          return json;
        });

    post(
        "/banks",
        (request, response) -> {
          String gameid = UniqueId++ + "";
          Collection<Account> gameacc = new ArrayList<Account>();
          Game json = gson.fromJson(request.body(), Game.class);
          json.setGameid(gameid);
          for (Player player : json.getPlayers()) {
            Account acc = new Account();
            acc.setPlayer(player);
            acc.setSaldo(0);
            acc.setUri(localUri + gameid + "/players/" + player.getId());
            gameacc.add(acc);
          }
          banks.put(gameid, gameacc);
          response.status(200);
          response.header("location", localUri + gameid);
          return "ok";
        });

    get(
        "/banks/transfers",
        (request, response) -> {
          return gson.toJson(transfers);
        });

    get(
        "/banks/transfers/:transferid",
        (request, response) -> {
          String uri = localUri + "transfers/" + request.params(":transferid");
          for (Transfer trans : transfers) {
            if (trans.getUri().equals(uri)) {
              response.status(200);
              return gson.toJson(trans);
            }
          }
          response.status(404);
          return "Transfer not found.";
        });

    put(
        "/banks/:gameid",
        (request, response) -> {
          Game json = gson.fromJson(request.body(), Game.class);
          String gameid = request.params(":gameid");
          Collection<Account> gameacc = banks.getOrDefault(gameid, new ArrayList<Account>());
          for (Player player : json.getPlayers()) {
            Account acc = new Account();
            acc.setPlayer(player);
            acc.setSaldo(0);
            acc.setUri(localUri + gameid + "/players/" + player.getId());
            gameacc.add(acc);
          }
          banks.put(gameid, gameacc);
          response.header("uri", localUri + gameid + "/players");
          return "";
        });

    get(
        "/banks/:gameid",
        (request, response) -> {
          String gameid = request.params(":gameid");
          String json = gson.toJson(banks.getOrDefault(gameid, new ArrayList<Account>()));
          return json;
        });

    post(
        "/banks/:gameid/players",
        (request, response) -> {
          Type collectionType = new TypeToken<ArrayList<Account>>() {}.getType();
          ArrayList<Account> json = gson.fromJson(request.body(), collectionType);
          Collection<Account> current =
              banks.getOrDefault(request.params(":gameid"), new ArrayList<Account>());
          Set<Account> set = new HashSet<Account>(current);

          for (Account acc : json) {
            if (set.contains(acc)) {
              response.status(409);
              return "Player " + acc.getPlayer().getId() + " already exists.";
            } else {
              set.add(acc);
              current.add(acc);
            }
          }

          banks.put(request.params(":gameid"), (ArrayList<Account>) current);
          response.status(201);
          return "bank account has been created";
        });

    get(
        "/banks/:gameid/players/:playerid",
        (request, response) -> {
          if (banks.containsKey(request.params(":gameid"))) {
            Collection<Account> accounts = banks.get(request.params(":gameid"));
            for (Account account : accounts) {
              if (account.getPlayer().getId().equals(request.params(":playerid"))) {
                response.status(200);
                return account.getSaldo();
              }
            }
            response.status(404);
            return "Player not found.";
          } else {
            response.status(404);
            return "Game not found.";
          }
        });

    post(
        "/banks/:gameid/transfer/to/:to/:amount",
        (request, response) -> {
          return banksService.transferFromBankToPlayer(request, response);
        });

    post(
        "/banks/:gameid/transfer/from/:from/:amount",
        (request, response) -> {
          return banksService.transferFromPlayerToBank(request, response);
        });

    post(
        "/banks/:gameid/transfer/from/:from/to/:to/:amount",
        (request, response) -> {
          return banksService.transferFromPlayerToPlayer(request, response);
        });

    post(
        "/banks/sync/:gameid",
        (request, response) -> {
          return banksService.updateAccounts(request, response);
        });

    post(
        "/banks/sync/all",
        (request, response) -> {
          return banksService.replaceBanks(request, response);
        });

    get(
        "/banks/sync/all",
        (request, response) -> {
          String tmp = gson.toJson(banksService.banks);
          Unirest.post(replication + "/sync/all")
              .header("Content-Type", "application/json")
              .body(tmp)
              .asString();
          return "success";
        });

    post(
        "/banks/change/replication",
        (request, response) -> {
          replication = request.body();
          return replication;
        });

    get(
        "/stop/service",
        (request, response) -> {
          stop();
          return "";
        });
  }