/**
   * Constructor
   *
   * @param netMan the NetworkServerManager so that we can send message
   * @throws Exception in case of an unexpected error
   */
  public RPServerManager(INetworkServerManager netMan) throws Exception {
    super("RPServerManager");

    try {
      stats = Statistics.getStatistics();
      keepRunning = true;
      isfinished = false;

      scheduler = new RPScheduler();
      contentsToTransfer = new HashMap<RPObject, List<TransferContent>>();
      playerContainer = PlayerEntryContainer.getContainer();

      playersToRemove = new LinkedList<PlayerEntry>();
      this.netMan = netMan;

      Configuration conf = Configuration.getConfiguration();
      /*
       * This method loads the extensions that implement the game server
       * code.
       */
      initializeExtensions(conf);

      String duration = conf.get("turn_length");
      turnDuration = Long.parseLong(duration);
      turn = 0;
    } catch (Exception e) {
      logger.warn("ABORT: Unable to create RPZone, RPRuleProcessor or RPAIManager instances", e);
      throw e;
    }
  }
  /**
   * Creates a character for a account of a player
   *
   * @param username player's username
   * @param character
   * @param template the template we are going to use to create the object.
   * @param address ip address of client
   * @return a Result indicating if account creation was done successfully or if it is not the
   *     cause.
   */
  public CharacterResult createCharacter(
      String username, String character, RPObject template, String address) {
    try {
      if (!Boolean.parseBoolean(
          Configuration.getConfiguration().get("allow_account_creation", "true"))) {
        return new CharacterResult(Result.FAILED_CREATE_ON_MAIN_INSTEAD, character, template);
      }
    } catch (IOException e) {
      logger.error(e, e);
    }

    // check account creation limits
    try {
      if (DAORegister.get()
          .get(CharacterDAO.class)
          .isCharacterCreationLimitReached(username, address)) {
        return new CharacterResult(Result.FAILED_TOO_MANY, character, template);
      }
    } catch (SQLException e) {
      logger.error(e, e);
      return new CharacterResult(Result.FAILED_EXCEPTION, character, template);
    } catch (IOException e) {
      logger.error(e, e);
      return new CharacterResult(Result.FAILED_EXCEPTION, character, template);
    }
    return ruleProcessor.createCharacter(username, character, template);
  }
  /**
   * Creates an account for a player in the game.
   *
   * @param username player's username
   * @param password player's password
   * @param email player's email
   * @param address ip address of client
   * @return a Result indicating if account creation was done successfully or not.
   */
  public AccountResult createAccount(
      String username, String password, String email, String address) {
    try {
      if (!Boolean.parseBoolean(
          Configuration.getConfiguration().get("allow_account_creation", "true"))) {
        return new AccountResult(Result.FAILED_CREATE_ON_MAIN_INSTEAD, username);
      }
    } catch (IOException e) {
      logger.error(e, e);
    }

    // check account creation limits
    try {
      if (DAORegister.get().get(AccountDAO.class).isAccountCreationLimitReached(address)) {
        return new AccountResult(Result.FAILED_TOO_MANY, username);
      }
    } catch (SQLException e) {
      logger.error(e, e);
      return new AccountResult(Result.FAILED_EXCEPTION, username);
    } catch (IOException e) {
      logger.error(e, e);
      return new AccountResult(Result.FAILED_EXCEPTION, username);
    }

    // forward the creation request to the game
    return ruleProcessor.createAccount(username, password, email);
  }
 /**
  * Add a login event to database each time player login, even if it fails.
  *
  * @param transaction DBTransactions
  * @param address the IP address that originated the request.
  * @param result 0 failed password, 1 successful login, 2 banned, 3 inactive, 4 blocked, 5 merged
  * @throws SQLException if there is any database problem.
  */
 public void addLoginEvent(DBTransaction transaction, InetAddress address, int result)
     throws SQLException {
   String service = null;
   try {
     Configuration conf = Configuration.getConfiguration();
     if (conf.has("server_service")) {
       service = conf.get("server_service");
     } else {
       service = conf.get("server_typeGame");
     }
   } catch (IOException e) {
     logger.error(e, e);
   }
   DAORegister.get()
       .get(LoginEventDAO.class)
       .addLoginEvent(transaction, username, address, service, seed, result);
 }
  /**
   * This method loads the extensions: IRPRuleProcessor and IRPWorld that are going to be used to
   * implement your game. This method loads these class from the class names passed as arguments in
   * Configuration
   *
   * @param conf the Configuration class
   * @throws ClassNotFoundException
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws SecurityException
   * @throws IllegalArgumentException
   */
  protected void initializeExtensions(Configuration conf)
      throws ClassNotFoundException, IllegalArgumentException, SecurityException,
          IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    Class<?> worldClass = Class.forName(conf.get("world", "marauroa.server.game.rp.RPWorld"));
    // call the get() method without parameters to retrieve the singleton
    // instance
    world =
        (RPWorld) worldClass.getDeclaredMethod("get", new Class[0]).invoke(null, (Object[]) null);
    RPWorld.set(world);
    world.onInit();

    Class<?> ruleProcessorClass =
        Class.forName(conf.get("ruleprocessor", "marauroa.server.game.rp.RPRuleProcessorImpl"));
    // call the get() method without parameters to retrieve the singleton
    // instance
    ruleProcessor =
        (IRPRuleProcessor)
            ruleProcessorClass.getDeclaredMethod("get", new Class[0]).invoke(null, (Object[]) null);
    ruleProcessor.setContext(this);
  }