add debug station
This commit is contained in:
parent
7c2896ddb7
commit
e9f79c6d59
4 changed files with 121 additions and 6 deletions
|
@ -23,6 +23,7 @@ pub struct PlanetWarsGame {
|
||||||
log_file: File,
|
log_file: File,
|
||||||
turns: u64,
|
turns: u64,
|
||||||
name: String,
|
name: String,
|
||||||
|
logged: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlanetWarsGame {
|
impl PlanetWarsGame {
|
||||||
|
@ -41,6 +42,7 @@ impl PlanetWarsGame {
|
||||||
log_file: file,
|
log_file: file,
|
||||||
turns: 0,
|
turns: 0,
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
|
logged: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,15 +194,20 @@ impl game::Controller for PlanetWarsGame {
|
||||||
|
|
||||||
fn is_done(&mut self) -> bool {
|
fn is_done(&mut self) -> bool {
|
||||||
if self.state.is_finished() {
|
if self.state.is_finished() {
|
||||||
|
if !self.logged {
|
||||||
|
|
||||||
let mut f = match OpenOptions::new().create(true).append(true).open("games.ini") { Err(_) => return true, Ok(f) => f };
|
|
||||||
|
|
||||||
let mut conf = Ini::new();
|
let mut f = match OpenOptions::new().create(true).append(true).open("games.ini") { Err(_) => return true, Ok(f) => f };
|
||||||
conf.with_section(Some(self.log_file_loc.clone()))
|
|
||||||
|
let mut conf = Ini::new();
|
||||||
|
conf.with_section(Some(self.log_file_loc.clone()))
|
||||||
.set("name", &self.name)
|
.set("name", &self.name)
|
||||||
.set("turns", format!("{}", self.turns));
|
.set("turns", format!("{}", self.turns));
|
||||||
|
|
||||||
conf.write_to(&mut f).unwrap();
|
conf.write_to(&mut f).unwrap();
|
||||||
|
|
||||||
|
self.logged = true;
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -65,6 +65,12 @@ async fn builder_get() -> Result<Template, String> {
|
||||||
Ok(Template::render("mapbuilder", &context))
|
Ok(Template::render("mapbuilder", &context))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/debug")]
|
||||||
|
async fn debug_get() -> Result<Template, String> {
|
||||||
|
let context = Context::new("Debug Station");
|
||||||
|
Ok(Template::render("debug", &context))
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/visualizer")]
|
#[get("/visualizer")]
|
||||||
async fn visualizer_get() -> Result<Template, String> {
|
async fn visualizer_get() -> Result<Template, String> {
|
||||||
let game_options = get_games().await?;
|
let game_options = get_games().await?;
|
||||||
|
@ -107,7 +113,7 @@ async fn game_post(game_req: Json<GameReq>, tp: State<'_, ThreadPool>, gm: State
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fuel(routes: &mut Vec<Route>) {
|
pub fn fuel(routes: &mut Vec<Route>) {
|
||||||
routes.extend(routes![files, index, map_post, map_get, lobby_get, builder_get, visualizer_get, game_post, state_get]);
|
routes.extend(routes![files, index, map_post, map_get, lobby_get, builder_get, visualizer_get, game_post, state_get, debug_get]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use async_std::prelude::*;
|
use async_std::prelude::*;
|
||||||
use async_std::fs;
|
use async_std::fs;
|
||||||
|
|
||||||
static NAV: [(&'static str, &'static str); 4] = [("/", "Home"), ("/lobby", "Lobby"), ("/mapbuilder", "Map Builder"), ("/visualizer", "Visualizer")];
|
static NAV: [(&'static str, &'static str); 5] = [("/", "Home"), ("/lobby", "Lobby"), ("/mapbuilder", "Map Builder"), ("/visualizer", "Visualizer"), ("/debug", "Debug Station")];
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
|
|
102
backend/templates/debug.html.tera
Normal file
102
backend/templates/debug.html.tera
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
{% extends "base" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
|
||||||
|
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
font: 10pt arial;
|
||||||
|
background-color: #222;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#mynetwork {
|
||||||
|
border: 1px solid lightgray;
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div style="width: 80vw; height: 80vh; margin: auto;" id="mynetwork"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var data = new vis.DataSet();
|
||||||
|
|
||||||
|
var nodes = new vis.DataSet();
|
||||||
|
var edges = new vis.DataSet();
|
||||||
|
|
||||||
|
var colour_cache = {};
|
||||||
|
|
||||||
|
function getColor() {
|
||||||
|
return "hsl(" + 360 * Math.random() + ',' +
|
||||||
|
(100 * Math.random()) + '%,' +
|
||||||
|
(60 + 20 * Math.random()) + '%)';
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_color(nodes) {
|
||||||
|
nodes = Array.isArray(nodes) ? nodes : [nodes];
|
||||||
|
for (let n of nodes) {
|
||||||
|
if (colour_cache[n.label] == undefined) {
|
||||||
|
colour_cache[n.label] = getColor();
|
||||||
|
}
|
||||||
|
n.color = colour_cache[n.label];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_event(event) {
|
||||||
|
if (event.type === "Init") {
|
||||||
|
nodes.clear();
|
||||||
|
edges.clear();
|
||||||
|
set_color(event.nodes);
|
||||||
|
|
||||||
|
nodes.add(event.nodes);
|
||||||
|
|
||||||
|
setTimeout(
|
||||||
|
() => {
|
||||||
|
edges.add(event.edges);
|
||||||
|
}, 200
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
const at = event.data_type === "Node" ? nodes : edges;
|
||||||
|
|
||||||
|
|
||||||
|
if (event.type === "Add") {
|
||||||
|
if (event.data_type === "Node") {
|
||||||
|
set_color(event.data);
|
||||||
|
}
|
||||||
|
at.add(event.data);
|
||||||
|
} else {
|
||||||
|
at.remove(event.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw() {
|
||||||
|
const container = document.getElementById('mynetwork');
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
nodes: nodes,
|
||||||
|
edges: edges
|
||||||
|
};
|
||||||
|
const options = {
|
||||||
|
physics: {
|
||||||
|
stabilization: {
|
||||||
|
enabled: true,
|
||||||
|
iterations: 10, // maximum number of iteration to stabilize
|
||||||
|
updateInterval: 10,
|
||||||
|
fit: true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const network = new vis.Network(container, data, options);
|
||||||
|
|
||||||
|
var ws = new WebSocket('ws://127.0.0.1:3012');
|
||||||
|
ws.onmessage = function (event) {
|
||||||
|
handle_event(JSON.parse(event.data));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
window.onload = () => draw();
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
Loading…
Reference in a new issue