partly ignore "Bahnhof " prefix added by bahnhof.de for user-input searches

This commit is contained in:
Mark 2025-05-02 11:12:09 +02:00
parent b4d7481063
commit a475248823
2 changed files with 40 additions and 11 deletions

View File

@ -202,7 +202,7 @@ impl Game {
}) {
Ok(act?)
} else {
Err("no such station".into())
Err(format!("no such station ({location})").into())
}
}

View File

@ -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)),
);
}
}
}
}