Support for new error handling.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -2,6 +2,8 @@ use crate::dynamic_data::choices::TurnChoice;
|
||||
use crate::dynamic_data::script_handling::ScriptSource;
|
||||
use crate::dynamic_data::Pokemon;
|
||||
use crate::{script_hook, ValueIdentifiable, ValueIdentifier};
|
||||
use anyhow::Result;
|
||||
use anyhow_ext::anyhow;
|
||||
use parking_lot::lock_api::MappedRwLockReadGuard;
|
||||
use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
|
||||
|
||||
@@ -48,12 +50,16 @@ impl ChoiceQueue {
|
||||
}
|
||||
|
||||
/// This reads what the next choice to execute will be, without modifying state.
|
||||
pub fn peek(&self) -> Option<MappedRwLockReadGuard<'_, RawRwLock, TurnChoice>> {
|
||||
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, TurnChoice>>> {
|
||||
let read_lock = self.queue.read();
|
||||
if self.current >= read_lock.len() {
|
||||
None
|
||||
Ok(None)
|
||||
} else {
|
||||
Some(RwLockReadGuard::map(read_lock, |a| a[self.current].as_ref().unwrap()))
|
||||
let v = RwLockReadGuard::try_map(read_lock, |a| a[self.current].as_ref());
|
||||
match v {
|
||||
Ok(v) => Ok(Some(v)),
|
||||
Err(_) => Err(anyhow!("Could not map choice")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +87,7 @@ impl ChoiceQueue {
|
||||
}
|
||||
|
||||
/// This moves the choice of a specific Pokemon up to the next choice to be executed.
|
||||
pub fn move_pokemon_choice_next(&self, pokemon: &Pokemon) -> bool {
|
||||
pub fn move_pokemon_choice_next(&self, pokemon: &Pokemon) -> Result<bool> {
|
||||
let mut queue_lock = self.queue.write();
|
||||
let mut desired_index = None;
|
||||
// Find the index for the choice we want to move up.
|
||||
@@ -93,26 +99,29 @@ impl ChoiceQueue {
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we couldn't find a choice, we can't execute, return.
|
||||
if desired_index.is_none() {
|
||||
return false;
|
||||
}
|
||||
let desired_index = desired_index.unwrap();
|
||||
// If the choice we want to move up is already the next choice, just return.
|
||||
if desired_index == self.current {
|
||||
return true;
|
||||
}
|
||||
let result = match desired_index {
|
||||
Some(desired_index) => {
|
||||
// If the choice we want to move up is already the next choice, just return.
|
||||
if desired_index == self.current {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// Take the choice we want to move forward out of it's place.
|
||||
let choice = queue_lock[desired_index].take().unwrap();
|
||||
// Iterate backwards from the spot before the choice we want to move up, push them all back
|
||||
// by 1 spot.
|
||||
for index in (self.current..desired_index).rev() {
|
||||
queue_lock.swap(index, index + 1);
|
||||
}
|
||||
// Place the choice that needs to be next in the next to be executed position.
|
||||
let _ = queue_lock[self.current].insert(choice);
|
||||
true
|
||||
// Take the choice we want to move forward out of it's place.
|
||||
let choice = queue_lock[desired_index]
|
||||
.take()
|
||||
.ok_or(anyhow!("Choice was already taken"))?;
|
||||
// Iterate backwards from the spot before the choice we want to move up, push them all back
|
||||
// by 1 spot.
|
||||
for index in (self.current..desired_index).rev() {
|
||||
queue_lock.swap(index, index + 1);
|
||||
}
|
||||
// Place the choice that needs to be next in the next to be executed position.
|
||||
let _ = queue_lock[self.current].insert(choice);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
};
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
|
||||
@@ -140,7 +149,7 @@ mod tests {
|
||||
fn create_empty_queue() {
|
||||
let queue = ChoiceQueue::new(Vec::new());
|
||||
assert!(!queue.has_next());
|
||||
assert!(queue.peek().is_none());
|
||||
assert!(queue.peek().unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -168,6 +177,7 @@ mod tests {
|
||||
0,
|
||||
&"test_nature".into(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -176,8 +186,8 @@ mod tests {
|
||||
|
||||
let queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
|
||||
assert!(queue.has_next());
|
||||
assert!(queue.peek().is_some());
|
||||
assert_eq!(7, queue.peek().unwrap().speed());
|
||||
assert!(queue.peek().unwrap().is_some());
|
||||
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -188,7 +198,7 @@ mod tests {
|
||||
assert!(queue.has_next());
|
||||
assert_eq!(7, queue.dequeue().unwrap().speed());
|
||||
assert!(!queue.has_next());
|
||||
assert!(queue.peek().is_none());
|
||||
assert!(queue.peek().unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -201,8 +211,8 @@ mod tests {
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2))),
|
||||
]);
|
||||
assert!(queue.has_next());
|
||||
assert!(queue.peek().is_some());
|
||||
assert_eq!(7, queue.peek().unwrap().speed());
|
||||
assert!(queue.peek().unwrap().is_some());
|
||||
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -247,12 +257,12 @@ mod tests {
|
||||
user2.change_level_by(60);
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().user().value_identifier()
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
queue.resort();
|
||||
assert_eq!(
|
||||
user2.value_identifier(),
|
||||
queue.peek().unwrap().user().value_identifier()
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -267,9 +277,9 @@ mod tests {
|
||||
]);
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().user().value_identifier()
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.move_pokemon_choice_next(user2.as_ref()));
|
||||
assert!(queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
|
||||
|
||||
assert_eq!(
|
||||
user2.value_identifier(),
|
||||
@@ -294,13 +304,13 @@ mod tests {
|
||||
user2.value_identifier(),
|
||||
queue.dequeue().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(!queue.move_pokemon_choice_next(user2.as_ref()));
|
||||
assert!(!queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
|
||||
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.dequeue().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.peek().is_none())
|
||||
assert!(queue.peek().unwrap().is_none())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -314,12 +324,12 @@ mod tests {
|
||||
]);
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().user().value_identifier()
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.move_pokemon_choice_next(user1.as_ref()));
|
||||
assert!(queue.move_pokemon_choice_next(user1.as_ref()).unwrap());
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().user().value_identifier()
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -346,9 +356,9 @@ mod tests {
|
||||
]);
|
||||
assert_eq!(
|
||||
users[0].value_identifier(),
|
||||
queue.peek().unwrap().user().value_identifier()
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.move_pokemon_choice_next(users[4].as_ref()));
|
||||
assert!(queue.move_pokemon_choice_next(users[4].as_ref()).unwrap());
|
||||
|
||||
assert_eq!(
|
||||
users[4].value_identifier(),
|
||||
|
||||
Reference in New Issue
Block a user