Skip to main content

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    CellValue, FooterData, GenericMenu, RowData, get_default_action_input,
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(format!("x{}", score.snake_growth_factor)),
32                CellValue::new(score.date.format("%d/%m/%Y").to_string()),
33                CellValue::new(score.version),
34            ]));
35        }
36    }
37
38    if rows.is_empty() {
39        rows.push(RowData::new(vec![
40            CellValue::new("No scores".to_string()),
41            CellValue::new("Play the game!".to_string()),
42            CellValue::new("🐍".to_string()),
43            CellValue::new("-".to_string()),
44            CellValue::new("-".to_string()),
45            CellValue::new("-".to_string()),
46            CellValue::new("-".to_string()),
47        ]));
48    }
49
50    rows
51}
52
53/// Returns the header labels for the high score information table
54#[must_use]
55fn highs_get_headers() -> Vec<String> {
56    vec![
57        "🏆 Rank".to_string(),
58        "👤 Head & Body".to_string(),
59        "🎯 Score".to_string(),
60        "🏁 Celerity ".to_string(),
61        "📏 Growth".to_string(),
62        "📅 Date".to_string(),
63        "🐍 Version".to_string(),
64    ]
65}
66
67/// Returns footer data for the high score table
68#[must_use]
69fn highs_get_footer_data() -> Vec<FooterData> {
70    vec![
71        FooterData {
72            symbol: "Esc/Tab".into(),
73            text: "Return to home".into(),
74            value: None,
75        },
76        FooterData {
77            symbol: "↕".into(),
78            text: "Scroll".into(),
79            value: None,
80        },
81    ]
82}