pub fn parse_room_text(
    state: State,
    narrative_text: String,
    event_message: String,
    event_id: Option<u16>,
) -> NRResult<EventMessage> {
    let current_room = match state
        .config
        .rooms
        .iter()
        .find(|room| room.id == state.current_room)
    {
        Some(room) => room,
        None => return Err(InvalidRoom.into()),
    };
    let player_items = state
        .player
        .inventory
        .items
        .clone()
        .iter()
        .map(|item| item.name.clone())
        .collect::<Vec<String>>();
    let room_items = state
        .config
        .items
        .clone()
        .iter()
        .filter(|item| current_room.stash.item_ids.contains(&item.id))
        .map(|item| item.name.clone())
        .collect::<Vec<_>>();
    let room_subjects = state
        .config
        .subjects
        .clone()
        .iter()
        .filter(|subject| current_room.subjects.contains(&subject.id))
        .map(|subject| subject.name.clone())
        .collect::<Vec<_>>();
    let mut event_items = vec![];
    let event = state
        .config
        .events
        .iter()
        .find(|event| Some(event.id) == event_id);
    if let Some(event) = event {
        if let Some(item_id) = event.add_item {
            if let Some(item) = state.config.items.iter().find(|item| item.id == item_id) {
                event_items.push(item.name.clone())
            };
        }
        if let Some(item_id) = event.remove_item {
            if let Some(item) = state.config.items.iter().find(|item| item.id == item_id) {
                event_items.push(item.name.clone())
            };
        }
    };

    let exits_vec = current_room
        .exits
        .clone()
        .iter()
        .map(|exit| {
            match state
                .config
                .rooms
                .iter()
                .find(|room| room.id == exit.room_id)
            {
                Some(room) => format!(
                    "to the {} you see {}",
                    exit.direction.clone(),
                    room.description.clone()
                ),
                None => String::new(),
            }
        })
        .collect::<Vec<String>>();
    let exits_string = match exits_vec.len() {
        0 => String::new(),
        _ => String::from("Exits:\n") + &exits_vec.join("\n")[..],
    };
    let items_and_subjects = player_items
        .iter()
        .chain(room_items.iter())
        .chain(event_items.iter())
        .chain(room_subjects.iter())
        .cloned()
        .collect::<Vec<_>>();
    let (room_text, templated_words_room) =
        process_templated_text(narrative_text, &items_and_subjects);
    let (event_text, templated_words_event) =
        process_templated_text(event_message, &items_and_subjects);
    let mut message_parts = HashMap::new();
    message_parts.insert(MessageParts::RoomText, room_text.clone());
    message_parts.insert(MessageParts::Exits, exits_string.clone());
    message_parts.insert(MessageParts::EventText, event_text.clone());
    let message = room_text + "\n" + event_text.as_str() + "\n\n" + exits_string.as_str();
    let mut templated_words = templated_words_room
        .iter()
        .chain(templated_words_event.iter())
        .cloned()
        .collect::<Vec<String>>();
    templated_words.sort_unstable();
    templated_words.dedup();
    Ok(EventMessage {
        message,
        message_parts,
        templated_words,
    })
}
/// This function is used to return the text
/// that should be displayed to the player in the room.
/// This text is composed of the current narrative associated
/// with this room, and the exits that are available to the player.
///
/// This function only returns highlting information if either the
/// item or the subject to be highlighted is in the room, otherwise
/// it returns the narrative withouth the template.
///
/// The items and subjects vectors are used to determine if the
/// corresponding items and subjects should be returned with the
/// room text. These can be used for highlighting the items and
/// subjects in the front-end.
///
/// This function will return a Result wrapping an EventMessage
/// with the following format:
/// ```rust
/// # use nightrunner_lib::parser::interpreter::{EventMessage, MessageParts};
/// # use std::collections::HashMap;
/// let mut message_parts = HashMap::new();
/// message_parts.insert(MessageParts::RoomText, "some message with highlighted text.".to_string());
/// message_parts.insert(MessageParts::RoomText, "You now have item1.".to_string());
/// message_parts.insert(MessageParts::RoomText, "Exits: to the south you see an alley.".to_string());
/// let event_message = EventMessage {
///     message: "some message with highlighted text.\nYou now have item1.\nExits: to the south you see an alley.".to_string(),
///     templated_words: vec!["highlighted".to_string()],
///     message_parts: message_parts,
/// };
/// ```
/// and the parser will return a respone wrappping this result.
/// `message_parts` contains the three parts of the message that
/// is returned and can be used for layouting the message in the
/// front-end, otherwise the message field can be used for simpler
/// applications.
This function is used to return the text
that should be displayed to the player in the room.
This text is composed of the current narrative associated
with this room, and the exits that are available to the player.

This function only returns highlting information if either the
item or the subject to be highlighted is in the room, otherwise
it returns the narrative withouth the template.

The items and subjects vectors are used to determine if the
corresponding items and subjects should be returned with the
room text. These can be used for highlighting the items and
subjects in the front-end.

This function will return a Result wrapping an EventMessage
with the following format:
```rust
This function is used to return the text that should be displayed to the player in the room . This text is composed of the current narrative associated with this room and the exits that are available to the player . This function only returns highlting information if either the item or the subject to be highlighted is in the room otherwise it returns the narrative withouth the template . The items and subjects vectors are used to determine if the corresponding items and subjects should be returned with the room text . These can be used for highlighting the items and subjects in the front - end . This function will return a Result wrapping an EventMessage with the following format : ` ` ` rust
pub fn parse_room_text(
    state: State,
    narrative_text: String,
    event_message: String,
    event_id: Option<u16>,
) -> NRResult<EventMessage> {
    let current_room = match state
        .config
        .rooms
        .iter()
        .find(|room| room.id == state.current_room)
    {
        Some(room) => room,
        None => return Err(InvalidRoom.into()),
    };
    let player_items = state
        .player
        .inventory
        .items
        .clone()
        .iter()
        .map(|item| item.name.clone())
        .collect::<Vec<String>>();
    let room_items = state
        .config
        .items
        .clone()
        .iter()
        .filter(|item| current_room.stash.item_ids.contains(&item.id))
        .map(|item| item.name.clone())
        .collect::<Vec<_>>();
    let room_subjects = state
        .config
        .subjects
        .clone()
        .iter()
        .filter(|subject| current_room.subjects.contains(&subject.id))
        .map(|subject| subject.name.clone())
        .collect::<Vec<_>>();
    let mut event_items = vec![];
    let event = state
        .config
        .events
        .iter()
        .find(|event| Some(event.id) == event_id);
    if let Some(event) = event {
        if let Some(item_id) = event.add_item {
            if let Some(item) = state.config.items.iter().find(|item| item.id == item_id) {
                event_items.push(item.name.clone())
            };
        }
        if let Some(item_id) = event.remove_item {
            if let Some(item) = state.config.items.iter().find(|item| item.id == item_id) {
                event_items.push(item.name.clone())
            };
        }
    };
    let exits_vec = current_room
        .exits
        .clone()
        .iter()
        .map(|exit| {
            match state
                .config
                .rooms
                .iter()
                .find(|room| room.id == exit.room_id)
            {
                Some(room) => format!(
                    "to the {} you see {}",
                    exit.direction.clone(),
                    room.description.clone()
                ),
                None => String::new(),
            }
        })
        .collect::<Vec<String>>();
    let exits_string = match exits_vec.len() {
        0 => String::new(),
        _ => String::from("Exits:\n") + &exits_vec.join("\n")[..],
    };
    let items_and_subjects = player_items
        .iter()
        .chain(room_items.iter())
        .chain(event_items.iter())
        .chain(room_subjects.iter())
        .cloned()
        .collect::<Vec<_>>();
    let (room_text, templated_words_room) =
        process_templated_text(narrative_text, &items_and_subjects);
    let (event_text, templated_words_event) =
        process_templated_text(event_message, &items_and_subjects);
    let mut message_parts = HashMap::new();
    message_parts.insert(MessageParts::RoomText, room_text.clone());
    message_parts.insert(MessageParts::Exits, exits_string.clone());
    message_parts.insert(MessageParts::EventText, event_text.clone());
    let message = room_text + "\n" + event_text.as_str() + "\n\n" + exits_string.as_str();
    let mut templated_words = templated_words_room
        .iter()
        .chain(templated_words_event.iter())
        .cloned()
        .collect::<Vec<String>>();
    templated_words.sort_unstable();
    templated_words.dedup();
    Ok(EventMessage {
        message,
        message_parts,
        templated_words,
    })
}
pub fn parse_room_text ( state : State , narrative_text : String , event_message : String , event_id : Option < u16 > , ) -> NRResult < EventMessage > { let current_room = match state . config . rooms . iter ( ) . find ( | room | room . id == state . current_room ) { Some ( room ) => room , None => return Err ( InvalidRoom . into ( ) ) , } ; let player_items = state . player . inventory . items . clone ( ) . iter ( ) . map ( | item | item . name . clone ( ) ) . collect :: < Vec < String > > ( ) ; let room_items = state . config . items . clone ( ) . iter ( ) . filter ( | item | current_room . stash . item_ids . contains ( & item . id ) ) . map ( | item | item . name . clone ( ) ) . collect :: < Vec < _ > > ( ) ; let room_subjects = state . config . subjects . clone ( ) . iter ( ) . filter ( | subject | current_room . subjects . contains ( & subject . id ) ) . map ( | subject | subject . name . clone ( ) ) . collect :: < Vec < _ > > ( ) ; let mut event_items = vec ! [ ] ; let event = state . config . events . iter ( ) . find ( | event | Some ( event . id ) == event_id ) ; if let Some ( event ) = event { if let Some ( item_id ) = event . add_item { if let Some ( item ) = state . config . items . iter ( ) . find ( | item | item . id == item_id ) { event_items . push ( item . name . clone ( ) ) } ; } if let Some ( item_id ) = event . remove_item { if let Some ( item ) = state . config . items . iter ( ) . find ( | item | item . id == item_id ) { event_items . push ( item . name . clone ( ) ) } ; } } ; let exits_vec = current_room . exits . clone ( ) . iter ( ) . map ( | exit | { match state . config . rooms . iter ( ) . find ( | room | room . id == exit . room_id ) { Some ( room ) => format ! ( " " exit direction clone ( ) room description clone ( ) ) , None => String :: new ( ) , } } ) . collect :: < Vec < String > > ( ) ; let exits_string = match exits_vec . len ( ) { 0 => String :: new ( ) , _ => String :: from ( " \n " ) + & exits_vec . join ( " \n " ) [ .. ] , } ; let items_and_subjects = player_items . iter ( ) . chain ( room_items . iter ( ) ) . chain ( event_items . iter ( ) ) . chain ( room_subjects . iter ( ) ) . cloned ( ) . collect :: < Vec < _ > > ( ) ; let ( room_text , templated_words_room ) = process_templated_text ( narrative_text , & items_and_subjects ) ; let ( event_text , templated_words_event ) = process_templated_text ( event_message , & items_and_subjects ) ; let mut message_parts = HashMap :: new ( ) ; message_parts . insert ( MessageParts :: RoomText , room_text . clone ( ) ) ; message_parts . insert ( MessageParts :: Exits , exits_string . clone ( ) ) ; message_parts . insert ( MessageParts :: EventText , event_text . clone ( ) ) ; let message = room_text + " \n " + event_text . as_str ( ) + " \n \n " + exits_string . as_str ( ) ; let mut templated_words = templated_words_room . iter ( ) . chain ( templated_words_event . iter ( ) ) . cloned ( ) . collect :: < Vec < String > > ( ) ; templated_words . sort_unstable ( ) ; templated_words . dedup ( ) ; Ok ( EventMessage { message , message_parts , templated_words , } ) }
This function is used to return the text
that should be displayed to the player in the room.
This function is used to return the text that should be displayed to the player in the room .
state
State
narrative_text
String
event_message
String
event_id
Option<u16>
state
State
narrative_text
String
event_message
String
event_id
Option<u16>
use nightrunner_lib::parser::interpreter::{eventmessage, messageparts};
None
use std::collections::hashmap;
let mut message_parts = HashMap::new();
message_parts.insert(MessageParts::RoomText, "some message with highlighted text.".to_string());
message_parts.insert(MessageParts::RoomText, "You now have item1.".to_string());
message_parts.insert(MessageParts::RoomText, "Exits: to the south you see an alley.".to_string());
let event_message = EventMessage {
message: "some message with highlighted text.\nYou now have item1.\nExits: to the south you see an alley.".to_string(),
templated_words: vec!["highlighted".to_string()],
message_parts: message_parts,
};
```
and the parser will return a respone wrappping this result.
`message_parts` contains the three parts of the message that
is returned and can be used for layouting the message in the
front-end, otherwise the message field can be used for simpler
applications.
let mut message_parts = HashMap :: new () ; message_parts . insert ( MessageParts :: RoomText " some message with highlighted text . " . to_string () ) ; message_parts . insert ( MessageParts :: RoomText " You now have item1 . " . to_string () ) ; message_parts . insert ( MessageParts :: RoomText " Exits : to the south you see an alley . " . to_string () ) ; let event_message = EventMessage { message : " some message with highlighted text . \ nYou now have item1 . \ nExits : to the south you see an alley . " . to_string () templated_words : vec! [ " highlighted " . to_string () ] message_parts : message_parts } ; ` ` ` and the parser will return a respone wrappping this result . ` message_parts ` contains the three parts of the message that is returned and can be used for layouting the message in the front - end otherwise the message field can be used for simpler applications .