[][src]Trait ultra_tournament::BattleSystem

pub trait BattleSystem<E: Debug + Display + Clone, M: Debug + Display + Clone + Default>: Clone {
    fn battle(a: Arc<RwLock<E>>, b: Arc<RwLock<E>>) -> BattleResult<M>;
fn tiebreaker(
        a: Arc<RwLock<E>>,
        b: Arc<RwLock<E>>
    ) -> (TournamentRoundResult, M); }

Implement this trait to create a system for solving battles betweeen two structs.

Example

The larger number wins. Ties are resolved randomly.

use crate::MyMetadata;

#[derive(Clone)]
struct U32BattleSystem;
impl BattleSystem<u32, MyMetadata> for U32BattleSystem {

	fn battle(
		a_arc: Arc<RwLock<u32>>,
		b_arc: Arc<RwLock<u32>>,
	) -> BattleResult<MyMetadata> {
		use TournamentRoundResult::*;
		let a = a_arc.read().unwrap();
		let b = b_arc.read().unwrap();

		if *a > *b {
			BattleResult::Solved(A, MyMetadata::new())
		} else if *a < *b {
			BattleResult::Solved(B, MyMetadata::new())
		} else {
			BattleResult::Tie
		}
	}

	fn tiebreaker(
		_: Arc<RwLock<u32>>,
		_: Arc<RwLock<u32>>,
	) -> (TournamentRoundResult, MyMetadata) {
		use rand::prelude::*;
		use TournamentRoundResult::*;
		(
			if random::<f32>() > 0.5 { A } else { B },
			MyMetadata::new()
		)
	}

}

Required methods

fn battle(a: Arc<RwLock<E>>, b: Arc<RwLock<E>>) -> BattleResult<M>

  • Resolves a round played between two entrants encapsulated in Arc<RwLock<E>>s, allowing for mutation of entrants between rounds.

  • Example funcationality: reduce a fighter's HP during a round, and retain the change in later rounds.

fn tiebreaker(
    a: Arc<RwLock<E>>,
    b: Arc<RwLock<E>>
) -> (TournamentRoundResult, M)

  • In case battle returns a BattleResult::Tie, run a tiebreaker that must return a successful result.
Loading content...

Implementors

Loading content...