rsnaker/graphics/menus/retro_parameter_table/
customized_with_highscore.rs

1use crate::game_logic::high_score::HighScoreManager;
2use crate::graphics::menus::retro_parameter_table::generic_logic::{
3    get_default_action_input, CellValue, FooterData, GenericMenu, RowData,
4};
5use ratatui::DefaultTerminal;
6
7/// Sets up and runs the high score table menu
8pub fn setup_and_run_highs_table_parameters(terminal: &mut DefaultTerminal) {
9    GenericMenu::new(
10        load_highs_info_in_table(),
11        &highs_get_headers(),
12        highs_get_footer_data(),
13        None,
14    )
15    .run(get_default_action_input(), terminal);
16}
17
18/// Loads highscore information into table rows for display
19#[must_use]
20fn load_highs_info_in_table() -> Vec<RowData> {
21    let mut rows = vec![];
22
23    if let Ok(manager) = HighScoreManager::new() {
24        let high_scores = manager.get_top_scores();
25        for (index, score) in high_scores.into_iter().enumerate() {
26            rows.push(RowData::new(vec![
27                CellValue::new(format!("#{}", index + 1)),
28                CellValue::new(score.symbols),
29                CellValue::new(format!("{}", score.score)),
30                CellValue::new(score.speed),
31                CellValue::new(score.date.format("%d/%m/%Y").to_string()),
32                CellValue::new(score.version),
33            ]));
34        }
35    }
36
37    if rows.is_empty() {
38        rows.push(RowData::new(vec![
39            CellValue::new("No scores".to_string()),
40            CellValue::new("Play the game!".to_string()),
41            CellValue::new("🐍".to_string()),
42            CellValue::new("-".to_string()),
43            CellValue::new("-".to_string()),
44            CellValue::new("-".to_string()),
45        ]));
46    }
47
48    rows
49}
50
51/// Returns the header labels for the high score information table
52#[must_use]
53fn highs_get_headers() -> Vec<String> {
54    vec![
55        "🏆 Rank".to_string(),
56        "👤 Head & Body".to_string(),
57        "🎯 Score".to_string(),
58        "🏁 Celerity ".to_string(),
59        "📅 Date".to_string(),
60        "🐍 Version".to_string(),
61    ]
62}
63
64/// Returns footer data for the high score table
65#[must_use]
66fn highs_get_footer_data() -> Vec<FooterData> {
67    vec![
68        FooterData {
69            symbol: "Esc".into(),
70            text: "Return to home".into(),
71            value: None,
72        },
73        FooterData {
74            symbol: "↕".into(),
75            text: "Scroll".into(),
76            value: None,
77        },
78    ]
79}