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

📄 wyrmserverbean.java

📁 CroftSoft Code Library是一个开源的可移植的纯Java游戏库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

       return createResponse (
         TYPE_DESTROY_USER,
         true,
         "User " + username + " destroyed.",
         username );
     }

     private Object  serveRequestDungeon ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = getPcLocal ( userLocal );

       checkState ( pcLocal, STATE_TOWN );

       pcLocal.setState ( STATE_DUNGEON );

       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage ( "You have entered the dungeon." );

       return responseState;
     }

     private Object  serveRequestFight ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = getPcLocal ( userLocal );

       checkState ( pcLocal, STATE_DUNGEON );

       boolean  victory = random.nextBoolean ( );

       String  message = null;

       if ( victory )
       {
         message = "You conquer and find treasure.";

         pcLocal.setWealth ( pcLocal.getWealth ( ) + 1 );

         long  experience = pcLocal.getExperience ( ) + 1;

         pcLocal.setExperience ( experience );

         long  level = pcLocal.getLevel ( );

         if ( MathLib.log ( experience, 10 ) >= level + 1 )
         {
           pcLocal.setLevel ( ++level );
         }
       }
       else
       {
         message = "You have been wounded.";

         pcLocal.setHealth ( pcLocal.getHealth ( ) - 1 );
       }

       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage ( message );

       return responseState;
     }

     private Object  serveRequestFlee ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = getPcLocal ( userLocal );

       checkState ( pcLocal, STATE_DUNGEON );

       pcLocal.setState ( STATE_TOWN );

       pcLocal.setHealth ( pcLocal.getHealth ( ) - 1 );

       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage ( "You are wounded as you turn to flee." );

       return responseState;
     }

     private Object  serveRequestHeal ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = getPcLocal ( userLocal );

       checkState ( pcLocal, STATE_HEALER );

       pcLocal.setWealth ( pcLocal.getWealth ( ) - HEALING_COST );

       pcLocal.setHealth ( INIT_PC_HEALTH );

       pcLocal.setState ( STATE_TOWN );

       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage (
         "You return to the town square after being healed." );

       return responseState;
     }

     private Object  serveRequestHealer ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = getPcLocal ( userLocal );

       checkState ( pcLocal, STATE_TOWN );

       pcLocal.setState ( STATE_HEALER );

       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage ( "You enter the Healer's hut." );

       return responseState;
     }

     private Object  serveRequestLogin ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage ( "Welcome to the Wyrm!" );

       return responseState;
     }

     private Object  serveRequestLogout ( String  username )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       return createResponse (
         TYPE_LOGOUT,
         true,
         "Goodbye.",
         username );
     }

     private Object  serveRequestTown ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = getPcLocal ( userLocal );

       checkState ( pcLocal, STATE_HEALER );

       pcLocal.setState ( STATE_TOWN );

       ResponseState  responseState = serveRequestState ( userLocal );

       responseState.setMessage ( "You enter the town square." );

       return responseState;
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     private UserLocal  getUserLocal (
       String  username,
       String  password )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       // Can I move some of this code to ejbCreate() so that it is only
       // executed once?

       InitialContext  initialContext = new InitialContext ( );

       Object  obj = initialContext.lookup ( JNDI_USER_LOCAL_HOME );

       UserLocalHome  userLocalHome = ( UserLocalHome )
         PortableRemoteObject.narrow ( obj, UserLocalHome.class );

       UserLocal  userLocal = null;

       try
       {
         userLocal = userLocalHome.findByUsername ( username );
       }
       catch ( ObjectNotFoundException  ex )
       {
         // Can I use userLocalHome.userExists(username) instead?

         return null;
       }

       if ( !password.equals ( userLocal.getPassword ( ) ) )
       {
         return null;
       }

       return userLocal;
     }

     private ResponseState  serveRequestState ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       ResponseState  responseState
         = ObjectFactory.createResponseState ( );

       responseState.setUsername ( userLocal.getUsername ( ) );

       responseState.setCredits ( userLocal.getCredits ( ) );

       PcLocal  pcLocal = getPcLocal ( userLocal );

       responseState.setState  ( pcLocal.getState  ( ) );

       responseState.setPcName ( pcLocal.getName   ( ) );

       responseState.setHealth ( pcLocal.getHealth ( ) );

       responseState.setWealth ( pcLocal.getWealth ( ) );

       responseState.setLevel  ( pcLocal.getLevel  ( ) );

       responseState.setExperience ( pcLocal.getExperience ( ) );

       responseState.setMessage ( "" );

       return responseState;
     }

     private PcLocal  getPcLocal ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       PcLocal  pcLocal = userLocal.getPcLocal ( );

       if ( pcLocal == null )
       {
         pcLocal = createPcLocal ( userLocal );
       }

       return pcLocal;
     }

     private PcLocal  createPcLocal ( UserLocal  userLocal )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       InitialContext  initialContext = new InitialContext ( );

       Object  obj = initialContext.lookup ( JNDI_PC_LOCAL_HOME );

       PcLocalHome  pcLocalHome = ( PcLocalHome )
         PortableRemoteObject.narrow ( obj, PcLocalHome.class );

       PcLocal  pcLocal = pcLocalHome.create ( );

       pcLocal.setName       ( INIT_PC_NAME       );

       pcLocal.setState      ( INIT_PC_STATE      );

       pcLocal.setHealth     ( INIT_PC_HEALTH     );

       pcLocal.setWealth     ( INIT_PC_WEALTH     );

       pcLocal.setLevel      ( INIT_PC_LEVEL      );

       pcLocal.setExperience ( INIT_PC_EXPERIENCE );

       userLocal.setPcLocal ( pcLocal );

       Long  envInitHealth
         = ( Long ) initialContext.lookup ( JNDI_INIT_HEALTH );

       if ( envInitHealth != null )
       {
         pcLocal.setHealth ( envInitHealth.longValue ( ) );
       }

       return pcLocal;
     }

     private void  checkState (
       PcLocal  pcLocal,
       String   desiredState )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       String  state = pcLocal.getState ( );

       if ( state == null )
       {
         state = INIT_PC_STATE;
       }

       if ( !state.equals ( desiredState ) )
       {
         throw new IllegalStateException ( );
       }       
     }

     private Response  createResponse (
       String   type,
       boolean  granted,
       String   message,
       String   username )
       throws Exception
     //////////////////////////////////////////////////////////////////////
     {
       Response  response = ObjectFactory.createResponse ( );

       response.setType ( type );

       response.setGranted ( granted );

       response.setMessage ( message != null ? message : "" );

       response.setUsername ( username != null ? username : "" );

       return response;
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public void  ejbActivate  ( ) { }

     public void  ejbCreate    ( ) { }

     public void  ejbPassivate ( ) { }

     public void  ejbRemove    ( ) { }

     public void  setSessionContext ( SessionContext  sessionContext ) { }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

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