local facebook = require("facebook") local json = require("json") local gameStat = require "src.db.gameStat" local utils = require "src.utils" GameEventDispatcher = require "src.GameEventDispatcher" -- your facebook app id local FB\_APP\_ID = "353403098199077" -- URL of the picture to post user's facebook feed local PIC\_URL = "http://imageshack.com/a/img673/5239/nMOrUQ.jpg" -- Facebook Commands local fbCommand local fbPendingCommand, fbPhotoArg local POST\_PHOTO = 4 local loggedIn = false local listener fb = {} local function handlePendingCommand() if fbPendingCommand == POST\_PHOTO then fbCommand = POST\_PHOTO facebook.login( FB\_APP\_ID, listener, {"publish\_actions"} ) end fbPendingCommand = nil end listener = function( event ) ----------------------------------------------------------------------------------------- -- After a successful login event, send the FB command -- Note: If the app is already logged in, we will still get a "login" phase -- if ( "session" == event.type ) then -- event.phase is one of: "login", "loginFailed", "loginCancelled", "logout" -- statusMessage.textObject.text = event.phase -- tjn Added if event.phase ~= "login" then GameEventDispatcher.dispatch(GameEventDispatcher.FB\_POST\_FAIL, {}) GameEventDispatcher.dispatch(GameEventDispatcher.FB\_LOGOUT, {}) loggedIn = false gameStat.fbLogout() return end loggedIn = true gameStat.fbLogin() GameEventDispatcher.dispatch(GameEventDispatcher.FB\_LOGIN, {}) if fbPendingCommand then handlePendingCommand() return end -- This code posts a photo image to your Facebook Wall -- if fbCommand == POST\_PHOTO then local attachment = { name = "Basketball Shooting", link = "https://play.google.com/store/apps/details?id=xxx", caption = "Very good game. I just score " .. fbPhotoArg.score .. " points!", description = "Welcome to Basketball Shooting Mania. Let's click here and try it today!", privacy = json.encode({ description = "Public", value = "EVERYONE", friends = "", networks = "", allow = "", deny = "" }), message = "This funny game is for you. You can try this game today", status\_type = "mobile\_status\_update", picture = PIC\_URL, actions = json.encode( { { name = "Free to play", link = "https://play.google.com/store/apps/details?id=xxx" } } ) } facebook.request( "me/feed", "POST", attachment ) -- posting the photo end elseif ( "request" == event.type ) then -- event.response is a JSON object from the FB server local response = event.response if ( not event.isError ) then response = json.decode( event.response ) if fbCommand == POST\_PHOTO then utils.popupNotifier({ images={ {finame="assets/gfx/helpbtt.png", w=50, h=50, x=40, y=40} }, texts={ {str="Secret mode unlocked", x=90, y=30} } }) GameEventDispatcher.dispatch(GameEventDispatcher.FB\_POST\_SUCCESS, {}) end fbCommand = nil else -- Post Failed utils.popupNotifier({ images={ {finame="assets/gfx/boosterx.png", w=28, h=28, x=40, y=40} }, texts={ {str="Unable to unlock. Try again", x=90, y=30} } }) GameEventDispatcher.dispatch(GameEventDispatcher.FB\_POST\_FAIL, {}) end end end fb.login = function() facebook.login( FB\_APP\_ID, listener ) end fb.logout = function() facebook.logout() end fb.postScore = function(score, bestScore, shots, miss, accuracy) fbPhotoArg = {score=score, bestScore=bestScore, shots=shots, miss=miss, accuracy=accuracy} fbCommand = nil fbPendingCommand = nil if not loggedIn then fbPendingCommand = POST\_PHOTO facebook.login( FB\_APP\_ID, listener ) else fbCommand = POST\_PHOTO facebook.login( FB\_APP\_ID, listener, {"publish\_actions"} ) end end fb.init = function() loggedIn = gameStat.isFbLogin() if loggedIn then fb.login() end end return fb