From fbb03719925d6df7f084558de56e5ae59e25f815 Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Sat, 29 Aug 2026 08:32:00 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20connexion=20et=20envoi=20du=20score=20P?= =?UTF-8?q?lay=20Jeux=20dans=20l'UI=20partag=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/commonMain/kotlin/com/livrets/App.kt | 23 ++++++++++++++++++- .../kotlin/com/livrets/ui/HomeScreen.kt | 4 ++++ .../kotlin/com/livrets/ui/ResultsScreen.kt | 5 ++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/App.kt b/sharedUI/src/commonMain/kotlin/com/livrets/App.kt index c1620bc..3418881 100644 --- a/sharedUI/src/commonMain/kotlin/com/livrets/App.kt +++ b/sharedUI/src/commonMain/kotlin/com/livrets/App.kt @@ -1,6 +1,7 @@ package com.livrets import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -9,6 +10,10 @@ import androidx.compose.runtime.setValue import com.livrets.data.HistoryRepository import com.livrets.game.GameConfig import com.livrets.game.GameResult +import com.livrets.games.GamesServices +import com.livrets.games.NoopGamesServices +import com.livrets.games.leaderboardId +import com.livrets.games.leaderboardScore import com.livrets.theme.AppTheme import com.livrets.ui.GameScreen import com.livrets.ui.HistoryScreen @@ -28,20 +33,30 @@ sealed interface Screen { @Composable fun App( filesDirPath: String, + gamesServices: GamesServices = NoopGamesServices, onThemeChanged: @Composable (isDark: Boolean) -> Unit = {}, ) = AppTheme(onThemeChanged) { val scope = rememberCoroutineScope() val history = remember { HistoryRepository("$filesDirPath/history.json".toPath()) } var screen by remember { mutableStateOf(Screen.Home) } + LaunchedEffect(gamesServices) { gamesServices.signIn() } + val homePreview: @Composable () -> Unit = { - HomeScreen(onPlay = {}, onHistory = {}) + HomeScreen(onPlay = {}, onHistory = {}, onLeaderboard = {}) } when (val current = screen) { Screen.Home -> HomeScreen( onPlay = { config -> screen = Screen.Game(config) }, onHistory = { screen = Screen.History }, + onLeaderboard = { config -> + scope.launch { + if (gamesServices.isSignedIn() || gamesServices.signIn()) { + gamesServices.showLeaderboard(config.leaderboardId()) + } + } + }, ) is Screen.Game -> GameScreen( @@ -49,6 +64,12 @@ fun App( onFinish = { result -> screen = Screen.Results(result) scope.launch { history.save(result) } + scope.launch { + val score = result.leaderboardScore() + if (score > 0 && gamesServices.isSignedIn()) { + gamesServices.submitScore(result.config.leaderboardId(), score) + } + } }, onQuit = { screen = Screen.Home }, ) diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt b/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt index 7e644b6..dfa9564 100644 --- a/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt +++ b/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt @@ -32,6 +32,7 @@ import com.livrets.game.GameConfig fun HomeScreen( onPlay: (GameConfig) -> Unit, onHistory: () -> Unit, + onLeaderboard: (GameConfig) -> Unit, ) { var questionCount by remember { mutableStateOf(10) } var secondsPerQuestion by remember { mutableStateOf(5) } @@ -60,6 +61,9 @@ fun HomeScreen( Text("Jouer", fontSize = 20.sp) } TextButton(onClick = onHistory) { Text("Historique") } + TextButton( + onClick = { onLeaderboard(GameConfig(questionCount, secondsPerQuestion, maxTable)) }, + ) { Text("Classement") } } } diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/ui/ResultsScreen.kt b/sharedUI/src/commonMain/kotlin/com/livrets/ui/ResultsScreen.kt index 6b30622..84a365b 100644 --- a/sharedUI/src/commonMain/kotlin/com/livrets/ui/ResultsScreen.kt +++ b/sharedUI/src/commonMain/kotlin/com/livrets/ui/ResultsScreen.kt @@ -24,6 +24,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import com.livrets.game.GameResult +import com.livrets.games.leaderboardScore @Composable fun ResultsScreen( @@ -55,6 +56,10 @@ fun ResultsScreen( text = "Temps moyen de réponse : ${result.averageResponseMs / 1000.0} s", style = MaterialTheme.typography.titleMedium, ) + Text( + text = "Points classement : ${result.leaderboardScore()}", + style = MaterialTheme.typography.titleMedium, + ) Spacer(Modifier.height(16.dp)) Text(