return winner when running a match

This commit is contained in:
Ilion Beyst 2022-04-27 20:08:48 +02:00
parent 39940aaabd
commit 450d22758e
2 changed files with 19 additions and 5 deletions

View file

@ -52,7 +52,11 @@ pub trait BotSpec: Send + Sync {
) -> Box<dyn PlayerHandle>; ) -> Box<dyn PlayerHandle>;
} }
pub async fn run_match(config: MatchConfig) { pub struct MatchOutcome {
pub winner: Option<usize>,
}
pub async fn run_match(config: MatchConfig) -> MatchOutcome {
let pw_config = PwConfig { let pw_config = PwConfig {
map_file: config.map_path, map_file: config.map_path,
max_turns: 100, max_turns: 100,
@ -103,8 +107,18 @@ pub async fn run_match(config: MatchConfig) {
// ) // )
// .unwrap(); // .unwrap();
let match_state = pw_match::PwMatch::create(match_ctx, pw_config); let mut match_state = pw_match::PwMatch::create(match_ctx, pw_config);
match_state.run().await; match_state.run().await;
let final_state = match_state.match_state.state();
let survivors = final_state.living_players();
let winner = if survivors.len() == 1 {
Some(survivors[0])
} else {
None
};
MatchOutcome { winner }
} }
// writing this as a closure causes lifetime inference errors // writing this as a closure causes lifetime inference errors

View file

@ -24,8 +24,8 @@ pub struct MatchConfig {
} }
pub struct PwMatch { pub struct PwMatch {
match_ctx: MatchCtx, pub match_ctx: MatchCtx,
match_state: PlanetWars, pub match_state: PlanetWars,
} }
impl PwMatch { impl PwMatch {
@ -39,7 +39,7 @@ impl PwMatch {
} }
} }
pub async fn run(mut self) { pub async fn run(&mut self) {
while !self.match_state.is_finished() { while !self.match_state.is_finished() {
let player_messages = self.prompt_players().await; let player_messages = self.prompt_players().await;