feat(core): snap score changes
Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import me.rhunk.snapenhance.common.data.TrackerRuleEvent
|
||||
import me.rhunk.snapenhance.common.util.toSerialized
|
||||
import me.rhunk.snapenhance.storage.getRuleTrackerScopes
|
||||
import me.rhunk.snapenhance.storage.getTrackerEvents
|
||||
import me.rhunk.snapenhance.storage.updateFriendScore
|
||||
|
||||
|
||||
class RemoteTracker(
|
||||
@@ -26,4 +27,8 @@ class RemoteTracker(
|
||||
ScopedTrackerRule(it.key, context.database.getRuleTrackerScopes(it.key.id))
|
||||
}).toSerialized()
|
||||
}
|
||||
|
||||
override fun updateFriendScore(userId: String, score: Long): Long {
|
||||
return context.database.updateFriendScore(userId, score)
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,10 @@ class AppDatabase(
|
||||
"params TEXT",
|
||||
"actions TEXT"
|
||||
),
|
||||
"friend_scores" to listOf(
|
||||
"userId CHAR(36) PRIMARY KEY",
|
||||
"score BIGINT"
|
||||
),
|
||||
"quick_tiles" to listOf(
|
||||
"key VARCHAR PRIMARY KEY",
|
||||
"position INTEGER",
|
||||
|
||||
@@ -9,6 +9,7 @@ import me.rhunk.snapenhance.common.data.TrackerRuleActionParams
|
||||
import me.rhunk.snapenhance.common.data.TrackerRuleEvent
|
||||
import me.rhunk.snapenhance.common.data.TrackerScopeType
|
||||
import me.rhunk.snapenhance.common.util.ktx.getInteger
|
||||
import me.rhunk.snapenhance.common.util.ktx.getLongOrNull
|
||||
import me.rhunk.snapenhance.common.util.ktx.getStringOrNull
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
@@ -195,3 +196,24 @@ fun AppDatabase.getRuleTrackerScopes(ruleId: Int, limit: Int = Int.MAX_VALUE): M
|
||||
}
|
||||
return scopes
|
||||
}
|
||||
|
||||
fun AppDatabase.updateFriendScore(userId: String, score: Long): Long {
|
||||
return runBlocking {
|
||||
suspendCoroutine { continuation ->
|
||||
executeAsync {
|
||||
val currentScore = database.rawQuery("SELECT score FROM friend_scores WHERE userId = ?", arrayOf(userId)).use { cursor ->
|
||||
if (!cursor.moveToFirst()) return@use null
|
||||
cursor.getLongOrNull("score")
|
||||
}
|
||||
|
||||
if (currentScore != null) {
|
||||
database.execSQL("UPDATE friend_scores SET score = ? WHERE userId = ?", arrayOf(score, userId))
|
||||
} else {
|
||||
database.execSQL("INSERT INTO friend_scores (userId, score) VALUES (?, ?)", arrayOf(userId, score))
|
||||
}
|
||||
|
||||
continuation.resumeWith(Result.success(currentScore ?: -1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user