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

📄 accountmanager.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
 * Copyright (C) 2001-2002 WOTLAS Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package wotlas.server;

import wotlas.common.*;
import wotlas.common.universe.*;

import wotlas.libs.persistence.*;
import wotlas.utils.Debug;
import wotlas.utils.FileTools;
import wotlas.utils.Tools;

import wotlas.common.objects.inventories.Inventory;

import java.io.File;

import java.util.HashMap;
import java.util.Iterator;


/** The AccountManager posseses all the client's game accounts. Note that all the
 *  methods of the AccountManager are not synchronized... so use the AccountManager with
 *  care ! It should only be used as server start-up or for daily persistence save in
 *  maintenance mode.
 *
 * @author Aldiss
 * @see wotlas.server.GameServer
 */

public class AccountManager {

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

  /** Format of the account file names.
   */
    public final static String CLIENT_PROFILE = "profile.cfg";
    public final static String PLAYER_PREFIX  = "player-save-";
    public final static String PLAYER_SUFFIX  = ".cfg";

  /** Inventory file name format ( I changed the suffix to be sure to don't mess with the player saves )
   */
    public final static String INVENTORY_PREFIX  = "inventory-save-";
    public final static String INVENTORY_SUFFIX  = ".ifg";

   /** Maximum number of save in a client account. If this number is reached we delete
    *  the oldest entry.
    */
      public final static int MAX_NUMBER_OF_SAVE = 3;

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

   /** Client Login ( equals to the client's directory name )
    */
      private HashMap accounts;

   /** Our resource Manager
    */
      private ResourceManager rManager;

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

   /** Constructor. Attempts to load the client accounts.
    */
     public AccountManager( ResourceManager rManager ) {
     	  this.rManager = rManager;
     }

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

  /** To init & load the client Accounts.
   */
     public void init() {
       int nbAccounts=0,nbAccountsLoaded=0;

       // Call to the PersistenceManager to load the accounts from the dataBase.
          GameAccount account_list[] = loadAccounts();

            if( account_list!=null )
                nbAccounts = account_list.length;

            // HashMap init. ( initial size is 1.5*Nb Accounts +10 )
            // The HashMap Load Factor is 75%. The size doubles after that.
               accounts = new HashMap( (int) (1.5*nbAccounts + 10) );

            // we fill the hash-map...
               for( int i=0; i<nbAccounts; i++ )
                   if( account_list[i]!=null ) {

                       if(account_list[i].getPlayer()==null) {
                       	  Debug.signal(Debug.ERROR, this, "Failed to load account "+account_list[i].getPrimaryKey()+"! No player data found !");
                       	  continue;
                       }

                       accounts.put( account_list[i].getAccountName(), account_list[i] );
                       nbAccountsLoaded++;
                   }

          Debug.signal( Debug.NOTICE, null, "AccountManager loaded "+nbAccountsLoaded+" accounts." );
     }

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

  /** Erases all the possessed data.
   */
     public void clear() {
     	accounts.clear();
     	accounts = null;
     	rManager = null;
     }

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

  /** To iterate over the game accounts.
   *
   * @return an iterator that review all the GameAccounts.
   */
     public Iterator getIterator() {
         return accounts.values().iterator();
     }

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

  /** To create a new acount. The account must have been fully initialized ( don't forget
   *  the setPlayer() ! even if it's a transient field we use it to save Player data in
   *  a separate file ).
   *
   * @param account a game account
   * @return true means success, false usually means that the account name already exists.
   */
     public synchronized boolean createAccount( GameAccount account ) {
           if( !createAccountFiles( account ) )
               return false;

        // we insert the account in our table
           accounts.put( account.getAccountName(), account );
           return true;
     }

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

  /** To check if an account exists.
   *
   * @param accountName account name.
   * @return true if the account exists, false otherwise.
   */
     public synchronized boolean checkAccountName( String accountName ) {
         return accounts.containsKey( accountName );
     }

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

  /** To add a new account (note that we don't check if it already exists).
   *
   * @param account account to add.
   */
     public synchronized void addAccount( GameAccount account ) {
         accounts.put( account.getAccountName(), account );
     }

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

  /** To remove an account (note that we don't check if it already exists nor we delete
   *  associated resources). Use with care !
   *
   * @param accountName name of the account to remove
   */
     public synchronized void removeAccount( String accountName ) {
         accounts.remove( accountName );
     }

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

  /** To get an account from its name.
   *
   * @param accountName the account name...
   * @return the wanted account, or null if the account doesnot exist...
   */
     public synchronized GameAccount getAccount( String accountName ) {
         return (GameAccount) accounts.get( accountName );
     }

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

  /** To delete an account from its name.
   *
   * @param accountName the account name...
   * @param closeIfConnected if true we close the player's connection if he is connected.
   * @return true if the account was deleted.
   */
     public synchronized boolean deleteAccount( String accountName, boolean closeIfConnected ) {
           WorldManager wManager = ServerDirector.getDataManager().getWorldManager();

        // We get the account
           GameAccount account = (GameAccount) accounts.get( accountName );

           if( account==null ) {
               Debug.signal( Debug.ERROR, this, "Account "+accountName+" not found" );
               return false;
           }

        // we remove the account hashmap entry
           accounts.remove( accountName );

           if( closeIfConnected && account.getPlayer().isConnectedToGame() ) {
               account.getPlayer().closeConnection();
               Debug.signal( Debug.WARNING, this, "Client connection was closed during the delete request.");
           }

        // we remove the character from the game...
           wManager.removePlayerFromUniverse( account.getPlayer() );

        // we delete the files
           if( !deleteAccountFiles( accountName ) ) {
               Debug.signal( Debug.ERROR, this, "Failed to delete Account files: "+accountName );
               return true; // account erased but some files remain !
           }

           return true;
     }

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

  /** To change the status of an account : a 'dead' account means its character has been
   *  killed in the game.
   *
   * @param account the account...
   * @return true if the account was deleted.
   */
     public synchronized boolean changeToDeadAccount( GameAccount account ) {
           WorldManager wManager = ServerDirector.getDataManager().getWorldManager();

        // We get the account           
           if( account==null || account.getIsDeadAccount() )
               return false;

           if( account.getPlayer().isConnectedToGame() )
               account.getPlayer().closeConnection();

        // we remove the character from the game...
           account.setIsDeadAccount(true);

⌨️ 快捷键说明

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