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