Skip to content

Commit

Permalink
reorganisation + template files
Browse files Browse the repository at this point in the history
  • Loading branch information
iiiii7d committed Jan 5, 2025
1 parent c08fbad commit 6a684c4
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 84 deletions.
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "cargo" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
target-branch: "dev"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: local
hooks:
- id: cargo fmt
name: cargo fmt
entry: cargo +nightly fmt
language: system
types: [rust]
pass_filenames: false
65 changes: 63 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,69 @@ color-eyre = "0.6.3"
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.13.0"
surf = "2.3.2"
csv = "1.3.1"
glam = "0.29.2"
glam = "0.29.2"


[workspace.lints.rust]
future-incompatible = "deny"

[workspace.lints.clippy]
all = "warn"
pedantic = "warn"
#cargo = "warn"
nursery = "warn"

missing_errors_doc = { level="allow", priority = 1 }
missing_panics_doc = { level="allow", priority = 1 }
cast_precision_loss = { level="allow", priority = 1 }
cast_possible_truncation = { level="allow", priority = 1 }
module_name_repetitions = { level="allow", priority = 1 }
multiple_crate_versions = { level="allow", priority = 1 }

allow_attributes = "warn"
#arithmetic_side_effects = "warn"
assertions_on_result_states = "warn"
clone_on_ref_ptr = "warn"
create_dir = "warn"
decimal_literal_representation = "warn"
default_union_representation = "warn"
deref_by_slicing = "warn"
empty_drop = "warn"
empty_enum_variants_with_brackets = "warn"
empty_structs_with_brackets = "warn"
exit = "warn"
filetype_is_file = "warn"
float_cmp_const = "warn"
format_push_string = "warn"
get_unwrap = "warn"
if_then_some_else_none = "warn"
impl_trait_in_params = "warn"
infinite_loop = "warn"
lossy_float_literal = "warn"
mem_forget = "warn"
missing_asserts_for_indexing = "warn"
#missing_inline_in_public_items = "warn"
mixed_read_write_in_expression = "warn"
mutex_atomic = "warn"
needless_raw_strings = "warn"
partial_pub_fields = "warn"
pathbuf_init_then_push = "warn"
rc_buffer = "warn"
rc_mutex = "warn"
redundant_type_annotations = "warn"
ref_patterns = "warn"
renamed_function_params = "warn"
rest_pat_in_fully_bound_structs = "warn"
semicolon_inside_block = "warn"
str_to_string = "warn"
string_lit_chars_any = "warn"
string_slice = "warn"
string_to_string = "warn"
try_err = "warn"
unnecessary_self_imports = "warn"
unneeded_field_pattern = "warn"
verbose_file_reads = "warn"
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group_imports="StdExternalCrate"
imports_granularity="Crate"
111 changes: 64 additions & 47 deletions src/airports.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
use std::sync::Arc;
use air_traffic_simulator::engine::world_data::{AirportData, Runway};
use air_traffic_simulator::WorldData;
use color_eyre::Report;
use glam::Vec2;
use color_eyre::Result;

use air_traffic_simulator::{
WorldData,
engine::world_data::{AirportData, Runway},
};
use color_eyre::{Report, Result};

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

pub async fn airports(world_data: &mut WorldData) -> Result<()> {
let client = surf::client().with(surf::middleware::Redirect::new(5));
let string = client.send(surf::get("https://docs.google.com/spreadsheets/d/11E60uIBKs5cOSIRHLz0O0nLCefpj7HgndS1gIXY_1hw/export?format=csv&gid=0"))
.await.map_err(|a| Report::msg(a.to_string()))?
.body_string()
.await.map_err(|a| Report::msg(a.to_string()))?;
let string = get_url("https://docs.google.com/spreadsheets/d/11E60uIBKs5cOSIRHLz0O0nLCefpj7HgndS1gIXY_1hw/export?format=csv&gid=0").await?;
let mut reader = csv::Reader::from_reader(string.as_bytes());
fn parse_coords(c: &str) -> Vec2 {
let mut a = c.trim().split(' ');
Vec2::new(a.next().and_then(|a| a.parse().ok()).unwrap(), a.next().and_then(|a| a.parse().ok()).unwrap())
}
world_data.airports = reader.records().map(|res| {
let res = res?;
if !res.get(1).unwrap().contains("Airfield") && !res.get(1).unwrap().contains("Airport") {
return Ok(None)
}
let mut runways = vec![];
for i in [3, 7, 11, 15] {
if res.get(i).is_none_or(|a| a.is_empty()) {
if i == 3 {
return Ok(None)

world_data.airports = reader
.records()
.map(|res| {
let res = res?;
if !res.get(1).unwrap().contains("Airfield") && !res.get(1).unwrap().contains("Airport")
{
return Ok(None);
}
let mut runways = vec![];
for i in [3, 7, 11, 15] {
if res.get(i).is_none_or(|a| a.is_empty()) {
if i == 3 {
return Ok(None);
}
break;
}
break
runways.push(Arc::new(Runway {
name: res.get(i + 2).unwrap().into(),
start: parse_coords(res.get(i).unwrap()),
end: parse_coords(res.get(i + 1).unwrap()),
altitude: 0.0,
class: res
.get(i + 3)
.unwrap()
.chars()
.next()
.unwrap()
.to_string()
.into(),
}));
runways.push(Arc::new(Runway {
name: res.get(i + 2).unwrap().into(),
start: parse_coords(res.get(i + 1).unwrap()),
end: parse_coords(res.get(i).unwrap()),
altitude: 0.0,
class: res
.get(i + 3)
.unwrap()
.chars()
.next()
.unwrap()
.to_string()
.into(),
}));
}
runways.push(Arc::new(Runway {
name: res.get(i+2).unwrap().into(),
start: parse_coords(res.get(i).unwrap()),
end: parse_coords(res.get(i+1).unwrap()),
altitude: 0.0,
class: res.get(i+3).unwrap().chars().next().unwrap().to_string().into(),
}));
runways.push(Arc::new(Runway {
name: res.get(i+2).unwrap().into(),
start: parse_coords(res.get(i+1).unwrap()),
end: parse_coords(res.get(i).unwrap()),
altitude: 0.0,
class: res.get(i+3).unwrap().chars().next().unwrap().to_string().into(),
}));
}
Ok(Some(Arc::new(AirportData {
name: res.get(0).unwrap().into(),
code: res.get(0).unwrap().into(),
runways: runways.into(),
})))
}).collect::<Result<Vec<_>>>()?.into_iter().flatten().collect();
Ok(Some(Arc::new(AirportData {
name: res.get(0).unwrap().into(),
code: res.get(0).unwrap().into(),
runways: runways.into(),
})))
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
.collect();
Ok(())
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod airports;
mod utils;
mod waypoints;

use air_traffic_simulator::WorldData;
use color_eyre::Result;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use crate::airports::airports;
use crate::waypoints::waypoints;
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

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

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -15,15 +16,16 @@ async fn main() -> Result<()> {
.with(fmt::layer())
.try_init()?;

let mut world_data: WorldData = serde_yaml::from_str(include_str!("wd.yml"))?;
let mut world_data: WorldData = serde_yaml::from_str(include_str!("config/wd.yml"))?;

airports(&mut world_data).await?;
waypoints(&mut world_data).await?;

let engine_config = serde_yaml::from_str(include_str!("engine_config.yml"))?;
let engine_config = serde_yaml::from_str(include_str!("config/engine_config.yml"))?;
let engine = air_traffic_simulator::Engine::new(world_data, engine_config);

air_traffic_simulator::run_server(engine, Some(include_str!("client_config.js"))).await?;
air_traffic_simulator::run_server(engine, Some(include_str!("config/client_config.js")))
.await?;

Ok(())
}
24 changes: 24 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::cell::LazyCell;

use color_eyre::{Report, Result};
use glam::Vec2;

pub fn parse_coords(c: &str) -> Vec2 {
let mut a = c.trim().split(' ');
Vec2::new(
a.next().and_then(|a| a.parse().ok()).unwrap(),
a.next().and_then(|a| a.parse().ok()).unwrap(),
)
}

pub static SURF_CLIENT: LazyCell<surf::Client> =
LazyCell::new(|| surf::client().with(surf::middleware::Redirect::new(5)));
pub async fn get_url(url: &'static str) -> Result<String> {
SURF_CLIENT
.send(surf::get(url))
.await
.map_err(|a| Report::msg(a.to_string()))?
.body_string()
.await
.map_err(|a| Report::msg(a.to_string()))
}
74 changes: 45 additions & 29 deletions src/waypoints.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::sync::Arc;
use air_traffic_simulator::engine::world_data::Waypoint;
use air_traffic_simulator::WorldData;
use color_eyre::Report;

use air_traffic_simulator::{WorldData, engine::world_data::Waypoint};
use color_eyre::{Report, Result};
use glam::Vec2;
use color_eyre::Result;
use itertools::Itertools;

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

fn nearest_waypoints(waypoints: &[(String, Vec2, Vec<&String>)], wp: Vec2) -> Vec<String> {
let mut radius = 0.0;
let mut nearest = vec![];
Expand All @@ -22,24 +23,23 @@ fn nearest_waypoints(waypoints: &[(String, Vec2, Vec<&String>)], wp: Vec2) -> Ve
}

pub async fn waypoints(world_data: &mut WorldData) -> Result<()> {
let client = surf::client().with(surf::middleware::Redirect::new(5));
let string = client.send(surf::get("https://docs.google.com/spreadsheets/d/11E60uIBKs5cOSIRHLz0O0nLCefpj7HgndS1gIXY_1hw/export?format=csv&gid=707730663"))
.await.map_err(|a| Report::msg(a.to_string()))?
.body_string()
.await.map_err(|a| Report::msg(a.to_string()))?;
let string = get_url("https://docs.google.com/spreadsheets/d/11E60uIBKs5cOSIRHLz0O0nLCefpj7HgndS1gIXY_1hw/export?format=csv&gid=707730663").await?;
let mut reader = csv::Reader::from_reader(string.as_bytes());
fn parse_coords(c: &str) -> Vec2 {
let mut a = c.trim().split(' ');
Vec2::new(a.next().and_then(|a| a.parse().ok()).unwrap(), a.next().and_then(|a| a.parse().ok()).unwrap())
}

let mut waypoints = reader.records().filter_map(|res| {
let res = res.unwrap();
if res.get(0).unwrap().starts_with("AA") {
return None
}
Some((res.get(0).unwrap().into(), parse_coords(res.get(1).unwrap()), vec![]))
}).collect::<Vec<_>>();
let mut waypoints = reader
.records()
.filter_map(|res| {
let res = res.unwrap();
if res.get(0).unwrap().starts_with("AA") {
return None;
}
Some((
res.get(0).unwrap().into(),
parse_coords(res.get(1).unwrap()),
vec![],
))
})
.collect::<Vec<_>>();

let mut airways = vec![];
for (name, coords, _) in &waypoints {
Expand All @@ -48,14 +48,30 @@ pub async fn waypoints(world_data: &mut WorldData) -> Result<()> {
}
}
for (name, _, conns) in &mut waypoints {
*conns = airways.iter().filter_map(|(a, b)| {
if *a == *name {Some(b)} else if *b == *name {Some(a)} else { None }
}).sorted().dedup().collect()
*conns = airways
.iter()
.filter_map(|(a, b)| {
if *a == *name {
Some(b)
} else if *b == *name {
Some(a)
} else {
None
}
})
.sorted()
.dedup()
.collect()
}
world_data.waypoints = waypoints.into_iter().map(|(name, coords, conns)| Arc::new(Waypoint {
name: name.into(),
pos: coords,
connections: conns.into_iter().map(Into::into).collect()
})).collect();
world_data.waypoints = waypoints
.into_iter()
.map(|(name, coords, conns)| {
Arc::new(Waypoint {
name: name.into(),
pos: coords,
connections: conns.into_iter().map(Into::into).collect(),
})
})
.collect();
Ok(())
}
}

0 comments on commit 6a684c4

Please sign in to comment.