Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fractum.networking.session.authenticate;
- import com.fractum.Build;
- import com.fractum.networking.NetworkHandler;
- import com.fractum.networking.codec.messages.builder.DataType;
- import com.fractum.networking.codec.messages.builder.MessageBuilder;
- import com.fractum.networking.session.types.RealmSession;
- import com.fractum.lobby.account.Account;
- import com.fractum.system.SystemLogger;
- import com.fractum.utilities.BufferUtility;
- import io.netty.buffer.ByteBuf;
- import io.netty.channel.Channel;
- import com.fractum.networking.codec.messages.lobby.RealmMessageDecoder;
- import com.fractum.networking.codec.messages.lobby.RealmMessageEncoder;
- import com.fractum.networking.session.authenticate.response.LoginAuthenticationResponses;
- /**
- * @author JTlr Frost 7/20/18 : 11:08 AM
- */
- public class LoginAuthentication {
- /**
- * Represents the Buffer Currently Using
- */
- private Channel channel;
- /**
- * Represents the Current Buffer
- */
- private ByteBuf buffer;
- /**
- * Represents the Response Type.
- */
- private LoginAuthenticationResponses responseType;
- /**
- *
- * @param buffer
- */
- public LoginAuthentication(Channel channel, ByteBuf buffer) {
- this.channel = channel;
- this.buffer = buffer;
- }
- /**
- * Handles the Current Login Request
- */
- public void handleRequest() {
- //Represents the 1.x.x
- int majorPatch = buffer.readByte();
- //Represents the x.23.x
- int minorPatch = buffer.readInt();
- //Represents the x.x.103431
- int hotPatch = buffer.readShort();
- //Represents the Requesting Username
- String accountName = BufferUtility.readString(buffer);
- //Represents the Requesting Password
- String password = BufferUtility.readString(buffer);
- //Represents if the 'Request' is for the 'World' or 'Lobby'
- boolean isWorld = buffer.readByte() == 1;
- //Logs the Request
- SystemLogger.sendSystemMessage("Requesting Login[Username=" + accountName + ", Password=" + password + "], ClientBuild[Major=" + majorPatch + ", MinorPatch=" + minorPatch + ", HotPatch: " + hotPatch + "].");
- /**
- * Checks to make sure the client is up to date with the current patching.
- */
- if (majorPatch != Build.getInstance().getProtocolConf().getBuildNumber()
- || minorPatch != Build.getInstance().getProtocolConf().getMinorBuildNumber()
- || hotPatch != Build.getInstance().getProtocolConf().getHotPatch()) {
- sendAuthenticationResponse(LoginAuthenticationResponses.ERR_INVALID_BUILD);
- return;
- }
- //--------------- Account Credentials -----------------\\
- //Attempts to find the Account Information
- AccountAuthentication authenticationRequest = new AccountAuthentication(accountName, password);
- // Attempts to find the collection with accountRequest (Insensitive)
- if (authenticationRequest.getResults() == null) {
- //Results were not found, and sending an Err
- sendAuthenticationResponse(LoginAuthenticationResponses.ERR_LOADING_ACCOUNT);
- return;
- } else {
- //If there are results with the requested entries, it will begin to fetch the information from the database.
- authenticationRequest.fetch();
- /* Checks if the Authentication has an ErrCode, If So it will send the response. */
- //---------- If the results are null, It will represent a (currently successful authentication! ----------\\
- if (authenticationRequest.getResponseType() != null) {
- sendAuthenticationResponse(authenticationRequest.getResponseType());
- return;
- }
- }
- //--------------- End Of Account Credentials -----------\\
- /** \\\\\\\\\\\\\\\\\\\\ IF Code reaches here' Sets the Authentication to 'Successful'. //////////////////////// */
- sendAuthenticationResponse(LoginAuthenticationResponses.SUCCESSFUL);
- /**
- * Checks the Response Requests
- */
- if (responseType == LoginAuthenticationResponses.SUCCESSFUL) {
- //Initializes the Account
- Account account = new Account(channel, authenticationRequest.getInformationResult());
- //Initializes the Lobby Decoders & Initializes the new Session
- channel.pipeline().replace("session-decoder", "realm-decoder", new RealmMessageDecoder(account));
- channel.pipeline().addAfter("realm-decoder", "realm-encoder", new RealmMessageEncoder());
- channel.attr(NetworkHandler.SESSION_KEY).set(new RealmSession(channel, account));
- //Logs the Request
- SystemLogger.sendSystemMessage("Successful LoginRequest[Username=" + accountName + ", Password=" + password + "].");
- //Writes the Player Information to the client
- sendInfoPlayer(account);
- //Initializes the Account Components
- account.onStart();
- } else
- //Writes a Response code that's resulted into an Error.
- sendAuthenticationResponse(responseType);
- }
- /**
- * Represents the Authentication Response
- * @param responseType
- */
- public void sendAuthenticationResponse(LoginAuthenticationResponses responseType) {
- this.responseType = responseType;
- MessageBuilder builder = new MessageBuilder(1);
- builder.write(DataType.BYTE, responseType.ordinal());
- channel.writeAndFlush(builder.getBuffer());
- SystemLogger.sendSystemMessage("Authentication Response Sent, ResponseType[Type=" + responseType.name() + "].");
- }
- /**
- *
- * @param account
- * @return
- */
- public void sendInfoPlayer(Account account) {
- MessageBuilder builder = new MessageBuilder(2);
- builder.write(DataType.BYTE, 0);//Lobby ? 0 : 1 World
- builder.write(DataType.BYTE, account.getAccountInformation().getAccountLevel().ordinal());
- builder.write(DataType.STRING, account.getAccountInformation().getAccountName());
- builder.write(DataType.STRING, account.getAccountInformation().getPassword());
- channel.writeAndFlush(builder.getBuffer());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement