From e8302833da0568da3062eb684715044441302ccb Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 17:53:28 +0200 Subject: [PATCH] implement stack-based conversation position --- src/chat.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index e880883..af11316 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -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) -> 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) {