Advertisement
Guest User

Untitled

a guest
Dec 31st, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 KB | None | 0 0
  1. package com.fractum.networking.session.authenticate;
  2.  
  3. import com.fractum.Build;
  4. import com.fractum.networking.NetworkHandler;
  5. import com.fractum.networking.codec.messages.builder.DataType;
  6. import com.fractum.networking.codec.messages.builder.MessageBuilder;
  7. import com.fractum.networking.session.types.RealmSession;
  8. import com.fractum.lobby.account.Account;
  9. import com.fractum.system.SystemLogger;
  10. import com.fractum.utilities.BufferUtility;
  11. import io.netty.buffer.ByteBuf;
  12. import io.netty.channel.Channel;
  13. import com.fractum.networking.codec.messages.lobby.RealmMessageDecoder;
  14. import com.fractum.networking.codec.messages.lobby.RealmMessageEncoder;
  15. import com.fractum.networking.session.authenticate.response.LoginAuthenticationResponses;
  16.  
  17. /**
  18.  * @author JTlr Frost 7/20/18 : 11:08 AM
  19.  */
  20. public class LoginAuthentication {
  21.  
  22.     /**
  23.      * Represents the Buffer Currently Using
  24.      */
  25.     private Channel channel;
  26.  
  27.     /**
  28.      * Represents the Current Buffer
  29.      */
  30.     private ByteBuf buffer;
  31.  
  32.     /**
  33.      * Represents the Response Type.
  34.      */
  35.     private LoginAuthenticationResponses responseType;
  36.  
  37.     /**
  38.      *
  39.      * @param buffer
  40.      */
  41.     public LoginAuthentication(Channel channel, ByteBuf buffer) {
  42.         this.channel = channel;
  43.         this.buffer = buffer;
  44.     }
  45.  
  46.     /**
  47.      * Handles the Current Login Request
  48.      */
  49.     public void handleRequest() {
  50.         //Represents the 1.x.x
  51.         int majorPatch = buffer.readByte();
  52.  
  53.         //Represents the x.23.x
  54.         int minorPatch = buffer.readInt();
  55.  
  56.         //Represents the x.x.103431
  57.         int hotPatch = buffer.readShort();
  58.  
  59.         //Represents the Requesting Username
  60.         String accountName = BufferUtility.readString(buffer);
  61.  
  62.         //Represents the Requesting Password
  63.         String password = BufferUtility.readString(buffer);
  64.  
  65.         //Represents if the 'Request' is for the 'World' or 'Lobby'
  66.         boolean isWorld = buffer.readByte() == 1;
  67.  
  68.         //Logs the Request
  69.         SystemLogger.sendSystemMessage("Requesting Login[Username=" + accountName + ", Password=" + password + "], ClientBuild[Major=" + majorPatch + ", MinorPatch=" + minorPatch + ", HotPatch: " + hotPatch + "].");
  70.  
  71.         /**
  72.          * Checks to make sure the client is up to date with the current patching.
  73.          */
  74.         if (majorPatch != Build.getInstance().getProtocolConf().getBuildNumber()
  75.                 || minorPatch != Build.getInstance().getProtocolConf().getMinorBuildNumber()
  76.                 || hotPatch != Build.getInstance().getProtocolConf().getHotPatch()) {
  77.             sendAuthenticationResponse(LoginAuthenticationResponses.ERR_INVALID_BUILD);
  78.             return;
  79.         }
  80.  
  81.         //--------------- Account Credentials -----------------\\
  82.  
  83.  
  84.         //Attempts to find the Account Information
  85.         AccountAuthentication authenticationRequest = new AccountAuthentication(accountName, password);
  86.  
  87.         // Attempts to find the collection with accountRequest (Insensitive)
  88.         if (authenticationRequest.getResults() == null) {
  89.             //Results were not found, and sending an Err
  90.             sendAuthenticationResponse(LoginAuthenticationResponses.ERR_LOADING_ACCOUNT);
  91.             return;
  92.         } else {
  93.  
  94.             //If there are results with the requested entries, it will begin to fetch the information from the database.
  95.             authenticationRequest.fetch();
  96.  
  97.             /* Checks if the Authentication has an ErrCode, If So it will send the response. */
  98.             //---------- If the results are null, It will represent a (currently successful authentication! ----------\\
  99.             if (authenticationRequest.getResponseType() != null) {
  100.                 sendAuthenticationResponse(authenticationRequest.getResponseType());
  101.                 return;
  102.             }
  103.         }
  104.  
  105.  
  106.         //--------------- End Of Account Credentials -----------\\
  107.  
  108.         /** \\\\\\\\\\\\\\\\\\\\ IF Code reaches here' Sets the Authentication to 'Successful'. //////////////////////// */
  109.  
  110.         sendAuthenticationResponse(LoginAuthenticationResponses.SUCCESSFUL);
  111.  
  112.         /**
  113.          * Checks the Response Requests
  114.          */
  115.         if (responseType == LoginAuthenticationResponses.SUCCESSFUL) {
  116.  
  117.             //Initializes the Account
  118.             Account account = new Account(channel, authenticationRequest.getInformationResult());
  119.  
  120.  
  121.             //Initializes the Lobby Decoders & Initializes the new Session
  122.             channel.pipeline().replace("session-decoder", "realm-decoder", new RealmMessageDecoder(account));
  123.             channel.pipeline().addAfter("realm-decoder", "realm-encoder", new RealmMessageEncoder());
  124.             channel.attr(NetworkHandler.SESSION_KEY).set(new RealmSession(channel, account));
  125.  
  126.             //Logs the Request
  127.             SystemLogger.sendSystemMessage("Successful LoginRequest[Username=" + accountName + ", Password=" + password + "].");
  128.  
  129.             //Writes the Player Information to the client
  130.             sendInfoPlayer(account);
  131.  
  132.             //Initializes the Account Components
  133.             account.onStart();
  134.  
  135.         } else
  136.             //Writes a Response code that's resulted into an Error.
  137.             sendAuthenticationResponse(responseType);
  138.     }
  139.  
  140.     /**
  141.      * Represents the Authentication Response
  142.      * @param responseType
  143.      */
  144.     public void sendAuthenticationResponse(LoginAuthenticationResponses responseType) {
  145.         this.responseType = responseType;
  146.         MessageBuilder builder = new MessageBuilder(1);
  147.         builder.write(DataType.BYTE, responseType.ordinal());
  148.         channel.writeAndFlush(builder.getBuffer());
  149.         SystemLogger.sendSystemMessage("Authentication Response Sent, ResponseType[Type=" + responseType.name() + "].");
  150.     }
  151.  
  152.     /**
  153.      *
  154.      * @param account
  155.      * @return
  156.      */
  157.     public void sendInfoPlayer(Account account) {
  158.         MessageBuilder builder = new MessageBuilder(2);
  159.         builder.write(DataType.BYTE, 0);//Lobby ? 0 : 1 World
  160.         builder.write(DataType.BYTE, account.getAccountInformation().getAccountLevel().ordinal());
  161.         builder.write(DataType.STRING, account.getAccountInformation().getAccountName());
  162.         builder.write(DataType.STRING, account.getAccountInformation().getPassword());
  163.         channel.writeAndFlush(builder.getBuffer());
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement