aboutsummaryrefslogtreecommitdiff
path: root/examples/text-adventure/side_effects.rs
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2023-04-02 15:10:52 +0200
committerAndreas Grois <andi@grois.info>2023-04-02 15:10:52 +0200
commitcc9bbc434d6df6dc6b414547837bb74e2323d262 (patch)
tree0bd5021f49b83d9f851f32875bba0597e9da5020 /examples/text-adventure/side_effects.rs
parentc9880dd12df2c614360cbc85609d859f3652507f (diff)
Example project is now runnable.
Diffstat (limited to 'examples/text-adventure/side_effects.rs')
-rw-r--r--examples/text-adventure/side_effects.rs49
1 files changed, 48 insertions, 1 deletions
diff --git a/examples/text-adventure/side_effects.rs b/examples/text-adventure/side_effects.rs
index 51f1f4c..ed0f9e1 100644
--- a/examples/text-adventure/side_effects.rs
+++ b/examples/text-adventure/side_effects.rs
@@ -1 +1,48 @@
-//this module interprets the domain specific language. \ No newline at end of file
+//! this module interprets the domain specific language as a text adventure.
+
+use crate::dsl::FreeSausageRoll;
+
+pub fn run<'a, 's:'a>(mut game : FreeSausageRoll<'a, 's, ()>) -> std::io::Result<()>{
+ //this function doesn't know who it is, or why it is here. It only knows it must deal.
+ //Deal with the few commands in the eDSL and nothing more.
+
+ //This would be easier to write recursively. However, in an actual project this might run for quite some time.
+ //Since we operate on the stack, let's rather be safe than sorry, and use a loop instead of recursion therefore.
+ while let FreeSausageRoll::Free(command) = game {
+ game = match *command {
+ crate::dsl::SausageRoll::SayDialogueLine { speaker, text, mood, next } => {
+ println!("{} says: \"{}\" with {} on their face.", speaker.text_description(), text, mood.text_description());
+ next
+ },
+ crate::dsl::SausageRoll::GivePlayerOptions { options, next } =>
+ {
+ println!("Your options are:");
+ for (id, option) in options.iter().enumerate().map(|(i,o)| (i+1,o)){
+ println!("{}: {}", id, option);
+ }
+
+ let mut input = String::new();
+ let mut chosen;
+ while let None = {
+ input.clear();
+ std::io::stdin().read_line(&mut input)?;
+ chosen = input.trim().parse().ok().filter(|o : &usize| *o > 0 && *o <= options.len());
+ chosen
+ } {
+ println!("Invalid choice. Please select one of the options given above.");
+ }
+ println!();
+ next(chosen.unwrap()-1)
+ },
+ crate::dsl::SausageRoll::PresentLocation { location, next } => {
+ println!("{}", location.get_text_description());
+ next
+ },
+ crate::dsl::SausageRoll::Exposition { text, next } => {
+ println!("{}", text);
+ next
+ },
+ };
+ };
+ std::io::Result::Ok(())
+} \ No newline at end of file