@@ -3,7 +3,7 @@ use crate::scoring::{TypingMetrics, ScoringEngine};
33use crossterm:: terminal;
44use super :: {
55 challenge:: Challenge ,
6- screens:: { TitleScreen , ResultScreen , CountdownScreen , TypingScreen , TitleAction } ,
6+ screens:: { TitleScreen , ResultScreen , CountdownScreen , TypingScreen , TitleAction , result_screen :: ResultAction } ,
77 stage_builder:: { StageBuilder , GameMode , DifficultyLevel } ,
88} ;
99
@@ -12,6 +12,7 @@ pub struct StageManager {
1212 current_challenges : Vec < Challenge > ,
1313 current_stage : usize ,
1414 stage_engines : Vec < ( String , ScoringEngine ) > ,
15+ current_game_mode : Option < GameMode > ,
1516}
1617
1718impl StageManager {
@@ -21,6 +22,7 @@ impl StageManager {
2122 current_challenges : Vec :: new ( ) ,
2223 current_stage : 0 ,
2324 stage_engines : Vec :: new ( ) ,
25+ current_game_mode : None ,
2426 }
2527 }
2628
@@ -48,33 +50,39 @@ impl StageManager {
4850 difficulty : difficulty. clone ( ) ,
4951 } ;
5052
51- let stage_builder = StageBuilder :: with_mode ( game_mode) ;
52- self . current_challenges = stage_builder. build_stages ( self . available_challenges . clone ( ) ) ;
53+ self . current_game_mode = Some ( game_mode. clone ( ) ) ;
5354
54- // Debug output
55- println ! ( "Selected difficulty: {:?}" , difficulty) ;
56- println ! ( "Built {} challenges with difficulty {:?}" , self . current_challenges. len( ) , difficulty) ;
57- for ( i, challenge) in self . current_challenges . iter ( ) . enumerate ( ) {
58- println ! ( " Challenge {}: {} ({} lines)" , i+1 , challenge. id, challenge. code_content. lines( ) . count( ) ) ;
59- }
60-
61- if self . current_challenges . is_empty ( ) {
62- println ! ( "No challenges found for difficulty {:?}" , difficulty) ;
63- continue ; // Go back to title screen
64- }
65-
66- // Reset session metrics
67- self . current_stage = 0 ;
68- self . stage_engines . clear ( ) ;
69-
70- match self . run_stages ( ) {
71- Ok ( _session_complete) => {
72- // After session completes, continue to title screen
73- // User can choose to play again or quit
74- } ,
75- Err ( e) => {
76- terminal:: disable_raw_mode ( ) ?;
77- return Err ( e) ;
55+ loop {
56+ let stage_builder = StageBuilder :: with_mode ( game_mode. clone ( ) ) ;
57+ self . current_challenges = stage_builder. build_stages ( self . available_challenges . clone ( ) ) ;
58+
59+ // Debug output
60+ println ! ( "Selected difficulty: {:?}" , difficulty) ;
61+ println ! ( "Built {} challenges with difficulty {:?}" , self . current_challenges. len( ) , difficulty) ;
62+ for ( i, challenge) in self . current_challenges . iter ( ) . enumerate ( ) {
63+ println ! ( " Challenge {}: {} ({} lines)" , i+1 , challenge. id, challenge. code_content. lines( ) . count( ) ) ;
64+ }
65+
66+ if self . current_challenges . is_empty ( ) {
67+ println ! ( "No challenges found for difficulty {:?}" , difficulty) ;
68+ break ; // Go back to title screen
69+ }
70+
71+ // Reset session metrics
72+ self . current_stage = 0 ;
73+ self . stage_engines . clear ( ) ;
74+
75+ match self . run_stages ( ) {
76+ Ok ( session_complete) => {
77+ if !session_complete {
78+ break ; // User chose to quit or back to title
79+ }
80+ // If session_complete is true, retry with same settings
81+ } ,
82+ Err ( e) => {
83+ terminal:: disable_raw_mode ( ) ?;
84+ return Err ( e) ;
85+ }
7886 }
7987 }
8088 } ,
@@ -113,8 +121,14 @@ impl StageManager {
113121 }
114122
115123 // All stages completed - show final results (raw mode still enabled)
116- self . show_session_summary ( ) ?;
117- Ok ( true ) // Return true to indicate session completed
124+ match self . show_session_summary ( ) ? {
125+ ResultAction :: Retry => Ok ( true ) , // Return true to indicate retry requested
126+ ResultAction :: Quit => {
127+ terminal:: disable_raw_mode ( ) ?;
128+ std:: process:: exit ( 0 ) ;
129+ } ,
130+ _ => Ok ( false ) , // Return false for back to title
131+ }
118132 }
119133
120134 fn show_stage_completion ( & self , metrics : & TypingMetrics ) -> Result < ( ) > {
@@ -135,13 +149,12 @@ impl StageManager {
135149 }
136150
137151
138- fn show_session_summary ( & self ) -> Result < ( ) > {
139- let _result = ResultScreen :: show_session_summary (
152+ fn show_session_summary ( & self ) -> Result < ResultAction > {
153+ ResultScreen :: show_session_summary_with_input (
140154 self . current_challenges . len ( ) ,
141155 self . stage_engines . len ( ) ,
142156 & self . stage_engines ,
143- ) ?;
144- Ok ( ( ) )
157+ )
145158 }
146159
147160 pub fn get_current_stage ( & self ) -> usize {
0 commit comments