diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 7f1a8b1..1c50db5 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -21,3 +21,5 @@ tracing-futures = "0.1.0" tracing-subscriber = "0.1.5" rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" } rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", branch = "async", features = ["handlebars_templates", "tera_templates"] } + +rust-ini = "0.15.2" diff --git a/backend/Rocket.toml b/backend/Rocket.toml new file mode 100644 index 0000000..52ae39e --- /dev/null +++ b/backend/Rocket.toml @@ -0,0 +1,11 @@ +[development] +address = "localhost" +port = 8000 + +[staging] +address = "0.0.0.0" +port = 80 + +[production] +address = "0.0.0.0" +port = 80 diff --git a/backend/games.ini b/backend/games.ini new file mode 100644 index 0000000..814de4e --- /dev/null +++ b/backend/games.ini @@ -0,0 +1,111 @@ +path=static/games/ + +[Alisha Lara.json] +name=Alisha Lara +turns=143 +players= "SimpleBot 2 (check if python is correct before executing)" "mybot2" + +[Andre Head.json] +name=Andre Head +turns=1229 +players= "mybot2" "SimpleBot 2 (check if python is correct before executing)" "mybot" + +[Angela Sims.json] +name=Angela Sims +turns=221 +players= "SimpleBot 2 (check if python is correct before executing)" "mybot2" + +[Buford Humphrey.json] +name=Buford Humphrey +turns=174 +players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" + +[Catalina Grant.json] +name=Catalina Grant +turns=202 +players= "mybot" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" + +[Cesar Jordan.json] +name=Cesar Jordan +turns=214 +players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" + +[Chandra Garrett.json] +name=Chandra Garrett +turns=202 +players= "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" + +[Cory Mooney.json] +name=Cory Mooney +turns=45 +players= "SimpleBot 2 (check if python is correct before executing)" "SimpleBot 1 (check if python is correct before executing)" + +[Deana Bishop.json] +name=Deana Bishop +turns=302 +players= "mybot" "mybot2" + +[game2.json] +name=game2 +turns=110 +players="bot_auto_player_full_rand" "adversary_auto_player_link_rand" + +[Georgina Wiley.json] +name=Georgina Wiley +turns=302 +players= "mybot2" "mybot" + +[hungergames.json] +name=hungergames +turns=119 +players= + +[Jeannie Swanson.json] +name=Jeannie Swanson +turns=251 +players= "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" "mybot2" "mybot" + +[Joanne Blair.json] +name=Joanne Blair +turns=202 +players= "mybot2" "mybot" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" + +[large.json] +name=large +turns=1166 +players= + +[Lester Berg.json] +name=Lester Berg +turns=1132 +players= "mybot" "mybot2" "SimpleBot 2 (check if python is correct before executing)" + +[Lindsey Farrell.json] +name=Lindsey Farrell +turns=461 +players= "jens" "SimpleBot 2 (check if python is correct before executing)" "dinkske" "SimpleBot 1 (check if python is correct before executing)" + +[Pansy Le.json] +name=Pansy Le +turns=417 +players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" "SammyBot" + +[pietmap.json] +name=pietmap +turns=2788 +players= + +[Randell Wolfe.json] +name=Randell Wolfe +turns=300 +players= "mybot" "SimpleBot 2 (check if python is correct before executing)" + +[spiral2.json] +name=spiral2 +turns=537 +players= + +[standard_game.json] +name=standard_game +turns=82 +players="bot_auto_player_full_rand" "adversary_auto_player_orfirst_rand" diff --git a/backend/src/main.rs b/backend/src/main.rs index 3076ed2..f26caa5 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -18,6 +18,8 @@ extern crate tracing_subscriber; extern crate rocket; extern crate rocket_contrib; +extern crate ini; + use tracing_subscriber::{EnvFilter, FmtSubscriber}; use std::net::SocketAddr; diff --git a/backend/src/routes.rs b/backend/src/routes.rs index 066420e..95d6876 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -23,7 +23,7 @@ async fn files(file: PathBuf) -> Option { #[get("/")] async fn index() -> Template { // let context = context(); - let context = Context::new("Home", None); + let context = Context::new("Home"); // context.insert("name".to_string(), "Arthur".to_string()); Template::render("index", &context) } @@ -54,25 +54,20 @@ async fn map_post(map_req: Json) -> Result { #[get("/lobby")] async fn maps_get() -> Result { let maps = get_maps().await?; - let context = Context::new("Lobby", Some(maps)); + let context = Context::new_with("Lobby", ContextT::Maps(maps)); Ok(Template::render("lobby", &context)) } #[get("/mapbuilder")] async fn builder_get() -> Result { - let context = Context::new("Map Builder", None); + let context = Context::new("Map Builder"); Ok(Template::render("mapbuilder", &context)) } -#[get("/frontend/index.html")] -async fn visualizer_get() -> Result { - let context = Context::new("Visualizer", None); - Ok(Template::render("visualizer", &context)) -} - #[get("/visualizer")] -async fn visualizer_get_bis() -> Result { - let context = Context::new("Visualizer", None); +async fn visualizer_get() -> Result { + let game_options = get_games().await?; + let context = Context::new_with("Visualizer", ContextT::Games(game_options)); Ok(Template::render("visualizer", &context)) } @@ -86,5 +81,5 @@ async fn map_get(file: String) -> Result { } pub fn fuel(routes: &mut Vec) { - routes.extend(routes![files, index, map_post, map_get, maps_get, builder_get, visualizer_get, visualizer_get_bis]); + routes.extend(routes![files, index, map_post, map_get, maps_get, builder_get, visualizer_get]); } diff --git a/backend/src/util.rs b/backend/src/util.rs index ce961ce..d360d4c 100644 --- a/backend/src/util.rs +++ b/backend/src/util.rs @@ -1,7 +1,7 @@ use async_std::prelude::*; use async_std::fs; -static NAV: [(&'static str, &'static str); 4] = [("/", "Home"), ("/lobby", "Lobby"), ("/mapbuilder", "Map Builder"), ("/frontend/index.html", "Visualizer")]; +static NAV: [(&'static str, &'static str); 4] = [("/", "Home"), ("/lobby", "Lobby"), ("/mapbuilder", "Map Builder"), ("/visualizer", "Visualizer")]; #[derive(Serialize)] pub struct Map { @@ -9,6 +9,22 @@ pub struct Map { url: String, } +#[derive(Serialize)] +pub struct GameOption { + name: String, + location: String, + turns: u64, +} +impl GameOption { + pub fn new(name: &str, location: &str, turns: u64) -> Self { + Self { + name: name.to_string(), + location: location.to_string(), + turns + } + } +} + #[derive(Serialize)] struct Link { name: String, @@ -16,19 +32,36 @@ struct Link { active: bool, } +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub enum ContextT { + Maps(Vec), + Games(Vec), +} + #[derive(Serialize)] pub struct Context { pub name: String, nav: Vec, - pub maps: Option>, + + #[serde(flatten)] + pub inner: Option, } impl Context { - pub fn new(active: &str, maps: Option>) -> Self { + pub fn new_with(active: &str, inner: ContextT) -> Self { let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect(); Context { - nav, name: String::from(""), maps + nav, name: String::from(""), inner: Some(inner) + } + } + + pub fn new(active: &str) -> Self { + let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect(); + + Context { + nav, name: String::from(""), inner: None, } } } @@ -45,3 +78,22 @@ pub async fn get_maps() -> Result, String> { Ok(maps) } + +use ini::Ini; +pub async fn get_games() -> Result, String> { + let mut games = Vec::new(); + + let content = fs::read_to_string("games.ini").await.map_err(|_| "IO error".to_string())?; + + let i = Ini::load_from_str(&content).map_err(|_| "Corrupt ini file".to_string())?; + + for (sec, prop) in i.iter() { + if let Some(sec) = sec { + let name = match prop.get("name") { None => continue, Some(v) => v}; + let turns = match prop.get("turns").and_then(|v| v.parse().ok()) { None => continue, Some(v) => v}; + games.push(GameOption::new(name, sec, turns)); + } + } + + Ok(games) +} diff --git a/backend/static/test.html b/backend/static/test.html deleted file mode 100644 index 408e9ea..0000000 --- a/backend/static/test.html +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - Document - - - -
-
-
-

Start new game

-
- - -
- -
-
map
- -
hex_up
- -
spiral2
- -
spiral
- -
hungergames
- -
hex
- -
test_map
- -
pietmap
- -
large
-
-
- - -
-
- - -
- -
- -
-
-
- - - - - \ No newline at end of file diff --git a/backend/templates/visualizer.html.tera b/backend/templates/visualizer.html.tera index 938fbb0..7ed2e83 100644 --- a/backend/templates/visualizer.html.tera +++ b/backend/templates/visualizer.html.tera @@ -4,66 +4,45 @@ -
+
-
+
-
+
0 / 0 -
-
- Ms per frame:  - -
-
- -
+
+
+ Ms per frame:  + +
+
+ +
-
- -
-
- Option three -
-
- Option three -
- Option three -
-
- Option three -
-
- Option three -
-
- Option three -
-
- Option three -
-
- Option three -
- Option three -
-
- Option three -
-
+
+ {% for game in games %} +
+

{{game.name}}

+

Turns: {{game.turns}}

+
+ {# {% else %} +
+ No options available +
#} + {% endfor %} +
+
- + + -