Using Geominer for Resource Tracking
Using Geominer for Resource Tracking
===
Using Geominer for Resource Tracking
Once again, we will make use of the Geominer API to perform the actual gathering of resources. Behind the scenes Geominer will track the amount of resources that have been gathered from a particular location and subtract that amount from the total amount
CHAPTER 12: Polishing and Packaging an App for Release
of resources (remember that these are in fact Gowalla check-ins) to report available quantities. For Geominer to be able to track these quantities, we will need to tell it we are gathering resources—and that is the purpose of this section.
The following code shows the modifications required to moundz.js to hook into the Geominer API functionality and start gathering resources:
MOUNDZ = (function() { ...
/* private functions */
function gatherResource() { var currentData = markerData[currentResource]; if (currentData && geominer) {
var qty = $('#slider').val(); geominer.gather(currentData.id, qty, function(totalGathered) {
// update the quantity available currentData.avail = Math.max(currentData.total - totalGathered, 0);
// if the resource is still the same, then update the display if (currentData.name === currentResource) {
updateResourceDetails(); } // if }); } // if } // gatherResource
/* exported functions */
function initScreen() { ...
$('#btnGather').live(supportsTouch ? 'tap' : 'click', gatherResource); } // initScreen
var module = { ... };
return module; })();
In this code we once again attach a tap handler to the Gather button on the Resource Details screen. When the user taps this button, the gatherResource function will then be called, which simply makes use of the bundled Geominer API via the exported gather function call. We pass the ID of the resource the quantity of resources that we wish to
CHAPTER 12: Polishing and Packaging an App for Release
gather and a callback that will receive the total quantity (from all users and previous gather operations) of resources that has been gathered for that particular resource.
With the information that we receive back, we can then update our own local data for the resource in an attempt to keep the two in sync. Should we receive a response from the server fast enough (which we should), the current resource screen will be updated to reflect the revised quantity of resources available.
As mentioned in previous chapters, if you are interested in what Geominer does behind the scenes, you can look at the source on GitHub, at the following URL: https://github.com/sidelab/geominer .
Remember, though, that Geominer is a bit of a work-in-progress, and has been built to support the samples in this book, so it would require a good deal of work before it could
be considered a truly useful API. While there is still so much that we could potentially do, and so much that needs to be
done to make Moundz a useful playable game, this book would never have been published if we didn’t draw the sample to a close somewhere. We still have work to do in the chapter, but, as far as implementing functionality in the Moundz application, this is where we will draw things to a close.
NOTE: If you are interested in how to take Moundz further or have questions about how Geominer works under the hood, feel free to join the Pro Android Web Apps group and ask questions there ( http://groups.google.com/group/pro-android-web-apps). We will try to find time to answer any questions posted.