Location based games (Pokemon GO for example) require an implementation which is both robust enough to service players at scale and flexible enough to keep up with a constantly changing data ecosystem. Managing a large data set which is rapidly changing can prove difficult. This difficulty increases significantly, of course, when we consider the need to service thousands of players. Achieving the fine balance between being performant and feature-rich is one of the primary headaches when developing online games.
GameSparks provides all the tools needed to satisfy both of these constraints, empowering you, the developer, to leverage the full power of the cloud. In this tutorial, we'll outline the various GameSparks components which you can combine to implement a Geo-Location based game:
- Data Persistence using Redis Cache
- Setting up Events for Adding/Getting Location
- Writing Cloud Code for Add/Get Events
Data Persistence using Redis Cache
All modern online games are data driven. However, location based games rely even more heavily on the reading and writing of data. Availability and latency are critical, so that the end users (your players) have a seamless gameplay experience. To tackle these needs requires a specific method of saving and loading data. We must consider the frequency of access on the backend and the room for variations in latency we have on the client. To meet our needs we'll be utilizing a Redis data cache, which is inherently more performant for the storing of simple key/value pairs, and in which format our location data is going to be stored.
Firstly, a couple of things of note about Redis:
- Redis is intended for the storing of simple key/value pairs, instead of more complex data sets which should be stored in a database (Geo-Objects, though they might be viewed as more complex data objects are the exception to this rule). For more details on the other methods of data persistence we provide, please see here.
- When used correctly (see above), Redis read and write times will remain in the low milliseconds range.
- The Redis cache is backed up to the database each hour, so you data is safe.
Redis will enable us to build and sustain our Geo-Location data infrastructure.
Setting Up Events for Adding/Getting Location
LogEventRequests are the primary driver for API Events on the GameSparks service. It's through these requests that the client communicates with the backend and through this mechanism that your Cloud Code will be executed.
How to set up and configure Events is outside of the scope of this tutorial but it's well documented here. For this tutorial, we'll need two events. We'll use these events to add and get the location data that we'll store in SparkRedis.
The Add Location Event
Our first Event is for adding a location to the Redis cache:
The Attributes for the Event are:
- A designation for this particular location, for example 'Dublin'.
- The longitude value for this location.
- The latitude value for this location.
- The key this Geo Object should be stored against, for example 'Europe'. This essentially partitions our location indexes so that we don't unnecessarily increase the performance cost of the query being ran.
These Attributes allow us to store a latitude and longitude against a location value. This can be anything you define.
The Get Location Event
Our second Event is for getting a location from the Redis cache:
- The location from which we'll perform a geo-spatial query, for example 'London'
- The range offset, which will define the geo-radius of our query.
- The key which we will query against.
The values for these Event Attributes can be obtained from the client. For instance, if you're building with our Unity SDK, you can utilize the Unity Location Service to measure a player's current location.
Writing the Cloud Code for Add/Get Events
For each Event , you'll need to add Cloud Code to execute the instructions which will write to and read from the Redis Cache. Below are two simple scripts, which will write to and read from in the required format for the GeoRedis API.
Cloud Code for the Add Location Event
Cloud Code for the Get Location Event
When we run GEO_GET in the Test Harness we should see something like the following Response:
As you can see, we've added some locations to the Redis Cache (your response will vary according to the locations you've added) and we've been able to query them according to distance we've defined.
When querying, it's worth noting that we're creating a center point in the location passed to our GEO_GET Request. It's from this centre point that we effectively draw a Radius with our Distance value. In our example, we used Dublin as our center point and queried for locations within the specific distance.