Skip to content

Commit

Permalink
run gatelogue flights
Browse files Browse the repository at this point in the history
  • Loading branch information
iiiii7d committed Jan 6, 2025
1 parent 55aaa4b commit 7926dd0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serde_yaml = "0.9.34"
tokio = { version = "1.42.0", features = ["full"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }

itertools = "0.13.0"
itertools = "0.14.0"
surf = "2.3.2"
csv = "1.3.1"
glam = "0.29.2"
Expand Down Expand Up @@ -79,4 +79,4 @@ string_to_string = "warn"
try_err = "warn"
unnecessary_self_imports = "warn"
unneeded_field_pattern = "warn"
verbose_file_reads = "warn"
verbose_file_reads = "warn"
7 changes: 4 additions & 3 deletions src/airports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ pub async fn airports(world_data: &mut WorldData) -> Result<()> {
let class = match size.as_deref() {
Some("XSmall") => "XS",
Some("Small") => "S",
Some("Large") | _ => "L",
Some("Medium") => "M",
_ => "L",
};
runways.push(Arc::new(Runway {
name: dir1.unwrap_or_default().into(),
name: dir1.unwrap_or_default(),
start: pos1,
end: pos2,
altitude: alt.unwrap_or_default(),
class: class.into(),
}));
runways.push(Arc::new(Runway {
name: dir2.unwrap_or_default().into(),
name: dir2.unwrap_or_default(),
start: pos2,
end: pos1,
altitude: alt.unwrap_or_default(),
Expand Down
57 changes: 57 additions & 0 deletions src/flights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::sync::Arc;

use air_traffic_simulator::{engine::world_data::Flight, WorldData};
use color_eyre::{eyre::OptionExt, Result};
use gatelogue_types::{AirMode, GatelogueData};

pub async fn flights(world_data: &mut WorldData, gatelogue_data: &GatelogueData) -> Result<()> {
world_data.flights = Some(
gatelogue_data
.air_flights()
.map(|a| {
if a.gates.len() < 2 || a.mode.as_ref().is_some_and(|a| **a != AirMode::WarpPlane) {
return Ok(None);
}
Ok(Some(Arc::new(Flight {
airline: gatelogue_data
.get_air_airline(*a.airline)?
.name
.to_owned()
.into(),
code: a.codes.first().ok_or_eyre("No code")?.into(),
from: {
let gate = gatelogue_data
.get_air_gate(**a.gates.first().ok_or_eyre("No from")?)?;
let code = gatelogue_data
.get_air_airport(*gate.airport)?
.code
.to_owned()
.into();
if !world_data.airports.iter().any(|a| a.code == code) {
return Ok(None);
};
code
},
to: {
let gate =
gatelogue_data.get_air_gate(**a.gates.get(1).ok_or_eyre("No to")?)?;
let code = gatelogue_data
.get_air_airport(*gate.airport)?
.code
.to_owned()
.into();
if !world_data.airports.iter().any(|a| a.code == code) {
return Ok(None);
};
code
},
plane: ["Airplane".into()].into(),
})))
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
.collect(),
);
Ok(())
}
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod airports;
mod flights;
mod utils;
mod waypoints;

use air_traffic_simulator::WorldData;
use color_eyre::Result;
use color_eyre::{eyre::eyre, Result};
use gatelogue_types::GatelogueData;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

use crate::{airports::airports, waypoints::waypoints};
use crate::{airports::airports, flights::flights, waypoints::waypoints};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -17,9 +19,13 @@ async fn main() -> Result<()> {
.try_init()?;

let mut world_data: WorldData = serde_yaml::from_str(include_str!("config/wd.yml"))?;
let gatelogue_data = GatelogueData::surf_get_no_sources()
.await
.map_err(|e| eyre!("{e}"))?;

airports(&mut world_data).await?;
waypoints(&mut world_data).await?;
waypoints(&mut world_data, &gatelogue_data).await?;
flights(&mut world_data, &gatelogue_data).await?;

let engine_config = serde_yaml::from_str(include_str!("config/engine_config.yml"))?;
let engine = air_traffic_simulator::Engine::new(world_data, engine_config);
Expand Down
28 changes: 10 additions & 18 deletions src/waypoints.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::sync::Arc;

use air_traffic_simulator::{engine::world_data::Waypoint, WorldData};
use color_eyre::{eyre::eyre, Report, Result};
use color_eyre::Result;
use gatelogue_types::GatelogueData;
use glam::{DVec2, Vec2};
use itertools::Itertools;
use rand::prelude::*;
use smol_str::SmolStr;

use crate::utils::{get_url, parse_coords};

struct WaypointNameGenerator(Vec<SmolStr>, StdRng);
impl WaypointNameGenerator {
fn new(seed: u64) -> Self {
Expand Down Expand Up @@ -58,23 +56,17 @@ fn nearest_waypoints(waypoints: &[(SmolStr, Vec2, Vec<SmolStr>)], wp: Vec2) -> V
nearest
}

pub async fn waypoints(world_data: &mut WorldData) -> Result<()> {
let data = GatelogueData::surf_get_no_sources()
.await
.map_err(|e| eyre!("{e}"))?;
pub async fn waypoints(world_data: &mut WorldData, gatelogue_data: &GatelogueData) -> Result<()> {
let mut gen = WaypointNameGenerator::new(0);

let mut waypoints: Vec<(SmolStr, Vec2, Vec<SmolStr>)> = data
.nodes
.values()
.filter_map(|a| {
a.as_town()
.and_then(|a| a.common.coordinates.to_owned())
.or_else(|| {
a.as_air_airport()
.and_then(|a| a.common.coordinates.to_owned())
})
})
let mut waypoints: Vec<(SmolStr, Vec2, Vec<SmolStr>)> = gatelogue_data
.air_airports()
.filter_map(|a| a.common.coordinates.to_owned())
.chain(
gatelogue_data
.towns()
.filter_map(|a| a.common.coordinates.to_owned()),
)
.map(|c| (gen.next().unwrap(), { DVec2::from(*c).as_vec2() }, vec![]))
.collect();

Expand Down

0 comments on commit 7926dd0

Please sign in to comment.