Don't store the TurnChoices as Arc<RwLock>>, to prevent locking issues.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
use crate::dynamic_data::choices::TurnChoice;
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use parking_lot::RwLock;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ChoiceQueue<'battle, 'library> {
|
||||
queue: Vec<Arc<RwLock<TurnChoice<'battle, 'library>>>>,
|
||||
queue: Vec<TurnChoice<'battle, 'library>>,
|
||||
current: usize,
|
||||
}
|
||||
|
||||
impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
|
||||
pub fn new(queue: Vec<Arc<RwLock<TurnChoice<'battle, 'library>>>>) -> Self {
|
||||
pub fn new(queue: Vec<TurnChoice<'battle, 'library>>) -> Self {
|
||||
Self { queue, current: 0 }
|
||||
}
|
||||
|
||||
pub fn dequeue<'b>(&'b mut self) -> &'b Arc<RwLock<TurnChoice<'battle, 'library>>> {
|
||||
pub fn dequeue<'b>(&'b mut self) -> &'b TurnChoice<'battle, 'library> {
|
||||
let c = &self.queue[self.current];
|
||||
self.current += 1;
|
||||
c
|
||||
}
|
||||
|
||||
pub fn peek(&mut self) -> &'battle Arc<RwLock<TurnChoice>> {
|
||||
pub fn peek(&mut self) -> &'battle TurnChoice {
|
||||
&self.queue[self.current]
|
||||
}
|
||||
|
||||
@@ -32,7 +30,7 @@ impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub(crate) fn get_queue(&self) -> &Vec<Arc<RwLock<TurnChoice<'battle, 'library>>>> {
|
||||
pub(crate) fn get_queue(&self) -> &Vec<TurnChoice<'battle, 'library>> {
|
||||
&self.queue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,17 +25,15 @@ impl<'own, 'library> Battle<'own, 'library> {
|
||||
// to check whether a pokemon was hit this turn. By resetting here, and setting a variable to true
|
||||
// they can then know this later on.)
|
||||
for choice in choice_queue.get_queue() {
|
||||
let choice_guard = choice.read();
|
||||
let c = choice_guard.deref();
|
||||
script_hook!(on_before_turn, c, c);
|
||||
script_hook!(on_before_turn, choice, choice);
|
||||
}
|
||||
|
||||
// Now we can properly begin executing choices.
|
||||
// One by one dequeue the turns, and run them. If the battle has ended we do not want to
|
||||
// continue running however.
|
||||
while choice_queue.has_next() && !self.has_ended() {
|
||||
let choice = choice_queue.dequeue().clone();
|
||||
self.execute_choice(choice.clone())?;
|
||||
let choice = choice_queue.dequeue();
|
||||
self.execute_choice(&choice)?;
|
||||
}
|
||||
|
||||
// If the battle is not ended, we have arrived at the normal end of a turn. and thus want
|
||||
@@ -63,15 +61,14 @@ impl<'own, 'library> Battle<'own, 'library> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_choice(&self, choice: Arc<RwLock<TurnChoice<'own, 'library>>>) -> PkmnResult<()> {
|
||||
let choice_guard = choice.read();
|
||||
if let TurnChoice::Pass(..) = choice_guard.deref() {
|
||||
fn execute_choice(&self, choice: &TurnChoice<'own, 'library>) -> PkmnResult<()> {
|
||||
if let TurnChoice::Pass(..) = choice {
|
||||
return Ok(());
|
||||
}
|
||||
if self.has_ended() {
|
||||
return Ok(());
|
||||
}
|
||||
let user = choice_guard.user().read();
|
||||
let user = choice.user().read();
|
||||
if !user.is_usable() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -79,15 +76,11 @@ impl<'own, 'library> Battle<'own, 'library> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !self.can_use(choice_guard.deref()) {
|
||||
if !self.can_use(&choice) {
|
||||
return Ok(());
|
||||
}
|
||||
match choice_guard.deref() {
|
||||
TurnChoice::Move(..) => {
|
||||
drop(user);
|
||||
drop(choice_guard);
|
||||
self.execute_move_choice(&choice)?
|
||||
}
|
||||
match choice {
|
||||
TurnChoice::Move(..) => self.execute_move_choice(&choice)?,
|
||||
TurnChoice::Item(_) => {}
|
||||
TurnChoice::Switch(_) => {}
|
||||
TurnChoice::Flee(_) => {}
|
||||
@@ -96,12 +89,8 @@ impl<'own, 'library> Battle<'own, 'library> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_move_choice<'func>(
|
||||
&'func self,
|
||||
choice: &'func Arc<RwLock<TurnChoice<'own, 'library>>>,
|
||||
) -> PkmnResult<()> {
|
||||
let mut write_guard = choice.write();
|
||||
let choice = write_guard.get_move_turn_data();
|
||||
fn execute_move_choice<'func>(&'func self, choice: &'func TurnChoice<'own, 'library>) -> PkmnResult<()> {
|
||||
let choice = choice.get_move_turn_data();
|
||||
let used_move = choice.used_move();
|
||||
let move_data_lock = used_move.read();
|
||||
let move_data = move_data_lock.move_data();
|
||||
|
||||
Reference in New Issue
Block a user