Skip to content

Commit e100e8a

Browse files
unhappychoiceclaude
andcommitted
refactor: add instance methods to GameData and SessionManager
Add instance methods to support DataProvider pattern: - GameData: processing_parameters(), repository(), completed(), failed(), mark_failed() - SessionManager: get_stage_info(), is_session_completed(), get_current_challenge(), etc. - StageRepository: count_challenges_by_difficulty() This eliminates static method calls (except ::instance()) and enables dependency injection through providers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8e15fbc commit e100e8a

File tree

3 files changed

+126
-51
lines changed

3 files changed

+126
-51
lines changed

src/presentation/game/game_data.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl GameData {
2525
pub fn initialize() -> Result<()> {
2626
let game_data = Arc::new(Mutex::new(GameData::default()));
2727
GLOBAL_GAME_DATA.set(game_data).map_err(|_| {
28-
crate::GitTypeError::TerminalError("GameData already initialized".to_string())
28+
GitTypeError::TerminalError("GameData already initialized".to_string())
2929
})?;
3030
Ok(())
3131
}
@@ -164,4 +164,40 @@ impl GameData {
164164
))
165165
}
166166
}
167+
168+
// Instance methods
169+
170+
/// Get processing parameters from this instance
171+
pub fn processing_parameters(
172+
&self,
173+
) -> Option<(Option<String>, Option<PathBuf>, ExtractionOptions)> {
174+
self.extraction_options.as_ref().map(|options| {
175+
(
176+
self.repo_spec.clone(),
177+
self.repo_path.clone(),
178+
options.clone(),
179+
)
180+
})
181+
}
182+
183+
/// Get git repository from this instance
184+
pub fn repository(&self) -> Option<GitRepository> {
185+
self.git_repository.clone()
186+
}
187+
188+
/// Check if loading is completed for this instance
189+
pub fn completed(&self) -> bool {
190+
self.loading_completed
191+
}
192+
193+
/// Check if loading failed for this instance
194+
pub fn failed(&self) -> bool {
195+
self.loading_failed
196+
}
197+
198+
/// Set loading failed state for this instance
199+
pub fn mark_failed(&mut self, error: String) {
200+
self.loading_failed = true;
201+
self.error_message = Some(error);
202+
}
167203
}

src/presentation/game/session_manager.rs

Lines changed: 80 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl SessionManager {
320320
) -> Result<()> {
321321
let instance = Self::instance();
322322
let mut manager = instance.lock().map_err(|e| {
323-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
323+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
324324
})?;
325325

326326
manager.git_repository = git_repository;
@@ -336,7 +336,7 @@ impl SessionManager {
336336
) -> Result<()> {
337337
let instance = Self::instance();
338338
let mut manager = instance.lock().map_err(|e| {
339-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
339+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
340340
})?;
341341

342342
manager.stage_trackers.push((stage_name, stage_tracker));
@@ -354,9 +354,56 @@ impl SessionManager {
354354
}
355355

356356
/// Calculate remaining skips for this session
357-
pub fn get_skips_remaining(&self) -> usize {
357+
pub fn get_skips_remaining(&self) -> Result<usize> {
358358
let used = self.get_skips_used();
359-
self.config.max_skips.saturating_sub(used)
359+
Ok(self.config.max_skips.saturating_sub(used))
360+
}
361+
362+
/// Get stage info (current_stage, total_stages)
363+
pub fn get_stage_info(&self) -> Result<(usize, usize)> {
364+
let current = match self.state {
365+
SessionState::InProgress { current_stage, .. } => current_stage,
366+
SessionState::Completed { .. } => {
367+
let completed = self
368+
.stage_results
369+
.iter()
370+
.filter(|sr| !sr.was_skipped && !sr.was_failed)
371+
.count();
372+
completed.max(1).min(self.config.max_stages)
373+
}
374+
_ => 0,
375+
};
376+
Ok((current, self.config.max_stages))
377+
}
378+
379+
/// Check if session is completed
380+
pub fn is_session_completed(&self) -> Result<bool> {
381+
Ok(matches!(self.state, SessionState::Completed { .. }))
382+
}
383+
384+
/// Get current challenge for the session
385+
pub fn get_current_challenge(&self) -> Result<Option<Challenge>> {
386+
if matches!(self.state, SessionState::InProgress { .. }) {
387+
StageRepository::get_global_challenge_for_difficulty(self.config.difficulty)
388+
} else {
389+
Ok(None)
390+
}
391+
}
392+
393+
/// Get best status for a given score
394+
pub fn get_best_status_for_score(&self, score: f64) -> Result<Option<BestStatus>> {
395+
log::debug!(
396+
"SessionManager::get_best_status_for_score: score={}, best_records_at_start={:?}",
397+
score,
398+
self.best_records_at_start
399+
);
400+
401+
let best_status = SessionRepository::determine_best_status_with_start_records(
402+
score,
403+
self.best_records_at_start.as_ref(),
404+
);
405+
406+
Ok(Some(best_status))
360407
}
361408

362409
/// Start the session
@@ -526,11 +573,6 @@ impl SessionManager {
526573
// StageTracker Management Methods
527574
// ============================================
528575

529-
/// Get current challenge (used by global API)
530-
fn get_current_challenge(&self) -> Option<Challenge> {
531-
self.get_next_challenge().unwrap_or_default()
532-
}
533-
534576
/// Get current stage number (used by global API)
535577
fn current_stage(&self) -> usize {
536578
match self.state {
@@ -595,7 +637,7 @@ impl SessionManager {
595637
/// Complete the current stage and calculate results
596638
/// Flow: StageTracker -> StageCalculator -> SessionTracker -> SessionCalculator
597639
pub fn skip_current_stage(&mut self) -> Result<(StageResult, usize, bool)> {
598-
if self.get_skips_remaining() == 0 {
640+
if self.get_skips_remaining()? == 0 {
599641
return Err(GitTypeError::TerminalError(
600642
"No skips remaining".to_string(),
601643
));
@@ -618,7 +660,7 @@ impl SessionManager {
618660

619661
// Collect data before borrowing conflicts - move tracker out
620662
let tracker_clone = self.current_stage_tracker.clone();
621-
let current_challenge = self.get_current_challenge();
663+
let current_challenge = self.get_current_challenge().ok().flatten();
622664
let stage_name = format!("Stage {}", self.current_stage());
623665

624666
// Clear current stage tracker for new challenge
@@ -638,7 +680,7 @@ impl SessionManager {
638680
self.stage_results.push(stage_result.clone());
639681

640682
// Return true to indicate new challenge should be generated
641-
let skips_remaining = self.get_skips_remaining();
683+
let skips_remaining = self.get_skips_remaining()?;
642684
Ok((stage_result, skips_remaining, true))
643685
} else {
644686
Err(GitTypeError::TerminalError(
@@ -670,7 +712,7 @@ impl SessionManager {
670712

671713
// 4. Collect data before borrowing conflicts - move tracker out to avoid borrowing issues
672714
let tracker_clone = self.current_stage_tracker.clone();
673-
let current_challenge = self.get_current_challenge();
715+
let current_challenge = self.get_current_challenge().ok().flatten();
674716
let stage_name = format!("Stage {}", self.current_stage());
675717

676718
// Clear current stage tracker to avoid borrow issues
@@ -707,6 +749,11 @@ impl SessionManager {
707749
&self.state
708750
}
709751

752+
/// Get session result (instance method)
753+
pub fn get_session_result(&self) -> Option<SessionResult> {
754+
self.generate_session_result()
755+
}
756+
710757
/// Set difficulty level for the session
711758
pub fn set_difficulty(&mut self, difficulty: DifficultyLevel) {
712759
self.config.difficulty = difficulty;
@@ -721,7 +768,7 @@ impl SessionManager {
721768
pub fn start_global_session() -> Result<()> {
722769
let instance = Self::instance();
723770
let mut manager = instance.lock().map_err(|e| {
724-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
771+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
725772
})?;
726773

727774
manager.start_session()
@@ -730,16 +777,16 @@ impl SessionManager {
730777
pub fn get_global_current_challenge() -> Result<Option<Challenge>> {
731778
let instance = Self::instance();
732779
let manager = instance.lock().map_err(|e| {
733-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
780+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
734781
})?;
735782

736-
Ok(manager.get_current_challenge())
783+
manager.get_current_challenge()
737784
}
738785

739786
pub fn get_global_stage_info() -> Result<(usize, usize)> {
740787
let instance = Self::instance();
741788
let manager = instance.lock().map_err(|e| {
742-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
789+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
743790
})?;
744791

745792
Ok((manager.current_stage(), manager.total_stages()))
@@ -748,7 +795,7 @@ impl SessionManager {
748795
pub fn is_global_session_completed() -> Result<bool> {
749796
let instance = Self::instance();
750797
let manager = instance.lock().map_err(|e| {
751-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
798+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
752799
})?;
753800

754801
Ok(manager.is_completed())
@@ -757,7 +804,7 @@ impl SessionManager {
757804
pub fn is_global_session_in_progress() -> Result<bool> {
758805
let instance = Self::instance();
759806
let manager = instance.lock().map_err(|e| {
760-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
807+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
761808
})?;
762809

763810
Ok(manager.is_in_progress())
@@ -766,28 +813,20 @@ impl SessionManager {
766813
pub fn get_global_session_result() -> Result<Option<SessionResult>> {
767814
let instance = Self::instance();
768815
let manager = instance.lock().map_err(|e| {
769-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
816+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
770817
})?;
771818

772819
Ok(manager.generate_session_result())
773820
}
774821

775-
/// Get best status using session start records
776-
pub fn get_best_status_for_score(session_score: f64) -> Result<Option<BestStatus>> {
822+
/// Get best status using session start records (static method)
823+
pub fn get_global_best_status_for_score(session_score: f64) -> Result<Option<BestStatus>> {
777824
let instance = Self::instance();
778825
let manager = instance.lock().map_err(|e| {
779-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
826+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
780827
})?;
781828

782-
log::debug!("SessionManager::get_best_status_for_score: session_score={}, best_records_at_start={:?}",
783-
session_score, manager.best_records_at_start);
784-
785-
let best_status = SessionRepository::determine_best_status_with_start_records(
786-
session_score,
787-
manager.best_records_at_start.as_ref(),
788-
);
789-
790-
Ok(Some(best_status))
829+
manager.get_best_status_for_score(session_score)
791830
}
792831

793832
// ============================================
@@ -801,7 +840,7 @@ impl SessionManager {
801840
) -> Result<()> {
802841
let instance = Self::instance();
803842
let mut manager = instance.lock().map_err(|e| {
804-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
843+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
805844
})?;
806845

807846
manager.init_stage_tracker(target_text, challenge_path)
@@ -811,7 +850,7 @@ impl SessionManager {
811850
pub fn set_global_stage_start_time(start_time: Instant) -> Result<()> {
812851
let instance = Self::instance();
813852
let mut manager = instance.lock().map_err(|e| {
814-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
853+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
815854
})?;
816855

817856
manager.set_stage_start_time(start_time)
@@ -821,17 +860,17 @@ impl SessionManager {
821860
pub fn get_global_skips_remaining() -> Result<usize> {
822861
let instance = Self::instance();
823862
let manager = instance.lock().map_err(|e| {
824-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
863+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
825864
})?;
826865

827-
Ok(manager.get_skips_remaining())
866+
manager.get_skips_remaining()
828867
}
829868

830869
/// Reset global SessionManager instance
831870
pub fn reset_global_session() -> Result<()> {
832871
let instance = Self::instance();
833872
let mut manager = instance.lock().map_err(|e| {
834-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
873+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
835874
})?;
836875

837876
manager.reduce(SessionAction::Reset)
@@ -845,7 +884,7 @@ impl SessionManager {
845884
pub fn on_session_start() -> Result<()> {
846885
let instance = Self::instance();
847886
let mut manager = instance.lock().map_err(|e| {
848-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
887+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
849888
})?;
850889

851890
manager.reduce(SessionAction::Start)
@@ -860,7 +899,7 @@ impl SessionManager {
860899
pub fn on_session_complete() -> Result<()> {
861900
let instance = Self::instance();
862901
let manager = instance.lock().map_err(|e| {
863-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
902+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
864903
})?;
865904

866905
manager.record_and_update_trackers()
@@ -870,7 +909,7 @@ impl SessionManager {
870909
pub fn on_session_retry() -> Result<()> {
871910
let instance = Self::instance();
872911
let mut manager = instance.lock().map_err(|e| {
873-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
912+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
874913
})?;
875914

876915
manager.reset();
@@ -881,7 +920,7 @@ impl SessionManager {
881920
pub fn on_session_failure() -> Result<()> {
882921
let instance = Self::instance();
883922
let mut manager = instance.lock().map_err(|e| {
884-
crate::GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
923+
GitTypeError::TerminalError(format!("Failed to lock SessionManager: {}", e))
885924
})?;
886925

887926
if let Some(ref mut tracker) = manager.current_stage_tracker {

0 commit comments

Comments
 (0)