Wednesday, January 9, 2013

Integration of MOAI Leaderboard

I have been experimenting with MOAI to solve some issues that are difficult to resolve in Codea, namely, storage and retrieval of data.  One of the first examples was to create a simple scoring storage system, which worked well as a custom solution, but it felt 'flimsy'. 

Reading up, I found that the system has a basic Leaderboard, which I felt would suit my needs much better. Excited, I jumped in, to try to integrate with it. 

It didn't go so well. After ever all days of trying on again off agin, I pretty much gave up on it, but not without a post to their forum, mostly as a last resort. I was hoping for some response similar to the energy I get I the Codea forums, but no luck - no one was biting on the post after two weeks.

Eventually, someone did post and renewed my interest in solving this. WHILE the post was not helpful (and actually was wrong), It made me look at the problem agai with fresh eyes. Below is the solution.

The problem: 
Whenever I attempted to post a score to my configured leaderboard, I would get 'authentication required'.

According to MOAI, authentication requires sending in the Client Key as a part of your POST. Eventually I was able to determine that this meant it had to be in the header. I made every attempts to post the client key, but was not able to get past the error.

The solution:
I finally got this to work from Codea. The trick, as mentioned in another post on their forums, was to force the client key into the headers, and place it in the x-clientKey field. Note that this isn't the same as putting the field in the http post table at the flat level and posting it; this requires making a "headers" table and posting that as a parameter of the post table. I wasn't doing that correctly, so I wasn't authenticating. 

Additionally, the body of the post is supposed to be a well-formed JSON string, which is stated but not very clearly.

The Example:

Tis example is more about the concepts than a huge area of working code. As I do more with MOAI, I'll have working code to post. For now I have chunks of my test project here.

 JSON data to post: 
{"username":"my user","unique":"false","sort":"desc","userid":"my userid","score":125000}

Codea:
--populated jsonData elsewhere with JSON:encode() function.
-- important to make this a seperate table!!!
local headers = {}
headers["x-clientKey"] = clientKey. -- this variable has the client key
-- now, make a pare terms table that will be posted. The headers table will get packed into this one.
local params = {}
params.data = jsonData -- properly JSON formatted data. This is the body.
params.headers = headers
params.method="POST"
--now do the post...define the functions Loaded to dump data,  httpError to trap for failure
http.get(url,
function(data,status,headers)
result = {}
result= JSON:decode(data)
Loaded(result) -- rank comes back for free!
end,
httpError, params)
end
Summary:
Knowing now that the headers have to be packed as a seperate table and be called "headers" in the http params and that the body is posted as "data" was the solution.  I'm now ready to dive into more MOAI experiments!