Skip to content

Commit 10aa134

Browse files
committed
add seeding
1 parent 8cf4944 commit 10aa134

18 files changed

Lines changed: 167 additions & 50 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package neth.iecal.trease
2+
3+
object Constants {
4+
val cdn = "https://trease-focus.github.io/cache-trees-cdn"
5+
}

composeApp/src/commonMain/kotlin/neth/iecal/trease/api/TreeRepository.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import io.ktor.client.plugins.contentnegotiation.*
77
import io.ktor.client.request.*
88
import io.ktor.serialization.kotlinx.json.*
99
import kotlinx.serialization.json.Json
10+
import neth.iecal.trease.Constants
11+
import neth.iecal.trease.models.TreeData
1012
import neth.iecal.trease.models.TreeResponse
1113

1214
object TreeRepository {
@@ -19,10 +21,10 @@ object TreeRepository {
1921
}
2022
}
2123

22-
suspend fun fetchTrees(): List<String> {
24+
suspend fun fetchTrees(): List<TreeData> {
2325
val response: TreeResponse = client
24-
.get("https://trease-focus.github.io/cache-trees/entity_data.json")
26+
.get("${Constants.cdn}/entity_data.json")
2527
.body()
26-
return response.trees
28+
return response.entities
2729
}
2830
}

composeApp/src/commonMain/kotlin/neth/iecal/trease/models/FocusStats.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ data class FocusStats @OptIn(ExperimentalUuidApi::class) constructor(
1313
val failureTree: String = "weathered",
1414
val id:String = Uuid.generateV7().toString(),
1515
val completedOn: Long = kotlin.time.Clock.System.now().toEpochMilliseconds(),
16+
val treeSeed: Int = 0,
1617

1718
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package neth.iecal.trease.models
2+
3+
import kotlinx.serialization.Serializable
4+
5+
6+
@Serializable
7+
data class TreeData(
8+
val id: String,
9+
val name: String,
10+
val description:String,
11+
val creator: String,
12+
val donate: String,
13+
val variants: Int,
14+
val basePrice: Int
15+
)

composeApp/src/commonMain/kotlin/neth/iecal/trease/models/TreeModels.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
data class TreeResponse(
7-
val trees: List<String>
7+
val entities: List<TreeData>,
8+
val count: Int,
89
)
910

1011
sealed class TreeUiState {
1112
object Loading : TreeUiState()
12-
data class Success(val trees: List<String>) : TreeUiState()
13+
data class Success(val trees: List<TreeData>) : TreeUiState()
1314
data class Error(val message: String) : TreeUiState()
1415
}

composeApp/src/commonMain/kotlin/neth/iecal/trease/ui/bottomsheet/TreeBottomSheet.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.layout.ContentScale
1616
import kotlinx.serialization.encodeToString
1717
import kotlinx.serialization.json.Json
18+
import neth.iecal.trease.Constants
1819
import neth.iecal.trease.api.TreeRepository
20+
import neth.iecal.trease.models.TreeData
1921
import neth.iecal.trease.models.TreeUiState
2022
import neth.iecal.trease.utils.CacheManager
2123

2224

2325
@OptIn(ExperimentalMaterial3Api::class)
2426
@Composable
25-
fun TreeBottomSheet(onDismiss: () -> Unit,onSelected: (String) -> Unit) {
27+
fun TreeBottomSheet(onDismiss: () -> Unit,onSelected: (TreeData) -> Unit) {
2628
var uiState by remember { mutableStateOf<TreeUiState>(TreeUiState.Loading) }
2729

2830
LaunchedEffect(Unit) {
@@ -58,17 +60,19 @@ fun TreeBottomSheet(onDismiss: () -> Unit,onSelected: (String) -> Unit) {
5860
}
5961

6062
@Composable
61-
fun TreeItem(treeId: String,onSelected: (String) -> Unit) {
63+
fun TreeItem(treeId: TreeData, onSelected: (TreeData) -> Unit) {
6264
Column(
6365
modifier = Modifier.padding(8.dp).combinedClickable(true,onClick = { onSelected(treeId) }),
6466
horizontalAlignment = Alignment.CenterHorizontally
6567
) {
68+
val treeUrl = "${Constants.cdn}/images/${treeId.id}_${treeId.variants-1}.png"
69+
print("Displaying tree $treeUrl")
6670
AsyncImage(
67-
model = "https://trease-focus.github.io/cache-trees/images/$treeId.png",
68-
contentDescription = treeId,
71+
model =treeUrl,
72+
contentDescription = treeId.name,
6973
modifier = Modifier.size(80.dp).clip(RoundedCornerShape(8.dp)),
7074
contentScale = ContentScale.Fit
7175
)
72-
Text(treeId, style = MaterialTheme.typography.bodySmall)
76+
Text(treeId.name, style = MaterialTheme.typography.bodySmall)
7377
}
7478
}

composeApp/src/commonMain/kotlin/neth/iecal/trease/ui/components/TreeGrider.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import kotlinx.coroutines.Dispatchers
1919
import kotlinx.coroutines.async
2020
import kotlinx.coroutines.awaitAll
2121
import kotlinx.coroutines.withContext
22+
import neth.iecal.trease.Constants
2223
import neth.iecal.trease.toComposeImageBitmap
2324
import kotlin.math.ceil
2425
import kotlin.math.min
@@ -44,27 +45,27 @@ private data class ForestLayout(
4445
)
4546

4647
@Composable
47-
fun IsometricForest(treeIds: List<String>) {
48+
fun IsometricForest(pngList: List<String>) {
4849
val context = LocalPlatformContext.current
4950

5051
var forestLayout by remember { mutableStateOf<ForestLayout?>(null) }
5152
var isLoading by remember { mutableStateOf(true) }
5253

53-
LaunchedEffect(treeIds) {
54+
LaunchedEffect(pngList) {
5455
isLoading = true
5556

5657
forestLayout = withContext(Dispatchers.Default) {
57-
if (treeIds.isEmpty()) return@withContext null
58+
if (pngList.isEmpty()) return@withContext null
5859

59-
val gridSize = ceil(sqrt(treeIds.size.toDouble())).toInt()
60+
val gridSize = ceil(sqrt(pngList.size.toDouble())).toInt()
6061

6162
val imageLoader = ImageLoader(context)
6263

63-
val deferredLoad = treeIds.mapIndexed { index, id ->
64+
val deferredLoad = pngList.mapIndexed { index, id ->
6465
async {
6566
val gridX = index % gridSize
6667
val gridY = index / gridSize
67-
val url = "https://trease-focus.github.io/cache-trees/images/${id}_grid.png"
68+
val url = "${Constants.cdn}/images/${id}"
6869

6970
try {
7071
val request = ImageRequest.Builder(context)

composeApp/src/commonMain/kotlin/neth/iecal/trease/ui/components/TreeGrowthPlayer.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
2323
import coil3.compose.AsyncImage
2424
import io.github.kdroidfilter.composemediaplayer.rememberVideoPlayerState
2525
import kotlinx.coroutines.delay
26+
import neth.iecal.trease.Constants
2627
import neth.iecal.trease.PlatformVideoPlayer
2728
import neth.iecal.trease.models.TimerStatus
28-
import neth.iecal.trease.utils.CacheManager
29+
import neth.iecal.trease.models.TreeData
2930
import neth.iecal.trease.utils.getCachedVideoPath
3031
import neth.iecal.trease.viewmodels.HomeScreenViewModel
3132

3233

3334
private data class PlayerState(
3435
val status: TimerStatus,
35-
val treeId: String
36+
val tree: TreeData?,
37+
val seed: Int
3638
)
3739
@Composable
3840
fun TreeGrowthPlayer(
@@ -43,11 +45,12 @@ fun TreeGrowthPlayer(
4345
val statePlayer = rememberVideoPlayerState()
4446
val crntStatus by viewModel.timerStatus.collectAsStateWithLifecycle()
4547

46-
val selectedTreeId by viewModel.selectedTree.collectAsStateWithLifecycle()
48+
val selectedTree by viewModel.selectedTree.collectAsStateWithLifecycle()
49+
val selectedSeed by viewModel.currentTreeSeedVariant.collectAsStateWithLifecycle()
4750
val selectedMinutes by viewModel.selectedMinutes.collectAsStateWithLifecycle()
4851

49-
val combinedState = remember(crntStatus, selectedTreeId) {
50-
PlayerState(crntStatus, selectedTreeId)
52+
val combinedState = remember(crntStatus, selectedTree,selectedSeed) {
53+
PlayerState(crntStatus, selectedTree, selectedSeed)
5154
}
5255
val infiniteTransition = rememberInfiniteTransition(label = "GlowTransition")
5356

@@ -71,15 +74,17 @@ fun TreeGrowthPlayer(
7174
label = "GlowAlpha"
7275
)
7376

74-
LaunchedEffect(selectedTreeId, selectedMinutes) {
75-
val remoteUrl = "https://trease-focus.github.io/cache-trees/video/$selectedTreeId.webm"
77+
LaunchedEffect(selectedTree, selectedSeed) {
78+
val remoteUrl = "${Constants.cdn}/video/${selectedTree?.id}_${selectedSeed}.webm"
7679

80+
println("loading tree $remoteUrl")
7781
try {
78-
val localPath = getCachedVideoPath(remoteUrl, selectedTreeId)
82+
val localPath = getCachedVideoPath(remoteUrl, selectedTree?.id ?: "tree",selectedSeed)
7983
statePlayer.openUri(localPath)
8084
isReady = true
8185

8286
val originalDurationMs = 30_000L
87+
println("duration ${statePlayer.durationText}")
8388
val targetDurationMs = selectedMinutes * 60_000L
8489
val stretchFactor = targetDurationMs.toDouble() / originalDurationMs
8590

@@ -138,8 +143,8 @@ fun TreeGrowthPlayer(
138143

139144
TimerStatus.POST_QUIT, TimerStatus.HAS_QUIT -> {
140145
AsyncImage(
141-
model = "https://trease-focus.github.io/cache-trees/images/weathered_grid.png",
142-
contentDescription = status.treeId,
146+
model = "${Constants.cdn}/images/weathered_grid.png",
147+
contentDescription = status.tree?.id,
143148
contentScale = ContentScale.Crop,
144149
modifier = Modifier.fillMaxSize(),
145150
filterQuality = FilterQuality.High
@@ -148,8 +153,8 @@ fun TreeGrowthPlayer(
148153
else -> {
149154
Box(Modifier.fillMaxSize()) {
150155
AsyncImage(
151-
model = "https://trease-focus.github.io/cache-trees/images/${status.treeId}_grid.png",
152-
contentDescription = status.treeId,
156+
model = "${Constants.cdn}/images/${status.tree?.id}_0_grid.png",
157+
contentDescription = status.tree?.name,
153158
contentScale = ContentScale.Crop,
154159
modifier = Modifier.fillMaxSize(),
155160
filterQuality = FilterQuality.High

composeApp/src/commonMain/kotlin/neth/iecal/trease/ui/dialogs/YouLost.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.ui.text.style.TextAlign
2424
import androidx.compose.ui.unit.dp
2525
import androidx.compose.ui.window.Dialog
2626
import coil3.compose.AsyncImage
27+
import neth.iecal.trease.Constants
2728
import neth.iecal.trease.viewmodels.HomeScreenViewModel
2829

2930

@@ -46,7 +47,7 @@ fun YouLost(viewModel: HomeScreenViewModel){
4647
Text("Your Tree weathered", style = MaterialTheme.typography.headlineLarge,textAlign = TextAlign.Center)
4748

4849
AsyncImage(
49-
model = "https://trease-focus.github.io/cache-trees/images/weathered_grid.png",
50+
model = "${Constants.cdn}/images/weathered_grid.png",
5051
contentDescription = "Dead Tree",
5152
contentScale = ContentScale.Crop,
5253
modifier = Modifier.size(280.dp)

composeApp/src/commonMain/kotlin/neth/iecal/trease/ui/dialogs/YouWon.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fun YouWon(viewModel: HomeScreenViewModel){
4444
Text("Wooww", style = MaterialTheme.typography.headlineLarge,textAlign = TextAlign.Center)
4545

4646
AsyncImage(
47-
model = "https://trease-focus.github.io/cache-trees/images/${viewModel.selectedTree.value}.png",
47+
model = "https://trease-focus.github.io/cache-trees/images/${viewModel.selectedTree.value.id}_{${viewModel.currentTreeSeedVariant.value}_grid.png",
4848
contentDescription = "Dead Tree",
4949
contentScale = ContentScale.Crop,
5050
modifier = Modifier.size(280.dp)

0 commit comments

Comments
 (0)