Hello
I am currently finishing my app and would like the add a leader board to it, based on facebook’s score api.
I checked dozen of posts and facebook’s guides and it is still kind of blurry. Can someone please direct me to the right way ? Is what i am doing in my code correct?
here it is, and many Thanks in advance for any assistance.
in main.lua i call for a single sign on:
fbAppID = “myAppId”; --replace with your Facebook App ID –
facebook.login( fbAppID, facebookListener, { “user_friends”, “publish_actions” } ); --there is a local listener in main.lua
so far all good, that works as expected, next:
in Highscore.lua: (realize there is a second local facebook listener here too, in order to get friends pictures, scores and names/ids and make use of them on this page, IS THIS A PROBLEM?)
local savedData = require("savedFile"); local facebook = require("facebook"); local json = require ("json"); local topScores = {}; local facebookListener = function( event ) if ( "session" == event.type ) then --options are: "login", "loginFailed", "loginCancelled", or "logout" if ( "login" == event.phase ) then local access\_token = event.token end elseif ( "request" == event.type ) then if ( not event.isError ) then local response = json.decode( event.response ); if(savedData["FBtarget"]=="post") then savedData["FBtarget"] = "none"; --use post here to post to user's wall. elseif(savedData["FBtarget"]=="friends") then savedData["FBtarget"] = "none"; ------------------------------- local data = response.data; --------------------------- local function showImage( event ) event.target.alpha = 0 event.target.width = dif\*120 event.target.height = dif\*120 event.target.xScale = 0.8 event.target.yScale = 0.8 transition.to( event.target, { alpha = 1.0 , xScale = 1, yS cale = 1} ) end local many = #data; if(many\>4) then many = 4; end if(many\>1) then for i=1,many,1 do --top 6 instead of #data topScores[i] = {}; topScores[i]["score"] = data[i].score; --IS THIS CORR ECT? topScores[i]["name"] = data[i].first\_name; --IS THIS? topScores[i]["img"] = display.loadRemoteImage("http:/ /graph.facebook.com/".. data[i].id .."/picture", "GET", showImage, "friend"..i, system.TemporaryDirectory, (\_W\*0.3), --x ((\_H\*0.3) + ((i-1)\*(\_H\*0.15))) --y end elseif(many\<2) then native.showAlert("None of your friend play this. Inv ite them", {"Okay"}); --in the meanwhile show player's highscores here: end end else local response = json.decode( event.response ); native.showAlert("error", {"Okay"}); end elseif ( "dialog" == event.type ) then end end --IF I DON'T CALL 'LOGIN' A SECOND TIME HOW WILL THIS KNOW TO USE THE LISTENER ON THIS PAGE FOR RETURNING RESPONSE? SHOULD I CALL FOR 'FACEBOOK.LOGIN' A SECOND TIME FOR THIS? savedData["FBtarget"] = "friends"; facebook.request( "me/friends", "GET" );
And one more thin, how do I get user ID of the player so i can compare the ‘data.user_id’, to it and apply some kind of highlight to it, to show where he stands on the list?
Any help or advice would be greatly appreciated.
Saiphan