in facebook-gamingservices/src/main/java/com/facebook/gamingservices/TournamentUpdater.kt [24:67]
fun update(tournament: Tournament, score: Number): TaskCompletionSource<Boolean>? {
val currentAccessToken: AccessToken? = getCurrentAccessToken()
if (currentAccessToken == null || currentAccessToken.isExpired) {
throw FacebookException("Attempted to fetch tournament with an invalid access token")
}
val isGamingLoggedIn =
(currentAccessToken.graphDomain != null &&
FacebookSdk.GAMING == currentAccessToken.graphDomain)
if (!isGamingLoggedIn) {
throw FacebookException("User is not using gaming login")
}
val task: TaskCompletionSource<Boolean> = TaskCompletionSource<Boolean>()
val graphPath = "${tournament.identifier}/update_score"
val params = Bundle()
params.putInt("score", score.toInt())
val request =
GraphRequest(
currentAccessToken,
graphPath,
params,
HttpMethod.POST,
GraphRequest.Callback { response ->
if (response.error != null) {
if (response.error?.exception != null) {
task.setError(response.error?.exception)
return@Callback
}
task.setError(GraphAPIException("Graph API Error"))
return@Callback
}
val jsonObject = response.getJSONObject()
val result = jsonObject?.optString(GRAPH_RESPONSE_SUCCESS_KEY)
if (result == null || result.isEmpty()) {
val errorMessage = "Graph API Error"
task.setError(GraphAPIException(errorMessage))
return@Callback
}
val success = result.equals("true")
task.setResult(success)
})
request.executeAsync()
return task
}