Ignore errors when awarding achievements that the user already has

This commit is contained in:
Rafael Caetano 2023-02-15 19:51:08 +00:00
parent 0339a8b5af
commit 89e45c0f7d
2 changed files with 14 additions and 3 deletions

View File

@ -185,6 +185,7 @@ class EmulatorActivity : AppCompatActivity(), RendererListener {
override fun onAchievementTriggered(achievementId: Long) {
Log.d("RetroAchievements", "Achievement triggered: $achievementId")
viewModel.onAchievementTriggered(achievementId)
}
override fun onAchievementUnprimed(achievementId: Long) {

View File

@ -132,7 +132,13 @@ class RAApi(
// TODO: Maybe send game hash?
PARAMETER_IS_HARDMODE to if (forHardcoreMode) VALUE_HARDMODE_ENABLED else VALUE_HARDMODE_DISABLED,
PARAMETER_SIGNATURE to signature,
)
),
errorHandler = {
// Ignore errors if the achievement has already been awarded to the user
if (it != "User already has") {
throw UnsuccessfulRequestException(it ?: "Unknown reason")
}
}
)
}
@ -152,7 +158,10 @@ class RAApi(
}
@OptIn(ExperimentalStdlibApi::class)
private suspend inline fun <reified T> get(parameters: Map<String, String>): Result<T> {
private suspend inline fun <reified T> get(
parameters: Map<String, String>,
errorHandler: (String?) -> Unit = { throw UnsuccessfulRequestException(it ?: "Unknown reason") }
): Result<T> {
val request = buildGetRequest(parameters)
return runCatching {
executeRequest(request)
@ -162,7 +171,8 @@ class RAApi(
val isSuccessful = json.asJsonObject["Success"].asBoolean
if (!isSuccessful) {
val reason = json.asJsonObject["Error"]?.asString
throw UnsuccessfulRequestException(reason ?: "Unknown reason")
// The error handler may choose to ignore the error
errorHandler.invoke(reason)
}
if (T::class == Unit::class) {