Crate nightrunner_lib

Source
Expand description

This library is a text-adventure game engine that can be used to create text based adventure games. It is designed to be used with a front-end which can be written in any language. Implementing this library in a language is a matter of writing a front-end an passing string data to the library for parsing.

The configuration of the game is done in the Config struct and can be initialized both with YAML files and serialized JSON data, so it is perfect for both web and desktop games.

The parse_input and parse_input_json functions are the only functions that need to be called by the front-end, but the library exposes some of the internal structs and functions to help developers understand how the library works, and to allow a little bit of flexibility in how the library is used.

§Example:

use nightrunner_lib::{NightRunner, NightRunnerBuilder, ParsingResult};
use nightrunner_lib::util::test_helpers::mock_json_data;
let data = mock_json_data();
let mut nr = NightRunnerBuilder::new().with_json_data(&data).build();
let result = nr.parse_input("look");
let json_result = nr.json_parse_input("look");
assert!(result.is_ok());
assert_eq!(result.unwrap(),
    ParsingResult::Look(
        "first room\n\nHere you see: \nan item1\nan item2\nsubject1".to_string()
    )
);
assert_eq!(json_result,
    r#"{"messageType":"look","data":"first room\n\nHere you see: \nan item1\nan item2\nsubject1"}"#.to_string()
);

for examples of valid YAML and JSON data, see the documentation for the config module.

Modules§

config
Module containing the configuration code for this library.
parser
The parser module contains a single function that parses the input string and returns a ParsingResult.
util
Helper functions.

Structs§

NightRunner
This is the main struct for this library and represents the game. It holds the state internally and passes it to the parser for processing along with the provided input.
NightRunnerBuilder
You can use this to build a NightRunner strut. While you can build the NightRunner struct directly, using this builder is a little more convenient.

Enums§

JsMessage
When compiling for the web, this struct is used to serialize the game state to JSON. messageType is the type of the message returned by the library to indicate which action was processed by the parser.
ParsingResult
This is the result of the parsing of the input. Each variant contains the output for the game and should be used by a front-end to display to the user.

Type Aliases§

NRResult
We use a type alias to make error handling easier.