refactor(presentation): guess page consumes gameProvider
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
4ac13bd233
commit
2c21b80a03
@ -1,236 +1,109 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:math';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/pokemon.dart';
|
||||
import '../database/pokedex_database.dart';
|
||||
import 'main_page.dart';
|
||||
import '../components/pokemon_image.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../domain/game/game_state.dart';
|
||||
import '../providers/game_provider.dart';
|
||||
import '../providers/navigation_provider.dart';
|
||||
import '../widgets/pokemon_image.dart';
|
||||
|
||||
class GuessPage extends StatefulWidget {
|
||||
class GuessPage extends ConsumerStatefulWidget {
|
||||
const GuessPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<GuessPage> createState() => _GuessPageState();
|
||||
ConsumerState<GuessPage> createState() => _GuessPageState();
|
||||
}
|
||||
|
||||
class _GuessPageState extends State<GuessPage> {
|
||||
Pokemon? _currentPokemon;
|
||||
class _GuessPageState extends ConsumerState<GuessPage> {
|
||||
final TextEditingController _guessController = TextEditingController();
|
||||
int _lives = 3;
|
||||
int _skips = 3;
|
||||
int _hints = 3;
|
||||
int _sessionCorrectCount = 0;
|
||||
bool _isGuessed = false;
|
||||
bool _isLoading = true;
|
||||
bool _isHintUsed = false;
|
||||
bool _isShiny = false;
|
||||
int _currentScore = 0;
|
||||
int _bestScore = 0;
|
||||
bool _started = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadBestScore();
|
||||
_startNewGame();
|
||||
}
|
||||
|
||||
void _startNewGame() {
|
||||
setState(() {
|
||||
_lives = 3;
|
||||
_skips = 3;
|
||||
_hints = 3;
|
||||
_sessionCorrectCount = 0;
|
||||
_currentScore = 0;
|
||||
});
|
||||
_loadRandomPokemon();
|
||||
}
|
||||
|
||||
Future<void> _loadBestScore() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
_bestScore = prefs.getInt('best_score') ?? 0;
|
||||
// Démarre la partie après le premier frame (le provider est prêt).
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!_started) {
|
||||
_started = true;
|
||||
ref.read(gameProvider.notifier).startNewGame();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _saveBestScore() async {
|
||||
if (_currentScore > _bestScore) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt('best_score', _currentScore);
|
||||
setState(() {
|
||||
_bestScore = _currentScore;
|
||||
});
|
||||
}
|
||||
@override
|
||||
void dispose() {
|
||||
_guessController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _loadRandomPokemon() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_isGuessed = false;
|
||||
_isHintUsed = false;
|
||||
_isShiny = Random().nextInt(10) == 0; // 10% chance for shiny
|
||||
_guessController.clear();
|
||||
});
|
||||
Future<void> _onGuess() async {
|
||||
final result = await ref.read(gameProvider.notifier).submitGuess(_guessController.text);
|
||||
if (!mounted) return;
|
||||
final state = ref.read(gameProvider);
|
||||
|
||||
try {
|
||||
// Pick a random ID between 1 and 1025 (Gen 9)
|
||||
int randomId = Random().nextInt(1025) + 1;
|
||||
Pokemon? pokemon = await Pokemon.fromID(randomId);
|
||||
|
||||
// We only want to guess uncaught ones for optimal experience,
|
||||
// but if all are caught, just play anyway.
|
||||
if (pokemon != null && pokemon.isCaught) {
|
||||
int count = await PokedexDatabase.getCaughtCount();
|
||||
if (count < 1025) {
|
||||
// Find an uncaught one
|
||||
for (int i = 1; i <= 1025; i++) {
|
||||
int attemptId = (randomId + i) % 1025 + 1;
|
||||
Pokemon? attempt = await Pokemon.fromID(attemptId);
|
||||
if (attempt != null && !attempt.isCaught) {
|
||||
pokemon = attempt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_currentPokemon = pokemon;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
switch (result) {
|
||||
case GuessResult.correct:
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text(state.isShiny
|
||||
? '✨ SHINY! You caught ${state.currentPokemon!.formatedName}! (+20 pts) ✨'
|
||||
: 'Correct! You caught ${state.currentPokemon!.formatedName}!'),
|
||||
backgroundColor: state.isShiny ? Colors.amber[800] : Colors.green,
|
||||
));
|
||||
break;
|
||||
case GuessResult.wrong:
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
|
||||
content: Text('Wrong guess! Try again.'), backgroundColor: Colors.orange));
|
||||
break;
|
||||
case GuessResult.gameOver:
|
||||
await _showGameOver();
|
||||
break;
|
||||
case GuessResult.invalid:
|
||||
break;
|
||||
}
|
||||
_guessController.clear();
|
||||
}
|
||||
|
||||
void _checkGuess() async {
|
||||
if (_currentPokemon == null) return;
|
||||
String guess = _guessController.text.trim().toLowerCase();
|
||||
String actual = _currentPokemon!.name.toLowerCase();
|
||||
Future<void> _showGameOver() async {
|
||||
final state = ref.read(gameProvider);
|
||||
final playAgain = await Navigator.pushNamed(
|
||||
context,
|
||||
'/game-over',
|
||||
arguments: {
|
||||
'pokemonName': state.currentPokemon!.formatedName,
|
||||
'score': state.currentScore,
|
||||
'streak': state.sessionCorrectCount,
|
||||
'pokemonImage': state.currentPokemon!.imageUrl,
|
||||
},
|
||||
) as bool?;
|
||||
|
||||
// Normalize both for accent-insensitive comparison
|
||||
String normalizedGuess = _normalizeString(guess);
|
||||
String normalizedActual = _normalizeString(actual);
|
||||
|
||||
if (normalizedGuess == normalizedActual || normalizedGuess == 'pikachu') {
|
||||
// Correct!
|
||||
_currentPokemon!.isCaught = true;
|
||||
_currentPokemon!.isSeen = true;
|
||||
await PokedexDatabase.updatePokemon(_currentPokemon!);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_currentScore += _isShiny ? 20 : 10;
|
||||
_isGuessed = true;
|
||||
_sessionCorrectCount++;
|
||||
if (_sessionCorrectCount % 5 == 0) _hints++;
|
||||
if (_sessionCorrectCount % 10 == 0) _skips++;
|
||||
});
|
||||
}
|
||||
await _saveBestScore();
|
||||
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(_isShiny
|
||||
? '✨ SHINY! You caught ${_currentPokemon!.formatedName}! (+20 pts) ✨'
|
||||
: 'Correct! You caught ${_currentPokemon!.formatedName}!'),
|
||||
backgroundColor: _isShiny ? Colors.amber[800] : Colors.green
|
||||
),
|
||||
);
|
||||
// Wait for user to click Continue
|
||||
} else {
|
||||
// Wrong
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_lives--;
|
||||
});
|
||||
}
|
||||
|
||||
if (_lives <= 0) {
|
||||
if (!mounted) return;
|
||||
final bool? playAgain = await Navigator.pushNamed(
|
||||
context,
|
||||
'/game-over',
|
||||
arguments: {
|
||||
'pokemonName': _currentPokemon!.formatedName,
|
||||
'score': _currentScore,
|
||||
'streak': _sessionCorrectCount,
|
||||
'pokemonImage': _currentPokemon!.imageUrl,
|
||||
},
|
||||
) as bool?;
|
||||
|
||||
if (playAgain == true) {
|
||||
_startNewGame();
|
||||
} else if (playAgain == false) {
|
||||
// Switch to Pokedex List tab
|
||||
if (mounted) {
|
||||
final mainState = context.findAncestorStateOfType<MainPageState>();
|
||||
mainState?.setIndex(0); // Index 0 is Pokemon List
|
||||
_startNewGame(); // Reset game state for next time
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Wrong guess! Try again.'), backgroundColor: Colors.orange),
|
||||
);
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (playAgain == true) {
|
||||
await ref.read(gameProvider.notifier).startNewGame();
|
||||
} else if (playAgain == false) {
|
||||
ref.read(selectedTabProvider.notifier).set(0); // onglet LIST
|
||||
await ref.read(gameProvider.notifier).startNewGame();
|
||||
}
|
||||
}
|
||||
|
||||
void _useHint() {
|
||||
if (_currentPokemon == null || _isHintUsed || _hints <= 0) return;
|
||||
setState(() {
|
||||
_isHintUsed = true;
|
||||
_hints--;
|
||||
});
|
||||
}
|
||||
|
||||
void _useSkip() {
|
||||
if (_skips > 0) {
|
||||
setState(() {
|
||||
_skips--;
|
||||
});
|
||||
_loadRandomPokemon();
|
||||
}
|
||||
}
|
||||
|
||||
String _normalizeString(String input) {
|
||||
var withDia = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
|
||||
var withoutDia = 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn';
|
||||
|
||||
String output = input;
|
||||
for (int i = 0; i < withDia.length; i++) {
|
||||
output = output.replaceAll(withDia[i], withoutDia[i]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_isLoading) {
|
||||
final state = ref.watch(gameProvider);
|
||||
|
||||
if (state.status == GameStatus.loading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (_currentPokemon == null) {
|
||||
if (state.currentPokemon == null || state.status == GameStatus.error) {
|
||||
return const Center(child: Text("Error loading Pokémon"));
|
||||
}
|
||||
|
||||
final pokemon = state.currentPokemon!;
|
||||
final isGuessed = state.status == GameStatus.roundWon;
|
||||
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: Color(0xFFC8D1D8), // Silver-ish grey background with scanlines simulated
|
||||
),
|
||||
decoration: const BoxDecoration(color: Color(0xFFC8D1D8)),
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: ListView.builder(
|
||||
itemCount: 100, // drawing artificial scanlines
|
||||
itemCount: 100,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemBuilder: (context, index) => Container(
|
||||
height: 4,
|
||||
@ -242,7 +115,7 @@ class _GuessPageState extends State<GuessPage> {
|
||||
SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
// Screen top showing the silhouette
|
||||
// Silhouette screen
|
||||
Container(
|
||||
height: 250,
|
||||
width: double.infinity,
|
||||
@ -258,19 +131,19 @@ class _GuessPageState extends State<GuessPage> {
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: _isGuessed
|
||||
? PokemonImage(
|
||||
imageUrl: _isShiny ? _currentPokemon!.shinyImageUrl : _currentPokemon!.imageUrl,
|
||||
fallbackUrl: _currentPokemon!.imageUrl,
|
||||
fit: BoxFit.contain,
|
||||
)
|
||||
: PokemonImage(
|
||||
imageUrl: _isShiny ? _currentPokemon!.shinyImageUrl : _currentPokemon!.imageUrl,
|
||||
fallbackUrl: _currentPokemon!.imageUrl,
|
||||
fit: BoxFit.contain,
|
||||
color: _isShiny ? Colors.yellow[700]! : Colors.black,
|
||||
colorBlendMode: BlendMode.srcIn,
|
||||
),
|
||||
child: isGuessed
|
||||
? PokemonImage(
|
||||
imageUrl: state.isShiny ? pokemon.shinyImageUrl : pokemon.imageUrl,
|
||||
fallbackUrl: pokemon.imageUrl,
|
||||
fit: BoxFit.contain,
|
||||
)
|
||||
: PokemonImage(
|
||||
imageUrl: state.isShiny ? pokemon.shinyImageUrl : pokemon.imageUrl,
|
||||
fallbackUrl: pokemon.imageUrl,
|
||||
fit: BoxFit.contain,
|
||||
color: state.isShiny ? Colors.yellow[700]! : Colors.black,
|
||||
colorBlendMode: BlendMode.srcIn,
|
||||
),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
@ -278,11 +151,11 @@ class _GuessPageState extends State<GuessPage> {
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Text(
|
||||
_isShiny ? "✨ SHINY POKÉMON DETECTED! ✨" : "WHO'S THAT POKÉMON?",
|
||||
state.isShiny ? "✨ SHINY POKÉMON DETECTED! ✨" : "WHO'S THAT POKÉMON?",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: _isShiny ? Colors.yellow[400] : Colors.white,
|
||||
fontSize: _isShiny ? 18 : 22,
|
||||
color: state.isShiny ? Colors.yellow[400] : Colors.white,
|
||||
fontSize: state.isShiny ? 18 : 22,
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 2,
|
||||
),
|
||||
@ -291,32 +164,28 @@ class _GuessPageState extends State<GuessPage> {
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Lives display
|
||||
// Lives
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: List.generate(3, (index) {
|
||||
return Icon(
|
||||
index < _lives ? Icons.favorite : Icons.favorite_border,
|
||||
index < state.lives ? Icons.favorite : Icons.favorite_border,
|
||||
color: Colors.red,
|
||||
size: 32,
|
||||
);
|
||||
}),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Guess Section
|
||||
// Guess section
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
"IDENTIFICATION INPUT",
|
||||
style: TextStyle(color: Colors.black54, fontSize: 12, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Text("IDENTIFICATION INPUT",
|
||||
style: TextStyle(color: Colors.black54, fontSize: 12, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 4),
|
||||
if (_isHintUsed && _currentPokemon != null)
|
||||
if (state.isHintUsed)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
@ -327,7 +196,7 @@ class _GuessPageState extends State<GuessPage> {
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
"HINT: ${_currentPokemon!.formatedName[0]}${List.filled(_currentPokemon!.formatedName.length - 2, '_').join()}${_currentPokemon!.formatedName[_currentPokemon!.formatedName.length - 1]}",
|
||||
"HINT: ${pokemon.formatedName[0]}${List.filled(pokemon.formatedName.length - 2, '_').join()}${pokemon.formatedName[pokemon.formatedName.length - 1]}",
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.amber[900], letterSpacing: 4),
|
||||
),
|
||||
@ -345,24 +214,22 @@ class _GuessPageState extends State<GuessPage> {
|
||||
border: InputBorder.none,
|
||||
hintText: 'Enter Pokémon name...',
|
||||
),
|
||||
onSubmitted: (_) => _checkGuess(),
|
||||
onSubmitted: (_) => _onGuess(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (_isGuessed)
|
||||
if (isGuessed)
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 60,
|
||||
child: ElevatedButton(
|
||||
onPressed: _loadRandomPokemon,
|
||||
onPressed: () => ref.read(gameProvider.notifier).loadNextPokemon(),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.green,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
),
|
||||
child: const Text(
|
||||
"CONTINUE",
|
||||
style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2),
|
||||
),
|
||||
child: const Text("CONTINUE",
|
||||
style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2)),
|
||||
),
|
||||
)
|
||||
else ...[
|
||||
@ -370,15 +237,13 @@ class _GuessPageState extends State<GuessPage> {
|
||||
width: double.infinity,
|
||||
height: 60,
|
||||
child: ElevatedButton(
|
||||
onPressed: _checkGuess,
|
||||
onPressed: _onGuess,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF3B6EE3),
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
),
|
||||
child: const Text(
|
||||
"GUESS!",
|
||||
style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2),
|
||||
),
|
||||
child: const Text("GUESS!",
|
||||
style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 2)),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
@ -386,9 +251,12 @@ class _GuessPageState extends State<GuessPage> {
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: (_isHintUsed || _hints <= 0) ? null : _useHint,
|
||||
onPressed: (state.isHintUsed || state.hints <= 0)
|
||||
? null
|
||||
: () => ref.read(gameProvider.notifier).useHint(),
|
||||
icon: const Icon(Icons.lightbulb, color: Colors.black87),
|
||||
label: Text("HINT ($_hints)", style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)),
|
||||
label: Text("HINT (${state.hints})",
|
||||
style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.amber,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
@ -399,9 +267,12 @@ class _GuessPageState extends State<GuessPage> {
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: _skips > 0 ? _useSkip : null,
|
||||
onPressed: state.skips > 0
|
||||
? () => ref.read(gameProvider.notifier).useSkip()
|
||||
: null,
|
||||
icon: const Icon(Icons.skip_next, color: Colors.black87),
|
||||
label: Text("SKIP ($_skips)", style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)),
|
||||
label: Text("SKIP (${state.skips})",
|
||||
style: const TextStyle(color: Colors.black87, fontWeight: FontWeight.bold, fontSize: 18)),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.grey[400],
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
@ -413,8 +284,7 @@ class _GuessPageState extends State<GuessPage> {
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Score Display
|
||||
// Score
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(16),
|
||||
@ -425,19 +295,13 @@ class _GuessPageState extends State<GuessPage> {
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Text(
|
||||
"CURRENT SCORE",
|
||||
style: TextStyle(color: Colors.black54, fontSize: 14, fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
"$_currentScore",
|
||||
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Color(0xFF3B6EE3)),
|
||||
),
|
||||
const Text("CURRENT SCORE",
|
||||
style: TextStyle(color: Colors.black54, fontSize: 14, fontWeight: FontWeight.bold)),
|
||||
Text("${state.currentScore}",
|
||||
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Color(0xFF3B6EE3))),
|
||||
const Divider(height: 24),
|
||||
Text(
|
||||
"PERSONAL BEST: $_bestScore",
|
||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black87),
|
||||
),
|
||||
Text("PERSONAL BEST: ${state.bestScore}",
|
||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black87)),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user