feat(presentation): add game state Notifier
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
2d87879af8
commit
4bca4b1ed5
77
lib/presentation/providers/game_provider.dart
Normal file
77
lib/presentation/providers/game_provider.dart
Normal file
@ -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<GameState> {
|
||||
static const _engine = GameEngine();
|
||||
final _random = Random();
|
||||
|
||||
@override
|
||||
GameState build() => const GameState();
|
||||
|
||||
Future<void> startNewGame() async {
|
||||
state = _engine.newGame(state);
|
||||
await _loadBestScore();
|
||||
await loadNextPokemon();
|
||||
}
|
||||
|
||||
Future<void> _loadBestScore() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
state = state.copyWith(bestScore: prefs.getInt(AppConstants.prefsBestScore) ?? 0);
|
||||
}
|
||||
|
||||
Future<void> 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<GuessResult> 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<void> _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<void> 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, GameState>(GameNotifier.new);
|
||||
Loading…
x
Reference in New Issue
Block a user