aboutsummaryrefslogtreecommitdiff
path: root/server/tools/src/diagram_dot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools/src/diagram_dot.rs')
-rw-r--r--server/tools/src/diagram_dot.rs57
1 files changed, 57 insertions, 0 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(),
+ }
+}