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) {