proper exiting of conversation branches

This commit is contained in:
yuni 2024-04-13 20:59:55 +02:00
parent e7df698225
commit 5bc76a3e2f

View file

@ -135,14 +135,15 @@ impl ChatDB {
return None; return None;
} }
// returns false if the advanced pointer is out of bounds // returns true if we reached the end of a branch and possibly popped the position stack
fn advance_pointer(&self, chat: &mut Chat) -> bool { fn advance_pointer(&self, chat: &mut Chat) -> bool {
let len = chat.position.len(); let len = chat.position.len();
if len == 0 { if len == 0 {
return false; return true; // out of bounds
} }
chat.position[len - 1] += 1; chat.position[len - 1] += 1;
let mut popped = false;
while chat.position.len() > 0 { while chat.position.len() > 0 {
dbg!(&chat.position); dbg!(&chat.position);
dbg!(self.at(chat.id, &chat.position)); dbg!(self.at(chat.id, &chat.position));
@ -150,6 +151,7 @@ impl ChatDB {
None => { None => {
dbg!("Pop."); dbg!("Pop.");
chat.position.pop(); chat.position.pop();
popped = true;
if chat.position.len() > 0 { if chat.position.len() > 0 {
let index = chat.position.len() - 1; let index = chat.position.len() - 1;
chat.position[index] += 1; chat.position[index] += 1;
@ -161,10 +163,9 @@ impl ChatDB {
} }
} }
if chat.position.len() == 0 { if chat.position.len() == 0 {
// out of bounds, return false. return true; // out of bounds
return false;
} }
return true; return popped;
} }
// Note that this (intentionally) may result in a pointer that's out of bounds. // Note that this (intentionally) may result in a pointer that's out of bounds.
@ -199,8 +200,7 @@ impl ChatDB {
} }
} }
None => { None => {
// Out of bounds. return None; // Out of bounds.
return None;
} }
_ => { _ => {
error!("Could not handle YAML value {value:?}"); error!("Could not handle YAML value {value:?}");
@ -216,6 +216,9 @@ impl ChatDB {
return result; return result;
} }
pub fn process_value(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
}
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);
let conv = &self.0.get(chat.id); let conv = &self.0.get(chat.id);
@ -223,42 +226,44 @@ impl ChatDB {
return; return;
} }
// Handle next entry in the chat list // Handle this entry in the chat list
let mut is_skipping_through = false; let current_item = self.at(chat.id, &chat.position);
if !self.advance_pointer(chat) { let mut add_choices = true;
event.send(ChatEvent::SpawnMessage("Disconnected.".to_string())); match current_item {
event.send(ChatEvent::DespawnAllChats); Some(Value::String(message)) => {
return;
}
else if let Some(_) = self.search_choice(self.at(chat.id, &chat.position).as_ref()) {
is_skipping_through = true;
}
else if let Some(Value::String(message)) = self.at(chat.id, &chat.position) {
event.send(ChatEvent::SpawnMessage(message.to_string())); event.send(ChatEvent::SpawnMessage(message.to_string()));
} }
Some(Value::Mapping(message)) => {
if let Some(_) = self.search_choice(Some(&Value::Mapping(message))) {
add_choices = false;
}
}
None => {
event.send(ChatEvent::SpawnMessage("Disconnected.".to_string()));
event.send(ChatEvent::DespawnAllChats);
}
_ => {
error!("Can't handle YAML value {current_item:?}");
}
}
// Check if the immediately following entries are choices // Move on to next entry
let mut pos = chat.position.clone(); let mut finished_branch = self.advance_pointer(chat);
self.pointer_lookahead(&mut pos);
// Add choices, if available
if add_choices {
let mut key: usize = 0; let mut key: usize = 0;
loop { while let Some((choice, _)) =
if is_skipping_through /*|| pos[0] >= conv.len()*/ { // TODO: out of bounds checking self.search_choice(self.at(chat.id, &chat.position).as_ref()) {
if finished_branch {
break; break;
} }
dbg!(&pos); let mut goto: Vec<usize> = chat.position.clone();
dbg!(self.at(chat.id, &pos));
let choice = self.search_choice(self.at(chat.id, &pos).as_ref());
dbg!(&choice);
if let Some((choice, _)) = choice {
let mut goto: Vec<usize> = pos.clone();
goto.push(0); goto.push(0);
event.send(ChatEvent::SpawnChoice(choice, key, goto)); event.send(ChatEvent::SpawnChoice(choice, key, goto));
key += 1; key += 1;
finished_branch = self.advance_pointer(chat);
} }
else {
break;
}
self.pointer_lookahead(&mut pos);
} }
} }
} }