2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::choices::TurnChoice;
|
2022-11-26 14:33:50 +00:00
|
|
|
use crate::dynamic_data::script_handling::ScriptSource;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::Pokemon;
|
2023-06-24 12:44:23 +00:00
|
|
|
use crate::{script_hook, PkmnError, VecExt};
|
2023-04-15 12:34:42 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use anyhow_ext::anyhow;
|
2022-09-07 16:01:26 +00:00
|
|
|
use parking_lot::lock_api::MappedRwLockReadGuard;
|
|
|
|
use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
|
2023-06-22 13:43:41 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
use std::sync::Arc;
|
2022-06-11 15:22:46 +00:00
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The ChoiceQueue is used to run choices one by one.
|
|
|
|
///
|
|
|
|
/// It functions internally by holding a vector of choices, and passing ownership of the turn choice
|
|
|
|
/// to the turn executor one by one, replacing it with empty spots at the start. It holds several
|
|
|
|
/// helper functions to change the turn order while doing the execution. This is needed, as several
|
|
|
|
/// moves in Pokemon actively mess with this order.
|
2022-06-03 14:35:18 +00:00
|
|
|
#[derive(Debug)]
|
2022-08-20 11:17:20 +00:00
|
|
|
pub struct ChoiceQueue {
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
|
|
|
|
/// executed.
|
2023-06-22 13:43:41 +00:00
|
|
|
queue: RwLock<Vec<Option<Arc<TurnChoice>>>>,
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The current index of the turn we need to execute next.
|
2023-06-22 13:43:41 +00:00
|
|
|
current: AtomicUsize,
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 11:17:20 +00:00
|
|
|
impl ChoiceQueue {
|
2022-11-26 14:33:50 +00:00
|
|
|
/// Initializes a ChoiceQueue, and sort the choices.
|
2023-06-22 13:43:41 +00:00
|
|
|
pub(crate) fn new(mut queue: Vec<Option<Arc<TurnChoice>>>) -> Self {
|
2022-11-26 14:33:50 +00:00
|
|
|
queue.sort_unstable_by(|a, b| b.cmp(a));
|
2022-09-07 16:01:26 +00:00
|
|
|
Self {
|
|
|
|
queue: RwLock::new(queue),
|
2023-06-22 13:43:41 +00:00
|
|
|
current: AtomicUsize::new(0),
|
2022-09-07 16:01:26 +00:00
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// 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.
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn dequeue(&self) -> Result<Option<Arc<TurnChoice>>> {
|
2022-11-26 14:33:50 +00:00
|
|
|
let mut write_lock = self.queue.write();
|
2023-06-22 13:43:41 +00:00
|
|
|
if self.current.load(Ordering::Relaxed) >= write_lock.len() {
|
2023-04-15 17:33:29 +00:00
|
|
|
return Ok(None);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
2023-04-15 17:33:29 +00:00
|
|
|
let c = write_lock
|
2023-06-22 13:43:41 +00:00
|
|
|
.get_mut(self.current.load(Ordering::Relaxed))
|
2023-04-15 17:33:29 +00:00
|
|
|
.ok_or(anyhow!("Unable to get current turn choice"))?
|
|
|
|
.take();
|
2023-06-22 13:43:41 +00:00
|
|
|
self.current.fetch_add(1, Ordering::Relaxed);
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(c)
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// This reads what the next choice to execute will be, without modifying state.
|
2023-06-22 13:43:41 +00:00
|
|
|
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, Arc<TurnChoice>>>> {
|
2022-09-07 16:01:26 +00:00
|
|
|
let read_lock = self.queue.read();
|
2023-06-22 13:43:41 +00:00
|
|
|
if self.current.load(Ordering::Relaxed) >= read_lock.len() {
|
2023-04-15 12:34:42 +00:00
|
|
|
Ok(None)
|
2022-11-26 14:33:50 +00:00
|
|
|
} else {
|
2023-06-22 13:43:41 +00:00
|
|
|
let v = RwLockReadGuard::try_map(read_lock, |a| match a.get(self.current.load(Ordering::Relaxed)) {
|
2023-04-15 17:33:29 +00:00
|
|
|
Some(Some(v)) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
});
|
2023-04-15 12:34:42 +00:00
|
|
|
match v {
|
|
|
|
Ok(v) => Ok(Some(v)),
|
|
|
|
Err(_) => Err(anyhow!("Could not map choice")),
|
|
|
|
}
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Check if we have any choices remaining.
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn has_next(&self) -> bool {
|
2023-06-22 13:43:41 +00:00
|
|
|
self.current.load(Ordering::Relaxed) < self.queue.read().len()
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// This resorts the yet to be executed choices. This can be useful for dealing with situations
|
|
|
|
/// such as Pokemon changing forms just after the very start of a turn, when turn order has
|
|
|
|
/// technically already been decided.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub fn resort(&mut self) -> Result<()> {
|
2022-09-07 16:01:26 +00:00
|
|
|
let len = self.queue.read().len();
|
2022-11-26 14:33:50 +00:00
|
|
|
let mut write_lock = self.queue.write();
|
2023-06-22 13:43:41 +00:00
|
|
|
for index in self.current.load(Ordering::Relaxed)..len {
|
2023-04-16 17:57:21 +00:00
|
|
|
let choice = &mut write_lock.get_mut_res(index)?;
|
2022-11-26 14:33:50 +00:00
|
|
|
if let Some(choice) = choice {
|
|
|
|
let mut speed = choice.user().boosted_stats().speed();
|
|
|
|
script_hook!(change_speed, (*choice), choice, &mut speed);
|
2023-06-22 13:43:41 +00:00
|
|
|
choice.set_speed(speed)
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 17:33:29 +00:00
|
|
|
write_lock
|
2023-06-22 13:43:41 +00:00
|
|
|
.get_mut(self.current.load(Ordering::Relaxed)..len)
|
2023-04-15 17:33:29 +00:00
|
|
|
.ok_or(PkmnError::IndexOutOfBounds {
|
2023-06-22 13:43:41 +00:00
|
|
|
index: self.current.load(Ordering::Relaxed),
|
2023-04-15 17:33:29 +00:00
|
|
|
len,
|
|
|
|
})?
|
|
|
|
.sort_unstable_by(|a, b| b.cmp(a));
|
|
|
|
Ok(())
|
2022-06-19 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This moves the choice of a specific Pokemon up to the next choice to be executed.
|
2023-04-15 12:34:42 +00:00
|
|
|
pub fn move_pokemon_choice_next(&self, pokemon: &Pokemon) -> Result<bool> {
|
2022-09-07 16:01:26 +00:00
|
|
|
let mut queue_lock = self.queue.write();
|
2022-08-28 13:34:40 +00:00
|
|
|
let mut desired_index = None;
|
|
|
|
// Find the index for the choice we want to move up.
|
2023-06-22 13:43:41 +00:00
|
|
|
for index in self.current.load(Ordering::Relaxed)..queue_lock.len() {
|
2023-04-15 17:33:29 +00:00
|
|
|
if let Some(Some(choice)) = &queue_lock.get(index) {
|
2023-06-24 12:44:23 +00:00
|
|
|
if pokemon.eq(choice.user()) {
|
2022-08-28 13:34:40 +00:00
|
|
|
desired_index = Some(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 12:34:42 +00:00
|
|
|
let result = match desired_index {
|
|
|
|
Some(desired_index) => {
|
|
|
|
// If the choice we want to move up is already the next choice, just return.
|
2023-06-22 13:43:41 +00:00
|
|
|
if desired_index == self.current.load(Ordering::Relaxed) {
|
2023-04-15 12:34:42 +00:00
|
|
|
return Ok(true);
|
|
|
|
}
|
2022-08-28 13:34:40 +00:00
|
|
|
|
2023-04-15 12:34:42 +00:00
|
|
|
// Take the choice we want to move forward out of it's place.
|
2023-04-15 17:33:29 +00:00
|
|
|
let choice = queue_lock
|
2023-04-16 17:57:21 +00:00
|
|
|
.get_mut_res(desired_index)?
|
2023-04-15 12:34:42 +00:00
|
|
|
.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.
|
2023-06-22 13:43:41 +00:00
|
|
|
let current = self.current.load(Ordering::Relaxed);
|
|
|
|
for index in (current..desired_index).rev() {
|
2023-04-15 12:34:42 +00:00
|
|
|
queue_lock.swap(index, index + 1);
|
|
|
|
}
|
|
|
|
// Place the choice that needs to be next in the next to be executed position.
|
2023-06-22 13:43:41 +00:00
|
|
|
let _ = queue_lock
|
|
|
|
.get_mut_res(self.current.load(Ordering::Relaxed))?
|
|
|
|
.insert(choice);
|
2023-04-15 12:34:42 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
None => false,
|
|
|
|
};
|
|
|
|
Ok(result)
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
|
2023-06-22 13:43:41 +00:00
|
|
|
pub(crate) fn get_queue(&self) -> Result<MappedRwLockReadGuard<'_, RawRwLock, [Option<Arc<TurnChoice>>]>> {
|
2022-09-07 16:01:26 +00:00
|
|
|
let read_lock = self.queue.read();
|
2023-06-22 13:43:41 +00:00
|
|
|
match RwLockReadGuard::try_map(read_lock, |a| {
|
|
|
|
a.get(self.current.load(Ordering::Relaxed)..self.queue.read().len())
|
|
|
|
}) {
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(v) => Ok(v),
|
|
|
|
Err(_) => Err(PkmnError::IndexOutOfBounds {
|
2023-06-22 13:43:41 +00:00
|
|
|
index: self.current.load(Ordering::Relaxed),
|
2023-04-15 17:33:29 +00:00
|
|
|
len: self.queue.read().len(),
|
|
|
|
}
|
|
|
|
.into()),
|
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
2022-11-26 14:33:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-16 17:57:21 +00:00
|
|
|
#[allow(clippy::unwrap_used)]
|
2023-04-19 16:44:11 +00:00
|
|
|
#[allow(clippy::indexing_slicing)]
|
2022-11-26 14:33:50 +00:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::defines::LevelInt;
|
2022-12-24 11:00:50 +00:00
|
|
|
use crate::dynamic_data::{DynamicLibrary, PassChoice};
|
|
|
|
use crate::static_data::{AbilityIndex, Gender};
|
2022-11-26 14:33:50 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
#[test]
|
|
|
|
fn create_empty_queue() {
|
|
|
|
let queue = ChoiceQueue::new(Vec::new());
|
|
|
|
assert!(!queue.has_next());
|
2023-04-15 12:34:42 +00:00
|
|
|
assert!(queue.peek().unwrap().is_none());
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dequeue_from_empty_queue() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let queue = ChoiceQueue::new(Vec::new());
|
2023-04-15 17:33:29 +00:00
|
|
|
assert!(queue.dequeue().unwrap().is_none());
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_user(level: LevelInt) -> Pokemon {
|
|
|
|
let lib = Arc::new(crate::dynamic_data::libraries::dynamic_library::test::build());
|
2023-01-02 10:25:36 +00:00
|
|
|
let species = lib.static_data().species().get(&"foo".into()).unwrap();
|
2022-11-26 14:33:50 +00:00
|
|
|
let form = species.get_form(&"default".into()).unwrap();
|
|
|
|
|
|
|
|
Pokemon::new(
|
|
|
|
lib,
|
|
|
|
species,
|
|
|
|
&form,
|
|
|
|
AbilityIndex {
|
|
|
|
hidden: false,
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
Gender::Male,
|
|
|
|
0,
|
|
|
|
&"test_nature".into(),
|
|
|
|
)
|
2023-04-15 12:34:42 +00:00
|
|
|
.unwrap()
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_queue_with_single_item() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user = get_user(10);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
let queue = ChoiceQueue::new(vec![Some(Arc::new(TurnChoice::Pass(PassChoice::new(user))))]);
|
2022-11-26 14:33:50 +00:00
|
|
|
assert!(queue.has_next());
|
2023-04-15 12:34:42 +00:00
|
|
|
assert!(queue.peek().unwrap().is_some());
|
|
|
|
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dequeue_from_queue_with_single_item() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user = get_user(10);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
let queue = ChoiceQueue::new(vec![Some(Arc::new(TurnChoice::Pass(PassChoice::new(user))))]);
|
2022-11-26 14:33:50 +00:00
|
|
|
assert!(queue.has_next());
|
2023-04-15 17:33:29 +00:00
|
|
|
assert_eq!(7, queue.dequeue().unwrap().unwrap().speed());
|
2022-11-26 14:33:50 +00:00
|
|
|
assert!(!queue.has_next());
|
2023-04-15 12:34:42 +00:00
|
|
|
assert!(queue.peek().unwrap().is_none());
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_queue_with_two_items_with_equal_order() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(10);
|
|
|
|
let user2 = get_user(10);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
|
|
|
let queue = ChoiceQueue::new(vec![
|
2023-06-22 13:43:41 +00:00
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1)))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
|
|
|
assert!(queue.has_next());
|
2023-04-15 12:34:42 +00:00
|
|
|
assert!(queue.peek().unwrap().is_some());
|
|
|
|
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_queue_with_two_items_get_queue() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(10);
|
|
|
|
let user2 = get_user(5);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
|
|
|
let queue = ChoiceQueue::new(vec![
|
2023-06-22 13:43:41 +00:00
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
2023-04-15 17:33:29 +00:00
|
|
|
let inner_queue = queue.get_queue().unwrap();
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(inner_queue[0].as_ref().unwrap().user().clone(), user1);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_queue_with_two_items_in_wrong_order_sorts_correctly() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(5);
|
|
|
|
let user2 = get_user(100);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
let queue = ChoiceQueue::new(vec![
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1)))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
2023-04-15 17:33:29 +00:00
|
|
|
assert_eq!(25, queue.dequeue().unwrap().unwrap().speed());
|
|
|
|
assert_eq!(6, queue.dequeue().unwrap().unwrap().speed());
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resort_with_two_choices() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(50);
|
|
|
|
let user2 = get_user(1);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
|
|
|
let mut queue = ChoiceQueue::new(vec![
|
2023-06-22 13:43:41 +00:00
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
|
|
|
|
2023-04-15 17:33:29 +00:00
|
|
|
user2.change_level_by(60).unwrap();
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user1, queue.peek().unwrap().unwrap().user().clone(),);
|
2023-04-15 17:33:29 +00:00
|
|
|
queue.resort().unwrap();
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user2, queue.peek().unwrap().unwrap().user().clone(),);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_pokemon_choice_first_with_two_choices() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(100);
|
|
|
|
let user2 = get_user(1);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
let queue = ChoiceQueue::new(vec![
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user1, queue.peek().unwrap().unwrap().user().clone(),);
|
2023-06-22 13:43:41 +00:00
|
|
|
assert!(queue.move_pokemon_choice_next(&user2).unwrap());
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user2, queue.dequeue().unwrap().unwrap().user().clone(),);
|
|
|
|
assert_eq!(user1, queue.dequeue().unwrap().unwrap().user().clone(),);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_pokemon_choice_first_when_choice_has_already_been() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(10);
|
|
|
|
let user2 = get_user(100);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
let queue = ChoiceQueue::new(vec![
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user2, queue.dequeue().unwrap().unwrap().user().clone(),);
|
2023-06-22 13:43:41 +00:00
|
|
|
assert!(!queue.move_pokemon_choice_next(&user2).unwrap());
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user1, queue.dequeue().unwrap().unwrap().user().clone(),);
|
2023-04-15 12:34:42 +00:00
|
|
|
assert!(queue.peek().unwrap().is_none())
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_pokemon_choice_first_when_choice_is_next() {
|
2023-06-22 13:43:41 +00:00
|
|
|
let user1 = get_user(100);
|
|
|
|
let user2 = get_user(10);
|
2022-11-26 14:33:50 +00:00
|
|
|
|
|
|
|
let queue = ChoiceQueue::new(vec![
|
2023-06-22 13:43:41 +00:00
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
|
|
|
|
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user1, queue.peek().unwrap().unwrap().user().clone(),);
|
2023-06-22 13:43:41 +00:00
|
|
|
assert!(queue.move_pokemon_choice_next(&user1).unwrap());
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(user1, queue.peek().unwrap().unwrap().user().clone(),);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_pokemon_choice_first_with_seven_choices() {
|
|
|
|
let users = [
|
2023-06-22 13:43:41 +00:00
|
|
|
get_user(100),
|
|
|
|
get_user(90),
|
|
|
|
get_user(80),
|
|
|
|
get_user(70),
|
|
|
|
get_user(60),
|
|
|
|
get_user(50),
|
|
|
|
get_user(40),
|
2022-11-26 14:33:50 +00:00
|
|
|
];
|
|
|
|
|
2023-06-22 13:43:41 +00:00
|
|
|
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())))),
|
2022-11-26 14:33:50 +00:00
|
|
|
]);
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(users[0], queue.peek().unwrap().unwrap().user().clone(),);
|
2023-06-22 13:43:41 +00:00
|
|
|
assert!(queue.move_pokemon_choice_next(&users[4]).unwrap());
|
2022-11-26 14:33:50 +00:00
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(users[4], queue.dequeue().unwrap().unwrap().user().clone(),);
|
2022-11-26 14:33:50 +00:00
|
|
|
for index in 0..4 {
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(users[index], queue.dequeue().unwrap().unwrap().user().clone(),);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
for index in 5..7 {
|
2023-06-24 12:44:23 +00:00
|
|
|
assert_eq!(users[index], queue.dequeue().unwrap().unwrap().user().clone(),);
|
2022-11-26 14:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|