src/main/java/com/webcheckers/ui/GetSpectatorGameRoute.java
/**
   * Render the WebCheckers Game route.
   *
   * @param request  the HTTP request
   * @param response the HTTP response
   * @return the rendered HTML for the Game page
   */
Render the WebCheckers Game route.
@param request  the HTTP request
@param response the HTTP response
@return the rendered HTML for the Game page
Render the WebCheckers Game route . @param request the HTTP request @param response the HTTP response @return the rendered HTML for the Game page
@Override
  public Object handle(Request request, Response response) throws InterruptedException{
    LOG.finer("GetSpectatorGameRoute is invoked.");
    final Session httpSession = request.session();
    Player spectator = httpSession.attribute(SessionAttributes.PLAYER);
    Map<String, Object> vm = new HashMap<>();
    Player playing = null;
    if (httpSession.attribute("isFirst") == null){
      String playingStr = request.queryParams("otherPlayer");
      playing = Application.playerLobby.getPlayers().get(playingStr);
      httpSession.attribute("Playing", playing);
    } else {
      playing = httpSession.attribute("Playing");
    }
    Game game = Application.gameCenter.getGameByPlayer(playing);
    if (httpSession.attribute("isFirst") == null){
      spectator.startSpectate(game);
      httpSession.attribute("isFirst", false);
    }
    Gson gson = new Gson();
    if (game == null){
      game = Application.gameCenter.getBySpectator(spectator);
      if (game == null){
        response.redirect("/");
        return null;
      }
    }
      vm.put("title", "Game page!");
      vm.put("currentUser", spectator);
      vm.put("redPlayer", game.getRedPlayer());
      vm.put("whitePlayer", game.getWhitePlayer());
      vm.put("viewMode", Game.ViewMode.SPECTATOR);
      vm.put("board", game.getBoardView());
      vm.put("activeColor", game.getActiveColor());
      httpSession.attribute("activeColor", game.getActiveColor());
      final Map<String, Object> modeOptions = new HashMap<>(2);
      modeOptions.put("isGameOver", true);
      if (game.getIsResigned() == playing){
        modeOptions.put("gameOverMessage", playing.getName() + "resigned.");
        vm.put("modeOptionsAsJSON", gson.toJson(modeOptions));
        spectator.stopSpectate(game);
        return templateEngine.render(new ModelAndView(vm, "game.ftl"));
      } else if (game.getIsResigned() != null){
        modeOptions.put("gameOverMessage", "Your Player wins. Game Over.");
        vm.put("modeOptionsAsJSON", gson.toJson(modeOptions));
        spectator.stopSpectate(game);
        return templateEngine.render(new ModelAndView(vm, "game.ftl"));
      } if (game.getIsGameOver()){
          if (playing.equals(game.getWinner())) { 
            modeOptions.put("gameOverMessage", playing.getName() + " wins");
          }
        vm.put("modeOptionsAsJSON", gson.toJson(modeOptions));
        spectator.stopSpectate(game);
        return templateEngine.render(new ModelAndView(vm, "game.ftl"));
      } else {
        vm.put("message", WELCOME_MSG);
        return templateEngine.render(new ModelAndView(vm, "game.ftl"));
      }
  }
@ Override public Object handle ( Request request , Response response ) throws InterruptedException { LOG . finer ( "GetSpectatorGameRoute is invoked." ) ; final Session httpSession = request . session ( ) ; Player spectator = httpSession . attribute ( SessionAttributes . PLAYER ) ; Map < String , Object > vm = new HashMap < > ( ) ; Player playing = null ; if ( httpSession . attribute ( "isFirst" ) == null ) { String playingStr = request . queryParams ( "otherPlayer" ) ; playing = Application . playerLobby . getPlayers ( ) . get ( playingStr ) ; httpSession . attribute ( "Playing" , playing ) ; } else { playing = httpSession . attribute ( "Playing" ) ; } Game game = Application . gameCenter . getGameByPlayer ( playing ) ; if ( httpSession . attribute ( "isFirst" ) == null ) { spectator . startSpectate ( game ) ; httpSession . attribute ( "isFirst" , false ) ; } Gson gson = new Gson ( ) ; if ( game == null ) { game = Application . gameCenter . getBySpectator ( spectator ) ; if ( game == null ) { response . redirect ( "/" ) ; return null ; } } vm . put ( "title" , "Game page!" ) ; vm . put ( "currentUser" , spectator ) ; vm . put ( "redPlayer" , game . getRedPlayer ( ) ) ; vm . put ( "whitePlayer" , game . getWhitePlayer ( ) ) ; vm . put ( "viewMode" , Game . ViewMode . SPECTATOR ) ; vm . put ( "board" , game . getBoardView ( ) ) ; vm . put ( "activeColor" , game . getActiveColor ( ) ) ; httpSession . attribute ( "activeColor" , game . getActiveColor ( ) ) ; final Map < String , Object > modeOptions = new HashMap < > ( 2 ) ; modeOptions . put ( "isGameOver" , true ) ; if ( game . getIsResigned ( ) == playing ) { modeOptions . put ( "gameOverMessage" , playing . getName ( ) + "resigned." ) ; vm . put ( "modeOptionsAsJSON" , gson . toJson ( modeOptions ) ) ; spectator . stopSpectate ( game ) ; return templateEngine . render ( new ModelAndView ( vm , "game.ftl" ) ) ; } else if ( game . getIsResigned ( ) != null ) { modeOptions . put ( "gameOverMessage" , "Your Player wins. Game Over." ) ; vm . put ( "modeOptionsAsJSON" , gson . toJson ( modeOptions ) ) ; spectator . stopSpectate ( game ) ; return templateEngine . render ( new ModelAndView ( vm , "game.ftl" ) ) ; } if ( game . getIsGameOver ( ) ) { if ( playing . equals ( game . getWinner ( ) ) ) { modeOptions . put ( "gameOverMessage" , playing . getName ( ) + " wins" ) ; } vm . put ( "modeOptionsAsJSON" , gson . toJson ( modeOptions ) ) ; spectator . stopSpectate ( game ) ; return templateEngine . render ( new ModelAndView ( vm , "game.ftl" ) ) ; } else { vm . put ( "message" , WELCOME_MSG ) ; return templateEngine . render ( new ModelAndView ( vm , "game.ftl" ) ) ; } }
Render the WebCheckers Game route.
Render the WebCheckers Game route .
//if (game != null) { //TODO: Fix (current player -> spectator) //}