pub fn init(config: Config) -> Rc<RefCell<Self>> {
        let items = &config.items;
        let mut rooms = config.clone().rooms;
        for room in &mut rooms {
            for room_item_id in &mut room.stash.item_ids {
                if items.iter().any(|i| i.id == *room_item_id) {
                    room.stash.items.push(
                        items
                            .iter()
                            .find(|i| i.id == *room_item_id)
                            .unwrap()
                            .to_owned(),
                    );
                }
            }
        }
        let state = Self {
            input: String::new(),
            current_room: 1,
            player: Player {
                inventory: Storage {
                    items: vec![],
                    item_ids: vec![],
                },
            },
            rooms,
            config,
        };
        Rc::new(RefCell::new(state))
    }
/// Takes a config struct and populates the state struct.
///
/// ## Example:
/// ```rust
/// # use nightrunner_lib::config::{Config, State};
/// # use nightrunner_lib::util::test_helpers::mock_json_data;
/// # let json_data = mock_json_data();
/// // Using yaml config files from a path
/// let config1 = Config::from_path("./fixtures/");
/// let state1 = State::init(config1);
/// // or using JSON data from a front-end
/// let config2 = Config::from_json(&json_data);
/// let state2 = State::init(config2);
/// ```
Takes a config struct and populates the state struct.
Takes a config struct and populates the state struct .
pub fn init(config: Config) -> Rc<RefCell<Self>> {
        let items = &config.items;
        let mut rooms = config.clone().rooms;
        for room in &mut rooms {
            for room_item_id in &mut room.stash.item_ids {
                if items.iter().any(|i| i.id == *room_item_id) {
                    room.stash.items.push(
                        items
                            .iter()
                            .find(|i| i.id == *room_item_id)
                            .unwrap()
                            .to_owned(),
                    );
                }
            }
        }
        let state = Self {
            input: String::new(),
            current_room: 1,
            player: Player {
                inventory: Storage {
                    items: vec![],
                    item_ids: vec![],
                },
            },
            rooms,
            config,
        };
        Rc::new(RefCell::new(state))
    }
pub fn init ( config : Config ) -> Rc < RefCell < Self > > { let items = & config . items ; let mut rooms = config . clone ( ) . rooms ; for room in & mut rooms { for room_item_id in & mut room . stash . item_ids { if items . iter ( ) . any ( | i | i . id == * room_item_id ) { room . stash . items . push ( items . iter ( ) . find ( | i | i . id == * room_item_id ) . unwrap ( ) . to_owned ( ) , ) ; } } } let state = Self { input : String :: new ( ) , current_room : 1 , player : Player { inventory : Storage { items : vec ! [ ] , item_ids : vec ! [ ] , } , } , rooms , config , } ; Rc :: new ( RefCell :: new ( state ) ) }
Takes a config struct and populates the state struct.
Takes a config struct and populates the state struct .
config
Config
config
Config
example:
None
use nightrunner_lib::config::{config, state};
None
use nightrunner_lib::util::test_helpers::mock_json_data;
None
let json_data = mock_json_data();
Using yaml config files from a path
let config1 = Config::from_path("./fixtures/");
let state1 = State::init(config1);
or using JSON data from a front-end
let config2 = Config::from_json(&json_data);
let state2 = State::init(config2);
```
Using yaml config files from a path let config1 = Config :: from_path ( " . / fixtures / " ) ; let state1 = State :: init ( config1 ) ; or using JSON data from a front - end let config2 = Config :: from_json ( &json_data ) ; let state2 = State :: init ( config2 ) ; ` ` `