hw1/src/main/java/controllers/PlayGame.java
/** Main method of the application.
   * @param args Command line arguments
   */
Main method of the application.
@param args Command line arguments
Main method of the application . @param args Command line arguments
public static void main(final String[] args) {
    try {
      database = new Database();
      board = database.get();
    } catch (Exception e) {
      database = null;
      e.printStackTrace();
    }
    app = Javalin.create(config -> {
      config.addStaticFiles("/public");
    }).start(PORT_NUMBER);
    app.get("/newgame", ctx -> {
      if (database != null) {
        database.unset();
        board = null;
      }
      ctx.redirect("/tictactoe.html");
    });
    app.post("/startgame", ctx -> {
      if (ctx.formParam("type").isBlank() || (ctx.formParam("type").charAt(0) != 'X'
          && ctx.formParam("type").charAt(0) != 'O')) {
        ctx.status(500).result("Invalid type");
        return;
      }
      final char type = ctx.formParam("type").charAt(0);
      board = new GameBoard(type);
      if (database != null) {
        database.set(board);
      }
      ctx.status(200).result(board.toJson());
    });
    app.get("/joingame", ctx -> {
      if (board == null) {
        ctx.status(500).result("Board not initialized");
        return;
      }
      try {
        board.joinGame();
      } catch (Exception e) {
        ctx.status(400).result(e.getMessage());
        return;
      }
      if (database != null) {
        database.set(board);
      }
      ctx.status(302).redirect("/tictactoe.html?p=2");
      sendGameBoardToAllPlayers(board.toJson(), 1);
    });
    app.post("/move/:playerId", ctx -> {
      if (board == null) {
        ctx.status(400).result("Board not initialized");
        return;
      }
      if (!board.isGameStarted()) {
        ctx.status(400).result("Game not started");
        return;
      }
      if (board.isGameOver()) {
        ctx.status(200).result(new Message(false, 101, "Game already over").toJson());
        return;
      }
      if (ctx.pathParam("playerId").length() != 1) {
        ctx.status(200).result(new Message(false, 102, "Invalid playerId").toJson());
        return;
      }
      final int playerId = Integer.parseInt(ctx.pathParam("playerId"));
      if (playerId != 1 && playerId != 2) {
        ctx.status(200).result(new Message(false, 103, "Incorrect playerId").toJson());
        return;
      }
      if (ctx.formParam("x").isBlank() || ctx.formParam("y").isBlank()) {
        ctx.status(200).result(new Message(false, 104, "Missing position").toJson());
        return;
      }
      final Player player = playerId == 1 ? board.getP1() : board.getP2();
      final int x = Integer.parseInt(ctx.formParam("x"));
      final int y = Integer.parseInt(ctx.formParam("y"));
      try {
        board.playTurn(player, x, y);
      } catch (Exception e) {
        ctx.status(200).result(new Message(false, 105, e.getMessage()).toJson());
        return;
      }
      if (database != null) {
        database.set(board);
      }
      ctx.status(200).result(new Message(true, 100, "").toJson());
      sendGameBoardToAllPlayers(board.toJson());
    });
    app.ws("/gameboard", new UiWebSocket());
  }
public static void main ( final String [ ] args ) { try { database = new Database ( ) ; board = database . get ( ) ; } catch ( Exception e ) { database = null ; e . printStackTrace ( ) ; } app = Javalin . create ( config -> { config . addStaticFiles ( "/public" ) ; } ) . start ( PORT_NUMBER ) ; app . get ( "/newgame" , ctx -> { if ( database != null ) { database . unset ( ) ; board = null ; } ctx . redirect ( "/tictactoe.html" ) ; } ) ; app . post ( "/startgame" , ctx -> { if ( ctx . formParam ( "type" ) . isBlank ( ) || ( ctx . formParam ( "type" ) . charAt ( 0 ) != 'X' && ctx . formParam ( "type" ) . charAt ( 0 ) != 'O' ) ) { ctx . status ( 500 ) . result ( "Invalid type" ) ; return ; } final char type = ctx . formParam ( "type" ) . charAt ( 0 ) ; board = new GameBoard ( type ) ; if ( database != null ) { database . set ( board ) ; } ctx . status ( 200 ) . result ( board . toJson ( ) ) ; } ) ; app . get ( "/joingame" , ctx -> { if ( board == null ) { ctx . status ( 500 ) . result ( "Board not initialized" ) ; return ; } try { board . joinGame ( ) ; } catch ( Exception e ) { ctx . status ( 400 ) . result ( e . getMessage ( ) ) ; return ; } if ( database != null ) { database . set ( board ) ; } ctx . status ( 302 ) . redirect ( "/tictactoe.html?p=2" ) ; sendGameBoardToAllPlayers ( board . toJson ( ) , 1 ) ; } ) ; app . post ( "/move/:playerId" , ctx -> { if ( board == null ) { ctx . status ( 400 ) . result ( "Board not initialized" ) ; return ; } if ( ! board . isGameStarted ( ) ) { ctx . status ( 400 ) . result ( "Game not started" ) ; return ; } if ( board . isGameOver ( ) ) { ctx . status ( 200 ) . result ( new Message ( false , 101 , "Game already over" ) . toJson ( ) ) ; return ; } if ( ctx . pathParam ( "playerId" ) . length ( ) != 1 ) { ctx . status ( 200 ) . result ( new Message ( false , 102 , "Invalid playerId" ) . toJson ( ) ) ; return ; } final int playerId = Integer . parseInt ( ctx . pathParam ( "playerId" ) ) ; if ( playerId != 1 && playerId != 2 ) { ctx . status ( 200 ) . result ( new Message ( false , 103 , "Incorrect playerId" ) . toJson ( ) ) ; return ; } if ( ctx . formParam ( "x" ) . isBlank ( ) || ctx . formParam ( "y" ) . isBlank ( ) ) { ctx . status ( 200 ) . result ( new Message ( false , 104 , "Missing position" ) . toJson ( ) ) ; return ; } final Player player = playerId == 1 ? board . getP1 ( ) : board . getP2 ( ) ; final int x = Integer . parseInt ( ctx . formParam ( "x" ) ) ; final int y = Integer . parseInt ( ctx . formParam ( "y" ) ) ; try { board . playTurn ( player , x , y ) ; } catch ( Exception e ) { ctx . status ( 200 ) . result ( new Message ( false , 105 , e . getMessage ( ) ) . toJson ( ) ) ; return ; } if ( database != null ) { database . set ( board ) ; } ctx . status ( 200 ) . result ( new Message ( true , 100 , "" ) . toJson ( ) ) ; sendGameBoardToAllPlayers ( board . toJson ( ) ) ; } ) ; app . ws ( "/gameboard" , new UiWebSocket ( ) ) ; }
Main method of the application.
Main method of the application .
// Initialize the database, if possible // Restore the previous state // Create a new game // Start a new game // Extract the player type and construct the board. // Update the board state in the database // Join an existing game // Try to start the game // Update the board state in the database // Send board after an async delay to allow p2 to redirect // Perform a move by the given player // Ensure the game has already started // Ensure the game is still going // Ensure a player ID is provided // Ensure the player ID is valid // Ensure position is provided // Extract the necessary information to play a turn // Try to play a turn and throw an exception if it cannot be played // Update the board state in the database // Web sockets - DO NOT DELETE or CHANGE