Skip to content

Commit

Permalink
First
Browse files Browse the repository at this point in the history
  • Loading branch information
RengaN02 committed Feb 18, 2025
1 parent 0100ce3 commit 3d9ce3f
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
129 changes: 129 additions & 0 deletions src/database/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use chrono::{NaiveDateTime};
use rocket::futures::StreamExt as _;
use serde::Serialize;
use sqlx::{Executor as _, MySqlPool, Row as _};

use super::error::Error;

#[derive(Debug, Serialize)]
pub struct Death {
pub name: String,
pub job: String,
pub pod: String,
pub brute: u32,
pub fire: u32,
pub oxy: u32,
pub tox: u32,
pub last_words: Option<String>,
pub suicide: bool,
pub round_id: u32,
#[serde(with = "crate::serde::datetime")]
pub timestamp: NaiveDateTime,
}

pub async fn get_deaths(
since: Option<&str>,
pool: &MySqlPool,
) -> Result<Vec<Death>, Error> {
let mut connection = pool.acquire().await?;

let mut sql = "SELECT name, job, pod, brute, fire, oxy, tox, last_words, suicide, round_id, tod AS timestamp FROM death".to_string();

if since.is_some() {
sql.push_str(" WHERE timestamp > ?");
}

sql.push_str(" GROUP BY timestamp");

let mut query = sqlx::query(&sql);

if let Some(since) = since {
query = query.bind(since);
}

let mut deaths = Vec::new();

{
let mut rows = connection.fetch(query);

while let Some(row) = rows.next().await {
let death = row?;

let death = Death {
name: death.try_get("name")?,
job: death.try_get("job")?,
pod: death.try_get("pod")?,
brute: death.try_get("brute")?,
fire: death.try_get("fire")?,
oxy: death.try_get("oxy")?,
tox: death.try_get("tox")?,
last_words: death.try_get("last_words")?,
suicide: death.try_get("suicide")?,
round_id: death.try_get("round_id")?,
timestamp: death.try_get("timestamp")?,
};

deaths.push(death);
}
}

connection.close().await?;
Ok(deaths)
}


#[derive(Debug, Serialize)]
pub struct Citation {
pub sender: String,
pub recipient: String,
pub message: String,
pub fine: u32,
pub round_id: u32,
#[serde(with = "crate::serde::datetime")]
pub timestamp: NaiveDateTime,
}

pub async fn get_citations(
since: Option<&str>,
pool: &MySqlPool,
) -> Result<Vec<Citation>, Error> {
let mut connection = pool.acquire().await?;

let mut sql = "SELECT round_id, sender_ic, recipient, message, fine, timestamp FROM citation".to_string();

if since.is_some() {
sql.push_str(" WHERE timestamp > ?");
}

sql.push_str(" GROUP BY timestamp");

let mut query = sqlx::query(&sql);

if let Some(since) = since {
query = query.bind(since);
}

let mut citations = Vec::new();

{
let mut rows = connection.fetch(query);

while let Some(row) = rows.next().await {
let citation = row?;

let citation = Citation {
round_id: citation.try_get("round_id")?,
sender: citation.try_get("sender_ic")?,
recipient: citation.try_get("recipient")?,
message: citation.try_get("message")?,
fine: citation.try_get("fine")?,
timestamp: citation.try_get("timestamp")?,
};

citations.push(citation);
}
}

connection.close().await?;
Ok(citations)
}
2 changes: 2 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ mod player;
mod state;
mod test_merges;
mod verify;
mod events;

pub use player::*;
pub use state::Database;
pub use test_merges::*;
pub use verify::*;
pub use events::*;
29 changes: 29 additions & 0 deletions src/routes/v2/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rocket::{get, http::Status, State};

use crate::{database::*, Database};

use super::{common::ApiKey, Json};

#[get("/events/deaths?<since>")]
pub async fn deaths(
since: Option<&str>,
database: &State<Database>,
_api_key: ApiKey,
) -> Result<Json<Vec<Death>>, Status> {
match get_deaths(since, &database.pool).await {
Ok(deaths) => Ok(Json::Ok(deaths)),
Err(_) => Err(Status::InternalServerError),
}
}

#[get("/events/citations?<since>")]
pub async fn citations(
since: Option<&str>,
database: &State<Database>,
_api_key: ApiKey,
) -> Result<Json<Vec<Citation>>, Status> {
match get_citations(since, &database.pool).await {
Ok(citations) => Ok(Json::Ok(citations)),
Err(_) => Err(Status::InternalServerError),
}
}
5 changes: 4 additions & 1 deletion src/routes/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rocket::{routes, Build, Rocket};
mod autocomplete;
mod common;
mod discord;
mod events;
mod patreon;
mod player;
mod server;
Expand Down Expand Up @@ -30,7 +31,9 @@ pub fn mount(rocket: Rocket<Build>) -> Rocket<Build> {
discord::member,
autocomplete::job,
autocomplete::ckey,
autocomplete::ic_name
autocomplete::ic_name,
events::deaths,
events::citations
],
)
}

0 comments on commit 3d9ce3f

Please sign in to comment.