question

opportunitiesgames avatar image
opportunitiesgames asked

Implement LeaderBoards in Corona SDK

Hi guys,

I'm trying to implement leaderboards in my Corona SDK game but there is little to none information on how to really implement all the playfab features in Corona SDK.

I've got my user successfully logged in, but can't find how to make the calls to the server. This is what I've got so far for my leaderboard implementation:

function M.uploadHighScore(highscore)
    local function networkListener( event )


        if ( event.isError ) then
            print( "Network error: ", event.response )
        else
            print ( "Upload complete!" )
        end
    end


    local headers = {}


    headers["Content-Type"] = "application/json"
    headers["X-API-Key"] = "13b6ac91a2"


    local params = {}
    params.headers = headers


    -- Tell network.request() to get the request body from a file:
    params.body = {
        filename = "object.json",
        baseDirectory = system.DocumentsDirectory
    }


    network.request( "https://XXXX.playfabapi.com/Client/UpdatePlayerStatistics", "POST", networkListener, params )
end

(I replaced the XXXX in the link with the correct code)

But I need the user_session_ticket_value for the X-API-KEY part. But how do I get that in Corona? And if I want to upload a single value (highscore = 6), will I have to convert that into a json table?

I'm very new to this so don't throw to many fancy words at me :)

Kind regards

Bram

sdksLeaderboards and Statistics
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

opportunitiesgames avatar image
opportunitiesgames answered

Hi Brandon, I really can't seem to figure it out :/

I tried to "prettify' it and that worked:

print(json.prettify(result.Leaderboard))

But I can't figure out on how to extract it... I tried this now:

 local d = json.decode(result.Leaderboard)
 print(d[StatValue])

I'm puzzled :/

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

If you're decoding Leaderboard, the result is an array of objects, each containing a Statistic Value, DIsplay Name, and Leaderboard Position. So rather than d[StatValue], what you want is the StatValue of the first element of the array d, which should be d[0].StatValue. What did your output of the prettify print look like?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

opportunitiesgames avatar image
opportunitiesgames answered

Hi,

so I had to pretiffy the json table first, put that table in a variable and after that I was able to get the data out. So decoding the result isn't enough, you got to pretiffy it first and decode it afterwards :)

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Fair enough - glad you're unblocked on this. If you'd like to post your example code here, we can just mark that as the accepted answer, so that it shows up immediately below the question.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.