diff options
author | metamuffin <metamuffin@disroot.org> | 2025-09-26 17:34:24 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-09-27 00:55:23 +0200 |
commit | cb434ae50fe7b0ad5df4b600da062623a272ba59 (patch) | |
tree | 71ed96fd72d5075199d42104e11b9491baf81e19 /server/tools | |
parent | fd7c0cf155760353b877c1f1551e872e1aca0401 (diff) | |
download | hurrycurry-cb434ae50fe7b0ad5df4b600da062623a272ba59.tar hurrycurry-cb434ae50fe7b0ad5df4b600da062623a272ba59.tar.bz2 hurrycurry-cb434ae50fe7b0ad5df4b600da062623a272ba59.tar.zst |
diagram dot output
Diffstat (limited to 'server/tools')
-rw-r--r-- | server/tools/src/diagram_dot.rs | 57 | ||||
-rw-r--r-- | server/tools/src/main.rs | 12 | ||||
-rw-r--r-- | server/tools/src/recipe_diagram.rs | 19 |
3 files changed, 86 insertions, 2 deletions
diff --git a/server/tools/src/diagram_dot.rs b/server/tools/src/diagram_dot.rs new file mode 100644 index 00000000..e78f831f --- /dev/null +++ b/server/tools/src/diagram_dot.rs @@ -0,0 +1,57 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ + +use anyhow::Result; +use hurrycurry_protocol::{Gamedata, Message, book::Diagram}; +use std::fmt::Write; + +pub fn diagram_dot(data: &Gamedata, diagram: &Diagram) -> Result<String> { + let mut out = String::new(); + writeln!(out, "digraph {{")?; + for (i, n) in diagram.nodes.iter().enumerate() { + let mut attrs = Vec::new(); + if let Some(color) = &n.color { + attrs.push(format!("style=filled")); + attrs.push(format!("fillcolor={color:?}")); + attrs.push(format!("color={color:?}")); + } + if let Some(shape) = &n.shape { + attrs.push(format!("shape={shape}")); + } + writeln!( + out, + "k{i} [label=\"{}\" {}]", + message_str(data, &n.label), + attrs.join(" ") + )?; + } + for edge in &diagram.edges { + writeln!(out, "k{} -> k{}", edge.src, edge.dst)?; + } + writeln!(out, "}}")?; + Ok(out) +} + +fn message_str(data: &Gamedata, message: &Message) -> String { + match message { + Message::Translation { .. } => todo!(), + Message::Text(x) => x.to_owned(), + Message::Item(item_index) => data.item_name(*item_index).to_string(), + Message::Tile(tile_index) => data.tile_name(*tile_index).to_string(), + } +} diff --git a/server/tools/src/main.rs b/server/tools/src/main.rs index 2836325e..96ecf46a 100644 --- a/server/tools/src/main.rs +++ b/server/tools/src/main.rs @@ -18,6 +18,7 @@ pub mod book; pub mod book_html; +pub mod diagram_dot; pub mod diagram_layout; pub mod graph; pub mod graph_summary; @@ -26,8 +27,10 @@ pub mod recipe_diagram; use crate::{ book::{book, print_book}, book_html::render_html_book, + diagram_dot::diagram_dot, graph::graph, graph_summary::graph_summary, + recipe_diagram::recipe_diagram, }; use anyhow::Result; use clap::Parser; @@ -37,6 +40,7 @@ use hurrycurry_server::data::DataIndex; enum Action { Graph, GraphSummary, + GraphSingle { out: String }, Book, BookHtml, MapDemands { map: String }, @@ -48,6 +52,14 @@ fn main() -> Result<()> { match action { Action::Graph => graph()?, Action::GraphSummary => graph_summary()?, + Action::GraphSingle { out } => { + let mut index = DataIndex::default(); + index.reload()?; + let (data, serverdata, _) = index.generate("5star")?; + let diagram = recipe_diagram(&data, &serverdata, &[out])?; + let dot = diagram_dot(&data, &diagram)?; + println!("{dot}"); + } Action::Book => { let mut index = DataIndex::default(); index.reload()?; diff --git a/server/tools/src/recipe_diagram.rs b/server/tools/src/recipe_diagram.rs index 7dc42c1e..453fedc6 100644 --- a/server/tools/src/recipe_diagram.rs +++ b/server/tools/src/recipe_diagram.rs @@ -18,7 +18,7 @@ use anyhow::Result; use hurrycurry_protocol::{ - Gamedata, ItemIndex, Message, RecipeIndex, + Gamedata, ItemIndex, Message, Recipe, RecipeIndex, book::{Diagram, DiagramEdge, DiagramNode}, glam::Vec2, }; @@ -84,16 +84,31 @@ pub(crate) fn recipe_diagram( diag.nodes.push(DiagramNode { label: Message::Item(i), position: Vec2::ZERO, + color: None, + shape: None, }); } for r in recipes { let index = diag.nodes.len(); + let recipe = data.recipe(r.index); + let (kind, color) = match recipe { + Recipe::Passive { .. } => ("Passive", "#c4a32b"), + Recipe::Active { .. } => ("Active", "#47c42b"), + Recipe::Instant { .. } => ("Instant", "#5452d8"), + }; diag.nodes.push(DiagramNode { position: Vec2::ZERO, - label: Message::Text("blub".to_string()), + label: Message::Text(if let Some(tile) = recipe.tile() { + format!("{kind} on {}", data.tile_name(tile)) + } else { + format!("{kind}") + }), + color: Some(color.to_string()), + shape: Some("box".to_string()), }); for i in r.inputs { + eprintln!("{}", data.item_name(i)); diag.edges.push(DiagramEdge { src: item_index[&i], dst: index, |