Wednesday, June 22, 2011

Foray into Photon - Part 15 - LoginOperation client side

This time around we are going to create our first real operation. I am going to start off in the AegisBornCommon project as we need to add some data there first. First we need to pull up the OperationCode.cs file. We are going to add a new operation code. In projects past, I tend to lump messages into groups. It makes it easier to know what type of message you are working with if you have your codes in groups. For this instance we are going to take numbers 100-110 for any operations before we are fully in the game. Exit Games uses some numbers for their own special operations and parameters. They have stated that operations 100 and up and Parameters 61 and up are open so for now just add this after Nil,

        /// <summary>
        /// Login to the server
        /// </summary>
        Login = 100,

Next we need to add our parameter codes after DebugMessage in ParameterCode.cs:

        /// <summary>
        /// The player's username
        /// </summary>
        UserName = 70,

        /// <summary>
        /// The player's password
        /// </summary>
        Password = 71,

Now that we have our operation code and parameter code, we can recompile the Common project and make sure it is in the same directory as our AegisBorn3dPhoton project so that Unity3d can use it. Now if you read through the SmartFoxServer blog posts, you'll know I hate building stuff manually, meaning if I'm going to build a message to send to a server, I build it in a class/function so that I don't accidentally have 15 versions floating around in code. Exit Games did this nicely with a static class called Operations. However, I feel lumping them all into the same class defeats the purpose of a class. So I'm going to build a file called LoginOperations and I'm going to put it in a directory called _Operations under _Scripts:

using System.Collections;
using AegisBornCommon;

public static class LoginOperations
{
    public static void Login(Game game, string username, string password)
    {
        var vartable = new Hashtable
                           {
                               {(byte) ParameterCode.UserName, username},
                               {(byte) ParameterCode.Password, password}
                           };

        game.SendOp(OperationCode.Login, vartable, true, 0, true);
    }
}

And that will now send the login operation to the server. Next we are going to grab our old GUI code from the SmartFoxServer code and put it into the Login.cs OnGUI with some minor modifications:

    private Game _engine;


    void Start()
    {
        _engine = MMOEngine.Engine;
        _engine.afterKeysExchanged += AfterKeysExchanged;
    }


    private string _username = "";
    private string _password = "";


    void OnGUI()
    {


        GUI.Label(new Rect(10, 116, 100, 100), "Userame: ");
        _username = GUI.TextField(new Rect(100, 116, 200, 20), _username, 25);
        GUI.Label(new Rect(10, 141, 100, 100), "Password: ");
        _password = GUI.PasswordField(new Rect(100, 141, 200, 20), _password, '*', 25);


        GUI.Label(new Rect(10, 225, 400, 100), _engine.State.ToString());


        if (GUI.Button(new Rect(100, 165, 100, 25), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
        {
            var peer = new PhotonPeer(_engine, false);


            _engine.Initialize(peer, "localhost:5055", "AegisBorn");
        }
        if (GUI.Button(new Rect(100, 195, 100, 25), "Logout"))
        {
            _engine.SetDisconnected(0);
        }
    }


    public void AfterKeysExchanged()
    {
        LoginOperations.Login(_engine, _username, _password);
    }

With encryption in place, we are now sending the server our username and password encrypted. The server will only send a response stating that it got the message. 


If you were to run the program at this point, you will see that we have our login screen, when you click Login you will see the state transfer from Disconnected to WaitingForConnect to Connected. The log should have a message stating connected and then Keys Exchanged and then an unknown response to our Login operation.


Next time we will look into modifying the server with NHibernate to begin looking up our data and checking our login information.


As an aside, I've joined Empire Avenue. {EAV_BLOG_VER:caf99dc9611a09b5}

No comments: