Refactor + added more structs

This commit is contained in:
BurnyLlama 2022-11-06 18:41:51 +01:00
parent 970803cfa8
commit 1934f294f9
5 changed files with 229 additions and 36 deletions

View File

@ -1,7 +1,57 @@
use mysql_common::{chrono::NaiveDateTime, frunk::HList}; 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. // Different tile types and whether they appear in the map.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -46,7 +96,7 @@ pub struct MapTileData {
pub boost: bool, pub boost: bool,
pub plasmaf: bool, pub plasmaf: bool,
pub plasmae: bool, pub plasmae: bool,
pub plasmau: bool pub plasmau: bool,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -60,7 +110,7 @@ pub struct Map {
pub timestamp: NaiveDateTime, pub timestamp: NaiveDateTime,
pub width: i32, pub width: i32,
pub height: i32, pub height: i32,
pub tile_data: MapTileData pub tile_data: MapTileData,
} }
/* /*

View File

@ -1,9 +1,9 @@
use std::env;
use std::result::Result;
use std::error::Error;
use mysql::*;
use mysql::prelude::*; use mysql::prelude::*;
use mysql::*;
use mysql_common::frunk::hlist_pat; use mysql_common::frunk::hlist_pat;
use std::env;
use std::error::Error;
use std::result::Result;
pub mod map; pub mod map;
@ -35,29 +35,117 @@ pub fn get_map_by_name(pool: &Pool, name: &str) -> Result<Option<map::Map>, Box<
let mut conn = pool.get_conn()?; 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 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<map::MapRow> = conn.exec_first( let map: Option<map::MapRow> = conn.exec_first(&stmt, (name,))?;
&stmt,
(name,)
)?;
Ok(match map { Ok(match map {
Some(row) => { 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; 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 { Some(map::Map {
name, name,
server, points, server,
stars, mapper, points,
stars,
mapper,
timestamp, timestamp,
width, width,
height, 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 } 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 { pub fn create_pool() -> Pool {
let url = match env::var("DB_URI") { let url = match env::var("DB_URI") {

41
src/database/race.rs Normal file
View File

@ -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
}

16
src/database/teamrace.rs Normal file
View File

@ -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
}

View File

@ -1,4 +1,5 @@
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
use database::create_pool; use database::create_pool;
use rocket::serde::json::Json; use rocket::serde::json::Json;
@ -8,8 +9,8 @@ extern crate dotenv;
use dotenv::dotenv; use dotenv::dotenv;
mod database; mod database;
use database::DatabasePoolStore;
use database::map::Map; use database::map::Map;
use database::DatabasePoolStore;
#[get("/maps")] #[get("/maps")]
fn get_all_maps(db_pool: &State<DatabasePoolStore>) -> Option<Json<Vec<Map>>> { fn get_all_maps(db_pool: &State<DatabasePoolStore>) -> Option<Json<Vec<Map>>> {
@ -25,10 +26,7 @@ fn get_all_maps(db_pool: &State<DatabasePoolStore>) -> Option<Json<Vec<Map>>> {
#[get("/maps/<map>")] #[get("/maps/<map>")]
fn get_map_by_name(db_pool: &State<DatabasePoolStore>, map: &str) -> Option<Json<Map>> { fn get_map_by_name(db_pool: &State<DatabasePoolStore>, map: &str) -> Option<Json<Map>> {
match database::get_map_by_name(&db_pool.0, map) { match database::get_map_by_name(&db_pool.0, map) {
Ok(map) => match map { Ok(map) => map.map(Json),
Some(map) => Some(Json(map)),
None => None
},
Err(err) => { Err(err) => {
println!("{err}"); println!("{err}");
None None