diff --git a/lib/presentation/providers/game_provider.dart b/lib/presentation/providers/game_provider.dart new file mode 100644 index 0000000..261e978 --- /dev/null +++ b/lib/presentation/providers/game_provider.dart @@ -0,0 +1,77 @@ +import 'dart:math'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import '../../core/config/app_constants.dart'; +import '../../domain/game/game_engine.dart'; +import '../../domain/game/game_state.dart'; +import 'pokedex_provider.dart'; +import 'repository_provider.dart'; + +/// Orchestration de la partie : relie GameEngine (règles), le repository (données) +/// et SharedPreferences (best score). +class GameNotifier extends Notifier { + static const _engine = GameEngine(); + final _random = Random(); + + @override + GameState build() => const GameState(); + + Future startNewGame() async { + state = _engine.newGame(state); + await _loadBestScore(); + await loadNextPokemon(); + } + + Future _loadBestScore() async { + final prefs = await SharedPreferences.getInstance(); + state = state.copyWith(bestScore: prefs.getInt(AppConstants.prefsBestScore) ?? 0); + } + + Future loadNextPokemon() async { + state = state.copyWith(status: GameStatus.loading); + final repo = ref.read(pokemonRepositoryProvider); + final isShiny = _random.nextInt(AppConstants.shinyOdds) == 0; + + final id = _random.nextInt(AppConstants.totalPokemon) + 1; + final pokemon = await repo.getById(id); + + if (pokemon == null) { + state = state.copyWith(status: GameStatus.error); + return; + } + state = _engine.startRound(state, pokemon, isShiny: isShiny); + } + + Future submitGuess(String guess) async { + final outcome = _engine.submitGuess(state, guess); + state = outcome.state; + + if (outcome.result == GuessResult.correct) { + final repo = ref.read(pokemonRepositoryProvider); + final caught = state.currentPokemon; + if (caught != null) await repo.update(caught); + await _persistBestScore(); + ref.invalidate(pokedexProvider); // rafraîchit la liste + } + return outcome.result; + } + + Future _persistBestScore() async { + final prefs = await SharedPreferences.getInstance(); + final stored = prefs.getInt(AppConstants.prefsBestScore) ?? 0; + if (state.currentScore > stored) { + await prefs.setInt(AppConstants.prefsBestScore, state.currentScore); + } + } + + void useHint() => state = _engine.useHint(state); + + Future useSkip() async { + final updated = _engine.useSkip(state); + if (updated.skips == state.skips) return; // pas de skip dispo + state = updated; + await loadNextPokemon(); + } +} + +final gameProvider = NotifierProvider(GameNotifier.new);