Major rework of extern ref system for WASM, fixes most possible panics in WASM handling
All checks were successful
continuous-integration/drone Build is passing
All checks were successful
continuous-integration/drone Build is passing
This commit is contained in:
@@ -6,6 +6,8 @@ use anyhow::Result;
|
||||
use anyhow_ext::anyhow;
|
||||
use parking_lot::lock_api::MappedRwLockReadGuard;
|
||||
use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The ChoiceQueue is used to run choices one by one.
|
||||
///
|
||||
@@ -14,51 +16,50 @@ use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
|
||||
/// helper functions to change the turn order while doing the execution. This is needed, as several
|
||||
/// moves in Pokemon actively mess with this order.
|
||||
#[derive(Debug)]
|
||||
|
||||
pub struct ChoiceQueue {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
|
||||
/// executed.
|
||||
queue: RwLock<Vec<Option<TurnChoice>>>,
|
||||
queue: RwLock<Vec<Option<Arc<TurnChoice>>>>,
|
||||
/// The current index of the turn we need to execute next.
|
||||
current: usize,
|
||||
current: AtomicUsize,
|
||||
}
|
||||
|
||||
impl ChoiceQueue {
|
||||
/// Initializes a ChoiceQueue, and sort the choices.
|
||||
pub(crate) fn new(mut queue: Vec<Option<TurnChoice>>) -> Self {
|
||||
pub(crate) fn new(mut queue: Vec<Option<Arc<TurnChoice>>>) -> Self {
|
||||
queue.sort_unstable_by(|a, b| b.cmp(a));
|
||||
Self {
|
||||
identifier: Default::default(),
|
||||
queue: RwLock::new(queue),
|
||||
current: 0,
|
||||
current: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Dequeues the next turn choice to be executed. This gives ownership to the callee, and replaces
|
||||
/// our own reference to the turn choice with an empty spot. It also increments the current position
|
||||
/// by one.
|
||||
pub fn dequeue(&mut self) -> Result<Option<TurnChoice>> {
|
||||
pub fn dequeue(&self) -> Result<Option<Arc<TurnChoice>>> {
|
||||
let mut write_lock = self.queue.write();
|
||||
if self.current >= write_lock.len() {
|
||||
if self.current.load(Ordering::Relaxed) >= write_lock.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let c = write_lock
|
||||
.get_mut(self.current)
|
||||
.get_mut(self.current.load(Ordering::Relaxed))
|
||||
.ok_or(anyhow!("Unable to get current turn choice"))?
|
||||
.take();
|
||||
self.current += 1;
|
||||
self.current.fetch_add(1, Ordering::Relaxed);
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
/// This reads what the next choice to execute will be, without modifying state.
|
||||
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, TurnChoice>>> {
|
||||
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, Arc<TurnChoice>>>> {
|
||||
let read_lock = self.queue.read();
|
||||
if self.current >= read_lock.len() {
|
||||
if self.current.load(Ordering::Relaxed) >= read_lock.len() {
|
||||
Ok(None)
|
||||
} else {
|
||||
let v = RwLockReadGuard::try_map(read_lock, |a| match a.get(self.current) {
|
||||
let v = RwLockReadGuard::try_map(read_lock, |a| match a.get(self.current.load(Ordering::Relaxed)) {
|
||||
Some(Some(v)) => Some(v),
|
||||
_ => None,
|
||||
});
|
||||
@@ -71,7 +72,7 @@ impl ChoiceQueue {
|
||||
|
||||
/// Check if we have any choices remaining.
|
||||
pub fn has_next(&self) -> bool {
|
||||
self.current < self.queue.read().len()
|
||||
self.current.load(Ordering::Relaxed) < self.queue.read().len()
|
||||
}
|
||||
|
||||
/// This resorts the yet to be executed choices. This can be useful for dealing with situations
|
||||
@@ -80,19 +81,19 @@ impl ChoiceQueue {
|
||||
pub fn resort(&mut self) -> Result<()> {
|
||||
let len = self.queue.read().len();
|
||||
let mut write_lock = self.queue.write();
|
||||
for index in self.current..len {
|
||||
for index in self.current.load(Ordering::Relaxed)..len {
|
||||
let choice = &mut write_lock.get_mut_res(index)?;
|
||||
if let Some(choice) = choice {
|
||||
let mut speed = choice.user().boosted_stats().speed();
|
||||
script_hook!(change_speed, (*choice), choice, &mut speed);
|
||||
*choice.speed_mut() = speed;
|
||||
choice.set_speed(speed)
|
||||
}
|
||||
}
|
||||
|
||||
write_lock
|
||||
.get_mut(self.current..len)
|
||||
.get_mut(self.current.load(Ordering::Relaxed)..len)
|
||||
.ok_or(PkmnError::IndexOutOfBounds {
|
||||
index: self.current,
|
||||
index: self.current.load(Ordering::Relaxed),
|
||||
len,
|
||||
})?
|
||||
.sort_unstable_by(|a, b| b.cmp(a));
|
||||
@@ -104,7 +105,7 @@ impl ChoiceQueue {
|
||||
let mut queue_lock = self.queue.write();
|
||||
let mut desired_index = None;
|
||||
// Find the index for the choice we want to move up.
|
||||
for index in self.current..queue_lock.len() {
|
||||
for index in self.current.load(Ordering::Relaxed)..queue_lock.len() {
|
||||
if let Some(Some(choice)) = &queue_lock.get(index) {
|
||||
if pokemon.value_identifier() == choice.user().value_identifier() {
|
||||
desired_index = Some(index);
|
||||
@@ -115,7 +116,7 @@ impl ChoiceQueue {
|
||||
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 {
|
||||
if desired_index == self.current.load(Ordering::Relaxed) {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
@@ -126,11 +127,14 @@ impl ChoiceQueue {
|
||||
.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() {
|
||||
let current = self.current.load(Ordering::Relaxed);
|
||||
for index in (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.get_mut_res(self.current)?.insert(choice);
|
||||
let _ = queue_lock
|
||||
.get_mut_res(self.current.load(Ordering::Relaxed))?
|
||||
.insert(choice);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
@@ -139,12 +143,14 @@ impl ChoiceQueue {
|
||||
}
|
||||
|
||||
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
|
||||
pub(crate) fn get_queue(&self) -> Result<MappedRwLockReadGuard<'_, RawRwLock, [Option<TurnChoice>]>> {
|
||||
pub(crate) fn get_queue(&self) -> Result<MappedRwLockReadGuard<'_, RawRwLock, [Option<Arc<TurnChoice>>]>> {
|
||||
let read_lock = self.queue.read();
|
||||
match RwLockReadGuard::try_map(read_lock, |a| a.get(self.current..self.queue.read().len())) {
|
||||
match RwLockReadGuard::try_map(read_lock, |a| {
|
||||
a.get(self.current.load(Ordering::Relaxed)..self.queue.read().len())
|
||||
}) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => Err(PkmnError::IndexOutOfBounds {
|
||||
index: self.current,
|
||||
index: self.current.load(Ordering::Relaxed),
|
||||
len: self.queue.read().len(),
|
||||
}
|
||||
.into()),
|
||||
@@ -176,7 +182,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn dequeue_from_empty_queue() {
|
||||
let mut queue = ChoiceQueue::new(Vec::new());
|
||||
let queue = ChoiceQueue::new(Vec::new());
|
||||
assert!(queue.dequeue().unwrap().is_none());
|
||||
}
|
||||
|
||||
@@ -204,9 +210,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_queue_with_single_item() {
|
||||
let user = Arc::new(get_user(10));
|
||||
let user = get_user(10);
|
||||
|
||||
let queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
|
||||
let queue = ChoiceQueue::new(vec![Some(Arc::new(TurnChoice::Pass(PassChoice::new(user))))]);
|
||||
assert!(queue.has_next());
|
||||
assert!(queue.peek().unwrap().is_some());
|
||||
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
|
||||
@@ -214,9 +220,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn dequeue_from_queue_with_single_item() {
|
||||
let user = Arc::new(get_user(10));
|
||||
let user = get_user(10);
|
||||
|
||||
let mut queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
|
||||
let queue = ChoiceQueue::new(vec![Some(Arc::new(TurnChoice::Pass(PassChoice::new(user))))]);
|
||||
assert!(queue.has_next());
|
||||
assert_eq!(7, queue.dequeue().unwrap().unwrap().speed());
|
||||
assert!(!queue.has_next());
|
||||
@@ -225,12 +231,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_queue_with_two_items_with_equal_order() {
|
||||
let user1 = Arc::new(get_user(10));
|
||||
let user2 = Arc::new(get_user(10));
|
||||
let user1 = get_user(10);
|
||||
let user2 = get_user(10);
|
||||
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1)))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
||||
]);
|
||||
assert!(queue.has_next());
|
||||
assert!(queue.peek().unwrap().is_some());
|
||||
@@ -239,12 +245,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_queue_with_two_items_get_queue() {
|
||||
let user1 = Arc::new(get_user(10));
|
||||
let user2 = Arc::new(get_user(5));
|
||||
let user1 = get_user(10);
|
||||
let user2 = get_user(5);
|
||||
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
||||
]);
|
||||
let inner_queue = queue.get_queue().unwrap();
|
||||
assert_eq!(
|
||||
@@ -255,12 +261,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_queue_with_two_items_in_wrong_order_sorts_correctly() {
|
||||
let user1 = Arc::new(get_user(5));
|
||||
let user2 = Arc::new(get_user(100));
|
||||
let user1 = get_user(5);
|
||||
let user2 = get_user(100);
|
||||
|
||||
let mut queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2))),
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1)))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
||||
]);
|
||||
assert_eq!(25, queue.dequeue().unwrap().unwrap().speed());
|
||||
assert_eq!(6, queue.dequeue().unwrap().unwrap().speed());
|
||||
@@ -268,12 +274,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn resort_with_two_choices() {
|
||||
let user1 = Arc::new(get_user(50));
|
||||
let user2 = Arc::new(get_user(1));
|
||||
let user1 = get_user(50);
|
||||
let user2 = get_user(1);
|
||||
|
||||
let mut queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
|
||||
]);
|
||||
|
||||
user2.change_level_by(60).unwrap();
|
||||
@@ -290,18 +296,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn move_pokemon_choice_first_with_two_choices() {
|
||||
let user1 = Arc::new(get_user(100));
|
||||
let user2 = Arc::new(get_user(1));
|
||||
let user1 = get_user(100);
|
||||
let user2 = get_user(1);
|
||||
|
||||
let mut queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
|
||||
]);
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
|
||||
assert!(queue.move_pokemon_choice_next(&user2).unwrap());
|
||||
|
||||
assert_eq!(
|
||||
user2.value_identifier(),
|
||||
@@ -315,18 +321,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn move_pokemon_choice_first_when_choice_has_already_been() {
|
||||
let user1 = Arc::new(get_user(10));
|
||||
let user2 = Arc::new(get_user(100));
|
||||
let user1 = get_user(10);
|
||||
let user2 = get_user(100);
|
||||
|
||||
let mut queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
|
||||
]);
|
||||
assert_eq!(
|
||||
user2.value_identifier(),
|
||||
queue.dequeue().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(!queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
|
||||
assert!(!queue.move_pokemon_choice_next(&user2).unwrap());
|
||||
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
@@ -337,18 +343,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn move_pokemon_choice_first_when_choice_is_next() {
|
||||
let user1 = Arc::new(get_user(100));
|
||||
let user2 = Arc::new(get_user(10));
|
||||
let user1 = get_user(100);
|
||||
let user2 = get_user(10);
|
||||
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(user2))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
||||
]);
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.move_pokemon_choice_next(user1.as_ref()).unwrap());
|
||||
assert!(queue.move_pokemon_choice_next(&user1).unwrap());
|
||||
assert_eq!(
|
||||
user1.value_identifier(),
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
@@ -358,29 +364,29 @@ mod tests {
|
||||
#[test]
|
||||
fn move_pokemon_choice_first_with_seven_choices() {
|
||||
let users = [
|
||||
Arc::new(get_user(100)),
|
||||
Arc::new(get_user(90)),
|
||||
Arc::new(get_user(80)),
|
||||
Arc::new(get_user(70)),
|
||||
Arc::new(get_user(60)),
|
||||
Arc::new(get_user(50)),
|
||||
Arc::new(get_user(40)),
|
||||
get_user(100),
|
||||
get_user(90),
|
||||
get_user(80),
|
||||
get_user(70),
|
||||
get_user(60),
|
||||
get_user(50),
|
||||
get_user(40),
|
||||
];
|
||||
|
||||
let mut queue = ChoiceQueue::new(vec![
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[0].clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[1].clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[2].clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[3].clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[4].clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[5].clone()))),
|
||||
Some(TurnChoice::Pass(PassChoice::new(users[6].clone()))),
|
||||
let queue = ChoiceQueue::new(vec![
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[0].clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[1].clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[2].clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[3].clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[4].clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[5].clone())))),
|
||||
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[6].clone())))),
|
||||
]);
|
||||
assert_eq!(
|
||||
users[0].value_identifier(),
|
||||
queue.peek().unwrap().unwrap().user().value_identifier()
|
||||
);
|
||||
assert!(queue.move_pokemon_choice_next(users[4].as_ref()).unwrap());
|
||||
assert!(queue.move_pokemon_choice_next(&users[4]).unwrap());
|
||||
|
||||
assert_eq!(
|
||||
users[4].value_identifier(),
|
||||
|
||||
Reference in New Issue
Block a user