From c78665e4fd83a64a67a6747ec9429c74a3d4a466 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 20 Jun 2024 02:16:41 +0200 Subject: generate dot graph from recipes --- server/src/bin/graph.rs | 38 ++++++++++++++++++++++++++++++++++++++ server/src/data.rs | 4 ++-- server/src/interaction.rs | 20 ++++++++++++++++++++ server/src/lib.rs | 17 ++++++++++++++--- server/src/main.rs | 11 +++-------- 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 server/src/bin/graph.rs (limited to 'server/src') diff --git a/server/src/bin/graph.rs b/server/src/bin/graph.rs new file mode 100644 index 00000000..9ff6f973 --- /dev/null +++ b/server/src/bin/graph.rs @@ -0,0 +1,38 @@ +use undercooked::{ + interaction::Recipe, + load_gamedata, + protocol::{ItemIndex, RecipeIndex}, +}; + +fn main() { + let data = load_gamedata(); + + println!("digraph {{"); + + for i in 0..data.item_names.len() { + println!("i{i} [label=\"{}\"]", data.item_name(ItemIndex(i))) + } + for (RecipeIndex(ri), recipe) in data.recipes() { + println!( + "r{ri} [label=\"{}\\non {}\" shape=box color=gray fillcolor=gray style=filled]", + match recipe { + Recipe::Passive { .. } => "Passive", + Recipe::Active { .. } => "Active", + Recipe::Instant { .. } => "Instant", + }, + if let Some(tile) = recipe.tile() { + data.tile_name(tile) + } else { + "anything" + } + ); + for ItemIndex(input) in recipe.inputs() { + println!("i{input} -> r{ri}") + } + for ItemIndex(output) in recipe.outputs() { + println!("r{ri} -> i{output}") + } + } + + println!("}}"); +} diff --git a/server/src/data.rs b/server/src/data.rs index e59b8b35..fd268cb5 100644 --- a/server/src/data.rs +++ b/server/src/data.rs @@ -52,8 +52,8 @@ pub struct Demand { pub struct Gamedata { recipes: Vec, pub demands: Vec, - item_names: Vec, - tile_names: Vec, + pub item_names: Vec, + pub tile_names: Vec, #[serde(skip)] pub initial_map: HashMap, pub chef_spawn: Vec2, diff --git a/server/src/interaction.rs b/server/src/interaction.rs index a4b79bd0..93f54b2d 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -42,6 +42,26 @@ impl Recipe { _ => None, } } + pub fn inputs(&self) -> Vec { + match self { + Recipe::Passive { input, .. } => vec![*input], + Recipe::Active { input, .. } => vec![*input], + Recipe::Instant { inputs, .. } => { + inputs.into_iter().flat_map(|e| e.to_owned()).collect() + } + } + } + pub fn outputs(&self) -> Vec { + match self { + Recipe::Passive { output, .. } => output.to_owned().into_iter().collect(), + Recipe::Active { outputs, .. } => { + outputs.into_iter().flat_map(|e| e.to_owned()).collect() + } + Recipe::Instant { outputs, .. } => { + outputs.into_iter().flat_map(|e| e.to_owned()).collect() + } + } + } pub fn supports_tile(&self, tile: TileIndex) -> bool { if let Some(tile_constraint) = self.tile() { if tile != tile_constraint { diff --git a/server/src/lib.rs b/server/src/lib.rs index a326190d..2f677d7c 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,5 +1,16 @@ -pub mod game; -pub mod protocol; +use data::{build_gamedata, Gamedata}; +use std::fs::File; + +pub mod customer; pub mod data; +pub mod game; pub mod interaction; -pub mod customer; +pub mod protocol; + +pub fn load_gamedata() -> Gamedata { + build_gamedata( + serde_yaml::from_reader(File::open("data/recipes.yaml").unwrap()).unwrap(), + serde_yaml::from_reader(File::open("data/map.yaml").unwrap()).unwrap(), + serde_yaml::from_reader(File::open("data/demands.yaml").unwrap()).unwrap(), + ) +} diff --git a/server/src/main.rs b/server/src/main.rs index 3af71e17..0f0bac5e 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,7 +1,7 @@ use anyhow::Result; use futures_util::{SinkExt, StreamExt}; use log::{debug, info, warn}; -use std::{fs::File, sync::Arc, time::Duration}; +use std::{sync::Arc, time::Duration}; use tokio::{ io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, net::TcpListener, @@ -12,8 +12,8 @@ use tokio::{ use tokio_tungstenite::tungstenite::Message; use undercooked::{ customer::customer, - data::build_gamedata, game::Game, + load_gamedata, protocol::{PacketC, PacketS, PlayerID}, }; @@ -28,12 +28,7 @@ async fn main() -> Result<()> { ); info!("listening for websockets on {}", ws_listener.local_addr()?); - let data = build_gamedata( - serde_yaml::from_reader(File::open("data/recipes.yaml").unwrap()).unwrap(), - serde_yaml::from_reader(File::open("data/map.yaml").unwrap()).unwrap(), - serde_yaml::from_reader(File::open("data/demands.yaml").unwrap()).unwrap(), - ); - + let data = load_gamedata(); let game = Arc::new(RwLock::new(Game::new(data.into()))); let (tx, rx) = broadcast::channel::(1024); -- cgit v1.2.3-70-g09d2