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 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,
}
/*

View File

@ -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;
@ -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 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(
&stmt,
(name,)
)?;
let map: Option<map::MapRow> = 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;
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 {
name,
server, points,
stars, mapper,
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 }
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,

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,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<DatabasePoolStore>) -> Option<Json<Vec<Map>>> {
@ -25,10 +26,7 @@ fn get_all_maps(db_pool: &State<DatabasePoolStore>) -> Option<Json<Vec<Map>>> {
#[get("/maps/<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) {
Ok(map) => match map {
Some(map) => Some(Json(map)),
None => None
},
Ok(map) => map.map(Json),
Err(err) => {
println!("{err}");
None