80 lines
3.8 KiB
Rust
80 lines
3.8 KiB
Rust
use std::env;
|
|
use std::result::Result;
|
|
use std::error::Error;
|
|
use mysql::*;
|
|
use mysql::prelude::*;
|
|
use mysql_common::frunk::hlist_pat;
|
|
|
|
pub mod map;
|
|
|
|
pub struct DatabasePoolStore(pub Pool);
|
|
|
|
pub fn get_maps(pool: &Pool) -> Result<Vec<map::Map>, Box<dyn Error>> {
|
|
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",
|
|
|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 {
|
|
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 }
|
|
}
|
|
}
|
|
)?;
|
|
|
|
Ok(maps)
|
|
}
|
|
|
|
pub fn get_map_by_name(pool: &Pool, name: &str) -> Result<Option<map::Map>, Box<dyn Error>> {
|
|
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,)
|
|
)?;
|
|
|
|
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 {
|
|
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
|
|
})
|
|
}
|
|
|
|
|
|
|
|
pub fn create_pool() -> Pool {
|
|
let url = match env::var("DB_URI") {
|
|
Ok(uri) => uri,
|
|
Err(err) => {
|
|
println!("You must provide an env var: 'DB_URI'!");
|
|
panic!("{}", err);
|
|
}
|
|
};
|
|
|
|
let pool = match Pool::new(url.as_str()) {
|
|
Ok(pool) => pool,
|
|
Err(err) => {
|
|
println!("Couldn't connect to the database!");
|
|
panic!("{}", err);
|
|
}
|
|
};
|
|
|
|
pool
|
|
} |