This commit is contained in:
yuni 2024-04-13 00:11:32 +02:00
parent 2aaecdc113
commit 45d3408f0c

View file

@ -34,9 +34,18 @@ pub struct Chat {
#[derive(Component)]
pub struct Choice;
// This is the only place where any YAML interaction should be happening.
#[derive(Resource)]
pub struct ChatDB(Vec<Yaml>);
impl ChatDB {
pub fn load_from_str(&mut self, yaml_string: &str) -> Result<(), ()> {
if let Ok(mut yaml_data) = YamlLoader::load_from_str(yaml_string) {
self.0.append(&mut yaml_data);
return Ok(());
}
return Err(());
}
pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> {
let mut found: Option<u32> = None;
for (index, object_yaml) in self.0.iter().enumerate() {
@ -54,6 +63,7 @@ impl ChatDB {
}
return Err(format!("No chat with the conversation ID `{id}` was found."));
}
pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
event.send(ChatEvent::DespawnAllChoices);
chat.position += 1;
@ -83,9 +93,7 @@ pub enum ChatEvent {
pub fn load_chats(mut chatdb: ResMut<ChatDB>) {
for chat_yaml in CHATS {
if let Ok(mut yaml_data) = YamlLoader::load_from_str(chat_yaml) {
chatdb.0.append(&mut yaml_data);
} else {
if chatdb.load_from_str(chat_yaml).is_err() {
error!("Could not load chat definitions. Validate files in `src/chats/` path.");
}
}