♻️ redo everything to get rid of the dependency injection.
It wasn't gonna work out the way I hoped.main
parent
574300cdad
commit
2a171a8e27
260
src/main.rs
260
src/main.rs
|
@ -46,52 +46,26 @@ fn read_input () -> Result <String> {
|
||||||
Ok (buffer)
|
Ok (buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Io {
|
fn print_help () -> Response {
|
||||||
fn print (&mut self, s: &str);
|
Response::PrintMany (vec! [
|
||||||
fn read_input (&mut self) -> Result <String>;
|
|
||||||
fn sleep (&mut self, milliseconds: u32);
|
|
||||||
|
|
||||||
fn print_many <'a, I: IntoIterator <Item=&'a str>> (&mut self, lines: I) {
|
|
||||||
for line in lines {
|
|
||||||
self.print (line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive (Default)]
|
|
||||||
struct Stdio {}
|
|
||||||
|
|
||||||
impl Io for Stdio {
|
|
||||||
fn print (&mut self, s: &str) {
|
|
||||||
print (s)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_input (&mut self) -> Result <String> {
|
|
||||||
read_input ()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sleep (&mut self, milliseconds: u32) {
|
|
||||||
use std::{
|
|
||||||
thread::sleep,
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
sleep (Duration::from_millis (milliseconds.into ()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_help <I: Io> (io: &mut I) {
|
|
||||||
io.print_many ([
|
|
||||||
"All commands are ASCII and case-insensitive.",
|
"All commands are ASCII and case-insensitive.",
|
||||||
"Commands should start with a verb like LOOK.",
|
"Commands should start with a verb like LOOK.",
|
||||||
"e.g. `look table`",
|
"e.g. `look table`",
|
||||||
"Single-word verbs are better, e.g. prefer `hint` over `give me a hint`",
|
"Single-word verbs are better, e.g. prefer `hint` over `give me a hint`",
|
||||||
"When in doubt, try generic verbs like `look` or `use` over specific verbs like `actuate`, `type`, or `consolidate`.",
|
"When in doubt, try generic verbs like `look` or `use` over specific verbs like `actuate`, `type`, or `consolidate`.",
|
||||||
]);
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_undetected_item <I: Io> (io: &mut I) {
|
fn line_response <S: Into <String>> (line: S) -> Response {
|
||||||
io.print ("That ITEM does not exist in this ROOM, or you have not detected it.");
|
Response::Print (line.into ())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn undetected_item () -> Response {
|
||||||
|
line_response ("That ITEM does not exist in this ROOM, or you have not detected it.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn just <T> (t: T) -> Vec <T> {
|
||||||
|
vec! [t]
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug, PartialEq)]
|
#[derive (Debug, PartialEq)]
|
||||||
|
@ -211,61 +185,74 @@ struct StateRoom1 {
|
||||||
detected_note: bool,
|
detected_note: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Response {
|
||||||
|
Print (String),
|
||||||
|
PrintMany (Vec <&'static str>),
|
||||||
|
Sleep (u32),
|
||||||
|
Quit,
|
||||||
|
JokeEnding,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive (Clone, Default)]
|
#[derive (Clone, Default)]
|
||||||
struct State {
|
struct State {
|
||||||
quitting: bool,
|
|
||||||
room_1: StateRoom1,
|
room_1: StateRoom1,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn room_1 <I: Io> (io: &mut I, state: &mut State) -> Result <()> {
|
impl State {
|
||||||
let input = io.read_input ()?;
|
fn room_1 (&mut self, input: String) -> Result <Vec <Response>> {
|
||||||
|
use Response::*;
|
||||||
|
|
||||||
let action = parse_input (&input);
|
let action = parse_input (&input);
|
||||||
|
|
||||||
match action {
|
Ok (match action {
|
||||||
PlayerAction::Quit => {
|
PlayerAction::Quit => {
|
||||||
io.print ("Bye.");
|
vec! [
|
||||||
state.quitting = true;
|
line_response ("Bye."),
|
||||||
|
Quit,
|
||||||
|
]
|
||||||
}
|
}
|
||||||
PlayerAction::Help => {
|
PlayerAction::Help => {
|
||||||
print_help (io);
|
just (print_help ())
|
||||||
},
|
},
|
||||||
PlayerAction::Hint => {
|
PlayerAction::Hint => {
|
||||||
io.print ("Hint for this room: Try using the `help` command.");
|
just (line_response ("Hint for this room: Try using the `help` command."))
|
||||||
},
|
},
|
||||||
PlayerAction::Nonsense => {
|
PlayerAction::Nonsense => {
|
||||||
io.print ("I couldn't understand that. Try `help` or `hint`.");
|
vec! [
|
||||||
io.print ("`hint` may contain spoilers. `help` will not.");
|
line_response ("I couldn't understand that. Try `help` or `hint`."),
|
||||||
|
line_response ("`hint` may contain spoilers. `help` will not."),
|
||||||
|
]
|
||||||
},
|
},
|
||||||
PlayerAction::Wait => {
|
PlayerAction::Wait => {
|
||||||
io.print ("You wait around a bit. You can hear humming from the electrical lights, and the distant rumble of the building's HVAC system. The room smells faintly of fresh paint. Nothing has changed.");
|
just (line_response ("You wait around a bit. You can hear humming from the electrical lights, and the distant rumble of the building's HVAC system. The room smells faintly of fresh paint. Nothing has changed."))
|
||||||
},
|
},
|
||||||
PlayerAction::LookAround => {
|
PlayerAction::LookAround => {
|
||||||
io.print ("You are in a small room. In one corner is a TABLE. Obvious exits are a locked DOOR, and an EMERGENCY EXIT.");
|
just (line_response ("You are in a small room. In one corner is a TABLE. Obvious exits are a locked DOOR, and an EMERGENCY EXIT."))
|
||||||
}
|
}
|
||||||
PlayerAction::Look (item_name) => {
|
PlayerAction::Look (item_name) => {
|
||||||
match item_name {
|
match item_name {
|
||||||
ItemName::Door => {
|
ItemName::Door => {
|
||||||
io.print ("You examine the DOOR. It is firmly locked, and you don't have any lock-picking tools. On the DOOR is an electronic KEYPAD.");
|
self.room_1.detected_keypad = true;
|
||||||
state.room_1.detected_keypad = true;
|
just (line_response ("You examine the DOOR. It is firmly locked, and you don't have any lock-picking tools. On the DOOR is an electronic KEYPAD."))
|
||||||
},
|
},
|
||||||
ItemName::EmergencyExit => {
|
ItemName::EmergencyExit => {
|
||||||
io.print ("The EMERGENCY EXIT reads, \"Emergency exit. Push bar to open. Alarm will sound. Door will unlock in 10 seconds.\". The EMERGENCY EXIT is period-accurate for an American Wal-Mart c. 2020 C.E.");
|
just (line_response ("The EMERGENCY EXIT reads, \"Emergency exit. Push bar to open. Alarm will sound. Door will unlock in 10 seconds.\". The EMERGENCY EXIT is period-accurate for an American Wal-Mart c. 2020 C.E."))
|
||||||
},
|
},
|
||||||
ItemName::Keypad => {
|
ItemName::Keypad => {
|
||||||
if ! state.room_1.detected_keypad {
|
if ! self.room_1.detected_keypad {
|
||||||
print_undetected_item (io);
|
return Ok (just (undetected_item ()));
|
||||||
return Ok (());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
io.print ("The DOOR is locked by an electronic KEYPAD. A soft amber power light indicates that the KEYPAD is likely functional. The KEYPAD buttons are the digits 0-9, Enter, and Clear. Experience tells you that the key code is likely 4 or 5 digits long.");
|
just (line_response ("The DOOR is locked by an electronic KEYPAD. A soft amber power light indicates that the KEYPAD is likely functional. The KEYPAD buttons are the digits 0-9, Enter, and Clear. Experience tells you that the key code is likely 4 or 5 digits long."))
|
||||||
},
|
},
|
||||||
ItemName::Note => {
|
ItemName::Note => {
|
||||||
if ! state.room_1.detected_note {
|
if ! self.room_1.detected_note {
|
||||||
print_undetected_item (io);
|
return Ok (vec! [
|
||||||
return Ok (());
|
undetected_item (),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
io.print_many ([
|
just (Response::PrintMany (vec! [
|
||||||
"You pick up the NOTE and read it.",
|
"You pick up the NOTE and read it.",
|
||||||
"",
|
"",
|
||||||
"Welcome to SEROTONIN DEPOSITORY.",
|
"Welcome to SEROTONIN DEPOSITORY.",
|
||||||
|
@ -280,76 +267,138 @@ fn room_1 <I: Io> (io: &mut I, state: &mut State) -> Result <()> {
|
||||||
" -- Phayle Sayf",
|
" -- Phayle Sayf",
|
||||||
"",
|
"",
|
||||||
"You notice that the NOTE is _not_ period-accurate.",
|
"You notice that the NOTE is _not_ period-accurate.",
|
||||||
])
|
]))
|
||||||
},
|
},
|
||||||
ItemName::Table => {
|
ItemName::Table => {
|
||||||
io.print ("You look at the TABLE. Your instincts tell you that it is period-accurate. Upon the TABLE sits a NOTE.");
|
self.room_1.detected_note = true;
|
||||||
state.room_1.detected_note = true;
|
vec! [
|
||||||
|
Response::Print ("You look at the TABLE. Your instincts tell you that it is period-accurate. Upon the TABLE sits a NOTE.".into ()),
|
||||||
|
]
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
print_undetected_item (io);
|
vec! [
|
||||||
|
undetected_item (),
|
||||||
|
]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PlayerAction::Use (item_name) => {
|
PlayerAction::Use (item_name) => {
|
||||||
match item_name {
|
match item_name {
|
||||||
ItemName::Door => {
|
ItemName::Door => {
|
||||||
io.print ("You can't USE the DOOR, it is locked.");
|
let mut output = vec! [
|
||||||
if ! state.room_1.detected_keypad {
|
line_response ("You can't USE the DOOR, it is locked."),
|
||||||
io.print ("You notice an electronic KEYPAD on the DOOR.");
|
];
|
||||||
state.room_1.detected_keypad = true;
|
|
||||||
|
if ! self.room_1.detected_keypad {
|
||||||
|
self.room_1.detected_keypad = true;
|
||||||
|
output.push (Response::Print ("You notice an electronic KEYPAD on the DOOR.".into ()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output
|
||||||
},
|
},
|
||||||
ItemName ::EmergencyExit => {
|
ItemName::EmergencyExit => {
|
||||||
io.print ("You push on the emergency exit. An alarm starts sounding. Your ADVENTURE GAME ENTHUSIAST friend is going to be very mad at you.");
|
vec! [
|
||||||
io.sleep (5000);
|
Response::Print ("You push on the emergency exit. An alarm starts sounding. Your ADVENTURE GAME ENTHUSIAST friend is going to be very mad at you.".into ()),
|
||||||
io.print ("The alarm is still sounding. You are getting embarrassed, but you have committed to this path of action.");
|
Response::Sleep (5000),
|
||||||
io.sleep (5000);
|
Response::Print ("The alarm is still sounding. You are getting embarrassed, but you have committed to this path of action.".into ()),
|
||||||
io.print ("The emergency exit unlocks, and you walk out of the game. Bye.");
|
Response::Sleep (5000),
|
||||||
state.quitting = true;
|
Response::Print ("The emergency exit unlocks, and you walk out of the game. Bye.".into ()),
|
||||||
|
Response::JokeEnding,
|
||||||
|
Response::Quit,
|
||||||
|
]
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
print_undetected_item (io);
|
vec! [
|
||||||
|
undetected_item (),
|
||||||
|
]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn game () -> Result <()> {
|
||||||
|
for line in [
|
||||||
|
"Welcome to SEROTONIN DEPOSITORY, the only adventure game ever made.",
|
||||||
|
"",
|
||||||
|
"You have been consensually kidnapped by a diabolical ADVENTURE GAME ENTHUSIAST and encouraged to solve PUZZLES for their sick PLEASURE. The only winning move is to solve all the PUZZLES.",
|
||||||
|
"",
|
||||||
|
"Press ENTER if you dare to begin.",
|
||||||
|
] {
|
||||||
|
print (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
let input = read_input ()?;
|
||||||
|
|
||||||
|
if ! input.is_empty () {
|
||||||
|
print ("That was more than just ENTER but OKAY, overachiever.");
|
||||||
|
}
|
||||||
|
|
||||||
|
print ("");
|
||||||
|
|
||||||
|
let mut state = State::default ();
|
||||||
|
|
||||||
|
print ("You are in a small room. In one corner is a TABLE.");
|
||||||
|
|
||||||
|
'main_loop: loop {
|
||||||
|
let input = read_input ()?;
|
||||||
|
let responses = state.room_1 (input)?;
|
||||||
|
|
||||||
|
for response in responses.into_iter () {
|
||||||
|
match response {
|
||||||
|
Response::Print (line) => print (&line),
|
||||||
|
Response::PrintMany (lines) => {
|
||||||
|
for line in lines {
|
||||||
|
print (line);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Response::Sleep (x) => std::thread::sleep (std::time::Duration::from_millis (x.into ())),
|
||||||
|
Response::Quit => break 'main_loop,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok (())
|
Ok (())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main () -> Result <()> {
|
fn main () -> Result <()> {
|
||||||
let mut io = Stdio::default ();
|
game ()?;
|
||||||
|
|
||||||
io.print_many ([
|
|
||||||
"Welcome to SEROTONIN DEPOSITORY, the only adventure game ever made.",
|
|
||||||
"",
|
|
||||||
"You have been consensually kidnapped by a diabolical ADVENTURE GAME ENTHUSIAST and encouraged to solve PUZZLES for their sick PLEASURE. The only winning move is to solve all the PUZZLES.",
|
|
||||||
"",
|
|
||||||
"Press ENTER if you dare to begin.",
|
|
||||||
]);
|
|
||||||
|
|
||||||
let input = io.read_input ()?;
|
|
||||||
|
|
||||||
if ! input.is_empty () {
|
|
||||||
io.print ("That was more than just ENTER but OKAY, overachiever.");
|
|
||||||
}
|
|
||||||
|
|
||||||
io.print ("");
|
|
||||||
|
|
||||||
let mut state = State::default ();
|
|
||||||
|
|
||||||
io.print ("You are in a small room. In one corner is a TABLE.");
|
|
||||||
|
|
||||||
while ! state.quitting {
|
|
||||||
room_1 (&mut io, &mut state)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok (())
|
Ok (())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg (test)]
|
#[cfg (test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use super::{
|
||||||
|
Io,
|
||||||
|
GameError,
|
||||||
|
Result,
|
||||||
|
State,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TestIo {
|
||||||
|
inputs: Vec <String>,
|
||||||
|
input_idx: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Io for TestIo {
|
||||||
|
fn print (&mut self, _s: &str) {}
|
||||||
|
|
||||||
|
fn read_input (&mut self) -> Result <String> {
|
||||||
|
if self.input_idx >= self.inputs.len () {
|
||||||
|
return Err (GameError::IoError);
|
||||||
|
}
|
||||||
|
|
||||||
|
let s = self.inputs [self.input_idx].clone ();
|
||||||
|
self.input_idx += 1;
|
||||||
|
Ok (s)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sleep (&mut self, _milliseconds: u32) {}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_input () {
|
fn parse_input () {
|
||||||
use super::{
|
use super::{
|
||||||
|
@ -371,4 +420,9 @@ mod test {
|
||||||
assert_eq! (actual, expected);
|
assert_eq! (actual, expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn joke_ending () {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue