hehe
This commit is contained in:
parent
9851ec3616
commit
7b6a770cee
|
@ -2273,6 +2273,7 @@ dependencies = [
|
|||
"axum-jsonschema",
|
||||
"axum-macros",
|
||||
"confy",
|
||||
"fastrand",
|
||||
"hyper",
|
||||
"mime_guess",
|
||||
"once_cell",
|
||||
|
@ -2281,8 +2282,6 @@ dependencies = [
|
|||
"serde",
|
||||
"serde_derive",
|
||||
"sqlx",
|
||||
"tinyrand",
|
||||
"tinyrand-std",
|
||||
"tokio",
|
||||
"tower-http",
|
||||
"tracing",
|
||||
|
@ -2410,21 +2409,6 @@ dependencies = [
|
|||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyrand"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87ffaad2263779579369d45f65cad0647c860893d27e4543cdcc1e428d07da2c"
|
||||
|
||||
[[package]]
|
||||
name = "tinyrand-std"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4328fbf2b798a35faaa4f1139fa7efe6ab0449efc31cac5d107d69844a5acd30"
|
||||
dependencies = [
|
||||
"tinyrand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
|
|
17
Cargo.toml
17
Cargo.toml
|
@ -31,13 +31,22 @@ tracing-subscriber = { version = "0.3", features = [
|
|||
"ansi",
|
||||
] }
|
||||
once_cell = "1"
|
||||
sqlx = { version = "0.7", features = ["runtime-tokio", "tls-rustls", "postgres", "macros"] }
|
||||
sqlx = { version = "0.7", features = [
|
||||
"runtime-tokio",
|
||||
"tls-rustls",
|
||||
"postgres",
|
||||
"macros",
|
||||
] }
|
||||
serde = "1"
|
||||
serde_derive = "1"
|
||||
axum-jsonschema = { version = "0.6", features = ["aide"] }
|
||||
axum-macros = "0.3"
|
||||
tinyrand = "0.5"
|
||||
tinyrand-std = "0.5"
|
||||
mime_guess = "2"
|
||||
hyper = "0.14"
|
||||
tower-http = {version = "0.4", features = ["compression-gzip", "trace", "cors", "timeout"]}
|
||||
tower-http = { version = "0.4", features = [
|
||||
"compression-gzip",
|
||||
"trace",
|
||||
"cors",
|
||||
"timeout",
|
||||
] }
|
||||
fastrand = { version = "2.0.0", features = ["std"] }
|
||||
|
|
34
src/stars.rs
34
src/stars.rs
|
@ -2,15 +2,12 @@ use aide::axum::{
|
|||
routing::{get_with, post_with},
|
||||
ApiRouter,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use axum::{extract::Query, http::StatusCode, Json};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::FromRow;
|
||||
|
||||
mod position;
|
||||
mod size;
|
||||
|
||||
pub fn routes() -> ApiRouter {
|
||||
|
@ -55,16 +52,11 @@ pub struct Protostar {
|
|||
pub color: String,
|
||||
}
|
||||
|
||||
impl Protostar {
|
||||
fn seed(&self) -> u64 {
|
||||
self.id.abs() as u64
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct Star {
|
||||
pub size: size::Size,
|
||||
pub core: Protostar,
|
||||
pub size: size::Size,
|
||||
pub position: position::Position,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
|
@ -74,10 +66,12 @@ pub struct DiscoveryLog {
|
|||
|
||||
impl Into<Star> for Protostar {
|
||||
fn into(self) -> Star {
|
||||
let seed = self.seed();
|
||||
let mut rng = fastrand::Rng::with_seed(self.id.abs() as u64);
|
||||
let range = 0..u64::MAX;
|
||||
Star {
|
||||
core: self,
|
||||
size: size::Size::random(seed),
|
||||
size: size::Size::random(rng.u64(range.clone())),
|
||||
position: position::Position::random(rng.u64(range.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -96,13 +90,18 @@ async fn discover(Json(log): Json<DiscoveryLog>) -> Result<Json<Star>, StatusCod
|
|||
}
|
||||
|
||||
/// show all stars
|
||||
async fn chart() -> Json<Vec<Protostar>> {
|
||||
async fn chart() -> Json<Vec<Star>> {
|
||||
let query = "SELECT * FROM stars ORDER BY id DESC";
|
||||
let protostars: Vec<Protostar> = sqlx::query_as(query)
|
||||
.fetch_all(crate::db::pool().await)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
Json(protostars)
|
||||
Json(
|
||||
protostars
|
||||
.iter()
|
||||
.map(|proto| Into::<Star>::into(proto.clone()))
|
||||
.collect::<Vec<Star>>(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
|
@ -117,8 +116,7 @@ async fn visit(data: Query<VisitorData>) -> Result<Json<Star>, StatusCode> {
|
|||
.bind(data.planet_id)
|
||||
.fetch_one(crate::db::pool().await)
|
||||
.await
|
||||
.unwrap();
|
||||
//.or(Err(StatusCode::NOT_FOUND))?;
|
||||
.or(Err(StatusCode::NOT_FOUND))?;
|
||||
let star: Star = star.into();
|
||||
Ok(Json(star))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
use fastrand::Rng;
|
||||
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(JsonSchema, Serialize, Deserialize)]
|
||||
pub struct Position {
|
||||
pub right: f64,
|
||||
pub bottom: f64,
|
||||
pub width: u8,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
pub fn random(seed: u64) -> Self {
|
||||
let mut rand = Rng::with_seed(seed);
|
||||
|
||||
Self {
|
||||
right: rand.f64() * 100.0,
|
||||
bottom: rand.f64() * 100.0,
|
||||
width: rand.u8(16..32),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
use tinyrand::{RandRange, Seeded, StdRand};
|
||||
|
||||
use fastrand::Rng;
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -13,15 +12,14 @@ pub struct Size {
|
|||
|
||||
impl Size {
|
||||
pub fn random(seed: u64) -> Self {
|
||||
let mut rand = StdRand::seed(seed);
|
||||
let mut rand = Rng::with_seed(seed);
|
||||
|
||||
// multipliers:
|
||||
// this planet is as large as 10 x item
|
||||
let small_item_multiplier: usize = rand.next_range(1..100);
|
||||
let large_item_multiplier: usize = rand.next_range(1..100);
|
||||
let range = 1..99;
|
||||
Self {
|
||||
small_item_multiplier: small_item_multiplier as u8,
|
||||
large_item_multiplier: large_item_multiplier as u8,
|
||||
small_item_multiplier: rand.choice(range.clone()).unwrap_or(1),
|
||||
large_item_multiplier: rand.choice(range.clone()).unwrap_or(1),
|
||||
small_item: "rats".into(),
|
||||
large_item: "ohios".into(),
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<nav>
|
||||
<router-link to="/">chart</router-link>
|
||||
<router-link to="/discover">discover</router-link>
|
||||
<a href="/docs">about</a>
|
||||
<a href="/docs">api</a>
|
||||
</nav>
|
||||
</div>
|
||||
</template>
|
||||
|
|
Loading…
Reference in New Issue