Skip to content

Commit

Permalink
Merge branch 'stations'
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpulvis committed Aug 10, 2024
2 parents 0a714e7 + e0472a8 commit 7dc68cb
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 39 deletions.
2 changes: 1 addition & 1 deletion eddn
Submodule eddn updated 3 files
+11 −13 examples/journal.rs
+20 −0 examples/other.rs
+6 −11 src/lib.rs
35 changes: 35 additions & 0 deletions galos_db/examples/stations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use sqlx::types::chrono::Utc;
use elite_journal::station::{Station as JournalStation, StationType, Service};
use elite_journal::faction::Faction;
use elite_journal::{Allegiance, Government};
use elite_journal::system::{System as JournalSystem, Coordinate};
use galos_db::systems::System;
use galos_db::stations::Station;
use galos_db::{Database, Error};

#[async_std::main]
async fn main() -> Result<(), Error> {
let db = Database::new().await?;
let system_address = 0;
let system = JournalSystem::new(system_address, "The Sun");
System::from_journal(&db, Utc::now(), &system).await;
let station = JournalStation {
dist_from_star_ls: None,
name: "Maxland".into(),
ty: Some(StationType::Orbis),
market_id: Some(1),
faction: Some(Faction { name: "Ours".into(), state: None }),
government: Some(Government::Theocracy),
allegiance: Some(Allegiance::PlayerPilots),
services: Some(vec![Service::Contacts]),
economies: None,
wanted: None,
};
let station = Station::from_journal(&db,
Utc::now(),
&station,
system_address).await?;
println!("{:#?}", station);

Ok(())
}
2 changes: 1 addition & 1 deletion galos_db/migrations/20210223205526_create_systems.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ CREATE TYPE Economy AS ENUM (
CREATE TABLE systems (
address bigint PRIMARY KEY,
name varchar NOT NULL,
position geometry(POINTZ) NOT NULL UNIQUE,
position geometry(POINTZ) UNIQUE,
population bigint,
security Security,
government Government,
Expand Down
74 changes: 74 additions & 0 deletions galos_db/migrations/20240809210548_create_stations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
CREATE TYPE StationType AS ENUM (
'AsteroidBase',
'Coriolis',
'CraterOutpost',
'CraterPort',
'FleetCarrier',
'MegaShip',
'Ocellus',
'Orbis',
'Outpost'
);


CREATE TYPE Service AS ENUM (
'Autodock',
'Blackmarket',
'CarrierFuel',
'CarrierManagement',
'Commodities',
'Contacts',
'CrewLounge',
'Dock',
'Engineer',
'Exploration',
'Facilitator',
'FlightController',
'Initiatives',
'MaterialTrader',
'Missions',
'MissionsGenerated',
'Modulepacks',
'Outfitting',
'Powerplay',
'Rearm',
'Refuel',
'Repair',
'SearchRescue',
'Shipyard',
'Shop',
'StationMenu',
'StationOperations',
'TechBroker',
'Tuning',
'VoucherRedemption',
'Livery',
'SocialSpace',
'Bartender',
'VistaGenomics',
'PioneerSupplies',
'ApexInterstellar',
'FrontlineSolutions'
);

CREATE TYPE EconomyShare AS (
name Economy,
proportion double precision
);

CREATE TABLE stations (
system_address bigint REFERENCES systems NOT NULL,
name varchar NOT NULL,
ty StationType NOT NULL,
market_id bigint,
faction varchar,
government Government,
allegiance Allegiance,
services Service[],
economies EconomyShare[],
updated_at timestamp NOT NULL,


FOREIGN KEY (system_address) REFERENCES systems (address),
PRIMARY KEY(system_address, name)
);
1 change: 1 addition & 0 deletions galos_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ pub mod articles;
pub mod bodies;
pub mod factions;
pub mod systems;
pub mod stations;
117 changes: 117 additions & 0 deletions galos_db/src/stations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use crate::{Database, Error};
use chrono::{DateTime, Utc};
use elite_journal::station::Station as JournalStation;
use elite_journal::station::{StationType, Service, EconomyShare};
use elite_journal::{Government, Allegiance};

#[derive(Debug, PartialEq, Eq)]
pub struct Station {
pub system_address: i64,
pub name: String,
pub ty: Option<StationType>,
pub market_id: Option<i64>,
pub faction: Option<String>, // TODO: Faction type?
pub government: Option<Government>, // TODO: Government type?
pub allegiance: Option<Allegiance>,
pub services: Option<Vec<Service>>,
pub economies: Option<Vec<EconomyShare>>,
pub updated_at: DateTime<Utc>,
}

impl Station {
pub async fn from_journal(
db: &Database,
timestamp: DateTime<Utc>,
station: &JournalStation,
system_address: i64,
) -> Result<Station, Error> {
let row = sqlx::query!(
r#"
INSERT INTO stations (
system_address,
name,
ty,
market_id,
faction,
government,
allegiance,
services,
economies,
updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (system_address, name)
DO UPDATE SET
ty = $3,
market_id = $4,
faction = $5,
government = $6,
allegiance = $7,
services = $8,
economies = $9,
updated_at = $10
RETURNING
system_address,
name,
ty as "ty: StationType",
market_id,
faction,
government as "government: Government",
allegiance as "allegiance: Allegiance",
services as "services: Vec<Service>",
economies as "economies: Vec<EconomyShare>",
updated_at
"#,
system_address,
station.name,
station.ty.clone() as Option<StationType>,
station.market_id,
station.faction.as_ref().map(|f| f.name.clone()),
station.government as Option<Government>,
station.allegiance as Option<Allegiance>,
station.services.clone() as Option<Vec<Service>>,
station.economies.clone() as Option<Vec<EconomyShare>>,
timestamp.naive_utc(),
)
.fetch_one(&db.pool)
.await?;

Ok(Station {
system_address: row.system_address,
name: row.name,
ty: Some(row.ty),
market_id: row.market_id,
faction: row.faction,
government: row.government,
allegiance: row.allegiance,
services: row.services,
economies: row.economies,
updated_at: row.updated_at.and_utc(),
})
}

// pub async fn fetch(db: &Database, system_address: i64, name: &str) -> Result<Self, Error> {
// let row = sqlx::query!(
// "
// SELECT *
// FROM stations
// WHERE system_address = $1 AND name = $2
// ",
// system_address,
// name
// )
// .fetch_one(&db.pool)
// .await?;

// Ok(Station {
// system_address: row.system_address,
// name: row.name,
// ty: row.ty,
// market_id: row.market_id,
// faction: row.faction,
// government: row.government,
// allegiance: row.allegiance,
// services: row.services,
// updated_at: row.updated_at.and_utc(),
// })
// }
}
Loading

0 comments on commit 7dc68cb

Please sign in to comment.