Refactor + added more structs
This commit is contained in:
parent
970803cfa8
commit
1934f294f9
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -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'!
|
Else the reads on `record_maps` will fail whenever the timestamp is '0000-00-00 00:00:00'!
|
||||||
TODO: Fix this server-side.
|
TODO: Fix this server-side.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
@ -13,21 +13,21 @@ pub fn get_maps(pool: &Pool) -> Result<Vec<map::Map>, Box<dyn Error>> {
|
||||||
let mut conn = pool.get_conn()?;
|
let mut conn = pool.get_conn()?;
|
||||||
|
|
||||||
let maps = conn.query_map(
|
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| {
|
|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;
|
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,
|
name,
|
||||||
server, points,
|
server, points,
|
||||||
stars, mapper,
|
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 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(maps)
|
Ok(maps)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,30 +35,118 @@ 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![
|
||||||
Some(map::Map {
|
|
||||||
name,
|
name,
|
||||||
server, points,
|
server,
|
||||||
stars, mapper,
|
points,
|
||||||
|
stars,
|
||||||
|
mapper,
|
||||||
timestamp,
|
timestamp,
|
||||||
width,
|
_name_again,
|
||||||
|
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 }
|
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 {
|
pub fn create_pool() -> Pool {
|
||||||
let url = match env::var("DB_URI") {
|
let url = match env::var("DB_URI") {
|
||||||
Ok(uri) => uri,
|
Ok(uri) => uri,
|
||||||
|
@ -77,4 +165,4 @@ pub fn create_pool() -> Pool {
|
||||||
};
|
};
|
||||||
|
|
||||||
pool
|
pool
|
||||||
}
|
}
|
||||||
|
|
41
src/database/race.rs
Normal file
41
src/database/race.rs
Normal 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
16
src/database/teamrace.rs
Normal 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
|
||||||
|
}
|
14
src/main.rs
14
src/main.rs
|
@ -1,15 +1,16 @@
|
||||||
#[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;
|
||||||
use rocket::State;
|
use rocket::State;
|
||||||
|
|
||||||
extern crate dotenv;
|
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
|
||||||
|
@ -45,4 +43,4 @@ fn rocket() -> _ {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.manage(DatabasePoolStore(db_pool))
|
.manage(DatabasePoolStore(db_pool))
|
||||||
.mount("/", routes![get_all_maps, get_map_by_name])
|
.mount("/", routes![get_all_maps, get_map_by_name])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user