Created
October 10, 2019 23:58
-
-
Save agramonte/f6140dfb57c0db06084df84609c43883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local gameSparkKey = "<myKey>" | |
local gameSparkSecret = "<mySecret>" | |
local gameSparkPropertyName = "pageServerVersions" | |
local json = require( "json" ) | |
local gs = require("plugin.gamesparks") | |
local G = {} | |
G.gameSparkNoAds = "removeAds" | |
G.gameSparkLeaderboardName = {"defaultLeaderboard", "gameCount", "totalScore"} | |
G.gameSparkPostScoreEvent = "defaultScore" | |
G.gameSparkProfileUpdateName = "update_profile" | |
G.gameSparkCurrencyUpdateName = "currencySink" | |
G.gameSparkConsumableSink = "consumableSink" | |
G.gameSparkConsumablePurchase = "consumablePurchase" | |
-- Leaderboard table | |
G.leaderboards = {} | |
G.properties = {} | |
G.virtualGoods = {} | |
G.userCurrency = 0 | |
G.gameSparkToken = nil | |
G.gameSparkUserId = nil | |
G.gameSparkIsAvailable = false | |
G.displayName = nil | |
G.isDebugMode = false | |
G.platform = "Android" | |
G.isFacebook = false | |
G.noAds = false | |
G.highscore = 0 | |
G.scoreRank = 0 | |
G.wins = 0 | |
G.eventDispatcher = system.newEventDispatcher() | |
G.printTable = function(table) | |
--if G.isDebugMode == false then | |
-- return | |
--end | |
if table ~= nil then | |
--print(json.prettify(table)) | |
end | |
end | |
G.printLog = function(text) | |
if G.isDebugMode == false then | |
return | |
end | |
--print(text) | |
end | |
G.availabilityCallback = function(isAvailable) | |
if isAvailable == true then | |
G.gameSparkIsAvailable = true | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="init",status="successful" } ) | |
else | |
G.gameSparkIsAvailable = false | |
end | |
end | |
G.changeDisplayName = function(displayName) | |
local requestBuilder = gs.getRequestBuilder() | |
local changeUserDetailsRequest = requestBuilder.createChangeUserDetailsRequest() | |
changeUserDetailsRequest:setDisplayName(displayName) | |
changeUserDetailsRequest:send(function(response) | |
G.printTable(response) | |
end) | |
end | |
G.requestAdditionalValues = function() | |
G.getPropertyRequest(gameSparkPropertyName) | |
G.getAccountDetails() | |
for i=0, #G.gameSparkLeaderboardName do | |
G.retrieveLeaderboard(G.gameSparkLeaderboardName[i], 1) | |
end | |
G.getVirtualGoodList() | |
end | |
G.processScriptData = function(scrptData) | |
if scrptData == nil then | |
return | |
end | |
for k, v in pairs(scrptData) do | |
if k ~= "@class" and k ~= "requestId" then | |
utils.data[k] = scrptData | |
end | |
end | |
local json = require("json") | |
end | |
G.machineConnectionRequest = function(displayName) | |
local requestBuilder = gs.getRequestBuilder() | |
local deviceAuthenticationRequest = requestBuilder.createDeviceAuthenticationRequest() | |
G.displayName = displayName | |
--Set values | |
deviceAuthenticationRequest:setDeviceId(system.getInfo( "deviceID" )) | |
deviceAuthenticationRequest:setDeviceOS(G.platform) | |
--Send and print authentication token | |
deviceAuthenticationRequest:send(function(authenticationResponse) | |
G.printTable(response) | |
G.gameSparkToken = authenticationResponse:getAuthToken() | |
G.gameSparkUserId = authenticationResponse:getUserId() | |
G.processScriptData(authenticationResponse:getScriptData()) | |
G.eventDispatcher:dispatchEvent( { name="backEnd", subType = "machine", type="auth",status="successful" } ) | |
end) | |
end | |
G.gameCenterAuthentication = function(url, salt, signature, timeStamp, alias, playerId) | |
local requestBuilder = gs.getRequestBuilder() | |
local gameCenterConnectRequest = requestBuilder.createGameCenterConnectRequest() | |
gameCenterConnectRequest:setPublicKeyUrl(url) | |
gameCenterConnectRequest:setSalt(salt) | |
gameCenterConnectRequest:setSignature(signature) | |
gameCenterConnectRequest:setDisplayName(alias) | |
gameCenterConnectRequest:setExternalPlayerId(playerId) | |
gameCenterConnectRequest:setTimestamp(timeStamp) | |
gameCenterConnectRequest:send(function(response) | |
if response.data.error ~= nil then | |
G.machineConnectionRequest(alias) | |
else | |
G.printTable(response) | |
G.gameSparkToken = response:getAuthToken() | |
G.gameSparkUserId = response:getUserId() | |
G.processScriptData(response:getScriptData()) | |
G.eventDispatcher:dispatchEvent( { name="backEnd", subType = "gameCenter", type="auth",status="successful" } ) | |
G.changeDisplayName(alias) | |
end | |
end) | |
end | |
G.getDownloadableRequest = function(shortCode) | |
local requestBuilder = gs.getRequestBuilder() | |
local getDownloadableRequest = requestBuilder.createGetDownloadableRequest() | |
getDownloadableRequest:setShortCode(shortCode) | |
getDownloadableRequest:send(function(gdr) | |
G.eventDispatcher:dispatchEvent( { name="backEnd", shortCode = shortCode, url = gdr:getUrl(), type="patch",status="successful" } ) | |
end) | |
end | |
G.facebookConnectionRequest = function(tokenId) | |
local requestBuilder = gs.getRequestBuilder() | |
local fbConnectionRequest = requestBuilder.createFacebookConnectRequest() | |
fbConnectionRequest:setAccessToken(tokenId) | |
fbConnectionRequest:setSyncDisplayName(true) | |
fbConnectionRequest:send(function(response) | |
G.printTable(response) | |
G.gameSparkToken = response:getAuthToken() | |
G.gameSparkUserId = response:getUserId() | |
G.processScriptData(response:getScriptData()) | |
G.isFacebook = true | |
G.eventDispatcher:dispatchEvent( { name="backEnd", subType = "facebook", type="auth",status="successful" } ) | |
end) | |
end | |
G.onMessageRecieved = function(event) | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type = "message", data = event.data } ) | |
end | |
G.initGameSpark = function(apiKey, apiSecret) | |
gs = createGS() | |
gs.setLogger(G.printLog) | |
gs.setApiKey(apiKey) | |
gs.setApiSecret(apiSecret) | |
gs.setMessageHandlerCallback(G.onMessageRecieved) | |
if G.isDebugMode == false then | |
gs.setUseLiveServices(true) | |
end | |
gs.setAvailabilityCallback(G.availabilityCallback) | |
gs.connect() | |
end | |
G.publishEvent = function(eventKey, attributes) | |
local requestBuilder = gs.getRequestBuilder() | |
local postEvent = requestBuilder.createLogEventRequest() | |
postEvent:setEventKey(eventKey) | |
if attributes ~= nil then | |
for k,v in pairs( attributes ) do | |
postEvent:setEventAttribute(k, v) | |
end | |
end | |
postEvent:send(function(response) | |
if response ~= nil then | |
if response.data ~= nil then | |
local json = require("json") | |
if eventKey == G.gameSparkPostScoreEvent then | |
for i=0, #G.gameSparkLeaderboardName do | |
G.retrieveLeaderboard(G.gameSparkLeaderboardName[i], 1) | |
end | |
elseif eventKey == G.gameSparkCurrencyUpdateName then | |
if response:getScriptData() ~= nil and response:getScriptData().playerCredit ~= nil then | |
G.userCurrency = response:getScriptData().playerCredit | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="currency"} ) | |
end | |
elseif eventKey == G.gameSparkConsumablePurchase then | |
G.getAccountDetails() | |
if response:getScriptData() ~= nil and response:getScriptData().playerCredit ~= nil then | |
G.userCurrency = response:getScriptData().playerCredit | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="currency"} ) | |
end | |
elseif response:getScriptData() ~= nil then | |
G.processScriptData(response:getScriptData()) | |
end | |
end | |
end | |
end) | |
end | |
G.postEvent = function(number, eventKey, attribute) | |
local requestBuilder = gs.getRequestBuilder() | |
local logEventRequest = requestBuilder.createLogEventRequest() | |
logEventRequest:setEventKey(eventKey) | |
if number ~= "" and attribute ~= "" then | |
logEventRequest:setEventAttribute(attribute, number) | |
end | |
--Send Request | |
logEventRequest:send(function(response) | |
local json = require("json") | |
if eventKey == G.gameSparkPostScoreEvent then | |
for i=0, #G.gameSparkLeaderboardName do | |
G.retrieveLeaderboard(G.gameSparkLeaderboardName[i], 1) | |
end | |
elseif eventKey == G.gameSparkCurrencyUpdateName then | |
if response:getScriptData() ~= nil and response:getScriptData().playerCredit ~= nil then | |
G.userCurrency = response:getScriptData().playerCredit | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="currency"} ) | |
end | |
elseif eventKey == G.gameSparkConsumableSink then | |
G.getAccountDetails() | |
elseif response:getScriptData() ~= nil then | |
G.processScriptData(response:getScriptData()) | |
end | |
end) | |
end | |
G.retrieveLeaderboard = function(leaderboardName, numberToRetrieve) | |
--Build Request | |
local requestBuilder = gs.getRequestBuilder() | |
local getEntryRequest = requestBuilder.createAroundMeLeaderboardRequest() | |
--Set values | |
getEntryRequest:setLeaderboardShortCode(leaderboardName) | |
getEntryRequest:setEntryCount(numberToRetrieve) | |
getEntryRequest:setIncludeFirst(20) | |
getEntryRequest:send(function(response) | |
--print( "******leaderboard:", json.prettify( response )) | |
local entries = response:getFirst() | |
local meEntry = response:getData()[2] | |
local otherEntries = response:getScriptData() | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="leaderboard", aroundMe = false, subType=leaderboardName, data=entries, me=meEntry } ) | |
G.retrieveHighScore(G.gameSparkLeaderboardName) | |
end) | |
end | |
G.retrieveLeaderboardAround = function(leaderboardName, numberToRetrieve) | |
local requestBuilder = gs.getRequestBuilder() | |
local getEntryRequest = requestBuilder.createAroundMeLeaderboardRequest() | |
--Set values | |
getEntryRequest:setLeaderboardShortCode(leaderboardName) | |
getEntryRequest:setEntryCount(numberToRetrieve) | |
getEntryRequest:setIncludeFirst(1) | |
getEntryRequest:send(function(response) | |
local entries = response:getData() | |
G.eventDispatcher:dispatchEvent( { name="backend", type="leaderboard", aroundMe = true, subType=leaderboardName, data=entries } ) | |
end) | |
end | |
G.retrieveHighScore = function(lbName) | |
local requestBuilder = gs.getRequestBuilder() | |
local getEntryRequest = requestBuilder.createGetLeaderboardEntriesRequest() | |
getEntryRequest:setLeaderboards(lbName) | |
getEntryRequest:send(function(response) | |
if response ~= nil then | |
if response.data ~= nil then | |
if response.data[lbName] ~= nil then | |
if response.data[lbName]["score"] ~= nil then | |
G.highscore = response.data[lbName]["score"] | |
G.scoreRank = response.data[lbName]["rank"] | |
G.wins = response.data[lbName]["wins"] | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="leaderboardEntries", subType = "leaderboardEntries", data=response.data}) | |
end | |
end | |
end | |
end | |
end) | |
end | |
G.getVirtualGoodList = function() | |
local requestBuilder = gs.getRequestBuilder() | |
local virtualGoodListRequest = requestBuilder.createListVirtualGoodsRequest() | |
virtualGoodListRequest:send(function(response) | |
if response.data.virtualGoods ~= nil then | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="virtualGoods", subType = "virtualGoods", data=response.data}) | |
end | |
end) | |
end | |
G.buyVirtualGoods = function(currencyType, goodShortCode, quantity) | |
local requestBuilder = gs.getRequestBuilder() | |
local buyVirtualGoodsRequest = requestBuilder.createBuyVirtualGoodsRequest() | |
buyVirtualGoodsRequest:setCurrencyType(currencyType) | |
buyVirtualGoodsRequest:setShortCode(goodShortCode) | |
buyVirtualGoodsRequest:setQuantity(quantity) | |
buyVirtualGoodsRequest:send(function(response) | |
G.getAccountDetails() | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="logEvent", subType = "purchased_vgood", data=response } ) | |
end) | |
end | |
G.pushRegistrationRequest = function(token, notificationType) | |
local requestBuilder = gs.getRequestBuilder() | |
local pushRegistrationRequest = requestBuilder.createPushRegistrationRequest() | |
pushRegistrationRequest:setDeviceOS( notificationType ) | |
pushRegistrationRequest:setPushId(token) | |
pushRegistrationRequest:send(function(response) | |
end) | |
end | |
G.getPropertyRequest = function(propertyName) | |
local requestBuilder = gs.getRequestBuilder() | |
local propertyRequest = requestBuilder.createGetPropertyRequest() | |
propertyRequest:setPropertyShortCode(propertyName) | |
propertyRequest:send(function(getPropertyResponse) | |
local eventdata = getPropertyResponse["data"]["property"] | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="property", subType = propertyName, data=eventdata } ) | |
end) | |
end | |
G.getAccountDetails = function() | |
local requestBuilder = gs.getRequestBuilder() | |
local accountDetailsRequest = requestBuilder.createAccountDetailsRequest() | |
accountDetailsRequest:send(function(response) | |
G.userCurrency = response:getCurrency1() | |
G.virtualGoods = response:getVirtualGoods() | |
local displayName = response:getDisplayName() | |
if G.virtualGoods ~= nil then | |
for k, v in pairs(G.virtualGoods) do | |
if k == G.gameSparkNoAds then | |
if v > 0 then | |
G.noAds = true | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="noAds" } ) | |
end | |
end | |
end | |
end | |
G.eventDispatcher:dispatchEvent( { name="backEnd", type="accountDetail", subType = "accountDetail", data=response.data } ) | |
end) | |
end | |
G.getMessages = function() | |
end | |
G.init = function(platform, isDebugMode, isFacebook) | |
G.platform = platform | |
G.isDebugMode = isDebugMode | |
G.isFacebook = isFacebook | |
G.initGameSpark(gameSparkKey,gameSparkSecret) | |
end | |
--Return it now | |
return G |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment