Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- --Tools to operate with Google Play Game Services
- --@module GooglePlayGameServices
- --@alias M
- ----
- local M = {}
- local const = require "const"
- local FileSystem = require "Libs.FileSystem"
- local platform = system.getInfo("platform")
- local gameNetwork = {}
- if platform == "android" then
- gameNetwork = require "plugin.gpgs.v2"
- elseif platform == "ios" then
- gameNetwork = require "gameNetwork"
- end
- gameNetwork.hasInitialised = false
- gameNetwork.hasLogin = false
- gameNetwork.showRating = false
- gameNetwork.showAchievements = false
- local playerId
- local playerName
- local userAuthListener
- local initCallback
- , networkLoginCallback
- , onEventLoadPhoto
- , trySendEvent
- , loadPlayerCallback
- , loadAvatarCallbackGpgs
- , tryLoadPhotoGpgs
- , isSuccessLoginEvent
- ---
- -- @field isError (<b>bool</b>)
- -- @field type (<b>str</b>) "init" || "login".
- -- @table EventGPGS
- ---Authenticate the user in GPGS.
- --@func authListener listener to call after login. Generates @EventGPGS
- function M.auth(authListener)
- if authListener then
- userAuthListener = authListener
- end
- if not gameNetwork.hasInitialised then
- if platform == "android" then
- gameNetwork.login( { userInitiated = true, listener = networkLoginCallback } )
- elseif platform == "ios" then
- gameNetwork.init( "gamecenter", networkLoginCallback )
- end
- else
- if not M.isLogin() then
- if platform == "android" then
- gameNetwork.login( {userInitiated=true, listener=networkLoginCallback} )
- end
- else
- trySendEvent({type = "login", isError = false})
- end
- end
- end
- ---Logout from GPGS account
- function M.logout()
- if (platform == 'android') then
- pcall(gameNetwork.logout)
- gameNetwork.hasInitialised = false
- end
- gameNetwork.hasLogin = false
- end
- ---Shows the rating table
- function M.showLeaderboards()
- if ( system.getInfo("environment") == "simulator") then
- print( "GooglePlayGameServices.showLeaderboards() is not available on the simulator" )
- elseif platform == "android" then
- if M.isLogin() then
- gameNetwork.leaderboards.show()
- else
- M.auth(userAuthListener)
- gameNetwork.showRating = true
- end
- elseif platform == "ios" then
- gameNetwork.show( "leaderboards", { leaderboard = {timeScope="AllTime"} } )
- end
- return true
- end
- ---Shows user achievements
- function M.showAchievements()
- if ( system.getInfo("environment") == "simulator") then
- print( "GooglePlayGameServices.showAchievements() is not available on the simulator" )
- elseif platform == "android" then
- if M.isLogin() then
- gameNetwork.achievements.show()
- else
- M.auth(userAuthListener)
- gameNetwork.showAchievements = true
- end
- elseif platform == "ios" then
- gameNetwork.show( "achievements" )
- end
- return true
- end
- ---Add user scores
- --@int score value
- function M.setHighscore(score)
- local function requestCallback( event )
- if ( event.type == "setHighScore" ) then
- print("score updated")
- end
- if not event.isError then
- if event.name == "submit" then
- print("score updated")
- end
- else
- print(event.errorCode, event.errorMessage)
- end
- end
- if M.isLogin() then
- if platform == "android" then
- gameNetwork.leaderboards.submit( {leaderboardId=const.GPGS_RATING_ID, score=score, listener=requestCallback} )
- elseif platform == "ios" then
- gameNetwork.request( "setHighScore", { localPlayerScore = { category=const.GPGS_RATING_ID, value=score }, listener = requestCallback} )
- end
- end
- end
- ---Unlock user achievement
- --@string achievement_id - key to specific achievement
- --@usage gpgs.unlockAchievement( "GPGS_unique_key" )
- function M.unlockAchievement(achievement_id)
- local function requestCallback( event )
- if ( event.type == "unlockAchievement" ) then
- print "achievement updated"
- end
- if not event.isError then
- if event.name == "submit" then
- print("achievement updated")
- end
- else
- print(event.errorCode, event.errorMessage)
- end
- end
- if M.isLogin() then
- if platform == "android" then
- gameNetwork.achievements.unlock( {achievementId=achievement_id, listener=requestCallback} )
- elseif platform == "ios" then
- gameNetwork.request( "unlockAchievement",{achievement={ identifier=achievement_id, percentComplete=100, showsCompletionBanner=true }, listener=requestCallback} )
- end
- end
- end
- --- To load achievements data from google/apple and execute function
- --@table requestCallback it listener executed after get data from server
- function M.loadAchievements(requestCallback)
- if ( system.getInfo("environment") == "simulator") then
- print( "GooglePlayGameServices.loadAchievements() is not available on the simulator" )
- return
- end
- if M.isLogin() then
- if platform == "android" then
- gameNetwork.achievements.load({ reload=true, listener=requestCallback })
- elseif platform == "ios" then
- gameNetwork.request("loadAchievementDescriptions", { listener=requestCallback })
- end
- end
- end
- --- To load image from google/ios by uri/identifier
- --@field uri <b>string</b> Image resource URI, it must be something that was retrieved from another call to Google Play services. (or identifer from apple)
- --@field filename <b>string</b> Path for the to be saved PNG image file.
- --@field baseDir <b>Constant</b> Constant corresponding to the base directory where the file will be located. Default value is system.CachesDirectory if the parameter is not provided.
- --@field listener <b>Listener</b> Receives loadImage event.
- --@table params it listener executed after get data from server
- function M.loadImage(params)
- if platform == "android" then
- gameNetwork.loadImage(params)
- elseif platform == "ios" then
- gameNetwork.request("loadAchievementImage", {
- achievementDescription = {
- identifier = params.uri
- },
- listener = function(event)
- if not event.data then
- if params.listener then
- params.listener()
- end
- else
- display.save(event.data.image, {
- filename = params.filename,
- baseDir = system.CachesDirectory,
- })
- event.data.image:removeSelf()
- if params.listener then
- params.listener()
- end
- end
- end
- })
- end
- end
- function initCallback( event )
- if not event.isError then
- gameNetwork.hasInitialised = true
- gameNetwork.login( { userInitiated = true, listener = networkLoginCallback } )
- trySendEvent({type = "init", isError = false})
- else
- native.showAlert( "Init failed!", event.errorMessage, { "OK" } )
- trySendEvent({type = "init", isError = true})
- end
- end
- function networkLoginCallback( event )
- if isSuccessLoginEvent(event) then
- gameNetwork.hasInitialised = true
- gameNetwork.hasLogin = true
- if gameNetwork.showRating == true then
- gameNetwork.showRating = false
- M.showLeaderboards()
- elseif gameNetwork.showAchievements == true then
- gameNetwork.showAchievements = false
- M.showAchievements()
- end
- if platform == "android" then
- gameNetwork.players.load( { listener = loadPlayerCallback} )
- elseif platform == "ios" then
- gameNetwork.request( "loadLocalPlayer", { listener = loadPlayerCallback } )
- end
- else
- trySendEvent({isError = true})
- end
- return true
- end
- function isSuccessLoginEvent(event)
- local result = false
- if (platform == 'android' and not event.isError and event.phase == 'logged in') then
- result = true
- elseif (platform == 'ios' and event.data) then
- result = true
- end
- return result
- end
- function loadPlayerCallback(event)
- local anroidPlayerPhoto
- if(event.isError) then
- trySendEvent({type = 'login', isError = true})
- return
- else
- if (platform == "android") then
- player = event.players[1]
- anroidPlayerPhoto = player.largeImageUri
- playerName = player.name
- playerId = player.id
- elseif platform == "ios" then
- playerId = event.data.playerID
- playerName = event.data.alias
- end
- trySendEvent({type = "login", isError = false, playerId = playerId, playerName = playerName})
- end
- if (not M.isExistPlayerPhoto()) then
- if platform == "android" then
- if (anroidPlayerPhoto) then
- tryLoadPhotoGpgs(anroidPlayerPhoto)
- end
- elseif platform == "ios" then
- gameNetwork.request( "localPlayerPhoto", {playerID=playerId, size="Normal", listener=onEventLoadPhoto} )
- end
- end
- end
- function tryLoadPhotoGpgs(imageUri)
- if (imageUri) then
- gameNetwork.loadImage({
- uri = imageUri
- , filename = const.GPGS_AVATAR_NAME
- , baseDir = system.CachesDirectory
- , listener = loadAvatarCallbackGpgs
- })
- end
- end
- function loadAvatarCallbackGpgs(event)
- if ( event.isError ) then
- print( "Network error - download failed" )
- else
- local fileName, baseDir = M.getPlayerPhotoInfo()
- trySendEvent({type = 'loadAvatar', isError = false, fileName = fileName, baseDir = baseDir})
- end
- end
- function onEventLoadPhoto(event)
- if event.data.photo then
- display.save(event.data.photo, {
- filename = const.GPGS_AVATAR_NAME,
- baseDir = system.CachesDirectory,
- })
- local fileName, baseDir = M.getPlayerPhotoInfo()
- trySendEvent({type = 'loadAvatar', isError = false, fileName = fileName, baseDir = baseDir})
- end
- end
- function trySendEvent(event)
- if (userAuthListener) then
- userAuthListener(event)
- end
- end
- ---Verifies that the module is initialized and the user is connected to the services
- --@treturn bool
- function M.isLogin()
- local result = false
- if (gameNetwork.hasInitialised == true and gameNetwork.hasLogin == true) then
- result = true
- end
- return result
- end
- ---Return player name in GameNetwork
- --@treturn string
- function M.getPlayerName()
- return playerName
- end
- ---Return player identifier in GameNetword
- --@treturn string
- function M.getPlayerId()
- return playerId
- end
- ---Сhecks the file has been downloaded
- --@treturn bool
- function M.isExistPlayerPhoto()
- local result = false
- if (FileSystem:fileIsExists(const.GPGS_AVATAR_NAME, system.CachesDirectory)) then
- result = true
- end
- return result
- end
- ---Return information about file player photo
- --@treturn string filename
- --@treturn string system directory
- function M.getPlayerPhotoInfo()
- return const.GPGS_AVATAR_NAME, system.CachesDirectory
- end
- return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement