Initial commit
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
/// The data contained from the json file generated by python.
|
||||
pub struct Data {
|
||||
/// The names of the IAs.
|
||||
pub ias: Vec<String>,
|
||||
|
||||
/// The results of the battles.
|
||||
pub battles: HashMap<String, f64>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
/// Creates some test data.
|
||||
pub fn test() -> Data {
|
||||
|
||||
// Open the file
|
||||
let mut file = File::open("./assets/test.json")
|
||||
.expect("Couldn't open file");
|
||||
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content)
|
||||
.expect("Couldn't read data");
|
||||
|
||||
serde_json::from_str(&content)
|
||||
.expect("Couldn't parse json")
|
||||
}
|
||||
|
||||
/// Returns the result for a certain battle.
|
||||
pub fn get(&self, ia1: &str, ia2: &str) -> Option<f64> {
|
||||
if let Some(value) = self.battles.get(&format!("{}/{}", ia1, ia2)) {
|
||||
Some(*value)
|
||||
} else if let Some(value) = self.battles.get(&format!("{}/{}", ia2, ia1)) {
|
||||
Some(1.0 - *value)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use rocket_contrib::templates::Template;
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
|
||||
use pytron_web::Data;
|
||||
|
||||
#[get("/")]
|
||||
fn hello() -> Template {
|
||||
Template::render("index", &Data::test())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![hello])
|
||||
.mount("/static", StaticFiles::from("static"))
|
||||
.attach(Template::fairing())
|
||||
.launch();
|
||||
}
|
||||
Reference in New Issue
Block a user