Skip to content

7) Server (Client Side)

Nicholas Ventimiglia edited this page Dec 30, 2015 · 4 revisions

On the client, the server is exposed through a number of services. You can find these in the /server/ folder. The services contain a number of methods which return tasks. Tasks, like coroutines, are yieldable and contain exception information, http metadata, and and result from the http operation.

// Task Example
var task = AccountService.Login();
yield return task;
//handle task.Exception or Task.Error

That said, it might help to take a look at the example script.

Setup and Configuration

Once the package is unpacked, you will need to do some light configuration

  • On the main menu run Tools/Foundation/Server Settings
    • In the Service URL place the url to your cloud instance.
    • If you are using an application key, add that as well.
    • Works against 'localhost' environments for testing

Account Service

The Account Service is responsible for signing in, password reset, account updating, and facebook integration. It is stateful and remembers the user's authentication token, account information, FB identity and between sessions.

//Get State
AccountService.Instance.IsAuthenticated

//Get Account Details
AccountService.Instance.Account

Guest Mode

Guest mode is for signing users into the service without requiring them to claim an email/password or Facebook token. Users are only using their unique id to authenticate themselves. Users are considered authenticated, and may save data to the server. Users may later update their account with a FB token or email.

//Signs new or existing guest account.
AccountService.Instance.Guest())

Email Accounts

Users may sign in with an email and password. If the user is new, a profile and email will be sent to them upon sign in. If the user loses their password they may request a password reset which will update and email them their new password.

AccountService.Instance.SignIn("myEmail", "myPassword")
AccountService.Instance.Reset("myEmail")
AccountService.Instance.Update("newEmail", "newPassword")

Facebook Accounts

Facebook uses the latest (7) sdk. You must first install and configure this. Once installed, you can connect or disconnect the account using the FB Connect methods

// Facebook sign in, server update, server validation in one call !
AccountService.Instance.FacebookConnect()
// Remove facebook permission
AccountService.Instance.FacebookDisconect()

Storage

Storage is a general purpose storage service. Internally, the data is saved as json blobs. This makes it hard to customize server-side, but, you can completely define your object model on the client. Perfect for prototyping.

Defining an object

Storage objects are clr objects with annotations. Here is an example.

[StorageTable("DemoObject")]
class DemoObject
{
        //This is a unique key, please use GUID.NewGuid().ToString() !
	[StorageIdentity]
	public string Id { get; set; }
	public string String { get; set; }
	public int Number { get; set; }
	public Color Color { get; set; }
	public Vector3 Vector { get; set; }
}

Using the service

Once the object is defined, now all you need to do is call the service ! Remember to sign in first !


DemoObjects[] demoObjects;

// Save or update an object
StorageService.Instance.Update(demoObjects.First());

// Save or update an collection of object
StorageService.Instance.Update(demoObjects);

// Get an object
StorageService.Instance.Get<DemoObject>("UUID [StorageKey]");

// Get an set of objects
StorageService.Instance.Get<DemoObject>(new []{ "UUID1", "UUID2", ...});

// Full OData Query Support !
var query = new ODataQuery<DemoObject>().WhereGreaterThan("Id", 5).OrderBy("String").Take(5);
StorageService.Instance.Query(query);

// Update a single property
StorageService.Instance.UpdateProperty("UUID", "PropertyName", "NewValue");

// Increment / Decrement a property
StorageService.Instance.UpdateDelta("UUID", "PropertyName", +/- 1);

// Sync (Gets the latest, either local or server)
StorageService.Instance.Sync(first);

// Delete
StorageService.Instance.Delete(demoObjects.First());

GameScores

Game scores is an example of strongly typed storage. This differs from the weakly typed storage (above) in that the objects are defined on the server and the client. This allows you to better control the server-side logic (since you are not dealing with a json blob).