implement stack-based conversation position

This commit is contained in:
yuni 2024-04-13 17:53:28 +02:00
parent d990726932
commit e8302833da

View file

@ -127,7 +127,16 @@ impl ChatDB {
// returns false if the advanced pointer is out of bounds
fn advance_pointer(&self, chat: &mut Chat) -> bool {
chat.position[0] += 1;
let index = chat.position.len() - 1;
chat.position[index] += 1;
while chat.position.len() > 0 {
if let None = self.at(chat.id, &chat.position) {
chat.position.pop();
}
let index = chat.position.len() - 1;
chat.position[index] += 1;
}
return true;
}
@ -137,10 +146,18 @@ impl ChatDB {
}
fn at(&self, id: usize, position: &Vec<usize>) -> Option<&Value> {
if let Some(Value::Sequence(seq)) = self.0.get(id) {
return Some(&seq[position[0]]); // TODO: panic
if position.len() == 0 {
return None;
}
return None;
let mut pointer: Option<&Value> = Some(&self.0[id]);
for index in position {
if let Some(Value::Sequence(seq)) = self.0.get(id) {
pointer = Some(&seq[*index]);
} else {
return None;
}
}
return pointer;
}
pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {