コード例 #1
0
ファイル: Server.java プロジェクト: Atraxi/GridGame
  public static void main(String[] args) throws IOException {
    String indexHTML = new String(Files.readAllBytes(Paths.get("index.html")));
    String createHTML = new String(Files.readAllBytes(Paths.get("create.html")));

    Spark.port(80);

    Spark.get(
        "/create-submit",
        (request, response) -> {
          Game newGame =
              new Game(request.queryParams("name"), Integer.valueOf(request.queryParams("size")));
          games.put(newGame.hashCode(), newGame);
          // response.redirect("/game/" + newGame.hashCode());
          response.redirect("/");
          return "success";
        });

    Spark.get(
        "/create",
        (request, response) -> {
          return createHTML;
        });

    Spark.get(
        "/game/:gameID/:player/:x/:y/:xOffset/:yOffset",
        (request, response) -> {
          Game game = games.get(Integer.parseInt(request.params(":gameID")));
          game.update(
              Integer.parseInt(request.params(":x")),
              Integer.parseInt(request.params(":y")),
              request.params(":player"),
              Integer.parseInt(request.params(":xOffset")),
              Integer.parseInt(request.params(":yOffset")));
          return game.draw(request.params(":player"));
        });

    Spark.get(
        "/game/:gameID/:player/:x/:y",
        (request, response) -> {
          Game game = games.get(Integer.parseInt(request.params(":gameID")));
          game.update(
              Integer.parseInt(request.params(":x")),
              Integer.parseInt(request.params(":y")),
              request.params(":player"),
              1,
              1);
          return game.draw(request.params(":player"));
        });

    Spark.get(
        "/game/:gameID/:player",
        (request, response) -> {
          return games
              .get(Integer.parseInt(request.params(":gameID")))
              .draw(request.params(":player"));
        });

    Spark.get(
        "/game/:gameID",
        (request, response) -> {
          return "";
        });

    // default route, home page
    Spark.get(
        "/",
        (request, response) -> {
          StringBuilder gameList = new StringBuilder();

          ArrayList<Game> gamesList = new ArrayList<>(games.values());
          Collections.sort(gamesList);
          for (Game game : gamesList) {
            gameList.append(
                "			"
                    + game.getName()
                    + "\t<a href=\"/game/"
                    + game.hashCode()
                    + "/green\">Green</a>\t<a href=\"/game/"
                    + game.hashCode()
                    + "/red\">Red</a>\n"
                    + "			</br>\n");
          }
          return indexHTML.replace("<!--INSERT_GAME_LIST-->", gameList.toString());
        });
  }
コード例 #2
0
 @Override
 public void paintComponent(Graphics g) {
   game.draw(g);
 }
コード例 #3
0
ファイル: GameCanvas.java プロジェクト: vorlenko/GameEngine
 @Override
 protected void paintComponent(Graphics g) {
   // super.paintComponent(g);
   game.draw(g);
 }