In this section you will learn how to authenticate the local user, create a new user and load your users.
Authentication
To authenticate the local user you need to call CombuManager.instance.platform.Authenticate:
CombuManager.platform.Authenticate( "username", "password", (bool success, string error) => {
if (success)
Debug.Log("Login success: ID " + CombuManager.localUser.id);
else
Debug.Log("Login failed: " + error);
});
Authenticate a saved session
If you want to auto-login a previously saved session then you need to call AuthenticateSession on a User object:
string sessionToken = PlayerPrefs.GetString("SessionToken", "");
long userId = 0;
if (!long.TryParse (PlayerPrefs.GetString ("UserId", "0"), out userId) || userId < 0)
userId = 0;
User.AuthenticateSession (userId, sessionToken, (bool success, string error) => {
Debug.Log ("AuthenticateSession - Success=" + success + " > Error=" + error);
});
Registration
To create a new user you need to create a new instance of User class, set at least username and password and then call Update on the instance:
User newUser = new User();
newUser.userName = "username";
newUser.password = "password";
newUser.Update( (bool success, string error) => {
if (success)
Debug.Log("Save success: ID " + newUser.id);
else
Debug.Log("Save failed: " + error);
});
Create a guest account
If you want to create a guest account then you need to call CreateGuest on a User object:
var user = new User ();
user.CreateGuest ((bool success, string error) => {
Debug.Log ("CreateGuest - Success=" + success + " > Error=" + error);
if (success) {
PlayerPrefs.SetString ("UserId", CombuManager.localUser.id);
PlayerPrefs.SetString ("SessionToken", CombuManager.localUser.sessionToken);
PlayerPrefs.Save ();
}
});
Load users
To load the users data you can call CombuManager.instance.platform.LoadUsers(), or one of the User.Load() overloads (with User.Load form you will not need to cast back from IUserProfile to User):
User.Load ( 123, ( User user ) => {
Debug.Log("Success: " + (user == null ? "false" : "true"));
});
User.Load ( "user1", ( User user ) => {
Debug.Log("Success: " + (user == null ? "false" : "true"));
});
User.Load ( new long[] { 123, 456 }, ( User[] users ) => {
Debug.Log("Loaded: " + users.Length);
});
SearchCustomData[] searchData = new SearchCustomData[] {
};
User.Load("part-of-username", "email@server.com", searchData, 1, 1, (User[] users, int resultsCount, int pagesCount) => {
Debug.Log("Loaded: " + users.Length);
});
You can also load a list of random users (excluding localUser):
SearchCustomData[] searchData = new SearchCustomData[] {
};
User.Random(searchData, 3, (User[] users) => {
foreach (User user in users)
{
if (user.lastSeen == null)
Debug.Log(user.userName + " Never seen");
else
{
Debug.Log(user.userName + " Last seen: " + seen.ToLongDateString() + " at " + seen.ToLongTimeString() + " - Online state: " + user.state);
}
}
});
Custom Data
You can store any type of data in the Hashtable customData of Profile class, for example you could store the user's virtual currency, level, experience in a RPG game:
CombuManager.localUser.customData["Coins"] = 100;
CombuManager.localUser.Update( (bool success, string error) => {
if (success)
Debug.Log("Success");
else
Debug.Log("Failed: " + error);
});
Online state
The Profile class implements the IUserProfile interface, so it also provides the state property to get the online state of your users. Of course remember that we are in an asynchronous environment, so your players are not really connected in real-time and if you need to rely on the online state then you will need to implement your own polling system to refresh your lists from time to time.
To mantain the online state CombuManager uses the settings pingIntervalSeconds, onlineSeconds and playingSeconds. Besides the ping function that is called every pingIntervalSeconds seconds (set 0 to disable, anyway we'd recommend to have the interval not too small, a value of 30 should be fine else you may suffer of high traffic), every action served by the webservices updates the "last action" date/time of a user.
Create your User class
Since you're able to extend the basic User class with the customData property, the best way to work with the system is to create your own class for users by inheriting from User.
This way you can create your own account properties, that for sure are much more readable than customData["myProperty"] (especially if you need non-string values, it would be lot of explicit casts or Parse!).
Remember to:
-
set their values in customData, so they will be passed to Update and saved to server
-
override FromHashtable to fill the internal variables from the customData received from server
public class CombuDemoUser :
Combu.User
{
string _myProperty1 = "";
int _myProperty2 = 0;
public string myProperty1
{
get { return _myProperty1; }
set { _myProperty1 = value; customData["myProperty1"] = _myProperty1; }
}
public int myProperty2
{
get { return _myProperty2; }
set { _myProperty2 = value; customData["myProperty2"] = _myProperty2; }
}
public CombuDemoUser()
{
myProperty1 = "";
myProperty2 = 0;
}
public override void FromHashtable (Hashtable hash)
{
base.FromHashtable (hash);
if (customData.ContainsKey("myProperty1"))
_myProperty1 = customData["myProperty1"].ToString();
if (customData.ContainsKey("myProperty2"))
_myProperty2 = int.Parse(customData["myProperty2"].ToString());
}
}
To use the new class in your code, you will need to pass the referenced type to the user-wise methods:
CombuManager.platform.Authenticate <CombuDemoUser> ( "username", "password", (bool success, string error) => {
if (success)
Debug.Log("Success");
else
Debug.Log("Failed: " + error);
});
CombuDemoUser newUser = new CombuDemoUser();
newUser.userName = "username";
newUser.password = "password";
newUser.myProperty1 = "Value";
newUser.myProperty2 = 100;
newUser.Update( (bool success, string error) => {
if (success)
Debug.Log("Save success: ID " + newUser.id);
else
Debug.Log("Save failed: " + error);
});