From a1d81ac774c0ae52f155cd764fd74fd1ba928a5f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 23 Sep 2022 21:34:57 +0200 Subject: ensure bots cleanly stop before a match completes --- planetwars-matchrunner/src/match_context.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'planetwars-matchrunner/src/match_context.rs') diff --git a/planetwars-matchrunner/src/match_context.rs b/planetwars-matchrunner/src/match_context.rs index 859b11d..bdc87a3 100644 --- a/planetwars-matchrunner/src/match_context.rs +++ b/planetwars-matchrunner/src/match_context.rs @@ -7,6 +7,7 @@ use std::{ collections::HashMap, sync::{Arc, Mutex}, }; +use tokio::task::JoinHandle; use crate::match_log::{MatchLogMessage, MatchLogger}; @@ -71,10 +72,19 @@ impl MatchCtx { pub fn log(&mut self, message: MatchLogMessage) { self.match_logger.send(message).expect("write failed"); } + + pub async fn shutdown(self) { + let join_handles = self + .players + .into_iter() + .map(|(_player_id, player_data)| player_data.handle.into_join_handle()); + futures::future::join_all(join_handles).await; + } } pub trait PlayerHandle: Send { fn send_request(&mut self, r: RequestMessage); + fn into_join_handle(self: Box) -> JoinHandle<()>; } struct PlayerData { -- cgit v1.2.3