Change to RwLock over Mutex, so we can read without locking, changes to BattleRandom to allow for cloning, and prevent race conditions.
This commit is contained in:
@@ -8,7 +8,7 @@ use crate::dynamic_data::models::battle_result::BattleResult;
|
||||
use crate::dynamic_data::models::battle_side::BattleSide;
|
||||
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
||||
use derive_getters::Getters;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Getters, Debug)]
|
||||
pub struct Battle<'a> {
|
||||
@@ -38,14 +38,14 @@ impl<'a> Battle<'a> {
|
||||
number_of_sides: u8,
|
||||
pokemon_per_side: u8,
|
||||
random_seed: Option<u128>,
|
||||
) -> Arc<Mutex<Self>> {
|
||||
) -> Arc<RwLock<Self>> {
|
||||
let random = if let Some(seed) = random_seed {
|
||||
BattleRandom::new_with_seed(seed)
|
||||
} else {
|
||||
BattleRandom::new()
|
||||
BattleRandom::default()
|
||||
};
|
||||
let sides = Vec::with_capacity(number_of_sides as usize);
|
||||
let battle = Arc::new(Mutex::new(Self {
|
||||
let battle = Arc::new(RwLock::new(Self {
|
||||
library,
|
||||
parties,
|
||||
can_flee,
|
||||
@@ -64,14 +64,14 @@ impl<'a> Battle<'a> {
|
||||
}));
|
||||
|
||||
for i in 0..number_of_sides {
|
||||
battle.lock().unwrap().sides[i as usize] =
|
||||
battle.write().unwrap().sides[i as usize] =
|
||||
BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
|
||||
}
|
||||
battle
|
||||
}
|
||||
|
||||
pub fn random(&mut self) -> &mut BattleRandom {
|
||||
&mut self.random
|
||||
pub fn random(&self) -> &BattleRandom {
|
||||
&self.random
|
||||
}
|
||||
|
||||
pub fn can_slot_be_filled(&self) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user