1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
pub mod bot_runner;
pub mod docker_runner;
pub mod match_context;
pub mod match_log;
pub mod pw_match;
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
use async_trait::async_trait;
use futures::{stream::FuturesOrdered, StreamExt};
use match_context::MatchCtx;
use match_log::{create_log_sink, MatchLogger};
use planetwars_rules::PwConfig;
pub use self::match_context::{EventBus, PlayerHandle};
pub struct MatchConfig {
pub map_name: String,
pub map_path: PathBuf,
pub log_path: PathBuf,
pub players: Vec<MatchPlayer>,
}
pub struct MatchPlayer {
pub bot_spec: Box<dyn BotSpec>,
}
#[async_trait]
pub trait BotSpec: Send + Sync {
async fn run_bot(
&self,
player_id: u32,
event_bus: Arc<Mutex<EventBus>>,
match_logger: MatchLogger,
) -> Box<dyn PlayerHandle>;
}
pub struct MatchOutcome {
pub winner: Option<usize>,
pub player_outcomes: Vec<PlayerOutcome>,
}
pub struct PlayerOutcome {
pub had_errors: bool,
pub crashed: bool,
}
pub async fn run_match(config: MatchConfig) -> MatchOutcome {
let pw_config = PwConfig {
map_file: config.map_path,
max_turns: 500,
};
let event_bus = Arc::new(Mutex::new(EventBus::new()));
let match_logger = create_log_sink(&config.log_path).await;
// start bots
// TODO: what happens when a bot fails?
let players = config
.players
.iter()
.enumerate()
.map(|(player_id, player)| {
let player_id = (player_id + 1) as u32;
start_bot(
player_id,
event_bus.clone(),
player.bot_spec.as_ref(),
match_logger.clone(),
)
})
.collect::<FuturesOrdered<_>>()
// await all results
.collect()
.await;
let match_ctx = MatchCtx::new(event_bus, players, match_logger);
let mut match_instance = pw_match::PwMatch::create(match_ctx, pw_config);
match_instance.run().await;
match_instance.match_ctx.shutdown().await;
let survivors = match_instance.match_state.state().living_players();
let winner = if survivors.len() == 1 {
Some(survivors[0])
} else {
None
};
let player_outcomes = (1..=config.players.len())
.map(|player_id| {
let player_status = &match_instance.player_status[&player_id];
PlayerOutcome {
had_errors: player_status.had_command_errors,
crashed: player_status.terminated,
}
})
.collect();
MatchOutcome {
winner,
player_outcomes,
}
}
// writing this as a closure causes lifetime inference errors
async fn start_bot(
player_id: u32,
event_bus: Arc<Mutex<EventBus>>,
bot_spec: &dyn BotSpec,
match_logger: MatchLogger,
) -> (u32, Box<dyn PlayerHandle>) {
let player_handle = bot_spec.run_bot(player_id, event_bus, match_logger).await;
(player_id, player_handle)
}
|