⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountmanager.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
           wManager.removePlayerFromUniverse( account.getPlayer() );
           Debug.signal( Debug.WARNING, this, "Account "+account.getAccountName()+" is dead...");
           return true;
     }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To get a list of online players.
   *
   * @return a list of online players
   */
    public synchronized HashMap getOnlinePlayers() {
     // HashMap init. ( initial size is Nb Accounts/2 )
       HashMap onlinePlayers = new HashMap((int)accounts.size()/2);
     
       Iterator it = accounts.values().iterator();
       PlayerImpl player;

       while ( it.hasNext() ) {
         player = ( (GameAccount) it.next() ).getPlayer();
         if (player.isConnectedToGame()) {
            onlinePlayers.put(player.getPrimaryKey(), player);
         }
       }
     
      return onlinePlayers;
    }
   
 /*------------------------------------------------------------------------------------*/

  /** To load all the client Accounts. Warning: the returned array can have null entries.
   *
   * @return all the client accounts found in the databasePath+"/"+ACCOUNTS_HOME
   */
   public GameAccount[] loadAccounts() {

      String accountHome =  rManager.getExternalPlayersHomeDir();
      String accountList[] = rManager.listDirectories( accountHome );

     // no accounts ?
        if( accountList==null || accountList.length==0 ) {
           Debug.signal( Debug.WARNING, this, "No player accounts found in: "+accountHome );
           return null;
        }

     // We load the different accounts...
        GameAccount accounts[] = new GameAccount[accountList.length];

        for( int i=0; i<accountList.length; i++ ) {

             if( accountList[i].indexOf('-')<0 )
                 continue;

          // we load the client's profile
             accounts[i] = (GameAccount) rManager.loadObject( accountList[i] + CLIENT_PROFILE );

             if(accounts[i]==null) {
                Debug.signal(Debug.ERROR, this, "Failed to load "+accountList[i]);
                continue;
             }

          // we now search for the latest saved player file.
             String latest = FileTools.findSave( rManager.listFiles( accountList[i], PLAYER_SUFFIX ),
                                                 PLAYER_PREFIX, PLAYER_SUFFIX, true );

          // have we found the latest saved file ?
             if( latest==null ) {
              // nope, then it's an invalid account, we ignore it...
                 Debug.signal( Debug.ERROR, this, "Failed to load account: "+ accountList[i] );
                 accounts[i] = null;
                 continue;
             }

             // PlayerImpl player = (PlayerImpl) rManager.loadObject( latest );
             PlayerImpl player = (PlayerImpl) rManager.RestoreObject( latest );

             if(player!=null)
                accounts[i].setPlayer( player );
             else {
                Debug.signal(Debug.ERROR,this,"Failed to load "+latest+" for player "+accounts[i].getPrimaryKey() );
                accounts[i] = null;
             }

          // we now search for the latest saved inventory file.
             latest = FileTools.findSave( rManager.listFiles( accountList[i], INVENTORY_SUFFIX ),
                                                 INVENTORY_PREFIX, INVENTORY_SUFFIX, true );

          // have we found the latest saved inventory file ?
             if( latest==null ) {
              // nope, then it's an invalid account, we ignore it...
                 Debug.signal( Debug.ERROR, this, "Failed to load account's inventory: "+ accountList[i] );
                 accounts[i] = null;
                 continue;
             }

             Inventory inventory = (Inventory) rManager.loadObject( latest );

             if(inventory!=null) {
              // Aldiss : we create a new ServerObjectManager and give it its inventory
                 ServerObjectManager objManager = new ServerObjectManager();
                 player.setObjectManager( objManager );
                 objManager.setInventory( inventory );
             }
             else {
                Debug.signal(Debug.ERROR,this,"Failed to load "+latest+" inventory for player "+accounts[i].getPrimaryKey() );
//                accounts[i] = null;
                // Petrus : if no inventory found : create a new one
                ServerObjectManager objManager = new ServerObjectManager();
                player.setObjectManager( objManager );
                objManager.setInventory( new Inventory());
             }
        }

      return accounts;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To save a client Account. Deletes the oldest entry if the account has too many.
   *
   * @param account client account
   * @return true if the account has been saved succesfully.
   */
   public boolean saveAccount( GameAccount account ) {

      String accountHome =  rManager.getExternalPlayersHomeDir();

//      if( !rManager.saveObject( account.getPlayer(),
//                                    accountHome + account.getAccountName() + File.separator
//                                    +PLAYER_PREFIX+Tools.getLexicalDate()+PLAYER_SUFFIX ) ) {
      if( !rManager.BackupObject( account.getPlayer(),
                                    accountHome + account.getAccountName() + File.separator
                                    +PLAYER_PREFIX+Tools.getLexicalDate()+PLAYER_SUFFIX ) ) {
          Debug.signal( Debug.ERROR, this, "Failed to save account: "
                            + accountHome + account.getAccountName() );
          return false;
      }

    // Aldiss : we save the player's inventory
      if( !rManager.saveObject( account.getPlayer().getObjectManager().getInventory(),
                                    accountHome + account.getAccountName() + File.separator
                                    +INVENTORY_PREFIX+Tools.getLexicalDate()+INVENTORY_SUFFIX ) ) {
          Debug.signal( Debug.ERROR, this, "Failed to save account inventory: "
                            + accountHome + account.getAccountName() );
          return false;
      }

   // ok, the save went ok... do we have to erase the oldest file ?
      String accountFiles[] = rManager.listFiles( accountHome + account.getAccountName(),
                                                  PLAYER_SUFFIX );

      if( accountFiles.length > MAX_NUMBER_OF_SAVE+1 ) {
        // ok, let's delete the oldest save	
           String oldest = FileTools.findSave( accountFiles, PLAYER_PREFIX, PLAYER_SUFFIX, false );

        // have we found the latest saved file ?
           if( oldest!=null ) {
             // is it the file we've just saved, (if so we won't delete it, otherwise we will)
               if( oldest.endsWith(PLAYER_PREFIX+Tools.getLexicalDate()+PLAYER_SUFFIX)
                   || new File( oldest ).delete() ) {

                   accountFiles = rManager.listFiles( accountHome + account.getAccountName(),
                                                      INVENTORY_SUFFIX );

                   oldest = FileTools.findSave( accountFiles, INVENTORY_PREFIX, INVENTORY_SUFFIX, false );

                // have we found the latest saved inventory ?
                   if( oldest!=null ) {
                    // is it the file we've just saved, (if so we won't delete it, otherwise we will)
                       if( oldest.endsWith(INVENTORY_PREFIX+Tools.getLexicalDate()+INVENTORY_SUFFIX)
                           || new File( oldest ).delete() ) {
                           return true;
                       }
                   }
               }
           }
                      
           Debug.signal( Debug.WARNING, this, "Failed to find old account entry : "
                                              + oldest );
      }
      
      return true;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To create a client Account.
   *
   * @param account client account
   * @return true if the account has been created succesfully.
   */
   public boolean createAccountFiles( GameAccount account ) {

      String accountHome =  rManager.getExternalPlayersHomeDir();
      File accountDir = new File( accountHome+account.getAccountName() );

      if( !accountDir.mkdir() ) {
          Debug.signal( Debug.ERROR, this, "Failed to create account: "
                            + accountDir.getName() );
          return false;
      }

      // we create the client's "profile.cfg"
      if( !rManager.saveObject( account,
                           accountHome + account.getAccountName() + File.separator + CLIENT_PROFILE ) ) {
          Debug.signal( Debug.ERROR, this, "Failed to save account's profile: "
                            + accountHome + account.getAccountName() );

          if( !deleteAccountFiles( account.getAccountName() ) )
               Debug.signal( Debug.WARNING, this, "Failed to delete bad account" );

          return false;
      }

    // we save the player data in something like "player-save-2001-09-23.cfg"
       if( !saveAccount( account ) ) {
           if( !deleteAccountFiles( account.getAccountName() ) )          
              Debug.signal( Debug.WARNING, this, "Failed to delete bad account" );

           return false;
       }
       
       return true;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To delete a client Account.
   *
   * @param accountName account name, use GameAccount.getAccountName().
   * @return true if the account has been deleted succesfully.
   */
   public boolean deleteAccountFiles( String accountName ) {

      String accountHome =  rManager.getExternalPlayersHomeDir();
      File accountDir = new File( accountHome+accountName );

      // account doesnot exists
         if( !accountDir.exists() )
             return true;

         File list[] = accountDir.listFiles();
      
      // we delete the account's file
         if( list!=null )
             for( int i=0; i<list.length; i++ )
                  if( !list[i].delete() )
                      return false;

      return accountDir.delete();
   }

 /*------------------------------------------------------------------------------------*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -