Modern Apps git

← Modern-Apps.git

alchemist: auto-unlock time at 100 elements with secret achievement and legacy recipe backfill

Commit
c69e931939f2aa35f4ca6035d256ca603ed85894
Author
Vayun Mathur <[email protected]>
When
2026-06-23 19:43 UTC (5 months ago)
Parents
7d6e614

Diff

games/alchemist/src/main/assets/achievements.json
diff --git a/games/alchemist/src/main/assets/achievements.json b/games/alchemist/src/main/assets/achievements.jsonindex 62deb165b2..2f233d336c 100644--- a/games/alchemist/src/main/assets/achievements.json+++ b/games/alchemist/src/main/assets/achievements.json@@ -31,5 +31,11 @@     "id": "created_life",     "name": "It's Alive!",     "description": "Create Life."+  },+  {+    "id": "unlocked_time",+    "name": "The Fourth Dimension",+    "description": "Unlock Time.",+    "isSecret": true   } ]
games/alchemist/src/main/java/com/vayunmathur/games/alchemist/util/AlchemistViewModel.kt
diff --git a/games/alchemist/src/main/java/com/vayunmathur/games/alchemist/util/AlchemistViewModel.kt b/games/alchemist/src/main/java/com/vayunmathur/games/alchemist/util/AlchemistViewModel.ktindex 0015f77cde..ee87c78207 100644--- a/games/alchemist/src/main/java/com/vayunmathur/games/alchemist/util/AlchemistViewModel.kt+++ b/games/alchemist/src/main/java/com/vayunmathur/games/alchemist/util/AlchemistViewModel.kt@@ -60,6 +60,7 @@ class AlchemistViewModel(application: Application) : AndroidViewModel(applicatio             _allItems.value = Alchemist.items             _recipes.value = Alchemist.recipes             seedInitialItemsIfEmpty()+            backfillLegacyRecipes()         }     } @@ -70,6 +71,38 @@ class AlchemistViewModel(application: Application) : AndroidViewModel(applicatio         }     } +    /**+     * One-time migration for users who unlocked items before recipe logging existed.+     * For every unlocked element that has no logged recipe creating it, add every recipe+     * that creates it whose inputs are themselves only such pre-existing elements+     * (unlocked with no logged recipe creating them).+     */+    private suspend fun backfillLegacyRecipes() {+        if (ds.getBoolean("recipes_backfilled", false)) return++        val unlocked = ds.stringSetFlow("available_items").first()+            .mapNotNull { it.toLongOrNull() }.toSet()+        val usedKeys = ds.stringSetFlow("used_recipes").first()+        val allRecipes = _recipes.value++        // Elements that already have a logged recipe creating them.+        val createdByLog = allRecipes+            .filter { recipeKey(it) in usedKeys }+            .flatMap { it.outputs }+            .toSet()++        // "Pre-existing" elements: unlocked but not created by any logged recipe.+        val legacy = unlocked - createdByLog++        allRecipes.forEach { recipe ->+            if (recipe.outputs.any { it in legacy } && recipe.inputs.all { it in legacy }) {+                ds.addStringToSet("used_recipes", recipeKey(recipe))+            }+        }++        ds.setBoolean("recipes_backfilled", true)+    }+     fun unlockItem(id: Long) {         if (id !in itemIds.value) {             ds.addStringToSet("available_items", id.toString())@@ -162,9 +195,26 @@ class AlchemistViewModel(application: Application) : AndroidViewModel(applicatio                 if (items.any { it.id == 44L }) {                     achievementsManager.onAchievementUnlocked("created_life")                 }++                // Auto-unlock Time once enough items have been discovered. This reacts to+                // both app open (initial emission) and newly created elements.+                if (items.size >= TIME_UNLOCK_THRESHOLD && TIME_ID !in itemIds.value) {+                    _allItems.value.find { it.id == TIME_ID }?.let { timeItem ->+                        unlockItem(TIME_ID)+                        _newUnlocksEvent.tryEmit(listOf(timeItem))+                    }+                }+                if (items.any { it.id == TIME_ID }) {+                    achievementsManager.onAchievementUnlocked("unlocked_time")+                }             }         }     }++    companion object {+        private const val TIME_ID = 41L+        private const val TIME_UNLOCK_THRESHOLD = 100+    } }  fun recipeKey(recipe: AlchemyRecipe): String =