log dispatches and timeouts

This commit is contained in:
Ilion Beyst 2022-09-18 13:00:32 +02:00
parent f5fe1c4f29
commit 3be0cfa0ea
2 changed files with 19 additions and 2 deletions

View file

@ -6,6 +6,8 @@ use tokio::{fs::File, io::AsyncWriteExt};
use planetwars_rules::protocol::State; use planetwars_rules::protocol::State;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use crate::pw_match::PlayerCommand;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum MatchLogMessage { pub enum MatchLogMessage {
@ -13,12 +15,19 @@ pub enum MatchLogMessage {
GameState(State), GameState(State),
#[serde(rename = "stderr")] #[serde(rename = "stderr")]
StdErr(StdErrMessage), StdErr(StdErrMessage),
#[serde(rename = "timeout")]
Timeout { player_id: u32 },
#[serde(rename = "bad_command")] #[serde(rename = "bad_command")]
BadCommand { BadCommand {
player_id: u32, player_id: u32,
command: String, command: String,
error: String, error: String,
}, },
#[serde(rename = "dispatches")]
Dispatches {
player_id: u32,
dispatches: Vec<PlayerCommand>,
},
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

View file

@ -117,6 +117,9 @@ impl PwMatch {
fn log_player_action(&mut self, player_id: usize, player_action: PlayerAction) { fn log_player_action(&mut self, player_id: usize, player_action: PlayerAction) {
match player_action { match player_action {
PlayerAction::Timeout => self.match_ctx.log(MatchLogMessage::Timeout {
player_id: player_id as u32,
}),
PlayerAction::ParseError { data, error } => { PlayerAction::ParseError { data, error } => {
// TODO: can this be handled better? // TODO: can this be handled better?
let command = let command =
@ -128,14 +131,19 @@ impl PwMatch {
error: error.to_string(), error: error.to_string(),
}); });
} }
// TODO: handle other action types PlayerAction::Commands(dispatches) => {
_ => {} self.match_ctx.log(MatchLogMessage::Dispatches {
player_id: player_id as u32,
dispatches,
});
}
} }
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerCommand { pub struct PlayerCommand {
#[serde(flatten)]
pub command: proto::Command, pub command: proto::Command,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<proto::CommandError>, pub error: Option<proto::CommandError>,