From 1934f294f913c32aebe3a3424c9ce4e4bbf78b87 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sun, 6 Nov 2022 18:41:51 +0100 Subject: [PATCH] Refactor + added more structs --- src/database/map.rs | 60 ++++++++++++++++-- src/database/mod.rs | 134 ++++++++++++++++++++++++++++++++------- src/database/race.rs | 41 ++++++++++++ src/database/teamrace.rs | 16 +++++ src/main.rs | 14 ++-- 5 files changed, 229 insertions(+), 36 deletions(-) create mode 100644 src/database/race.rs create mode 100644 src/database/teamrace.rs diff --git a/src/database/map.rs b/src/database/map.rs index cb1ba49..dd5e644 100644 --- a/src/database/map.rs +++ b/src/database/map.rs @@ -1,7 +1,57 @@ use mysql_common::{chrono::NaiveDateTime, frunk::HList}; -use rocket::serde::{Serialize, Deserialize}; +use rocket::serde::{Deserialize, Serialize}; -pub type MapRow = HList!(String, String, i8, i8, String, NaiveDateTime, String, i32, i32, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool); +pub type MapRow = HList!( + String, + String, + i8, + i8, + String, + NaiveDateTime, + String, + i32, + i32, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool, + bool +); // Different tile types and whether they appear in the map. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -46,7 +96,7 @@ pub struct MapTileData { pub boost: bool, pub plasmaf: bool, pub plasmae: bool, - pub plasmau: bool + pub plasmau: bool, } #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -60,7 +110,7 @@ pub struct Map { pub timestamp: NaiveDateTime, pub width: i32, pub height: i32, - pub tile_data: MapTileData + pub tile_data: MapTileData, } /* @@ -70,4 +120,4 @@ pub struct Map { ``` Else the reads on `record_maps` will fail whenever the timestamp is '0000-00-00 00:00:00'! TODO: Fix this server-side. -*/ \ No newline at end of file +*/ diff --git a/src/database/mod.rs b/src/database/mod.rs index 062b91c..e7e3099 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,9 +1,9 @@ -use std::env; -use std::result::Result; -use std::error::Error; -use mysql::*; use mysql::prelude::*; +use mysql::*; use mysql_common::frunk::hlist_pat; +use std::env; +use std::error::Error; +use std::result::Result; pub mod map; @@ -13,21 +13,21 @@ pub fn get_maps(pool: &Pool) -> Result, Box> { let mut conn = pool.get_conn()?; let maps = conn.query_map( - "SELECT * FROM record_maps AS maps JOIN record_mapinfo AS mapinfo ON maps.Map = mapinfo.Map", + "SELECT * FROM record_maps AS maps JOIN record_mapinfo AS mapinfo ON maps.Map = mapinfo.Map", |row: map::MapRow| { let hlist_pat![ name, server, points, stars, mapper, timestamp, _name_again, width, height, death, through, jump, dfreeze, hit_end, ehook_start, solo_start, tele_gun, tele_grenade, tele_laser, npc_start, super_start, jetpack_start, walljump, nph_start, weapon_shotgun, weapon_grenade, powerup_ninja, weapon_rifle, laser_stop, crazy_shotgun, dragger, door, switch_timed, switch, stop, through_all, tune, oldlaser, teleinevil, telein, telecheck, teleinweapon, teleinhook, checkpoint_first, bonus, boost, plasmaf, plasmae, plasmau ] = row; - map::Map { + map::Map { name, server, points, stars, mapper, timestamp, - width, + width, height, tile_data: map::MapTileData { death, through, jump, dfreeze, hit_end, ehook_start, solo_start, tele_gun, tele_grenade, tele_laser, npc_start, super_start, jetpack_start, walljump, nph_start, weapon_shotgun, weapon_grenade, powerup_ninja, weapon_rifle, laser_stop, crazy_shotgun, dragger, door, switch_timed, switch, stop, through_all, tune, oldlaser, teleinevil, telein, telecheck, teleinweapon, teleinhook, checkpoint_first, bonus, boost, plasmaf, plasmae, plasmau } } } )?; - + Ok(maps) } @@ -35,30 +35,118 @@ pub fn get_map_by_name(pool: &Pool, name: &str) -> Result, Box< let mut conn = pool.get_conn()?; let stmt = conn.prep("SELECT * FROM record_maps AS maps JOIN record_mapinfo AS mapinfo ON maps.Map = mapinfo.Map WHERE maps.Map = ?")?; - let map: Option = conn.exec_first( - &stmt, - (name,) - )?; + let map: Option = conn.exec_first(&stmt, (name,))?; Ok(match map { Some(row) => { - let hlist_pat![ name, server, points, stars, mapper, timestamp, _name_again, width, height, death, through, jump, dfreeze, hit_end, ehook_start, solo_start, tele_gun, tele_grenade, tele_laser, npc_start, super_start, jetpack_start, walljump, nph_start, weapon_shotgun, weapon_grenade, powerup_ninja, weapon_rifle, laser_stop, crazy_shotgun, dragger, door, switch_timed, switch, stop, through_all, tune, oldlaser, teleinevil, telein, telecheck, teleinweapon, teleinhook, checkpoint_first, bonus, boost, plasmaf, plasmae, plasmau ] = row; - Some(map::Map { + let hlist_pat![ name, - server, points, - stars, mapper, + server, + points, + stars, + mapper, timestamp, - width, + _name_again, + width, height, - tile_data: map::MapTileData { death, through, jump, dfreeze, hit_end, ehook_start, solo_start, tele_gun, tele_grenade, tele_laser, npc_start, super_start, jetpack_start, walljump, nph_start, weapon_shotgun, weapon_grenade, powerup_ninja, weapon_rifle, laser_stop, crazy_shotgun, dragger, door, switch_timed, switch, stop, through_all, tune, oldlaser, teleinevil, telein, telecheck, teleinweapon, teleinhook, checkpoint_first, bonus, boost, plasmaf, plasmae, plasmau } + death, + through, + jump, + dfreeze, + hit_end, + ehook_start, + solo_start, + tele_gun, + tele_grenade, + tele_laser, + npc_start, + super_start, + jetpack_start, + walljump, + nph_start, + weapon_shotgun, + weapon_grenade, + powerup_ninja, + weapon_rifle, + laser_stop, + crazy_shotgun, + dragger, + door, + switch_timed, + switch, + stop, + through_all, + tune, + oldlaser, + teleinevil, + telein, + telecheck, + teleinweapon, + teleinhook, + checkpoint_first, + bonus, + boost, + plasmaf, + plasmae, + plasmau + ] = row; + Some(map::Map { + name, + server, + points, + stars, + mapper, + timestamp, + width, + height, + tile_data: map::MapTileData { + death, + through, + jump, + dfreeze, + hit_end, + ehook_start, + solo_start, + tele_gun, + tele_grenade, + tele_laser, + npc_start, + super_start, + jetpack_start, + walljump, + nph_start, + weapon_shotgun, + weapon_grenade, + powerup_ninja, + weapon_rifle, + laser_stop, + crazy_shotgun, + dragger, + door, + switch_timed, + switch, + stop, + through_all, + tune, + oldlaser, + teleinevil, + telein, + telecheck, + teleinweapon, + teleinhook, + checkpoint_first, + bonus, + boost, + plasmaf, + plasmae, + plasmau, + }, }) - }, - None => None + } + None => None, }) } - - pub fn create_pool() -> Pool { let url = match env::var("DB_URI") { Ok(uri) => uri, @@ -77,4 +165,4 @@ pub fn create_pool() -> Pool { }; pool -} \ No newline at end of file +} diff --git a/src/database/race.rs b/src/database/race.rs new file mode 100644 index 0000000..b8a343c --- /dev/null +++ b/src/database/race.rs @@ -0,0 +1,41 @@ +use mysql_common::{chrono::NaiveDateTime, frunk::HList}; +use rocket::serde::{Serialize, Deserialize}; + +type RaceRow = HList!(String, MapName, f64, NaiveDateTime, Servers, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, f64, String, bool); + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(crate = "rocket::serde")] +struct Race { + name: String, + map: MapName, + time: f64, + timestamp: NaiveDateTime, + server: Servers, + cp1: f64, + cp2: f64, + cp3: f64, + cp4: f64, + cp5: f64, + cp6: f64, + cp7: f64, + cp8: f64, + cp9: f64, + cp10: f64, + cp11: f64, + cp12: f64, + cp13: f64, + cp14: f64, + cp15: f64, + cp16: f64, + cp17: f64, + cp18: f64, + cp19: f64, + cp20: f64, + cp21: f64, + cp22: f64, + cp23: f64, + cp24: f64, + cp25: f64, + gameid: String, + ddnet7: bool +} \ No newline at end of file diff --git a/src/database/teamrace.rs b/src/database/teamrace.rs new file mode 100644 index 0000000..e4ec480 --- /dev/null +++ b/src/database/teamrace.rs @@ -0,0 +1,16 @@ +use mysql_common::{chrono::NaiveDateTime, frunk::HList}; +use rocket::serde::{Serialize, Deserialize}; + +type TeamRaceRow = HList!(i8); + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(crate = "rocket::serde")] +struct TeamRace { + name: String, + map: MapName, + time: f64, + timestamp: NaiveDateTime, + id: String, + gameid: String, + ddnet7: bool +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 18d6948..12ae422 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,16 @@ -#[macro_use] extern crate rocket; +#[macro_use] +extern crate rocket; use database::create_pool; use rocket::serde::json::Json; -use rocket::State; +use rocket::State; extern crate dotenv; use dotenv::dotenv; mod database; -use database::DatabasePoolStore; use database::map::Map; +use database::DatabasePoolStore; #[get("/maps")] fn get_all_maps(db_pool: &State) -> Option>> { @@ -25,10 +26,7 @@ fn get_all_maps(db_pool: &State) -> Option>> { #[get("/maps/")] fn get_map_by_name(db_pool: &State, map: &str) -> Option> { match database::get_map_by_name(&db_pool.0, map) { - Ok(map) => match map { - Some(map) => Some(Json(map)), - None => None - }, + Ok(map) => map.map(Json), Err(err) => { println!("{err}"); None @@ -45,4 +43,4 @@ fn rocket() -> _ { rocket::build() .manage(DatabasePoolStore(db_pool)) .mount("/", routes![get_all_maps, get_map_by_name]) -} \ No newline at end of file +}