read games from ini in rocket

This commit is contained in:
ajuvercr 2020-03-27 17:32:18 +01:00
parent e7823f9135
commit b650f8d95a
8 changed files with 215 additions and 123 deletions

View file

@ -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"

11
backend/Rocket.toml Normal file
View file

@ -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

111
backend/games.ini Normal file
View file

@ -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"

View file

@ -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;

View file

@ -23,7 +23,7 @@ async fn files(file: PathBuf) -> Option<NamedFile> {
#[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<MapReq>) -> Result<String, String> {
#[get("/lobby")]
async fn maps_get() -> Result<Template, String> {
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<Template, String> {
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<Template, String> {
let context = Context::new("Visualizer", None);
Ok(Template::render("visualizer", &context))
}
#[get("/visualizer")]
async fn visualizer_get_bis() -> Result<Template, String> {
let context = Context::new("Visualizer", None);
async fn visualizer_get() -> Result<Template, String> {
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<Template, String> {
}
pub fn fuel(routes: &mut Vec<Route>) {
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]);
}

View file

@ -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<Map>),
Games(Vec<GameOption>),
}
#[derive(Serialize)]
pub struct Context {
pub name: String,
nav: Vec<Link>,
pub maps: Option<Vec<Map>>,
#[serde(flatten)]
pub inner: Option<ContextT>,
}
impl Context {
pub fn new(active: &str, maps: Option<Vec<Map>>) -> 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<Vec<Map>, String> {
Ok(maps)
}
use ini::Ini;
pub async fn get_games() -> Result<Vec<GameOption>, 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)
}

View file

@ -1,60 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style/style.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="main">
<div class="lobby"></div>
<div class="creator">
<h1>Start new game</h1>
<div class="input_container">
<label for="name">Name:</label>
<input id="name" type="text"></input>
</div>
<label>Select Map</label>
<div class="map_select">
<div class="map" onclick="handle_map_click('maps/map.json', event)"> map </div>
<div class="map" onclick="handle_map_click('maps/hex_up.json', event)"> hex_up </div>
<div class="map" onclick="handle_map_click('maps/spiral2.json', event)"> spiral2 </div>
<div class="map" onclick="handle_map_click('maps/spiral.json', event)"> spiral </div>
<div class="map" onclick="handle_map_click('maps/hungergames.json', event)"> hungergames </div>
<div class="map" onclick="handle_map_click('maps/hex.json', event)"> hex </div>
<div class="map" onclick="handle_map_click('maps/test_map.json', event)"> test_map </div>
<div class="map" onclick="handle_map_click('maps/pietmap.json', event)"> pietmap </div>
<div class="map" onclick="handle_map_click('maps/large.json', event)"> large </div>
</div>
<div class="input_container number">
<label for="turns">Max turns:</label>
<input id="turns" type="number" value=200></input>
</div>
<div class="input_container number">
<label for="nop">Number of players:</label>
<input id="nop" type="number" value=2></input>
</div>
<button>
Start
</button>
<div id="map_holder">
</div>
</div>
</div>
<script src="script/script.js"></script>
</body>
</html>

View file

@ -25,35 +25,17 @@
</div>
</div>
<div id=options>
<div class="option">
Option three
<div class="options">
{% for game in games %}
<div class="option" onclick="visualizer.handle('/games/{{ game.location}}', '{{ game.name }}')">
<p>{{game.name}}</p>
<p>Turns: {{game.turns}}</p>
</div>
{# {% else %}
<div class="option">
Option three
</div><div class="option">
Option three
</div>
<div class="option">
Option three
</div>
<div class="option">
Option three
</div>
<div class="option">
Option three
</div>
<div class="option">
Option three
</div>
<div class="option">
Option three
</div><div class="option">
Option three
</div>
<div class="option">
Option three
</div>
No options available
</div> #}
{% endfor %}
</div>
</div>
@ -63,7 +45,4 @@
console.log(window);
</script>
<script src="frontend/bootstrap.js"></script>
<button onclick='visualizer.handle("static/games/spiral2.json", "spiral2")'>
HANDLE
</button
{% endblock %}