public void saveStandings() {
   try {
     int bootLength = properties.getPropertyInt("bootLength");
     gameLength = MemStore.getGameLengths().get(gameId) - bootLength;
     lastTick = Integer.parseInt(MemStore.getGameHeartbeats().get(gameId)[0]) - bootLength;
   } catch (Exception ignored) {
   }
 }
  private int getGameId(HttpServletRequest request) {
    int gameId;

    try {
      gameId = Integer.parseInt(request.getParameter(Rest.REQ_PARAM_GAMEID));
    } catch (Exception ignored) {
      String niceName = request.getParameter(Rest.REQ_PARAM_GAMEID);
      gameId = MemStore.getGameId(niceName);
    }

    return gameId;
  }
  private String handleGET(HttpServletRequest request) {
    try {
      String actionString = request.getParameter(Rest.REQ_PARAM_ACTION);
      if (actionString.equalsIgnoreCase(Rest.REQ_PARAM_STATUS)) {
        if (!MemStore.checkMachineAllowed(request.getRemoteAddr())) {
          return "error";
        }

        return handleStatus(request);
      } else if (actionString.equalsIgnoreCase(Rest.REQ_PARAM_BOOT)) {
        return serveBoot(getGameId(request));
      } else if (actionString.equalsIgnoreCase(Rest.REQ_PARAM_HEARTBEAT)) {
        if (!MemStore.checkMachineAllowed(request.getRemoteAddr())) {
          return "error";
        }

        return handleHeartBeat(request);
      }
    } catch (Exception ignored) {
    }
    return "error";
  }
  private String handleHeartBeat(HttpServletRequest request) {
    int gameId;

    // Write heartbeat + elapsed time to the MemStore
    try {
      String message = request.getParameter(Rest.REQ_PARAM_MESSAGE);
      gameId = getGameId(request);

      if (!(gameId > 0)) {
        log.debug("The message didn't have a gameId!");
        return "error";
      }
      MemStore.addGameHeartbeat(gameId, message);

      long elapsedTime = Long.parseLong(request.getParameter(Rest.REQ_PARAM_ELAPSED_TIME));
      if (elapsedTime > 0) {
        MemStore.addElapsedTime(gameId, elapsedTime);
      }
    } catch (Exception e) {
      e.printStackTrace();
      return "error";
    }

    // Write heartbeat to the DB
    Session session = HibernateUtil.getSession();
    Transaction transaction = session.beginTransaction();
    try {
      Game game = (Game) session.get(Game.class, gameId);
      String standings = request.getParameter(Rest.REQ_PARAM_STANDINGS);

      return new GameHandler(game).handleStandings(session, standings, false);
    } catch (Exception e) {
      transaction.rollback();
      e.printStackTrace();
      return "error";
    } finally {
      session.close();
    }
  }
  private String handleStatus(HttpServletRequest request) {
    String statusString = request.getParameter(Rest.REQ_PARAM_STATUS);
    int gameId = getGameId(request);

    log.info(String.format("Received %s message from game: %s", statusString, gameId));

    Session session = HibernateUtil.getSession();
    Transaction transaction = session.beginTransaction();
    try {
      Query query = session.createQuery(Constants.HQL.GET_GAME_BY_ID);
      query.setInteger("gameId", gameId);
      Game game = (Game) query.uniqueResult();

      if (game == null) {
        log.warn(
            String.format(
                "Trying to set status %s on non-existing " + "game : %s", statusString, gameId));
        return "error";
      }

      new GameHandler(game).handleStatus(session, statusString);

      transaction.commit();
    } catch (Exception e) {
      transaction.rollback();
      e.printStackTrace();
      return "error";
    } finally {
      session.close();
    }

    String gameLength = request.getParameter(Rest.REQ_PARAM_GAMELENGTH);
    if (gameLength != null && transaction.wasCommitted()) {
      log.info(String.format("Received gamelength %s for game %s", gameLength, gameId));
      MemStore.addGameLength(gameId, gameLength);
    }

    return "success";
  }
  /** Handle 'POST' to serverInterface.jsp, this is an end-of-game message */
  private String handlePOST(HttpServletRequest request) {
    if (!MemStore.checkMachineAllowed(request.getRemoteAddr())) {
      return "error";
    }

    try {
      String actionString = request.getParameter(Rest.REQ_PARAM_ACTION);
      if (!actionString.equalsIgnoreCase(Rest.REQ_PARAM_GAMERESULTS)) {
        log.debug("The message didn't have the right action-string!");
        return "error";
      }

      int gameId = getGameId(request);
      if (!(gameId > 0)) {
        log.debug("The message didn't have a gameId!");
        return "error";
      }

      Session session = HibernateUtil.getSession();
      Transaction transaction = session.beginTransaction();
      try {
        Game game = (Game) session.get(Game.class, gameId);
        String standings = request.getParameter(Rest.REQ_PARAM_MESSAGE);
        return new GameHandler(game).handleStandings(session, standings, true);
      } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
        return "error";
      } finally {
        session.close();
      }
    } catch (Exception e) {
      log.error("Something went wrong with receiving the POST message!");
      log.error(e.getMessage());
      return "error";
    }
  }
  /** Handle 'PUT' to serverInterface.jsp, either boot.xml or (Boot|Sim) log */
  private String handlePUT(HttpServletRequest request) {
    if (!MemStore.checkMachineAllowed(request.getRemoteAddr())) {
      return "error";
    }

    try {
      String fileName = request.getParameter(Rest.REQ_PARAM_FILENAME);
      log.info("Received a file " + fileName);

      String logLoc =
          fileName.endsWith("boot.xml")
              ? properties.getProperty("bootLocation")
              : properties.getProperty("logLocation");
      String pathString = logLoc + fileName;

      // Write to file
      InputStream is = request.getInputStream();
      FileOutputStream fos = new FileOutputStream(pathString);
      byte buf[] = new byte[1024];
      int letti;
      while ((letti = is.read(buf)) > 0) {
        fos.write(buf, 0, letti);
      }
      is.close();
      fos.close();

      // If sim-logs received, extract end-of-game standings
      new ParserSimlog(pathString).run();

      // Create softlinks to named versions
      String gameName = request.getParameter(Rest.REQ_PARAM_GAMENAME);
      createSoftLinks(fileName, logLoc, gameName);
    } catch (Exception e) {
      return "error";
    }
    return "success";
  }