From fa71adcfbbb7ed7d7b7798a9dd0a8742676d6108 Mon Sep 17 00:00:00 2001 From: Mark <> Date: Fri, 2 May 2025 11:12:09 +0200 Subject: [PATCH] partly ignore "Bahnhof " prefix added by bahnhof.de for user-input searches --- src/game.rs | 2 +- src/stations_list.rs | 49 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/game.rs b/src/game.rs index 3a94835..8b81310 100644 --- a/src/game.rs +++ b/src/game.rs @@ -202,7 +202,7 @@ impl Game { }) { Ok(act?) } else { - Err("no such station".into()) + Err("no such station ({location})".into()) } } diff --git a/src/stations_list.rs b/src/stations_list.rs index 38be063..8317b2f 100644 --- a/src/stations_list.rs +++ b/src/stations_list.rs @@ -257,6 +257,7 @@ impl StationsList { pub fn find_stations(&self, name: &str) -> Vec<(String, String)> { let stations = self.stations.blocking_lock(); let name = name.to_lowercase(); + let name2 = format!("bahnhof {}", name); if let Some(s) = stations.get(&name).and_then(|v| v.station.as_ref()) { vec![(name.to_owned(), s.name.clone())] } else { @@ -271,22 +272,50 @@ impl StationsList { if o.len() < 3 { for (id, station) in stations.iter() { if let Some(station) = &station.station { - if station.name.to_lowercase().starts_with(&name) { - o.insert( - id.clone(), - ( - station.name.clone(), - station.name.len().saturating_sub(name.len()).min(100), - ), - ); + if station.name.to_lowercase() == name2 { + o.insert(id.clone(), (station.name.clone(), 1)); } } } if o.len() < 3 { for (id, station) in stations.iter() { if let Some(station) = &station.station { - if let Some(pos) = station.name.to_lowercase().find(&name) { - o.insert(id.clone(), (station.name.clone(), (100 + pos).min(200))); + if station.name.to_lowercase().starts_with(&name) { + o.insert( + id.clone(), + ( + station.name.clone(), + 100 + station + .name + .len() + .saturating_sub(name.len()) + .min(100), + ), + ); + } else if station.name.to_lowercase().starts_with(&name2) { + o.insert( + id.clone(), + ( + station.name.clone(), + 100 + station + .name + .len() + .saturating_sub(name2.len()) + .min(100), + ), + ); + } + } + } + if o.len() < 3 { + for (id, station) in stations.iter() { + if let Some(station) = &station.station { + if let Some(pos) = station.name.to_lowercase().find(&name) { + o.insert( + id.clone(), + (station.name.clone(), 200 + pos.min(100)), + ); + } } } }