<p>This post is part 2 of our series on connecting gaming devices with the wot.io data service exchange™.</p>
<h2 id="routinggameplayeventdatawithwotio">Routing Gameplay Event Data with wot.io</h2>
<p>In the <a href="connecting-gaming-devices-with-wot-io-and-pubnub">last post</a> we saw that after retrofitting an open source chess application to connect via <a href="http://www.pubnub.com">PubNub</a>, connecting data services was easy since wot.io already has a PubNub adapter. No further changes were required to the application source code to connect to wot.io and send game play events to its data services or connected devices.</p>
<p>In fact, the only thing left to do was decide which wot.io data services to use in conjunction with our gaming system.</p>
<h2 id="storinggamestatisticswithmongodb">Storing Game Statistics with MongoDB</h2>
<p>The data format we created for representing game play events uses a JSON variant of <a href="https://en.wikipedia.org/wiki/Portable_Game_Notation">Portable Game Notation</a>, or PGN. An example move message might look something like this:</p>
<pre><code class="language-prettyprint lang-javascript">{
"gameId": "123",
"game": "continue",
"user": "wotiosteve",
"move": "e2-e4",
"moveSeq": 1
}
</code></pre>
<p>and a game ended message might look something like this:</p>
<pre><code class="language-prettyprint lang-javascript">{
"gameId": "123",
"black": "wotiosteve",
"white": "wotiojim",
"moves": [ [ "f3", "e6" ], [ "g4", "Qh4#" ] ],
"winner": "wotiosteve",
"gameResult": "0-1",
"game": "end",
"date": "2015.09.21"
}
</code></pre>
<p>Since we wanted to store every move made by every player in every game played across our system, we looked at the datastore options available in the wot.io data service exchange. For simplicity, we opted for the <a href="https://en.wikipedia.org/wiki/NoSQL">No-SQL</a> flexibility of <a href="https://www.mongodb.org">MongoDB</a> and its native support for JSON documents like our game messages.</p>
<p>We chose the PGN format for our moves, but there are other formats that represent chess moves as well. Since MongoDB doesn't require you to define a fixed schema, we could easily add new formats in the future without requiring changes. Whether storing chess moves or IoT data from many different devices or device management platforms, this flexibility makes MongoDB a nice choice for storing data whose format can change over time.</p>
<h2 id="quickreviewonadapters">Quick Review on Adapters</h2>
<p>As a review, wot.io adapters can be configured to listen for messages on one bus resource and send any messages it may generate to some other bus resource. Typically, these resource paths are set declaratively and stored in the wot.io configuration service, where they are represented as environment variables that get passed into instances of the <a href="http://docker.com">Docker</a> containers which comprise running data services or adapters.</p>
<p>What that means is that while we can, of course, connect directly to our MongoDB datastore using any of the available drivers or clients, we can <em>also</em> interact with it simply by sending messages on the wot.io bus to and from a MongoDB adapter.</p>
<h2 id="insertingdataintomongodb">Inserting data into MongoDB</h2>
<p>The wot.io adapter for MongoDB makes it possible to insert and query a MongoDB datastore by sending messages on the wot.io bus.</p>
<p>Since each MongoDB adapter can be configured to connect to a specific database and collection in a particular specific instance of the MongoDB data service, to insert a document, one need only route a JSON message that looks like this</p>
<pre><code class="language-prettyprint lang-javascript">[ "insert", {some-JSON-object} ]
</code></pre>
<p>to the adapter, <em>et voila</em>, the document will be asynchronously inserted into the configured MongoDB collection.</p>
<h2 id="readingdatafrommongodb">Reading data from MongoDB</h2>
<p>Similarly, we can fetch data by routing a JSON message that looks like this</p>
<pre><code class="language-prettyprint lang-javascript">[ "find", {some-BSON-query} ]
</code></pre>
<p>to the same adapter, and the query result will be sent to the destination resource to which the adapter has been bound. (There are additional options for controlling the number of documents in the response, etc., but we'll keep it simple for this example.)</p>
<p>We can also send messages requesting aggregate queries. For example, the message our chess example sends to retrieve statistics for a given user looks like this:</p>
<pre><code class="language-prettyprint lang-javascript">[
"aggregate",
[
{
"$match": {
"gameResult": { "$exists": true },
"$or": [
{ "white": "wotiosteve" },
{ "black": "wotiosteve" }
]
}
},
{
"$project": {
"win": {
"$cond": [
{ "$eq": [ "$winner", "wotiosteve" ] },
1,
0
]
},
"lose": {
"$cond": [
{
"$and": [
{ "$ne": [ "$winner", null ] },
{ "$ne": [ "$winner", "wotiosteve" ] }
]
},
1,
0
]
},
"draw": {
"$cond": [
{ "$eq": [ "$winner", null ] },
1,
0
]
},
"user": "$user"
}
},
{
"$group": {
"_id": null,
"total": { "$sum": 1 },
"win": { "$sum": "$win" },
"lose": { "$sum": "$lose" },
"draw": { "$sum": "$draw" }
}
},
{
"$project": {
"user": { "$concat": [ "wotiosteve" ] },
"total": 1,
"win": 1,
"lose": 1,
"draw": 1,
"action": { "$concat": [ "statistics" ] }
}
}
]
]
</code></pre>
<p>Clearly, <a href="https://docs.mongodb.org/manual/tutorial/query-documents/">MongoDB query documents</a> can get rather complex—but they are also very expressive. And wot.io makes it easy for other data services to interact with our MongoDB datastore.</p>
<h2 id="composabledataservices">Composable Data Services</h2>
<p>What do we mean by making it easy for other data services to interact with our MongoDB datastore? Well, for example, we might employ a data service like <a href="http://scriptr.io">scritpr</a> which allows us to route messages to custom JavaScript logic endpoints.</p>
<p>Let's say that we have coded an algorithm to calculate the minimum number of moves that could have been used to beat an opponent. We can route our game-end messages (like the one shown above) at this data service.</p>
<p><img src="http://idfiles.leveelabs.com/55bd0288af0b0930ba599bd0c4b7ca38/resources/img_new/labs_wot_io/chess-scriptr.png" alt="" /></p>
<p>Note that scriptr does not have to possess any MongoDB integration. Nor does the script have to even know that its response might be routed at MongoDB. The <code>"insert"</code> message could just as easily be processed by a different data service—say, <a href="http://basho.com/products">Riak</a>. Or both for that matter!</p>
<p>This is what we mean when we say that wot.io's data routing architecture allows for loosely coupled, composable solutions.</p>
<h2 id="nexttime">Next Time...</h2>
<p>Next time we'll take a look at how we can connect another wot.io data service, <a href="http://www.pentaho.com">Pentaho</a>, with our new MongoDB datastore to produce some custom reports.</p>
Connecting Gaming Devices with wot.io and MongoDB
Nov 2015/ Posted By: wotio team