Skip to content

Commit

Permalink
Merge pull request #367 from lucasnlm/update-app
Browse files Browse the repository at this point in the history
Fix achievements
  • Loading branch information
lucasnlm authored Mar 8, 2022
2 parents 7bacdf0 + f81fd01 commit ee74469
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 39 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {

defaultConfig {
// versionCode and versionName must be hardcoded to support F-droid
versionCode 1507001
versionName '15.7.0'
versionCode 1507011
versionName '15.7.1'
minSdkVersion 21
targetSdkVersion 31
multiDexEnabled true
Expand Down
6 changes: 4 additions & 2 deletions app/src/test/java/dev/lucasnlm/antimine/di/TestAppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ val AppModule = module {

override fun openLeaderboards(activity: Activity) {}

override fun unlockAchievement(achievement: Achievement) {}
override suspend fun incrementAchievement(achievement: Achievement, value: Int) {}

override fun incrementAchievement(achievement: Achievement) {}
override suspend fun setAchievementSteps(achievement: Achievement, value: Int) {}

override suspend fun unlockAchievement(achievement: Achievement) {}

override fun submitLeaderboard(leaderboard: Leaderboard, value: Long) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class MinefieldRepository : IMinefieldRepository {
val fitWidth = calculatedWidth.coerceAtLeast(MIN_STANDARD_WIDTH)
val fitHeight = calculatedHeight.coerceAtLeast(MIN_STANDARD_HEIGHT)
val fieldArea = fitWidth * fitHeight
val fitMines =
((fieldArea * CUSTOM_LEVEL_MINE_RATIO).toInt() + progressiveMines)
.coerceAtMost((fieldArea * MAX_LEVEL_MINE_RATIO).toInt())
val fitMines = ((fieldArea * CUSTOM_LEVEL_MINE_RATIO).toInt() + progressiveMines)
return Minefield(fitWidth, fitHeight, fitMines)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.LayoutDirection
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.layoutDirection
import androidx.lifecycle.viewModelScope
import dev.lucasnlm.antimine.common.R
import dev.lucasnlm.antimine.common.level.GameController
import dev.lucasnlm.antimine.common.level.database.models.FirstOpen
Expand Down Expand Up @@ -37,6 +38,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Locale

Expand Down Expand Up @@ -667,7 +669,9 @@ open class GameViewModel(
}

if (clock.time() < 30L) {
playGamesManager.unlockAchievement(Achievement.ThirtySeconds)
withContext(Dispatchers.Main) {
playGamesManager.unlockAchievement(Achievement.ThirtySeconds)
}
}

checkVictoryAchievements()
Expand Down Expand Up @@ -701,7 +705,9 @@ open class GameViewModel(
private suspend fun checkVictoryAchievements() = with(gameController) {
state.field.count { it.mark.isFlag() }.also {
if (it > 0) {
playGamesManager.unlockAchievement(Achievement.Flags)
withContext(Dispatchers.Main) {
playGamesManager.incrementAchievement(Achievement.Flags, it)
}
}
}

Expand Down Expand Up @@ -735,39 +741,52 @@ open class GameViewModel(
statsRepository.getAllStats(0).count {
it.victory == 1
}.also {
if (it >= 20) {
playGamesManager.incrementAchievement(Achievement.Beginner)
}
if (it > 0) {
viewModelScope.launch(Dispatchers.Main) {
playGamesManager.setAchievementSteps(Achievement.Beginner, it)
}

if (it >= 50) {
playGamesManager.incrementAchievement(Achievement.Intermediate)
}
viewModelScope.launch(Dispatchers.Main) {
playGamesManager.setAchievementSteps(Achievement.Intermediate, it)
}

if (it >= 100) {
playGamesManager.incrementAchievement(Achievement.Expert)
viewModelScope.launch(Dispatchers.Main) {
playGamesManager.setAchievementSteps(Achievement.Expert, it)
}
}
}
}
}

private fun checkGameOverAchievements() = with(gameController) {
if (getActionsCount() < 3) {
playGamesManager.unlockAchievement(Achievement.NoLuck)
}
viewModelScope.launch {
if (getActionsCount() < 3) {
withContext(Dispatchers.Main) {
playGamesManager.unlockAchievement(Achievement.NoLuck)
}
}

if (almostAchievement()) {
playGamesManager.unlockAchievement(Achievement.Almost)
}
if (almostAchievement()) {
withContext(Dispatchers.Main) {
playGamesManager.unlockAchievement(Achievement.Almost)
}
}

state.field.count { it.mark.isFlag() }.also {
if (it > 0) {
playGamesManager.incrementAchievement(Achievement.Flags)
state.field.count { it.mark.isFlag() }.also {
if (it > 0) {
withContext(Dispatchers.Main) {
playGamesManager.incrementAchievement(Achievement.Flags, it)
}
}
}
}

state.field.count { it.hasMine && it.mistake }.also {
if (it > 0) {
playGamesManager.incrementAchievement(Achievement.Boom)
state.field.count { it.hasMine && it.mistake }.also {
if (it > 0) {

withContext(Dispatchers.Main) {
playGamesManager.incrementAchievement(Achievement.Boom, it)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class MinefieldRepositoryTest {
preferencesRepository
)

assertEquals(Minefield(139, 137, 4321), minefield)
assertEquals(Minefield(227, 225, 11728), minefield)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ interface IPlayGamesManager {
fun isLogged(): Boolean
fun openAchievements(activity: Activity)
fun openLeaderboards(activity: Activity)
fun unlockAchievement(achievement: Achievement)
fun incrementAchievement(achievement: Achievement)
suspend fun unlockAchievement(achievement: Achievement)
suspend fun incrementAchievement(achievement: Achievement, value: Int)
suspend fun setAchievementSteps(achievement: Achievement, value: Int)
fun submitLeaderboard(leaderboard: Leaderboard, value: Long)
fun keepRequestingLogin(status: Boolean)
fun shouldRequestLogin(): Boolean
Expand Down
8 changes: 6 additions & 2 deletions foss/src/main/java/dev/lucasnlm/external/PlayGamesManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ class PlayGamesManager(
// F-droid build doesn't have Google Play Games
}

override fun unlockAchievement(achievement: Achievement) {
override suspend fun unlockAchievement(achievement: Achievement) {
// F-droid build doesn't have Google Play Games
}

override fun incrementAchievement(achievement: Achievement) {
override suspend fun incrementAchievement(achievement: Achievement, value: Int) {
// F-droid build doesn't have Google Play Games
}

override suspend fun setAchievementSteps(achievement: Achievement, value: Int) {
// F-droid build doesn't have Google Play Games
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,33 @@ class PlayGamesManager(
}
}

override fun unlockAchievement(achievement: Achievement) {
override suspend fun unlockAchievement(achievement: Achievement) {
account?.let {
Games.getAchievementsClient(context, it).unlock(achievement.value)
try {
Games.getAchievementsClient(context, it).unlockImmediate(achievement.value).await()
} catch (e: Exception) {
// Ignore
}
}
}

override suspend fun incrementAchievement(achievement: Achievement, value: Int) {
account?.let {
try {
Games.getAchievementsClient(context, it).incrementImmediate(achievement.value, value).await()
} catch (e: Exception) {
// Ignore
}
}
}

override fun incrementAchievement(achievement: Achievement) {
override suspend fun setAchievementSteps(achievement: Achievement, value: Int) {
account?.let {
Games.getAchievementsClient(context, it).increment(achievement.value, 1)
try {
Games.getAchievementsClient(context, it).setStepsImmediate(achievement.value, value).await()
} catch (e: Exception) {
// Ignore
}
}
}

Expand Down

0 comments on commit ee74469

Please sign in to comment.